Search in sources :

Example 31 with FrameImage2D

use of androidx.media.filterfw.FrameImage2D in project android_frameworks_base by crdroidandroid.

the class ExposureFilterTest method testExposureFilter.

public void testExposureFilter() throws Exception {
    final int INPUT_WIDTH = 480;
    final int INPUT_HEIGHT = 640;
    FrameImage2D image = createFrame(FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.READ_CPU), new int[] { INPUT_WIDTH, INPUT_HEIGHT }).asFrameImage2D();
    Bitmap bitmap = BitmapFactory.decodeStream(assetMgr.open("0002_000390.jpg"));
    image.setBitmap(bitmap);
    injectInputFrame("image", image);
    process();
    final float EXPECTED_OVEREXPOSURE = 0.00757f;
    assertEquals(EXPECTED_OVEREXPOSURE, ((Float) getOutputFrame("overExposureRating").asFrameValue().getValue()).floatValue(), 0.001f);
    final float EXPECTED_UNDEREXPOSURE = 0.2077f;
    assertEquals(EXPECTED_UNDEREXPOSURE, ((Float) getOutputFrame("underExposureRating").asFrameValue().getValue()).floatValue(), 0.001f);
}
Also used : Bitmap(android.graphics.Bitmap) FrameImage2D(androidx.media.filterfw.FrameImage2D)

Example 32 with FrameImage2D

use of androidx.media.filterfw.FrameImage2D in project android_frameworks_base by crdroidandroid.

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);
}
Also used : Bitmap(android.graphics.Bitmap) FrameImage2D(androidx.media.filterfw.FrameImage2D) FrameValue(androidx.media.filterfw.FrameValue)

Example 33 with FrameImage2D

use of androidx.media.filterfw.FrameImage2D in project android_frameworks_base by crdroidandroid.

the class CropFilter method onProcess.

@Override
protected void onProcess() {
    OutputPort outPort = getConnectedOutputPort("image");
    // Pull input frame
    FrameImage2D inputImage = getConnectedInputPort("image").pullFrame().asFrameImage2D();
    int[] inDims = inputImage.getDimensions();
    int[] croppedDims = { (int) Math.ceil(mCropRect.xEdge().length() * inDims[0]), (int) Math.ceil(mCropRect.yEdge().length() * inDims[1]) };
    int[] outDims = { getOutputWidth(croppedDims[0], croppedDims[1]), getOutputHeight(croppedDims[0], croppedDims[1]) };
    FrameImage2D outputImage = outPort.fetchAvailableFrame(outDims).asFrameImage2D();
    if (isOpenGLSupported()) {
        FrameImage2D sourceFrame;
        Quad sourceQuad = null;
        boolean scaleDown = (outDims[0] < croppedDims[0]) || (outDims[1] < croppedDims[1]);
        if (scaleDown && mUseMipmaps) {
            mPow2Frame = TransformUtils.makeMipMappedFrame(mPow2Frame, croppedDims);
            int[] extDims = mPow2Frame.getDimensions();
            float targetWidth = croppedDims[0] / (float) extDims[0];
            float targetHeight = croppedDims[1] / (float) extDims[1];
            Quad targetQuad = Quad.fromRect(0f, 0f, targetWidth, targetHeight);
            mShader.setSourceQuad(mCropRect);
            mShader.setTargetQuad(targetQuad);
            mShader.process(inputImage, mPow2Frame);
            TransformUtils.generateMipMaps(mPow2Frame);
            sourceFrame = mPow2Frame;
            sourceQuad = targetQuad;
        } else {
            sourceFrame = inputImage;
            sourceQuad = mCropRect;
        }
        mShader.setSourceQuad(sourceQuad);
        mShader.setTargetRect(0f, 0f, 1f, 1f);
        mShader.process(sourceFrame, outputImage);
    } else {
        // Convert quads to canvas coordinate space
        Quad sourceQuad = mCropRect.scale2(inDims[0], inDims[1]);
        Quad targetQuad = Quad.fromRect(0f, 0f, inDims[0], inDims[1]);
        // Calculate transform for crop
        Matrix transform = Quad.getTransform(sourceQuad, targetQuad);
        transform.postScale(outDims[0] / (float) inDims[0], outDims[1] / (float) inDims[1]);
        // Create target canvas
        Bitmap.Config config = Bitmap.Config.ARGB_8888;
        Bitmap cropped = Bitmap.createBitmap(outDims[0], outDims[1], config);
        Canvas canvas = new Canvas(cropped);
        // Draw source bitmap into target canvas
        Paint paint = new Paint();
        paint.setFilterBitmap(true);
        Bitmap sourceBitmap = inputImage.toBitmap();
        canvas.drawBitmap(sourceBitmap, transform, paint);
        // Assign bitmap to output frame
        outputImage.setBitmap(cropped);
    }
    outPort.pushFrame(outputImage);
}
Also used : OutputPort(androidx.media.filterfw.OutputPort) Quad(androidx.media.filterfw.geometry.Quad) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) FrameImage2D(androidx.media.filterfw.FrameImage2D) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint)

Example 34 with FrameImage2D

use of androidx.media.filterfw.FrameImage2D in project android_frameworks_base by crdroidandroid.

the class RotateFilter method onProcess.

@Override
protected void onProcess() {
    OutputPort outPort = getConnectedOutputPort("image");
    FrameImage2D inputImage = getConnectedInputPort("image").pullFrame().asFrameImage2D();
    int[] inDims = inputImage.getDimensions();
    FrameImage2D outputImage = outPort.fetchAvailableFrame(inDims).asFrameImage2D();
    mShader.setSourceQuad(mSourceRect);
    Quad targetQuad = mSourceRect.rotated((float) (mRotateAngle / 180 * Math.PI));
    mShader.setTargetQuad(targetQuad);
    mShader.process(inputImage, outputImage);
    outPort.pushFrame(outputImage);
}
Also used : OutputPort(androidx.media.filterfw.OutputPort) Quad(androidx.media.filterfw.geometry.Quad) FrameImage2D(androidx.media.filterfw.FrameImage2D)

Example 35 with FrameImage2D

use of androidx.media.filterfw.FrameImage2D in project android_frameworks_base by crdroidandroid.

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);
    }
}
Also used : OutputPort(androidx.media.filterfw.OutputPort) FrameImage2D(androidx.media.filterfw.FrameImage2D) ByteBuffer(java.nio.ByteBuffer)

Aggregations

FrameImage2D (androidx.media.filterfw.FrameImage2D)72 OutputPort (androidx.media.filterfw.OutputPort)48 Bitmap (android.graphics.Bitmap)32 FrameValue (androidx.media.filterfw.FrameValue)32 ByteBuffer (java.nio.ByteBuffer)24 Face (android.hardware.Camera.Face)8 FrameBuffer2D (androidx.media.filterfw.FrameBuffer2D)8 FrameValues (androidx.media.filterfw.FrameValues)8 Quad (androidx.media.filterfw.geometry.Quad)8 Canvas (android.graphics.Canvas)4 Matrix (android.graphics.Matrix)4 Paint (android.graphics.Paint)4 Rect (android.graphics.Rect)4 Camera (android.hardware.Camera)4 RenderTarget (androidx.media.filterfw.RenderTarget)4