use of androidx.media.filterfw.FrameImage2D in project android_frameworks_base by DirtyUnicorns.
the class ExposureFilter method onProcess.
@Override
protected void onProcess() {
FrameImage2D inputImage = getConnectedInputPort("image").pullFrame().asFrameImage2D();
float overExposedPixels, underExposedPixels;
ByteBuffer inputBuffer = inputImage.lockBytes(Frame.MODE_READ);
overExposedPixels = overExposureOperator(inputImage.getWidth(), inputImage.getHeight(), inputBuffer);
underExposedPixels = underExposureOperator(inputImage.getWidth(), inputImage.getHeight(), inputBuffer);
inputImage.unlock();
if (mLogVerbose)
Log.v(TAG, "underExposedPixelCount: " + underExposedPixels);
OutputPort underPort = getConnectedOutputPort("underExposedNum");
if (underPort != null) {
FrameValue underOutFrame = underPort.fetchAvailableFrame(null).asFrameValue();
underOutFrame.setValue(underExposedPixels * inputImage.getWidth() * inputImage.getHeight());
underPort.pushFrame(underOutFrame);
}
OutputPort underPort2 = getConnectedOutputPort("underExposureRating");
FrameValue underOutFrame2 = underPort2.fetchAvailableFrame(null).asFrameValue();
underOutFrame2.setValue(underExposedPixels);
underPort2.pushFrame(underOutFrame2);
if (mLogVerbose)
Log.v(TAG, "overExposedPixelCount: " + overExposedPixels);
OutputPort overPort = getConnectedOutputPort("overExposedNum");
if (overPort != null) {
FrameValue overOutFrame = overPort.fetchAvailableFrame(null).asFrameValue();
overOutFrame.setValue(overExposedPixels * inputImage.getWidth() * inputImage.getHeight());
overPort.pushFrame(overOutFrame);
}
OutputPort overPort2 = getConnectedOutputPort("overExposureRating");
FrameValue overOutFrame2 = overPort2.fetchAvailableFrame(null).asFrameValue();
overOutFrame2.setValue(overExposedPixels);
overPort2.pushFrame(overOutFrame2);
}
use of androidx.media.filterfw.FrameImage2D in project android_frameworks_base by DirtyUnicorns.
the class FaceSquareFilter method onProcess.
/**
* @see androidx.media.filterfw.Filter#onProcess()
*/
@Override
protected void onProcess() {
// Get inputs
FrameImage2D imageFrame = getConnectedInputPort("image").pullFrame().asFrameImage2D();
FrameValues facesFrame = getConnectedInputPort("faces").pullFrame().asFrameValues();
Face[] faces = (Face[]) facesFrame.getValues();
int[] dims = imageFrame.getDimensions();
ByteBuffer buffer = imageFrame.lockBytes(Frame.MODE_WRITE);
byte[] pixels = buffer.array();
// For every face in faces, draw a white rect around the
// face following the rect member of the Face
drawBoxes(pixels, faces, dims);
imageFrame.unlock();
OutputPort outPort = getConnectedOutputPort("image");
outPort.pushFrame(imageFrame);
}
use of androidx.media.filterfw.FrameImage2D in project android_frameworks_base by DirtyUnicorns.
the class SobelFilter method onProcess.
@Override
protected void onProcess() {
OutputPort magnitudePort = getConnectedOutputPort("magnitude");
OutputPort directionPort = getConnectedOutputPort("direction");
FrameImage2D inputImage = getConnectedInputPort("image").pullFrame().asFrameImage2D();
int[] inputDims = inputImage.getDimensions();
FrameImage2D magImage = (magnitudePort != null) ? magnitudePort.fetchAvailableFrame(inputDims).asFrameImage2D() : null;
FrameImage2D dirImage = (directionPort != null) ? directionPort.fetchAvailableFrame(inputDims).asFrameImage2D() : null;
if (isOpenGLSupported()) {
FrameImage2D gxFrame = Frame.create(mImageType, inputDims).asFrameImage2D();
FrameImage2D gyFrame = Frame.create(mImageType, inputDims).asFrameImage2D();
mGradientXShader.setUniformValue("pix", new float[] { 1f / inputDims[0], 1f / inputDims[1] });
mGradientYShader.setUniformValue("pix", new float[] { 1f / inputDims[0], 1f / inputDims[1] });
mGradientXShader.process(inputImage, gxFrame);
mGradientYShader.process(inputImage, gyFrame);
FrameImage2D[] gradientFrames = new FrameImage2D[] { gxFrame, gyFrame };
if (magnitudePort != null) {
mMagnitudeShader.processMulti(gradientFrames, magImage);
}
if (directionPort != null) {
mDirectionShader.processMulti(gradientFrames, dirImage);
}
gxFrame.release();
gyFrame.release();
} else {
ByteBuffer inputBuffer = inputImage.lockBytes(Frame.MODE_READ);
ByteBuffer magBuffer = (magImage != null) ? magImage.lockBytes(Frame.MODE_WRITE) : null;
ByteBuffer dirBuffer = (dirImage != null) ? dirImage.lockBytes(Frame.MODE_WRITE) : null;
sobelOperator(inputImage.getWidth(), inputImage.getHeight(), inputBuffer, magBuffer, dirBuffer);
inputImage.unlock();
if (magImage != null) {
magImage.unlock();
}
if (dirImage != null) {
dirImage.unlock();
}
}
if (magImage != null) {
magnitudePort.pushFrame(magImage);
}
if (dirImage != null) {
directionPort.pushFrame(dirImage);
}
}
use of androidx.media.filterfw.FrameImage2D in project android_frameworks_base by DirtyUnicorns.
the class FaceSquareFilterTest method testFaceSquareFilter.
public void testFaceSquareFilter() throws Exception {
final int INPUT_WIDTH = 1536;
final int INPUT_HEIGHT = 2048;
FrameImage2D image = createFrame(FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.READ_CPU), new int[] { INPUT_WIDTH, INPUT_HEIGHT }).asFrameImage2D();
FrameValues facesFrame = createFrame(FrameType.array(Camera.Face.class), new int[] { 1, 1 }).asFrameValues();
Bitmap bitmap = BitmapFactory.decodeStream(assetMgr.open("XZZ019.jpg"));
image.setBitmap(bitmap);
injectInputFrame("image", image);
Face face = new Face();
Rect faceRect = new Rect();
// These are the values for image 141 with 1 face
faceRect.set(-533, -453, 369, 224);
face.rect = faceRect;
Face[] faces = new Face[1];
faces[0] = face;
facesFrame.setValue(faces);
injectInputFrame("faces", facesFrame);
process();
// ensure the output image has the rectangle in the right place
FrameImage2D outputImage = getOutputFrame("image").asFrameImage2D();
int[] pixels = new int[bitmap.getByteCount()];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
final int FACE_X_RANGE = 2000;
final int WIDTH_OFFSET = 1000;
final int HEIGHT_OFFSET = 1000;
int top = (faceRect.top + HEIGHT_OFFSET) * bitmap.getHeight() / FACE_X_RANGE;
int bottom = (faceRect.bottom + HEIGHT_OFFSET) * bitmap.getHeight() / FACE_X_RANGE;
int left = (faceRect.left + WIDTH_OFFSET) * bitmap.getWidth() / FACE_X_RANGE;
int right = (faceRect.right + WIDTH_OFFSET) * bitmap.getWidth() / FACE_X_RANGE;
if (top < 0) {
top = 0;
} else if (top > bitmap.getHeight()) {
top = bitmap.getHeight();
}
if (left < 0) {
left = 0;
} else if (left > bitmap.getWidth()) {
left = bitmap.getWidth();
}
if (bottom > bitmap.getHeight()) {
bottom = bitmap.getHeight();
} else if (bottom < 0) {
bottom = 0;
}
if (right > bitmap.getWidth()) {
right = bitmap.getWidth();
} else if (right < 0) {
right = 0;
}
for (int j = 0; j < (bottom - top); j++) {
// Left edge
if (left > 0 && top > 0) {
pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + left) + ImageConstants.RED_OFFSET] = (byte) ImageConstants.MAX_BYTE;
pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + left) + ImageConstants.GREEN_OFFSET] = (byte) ImageConstants.MAX_BYTE;
pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + left) + ImageConstants.BLUE_OFFSET] = (byte) ImageConstants.MAX_BYTE;
}
// Right edge
if (right > 0 && top > 0) {
pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + right) + ImageConstants.RED_OFFSET] = (byte) ImageConstants.MAX_BYTE;
pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + right) + ImageConstants.GREEN_OFFSET] = (byte) ImageConstants.MAX_BYTE;
pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * (top + j) + right) + ImageConstants.BLUE_OFFSET] = (byte) ImageConstants.MAX_BYTE;
}
}
for (int k = 0; k < (right - left); k++) {
// Top edge
if (top < bitmap.getHeight()) {
pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * top + left + k) + ImageConstants.RED_OFFSET] = (byte) ImageConstants.MAX_BYTE;
pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * top + left + k) + ImageConstants.GREEN_OFFSET] = (byte) ImageConstants.MAX_BYTE;
pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * top + left + k) + ImageConstants.BLUE_OFFSET] = (byte) ImageConstants.MAX_BYTE;
}
// Bottom edge
if (bottom < bitmap.getHeight()) {
pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * bottom + left + k) + ImageConstants.RED_OFFSET] = (byte) ImageConstants.MAX_BYTE;
pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * bottom + left + k) + ImageConstants.GREEN_OFFSET] = (byte) ImageConstants.MAX_BYTE;
pixels[ImageConstants.PIX_CHANNELS * (bitmap.getWidth() * bottom + left + k) + ImageConstants.BLUE_OFFSET] = (byte) ImageConstants.MAX_BYTE;
}
}
Bitmap outputBitmap = outputImage.toBitmap();
int[] outputPixels = new int[outputBitmap.getByteCount()];
outputBitmap.getPixels(outputPixels, 0, outputBitmap.getWidth(), 0, 0, outputBitmap.getWidth(), outputBitmap.getHeight());
int equalCount = 0;
for (int i = 0; i < outputBitmap.getByteCount(); i++) {
if (pixels[i] == outputPixels[i])
equalCount++;
}
if (equalCount + (0.05f * outputBitmap.getByteCount()) < outputBitmap.getByteCount()) {
// Assertion will fail if condition is true
assertEquals(equalCount, outputBitmap.getByteCount());
}
}
use of androidx.media.filterfw.FrameImage2D in project android_frameworks_base by DirtyUnicorns.
the class IfElseFilterTest method testIfElseFilterFalse.
public void testIfElseFilterFalse() throws Exception {
FrameImage2D image = createFrame(FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.READ_CPU), new int[] { BIG_INPUT_WIDTH, BIG_INPUT_HEIGHT }).asFrameImage2D();
FrameImage2D video = createFrame(FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.READ_CPU), new int[] { SMALL_INPUT_WIDTH, SMALL_INPUT_HEIGHT }).asFrameImage2D();
// Image of legs
Bitmap videoBitmap = BitmapFactory.decodeStream(assetMgr.open("0002_000390.jpg"));
// Image of a face
Bitmap imageBitmap = BitmapFactory.decodeStream(assetMgr.open("XZZ019.jpg"));
image.setBitmap(imageBitmap);
injectInputFrame("falseResult", image);
video.setBitmap(videoBitmap);
injectInputFrame("trueResult", video);
FrameValue conditionFrame = createFrame(FrameType.single(boolean.class), new int[] { 1 }).asFrameValue();
conditionFrame.setValue(false);
injectInputFrame("condition", conditionFrame);
process();
// Ensure that for true, we use the video input
FrameImage2D outputImage = getOutputFrame("output").asFrameImage2D();
assertEquals(outputImage, image);
}
Aggregations