Search in sources :

Example 11 with FrameBuffer2D

use of androidx.media.filterfw.FrameBuffer2D in project android_frameworks_base by DirtyUnicorns.

the class IfElseFilter method onProcess.

@Override
protected void onProcess() {
    OutputPort outPort = getConnectedOutputPort("output");
    FrameImage2D trueFrame = getConnectedInputPort("trueResult").pullFrame().asFrameImage2D();
    FrameImage2D falseFrame = getConnectedInputPort("falseResult").pullFrame().asFrameImage2D();
    FrameValue boolFrameValue = getConnectedInputPort("condition").pullFrame().asFrameValue();
    boolean condition = (Boolean) boolFrameValue.getValue();
    FrameBuffer2D outputFrame;
    // If the condition is true, then we want to use the camera, else use the gallery
    if (condition) {
        outputFrame = trueFrame;
    } else {
        outputFrame = falseFrame;
    }
    outPort.pushFrame(outputFrame);
}
Also used : OutputPort(androidx.media.filterfw.OutputPort) FrameBuffer2D(androidx.media.filterfw.FrameBuffer2D) FrameImage2D(androidx.media.filterfw.FrameImage2D) FrameValue(androidx.media.filterfw.FrameValue)

Example 12 with FrameBuffer2D

use of androidx.media.filterfw.FrameBuffer2D in project android_frameworks_base by DirtyUnicorns.

the class ToGrayValuesFilter method onProcess.

@Override
protected void onProcess() {
    OutputPort outPort = getConnectedOutputPort("image");
    FrameImage2D inputImage = getConnectedInputPort("image").pullFrame().asFrameImage2D();
    int[] dim = inputImage.getDimensions();
    FrameBuffer2D outputFrame;
    ByteBuffer grayBuffer;
    if (isOpenGLSupported()) {
        // crop out the portion of inputImage that will be used to generate outputFrame.
        int modular = dim[0] % 4;
        int[] outDim = new int[] { dim[0] - modular, dim[1] };
        outputFrame = outPort.fetchAvailableFrame(outDim).asFrameBuffer2D();
        grayBuffer = outputFrame.lockBytes(Frame.MODE_WRITE);
        int[] targetDims = new int[] { outDim[0] / 4, outDim[1] };
        FrameImage2D targetFrame = Frame.create(mImageInType, targetDims).asFrameImage2D();
        mShader.setSourceQuad(Quad.fromRect(0f, 0f, ((float) outDim[0]) / dim[0], 1f));
        mShader.setUniformValue("pix_stride", 1f / outDim[0]);
        mShader.process(inputImage, targetFrame);
        RenderTarget grayTarget = targetFrame.lockRenderTarget();
        grayTarget.readPixelData(grayBuffer, targetDims[0], targetDims[1]);
        targetFrame.unlock();
        targetFrame.release();
    } else {
        outputFrame = outPort.fetchAvailableFrame(dim).asFrameBuffer2D();
        grayBuffer = outputFrame.lockBytes(Frame.MODE_WRITE);
        ByteBuffer inputBuffer = inputImage.lockBytes(Frame.MODE_READ);
        if (!toGrayValues(inputBuffer, grayBuffer)) {
            throw new RuntimeException("Native implementation encountered an error during processing!");
        }
        inputImage.unlock();
    }
    outputFrame.unlock();
    outPort.pushFrame(outputFrame);
}
Also used : OutputPort(androidx.media.filterfw.OutputPort) FrameBuffer2D(androidx.media.filterfw.FrameBuffer2D) FrameImage2D(androidx.media.filterfw.FrameImage2D) RenderTarget(androidx.media.filterfw.RenderTarget) ByteBuffer(java.nio.ByteBuffer)

Example 13 with FrameBuffer2D

use of androidx.media.filterfw.FrameBuffer2D in project android_frameworks_base by DirtyUnicorns.

the class ColorfulnessFilter method onProcess.

@Override
protected void onProcess() {
    FrameBuffer2D histogramFrame = getConnectedInputPort("histogram").pullFrame().asFrameBuffer2D();
    ByteBuffer byteBuffer = histogramFrame.lockBytes(Frame.MODE_READ);
    byteBuffer.order(ByteOrder.nativeOrder());
    FloatBuffer histogramBuffer = byteBuffer.asFloatBuffer();
    histogramBuffer.rewind();
    // Create a hue histogram from hue-saturation histogram
    int hueBins = histogramFrame.getWidth();
    int saturationBins = histogramFrame.getHeight() - 1;
    float[] hueHistogram = new float[hueBins];
    float total = 0;
    for (int r = 0; r < saturationBins; ++r) {
        float weight = (float) Math.pow(2, r);
        for (int c = 0; c < hueBins; c++) {
            float value = histogramBuffer.get() * weight;
            hueHistogram[c] += value;
            total += value;
        }
    }
    float colorful = 0f;
    for (int c = 0; c < hueBins; ++c) {
        float value = hueHistogram[c] / total;
        if (value > 0f) {
            colorful -= value * ((float) Math.log(value));
        }
    }
    colorful /= Math.log(2);
    histogramFrame.unlock();
    OutputPort outPort = getConnectedOutputPort("score");
    FrameValue frameValue = outPort.fetchAvailableFrame(null).asFrameValue();
    frameValue.setValue(colorful);
    outPort.pushFrame(frameValue);
}
Also used : OutputPort(androidx.media.filterfw.OutputPort) FrameBuffer2D(androidx.media.filterfw.FrameBuffer2D) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) FrameValue(androidx.media.filterfw.FrameValue)

Example 14 with FrameBuffer2D

use of androidx.media.filterfw.FrameBuffer2D in project android_frameworks_base by DirtyUnicorns.

the class NewChromaHistogramFilter method onProcess.

@Override
protected void onProcess() {
    FrameBuffer2D imageFrame = getConnectedInputPort("image").pullFrame().asFrameImage2D();
    OutputPort outPort = getConnectedOutputPort("histogram");
    mValueBins = mHueBins;
    int[] outDims = new int[] { mHueBins, mSaturationBins + 1 };
    FrameBuffer2D histogramFrame = outPort.fetchAvailableFrame(outDims).asFrameBuffer2D();
    ByteBuffer imageBuffer = imageFrame.lockBytes(Frame.MODE_READ);
    ByteBuffer histogramBuffer = histogramFrame.lockBytes(Frame.MODE_READ);
    histogramBuffer.order(ByteOrder.nativeOrder());
    FloatBuffer floatHistogram = histogramBuffer.asFloatBuffer();
    // Run native method
    extractChromaHistogram(imageBuffer, floatHistogram, mHueBins, mSaturationBins, mValueBins, mSaturationThreshold, mValueThreshold);
    imageFrame.unlock();
    histogramFrame.unlock();
    outPort.pushFrame(histogramFrame);
}
Also used : OutputPort(androidx.media.filterfw.OutputPort) FrameBuffer2D(androidx.media.filterfw.FrameBuffer2D) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 15 with FrameBuffer2D

use of androidx.media.filterfw.FrameBuffer2D in project android_frameworks_base by ResurrectionRemix.

the class ColorfulnessFilter method onProcess.

@Override
protected void onProcess() {
    FrameBuffer2D histogramFrame = getConnectedInputPort("histogram").pullFrame().asFrameBuffer2D();
    ByteBuffer byteBuffer = histogramFrame.lockBytes(Frame.MODE_READ);
    byteBuffer.order(ByteOrder.nativeOrder());
    FloatBuffer histogramBuffer = byteBuffer.asFloatBuffer();
    histogramBuffer.rewind();
    // Create a hue histogram from hue-saturation histogram
    int hueBins = histogramFrame.getWidth();
    int saturationBins = histogramFrame.getHeight() - 1;
    float[] hueHistogram = new float[hueBins];
    float total = 0;
    for (int r = 0; r < saturationBins; ++r) {
        float weight = (float) Math.pow(2, r);
        for (int c = 0; c < hueBins; c++) {
            float value = histogramBuffer.get() * weight;
            hueHistogram[c] += value;
            total += value;
        }
    }
    float colorful = 0f;
    for (int c = 0; c < hueBins; ++c) {
        float value = hueHistogram[c] / total;
        if (value > 0f) {
            colorful -= value * ((float) Math.log(value));
        }
    }
    colorful /= Math.log(2);
    histogramFrame.unlock();
    OutputPort outPort = getConnectedOutputPort("score");
    FrameValue frameValue = outPort.fetchAvailableFrame(null).asFrameValue();
    frameValue.setValue(colorful);
    outPort.pushFrame(frameValue);
}
Also used : OutputPort(androidx.media.filterfw.OutputPort) FrameBuffer2D(androidx.media.filterfw.FrameBuffer2D) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) FrameValue(androidx.media.filterfw.FrameValue)

Aggregations

FrameBuffer2D (androidx.media.filterfw.FrameBuffer2D)20 OutputPort (androidx.media.filterfw.OutputPort)20 ByteBuffer (java.nio.ByteBuffer)16 FrameValue (androidx.media.filterfw.FrameValue)12 FrameImage2D (androidx.media.filterfw.FrameImage2D)8 FloatBuffer (java.nio.FloatBuffer)8 RenderTarget (androidx.media.filterfw.RenderTarget)4