FileDocCategorySizeDatePackage
AutoFixFilter.javaAPI DocAndroid 5.1 API14521Thu Mar 12 22:22:30 GMT 2015android.filterpacks.imageproc

AutoFixFilter

public class AutoFixFilter extends android.filterfw.core.Filter

Fields Summary
private int
mTileSize
private float
mScale
private static final int[]
normal_cdf
private final String
mAutoFixShader
private android.filterfw.core.Program
mShaderProgram
private android.filterfw.core.Program
mNativeProgram
private int
mWidth
private int
mHeight
private int
mTarget
private android.filterfw.core.Frame
mHistFrame
private android.filterfw.core.Frame
mDensityFrame
Constructors Summary
public AutoFixFilter(String name)


       
        super(name);
    
Methods Summary
private voidcreateHistogramFrame(android.filterfw.core.FilterContext context, int width, int height, int[] data)

        int histDims = 255 * 3 + 1;
        int[] histArray = new int[histDims];

        float border_thickness_ratio = 0.05f;
        int y_border_thickness = (int) (height * border_thickness_ratio);
        int x_border_thickness = (int) (width * border_thickness_ratio);
        int pixels = (width - 2 * x_border_thickness) * (height - 2 * y_border_thickness);

        float count = 0f;
        for (int y = y_border_thickness; y < height - y_border_thickness; ++y) {
            for (int x = x_border_thickness; x < width - x_border_thickness; ++x) {
                int index = y * width + x;
                int energy = (data[index] & 0xFF) + ((data[index] >> 8) & 0xFF) +
                    ((data[index] >> 16) & 0xFF);
                histArray[energy] ++;
            }
        }

        for (int i = 1; i < histDims; i++) {
            histArray[i] += histArray[i-1];
        }

        for (int i = 0; i < histDims; i++) {
            long temp = (256 * 256 - 1l) * histArray[i] / pixels;
            histArray[i] =  (int) temp;
        }

        FrameFormat shaderHistFormat = ImageFormat.create(histDims, 1,
                                                          ImageFormat.COLORSPACE_RGBA,
                                                          FrameFormat.TARGET_GPU);
        if (mHistFrame != null)
            mHistFrame.release();

        mHistFrame = context.getFrameManager().newFrame(shaderHistFormat);
        mHistFrame.setInts(histArray);
    
public voidfieldPortValueUpdated(java.lang.String name, android.filterfw.core.FilterContext context)

        if (mShaderProgram != null) {
            mShaderProgram.setHostValue("scale", mScale);
        }
    
public android.filterfw.core.FrameFormatgetOutputFormat(java.lang.String portName, android.filterfw.core.FrameFormat inputFormat)

        return inputFormat;
    
private voidinitParameters()

        mShaderProgram.setHostValue("shift_scale", 1.0f / 256f);
        mShaderProgram.setHostValue("hist_offset", 0.5f / 766f);
        mShaderProgram.setHostValue("hist_scale", 765f / 766f);
        mShaderProgram.setHostValue("density_offset", 0.5f / 1024f);
        mShaderProgram.setHostValue("density_scale", 1023f / 1024f);
        mShaderProgram.setHostValue("scale", mScale);
    
public voidinitProgram(android.filterfw.core.FilterContext context, int target)

        switch (target) {
            case FrameFormat.TARGET_GPU:
                ShaderProgram shaderProgram = new ShaderProgram(context, mAutoFixShader);
                shaderProgram.setMaximumTileSize(mTileSize);
                mShaderProgram = shaderProgram;
                break;

            default:
                throw new RuntimeException("Filter Sharpen does not support frames of " +
                    "target " + target + "!");
        }
        mTarget = target;
    
protected voidprepare(android.filterfw.core.FilterContext context)

        int densityDim = 1024;
        int histDim = 255 * 3 + 1;
        long precision = (256l * 256l - 1l);

        int[] densityTable = new int[densityDim];
        for (int i = 0; i < densityDim; ++i) {
            long temp = normal_cdf[i] * precision / histDim;
            densityTable[i] = (int) temp;
        }

        FrameFormat densityFormat = ImageFormat.create(densityDim, 1,
                                                       ImageFormat.COLORSPACE_RGBA,
                                                       FrameFormat.TARGET_GPU);
        mDensityFrame = context.getFrameManager().newFrame(densityFormat);
        mDensityFrame.setInts(densityTable);
    
public voidprocess(android.filterfw.core.FilterContext context)

        // Get input frame
        Frame input = pullInput("image");
        FrameFormat inputFormat = input.getFormat();

        // Create program if not created already
        if (mShaderProgram == null || inputFormat.getTarget() != mTarget) {
            initProgram(context, inputFormat.getTarget());
            initParameters();
        }

        // Check if the frame size has changed
        if (inputFormat.getWidth() != mWidth || inputFormat.getHeight() != mHeight) {
            mWidth = inputFormat.getWidth();
            mHeight = inputFormat.getHeight();
            createHistogramFrame(context, mWidth, mHeight, input.getInts());
        }

        // Create output frame
        Frame output = context.getFrameManager().newFrame(inputFormat);

        // Process
        Frame[] inputs = {input, mHistFrame, mDensityFrame};
        mShaderProgram.process(inputs, output);

        // Push output
        pushOutput("image", output);

        // Release pushed frame
        output.release();
    
public voidsetupPorts()

        addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
        addOutputBasedOnInput("image", "image");
    
public voidtearDown(android.filterfw.core.FilterContext context)

        if (mDensityFrame != null) {
            mDensityFrame.release();
            mDensityFrame = null;
        }

        if (mHistFrame != null) {
            mHistFrame.release();
            mHistFrame = null;
        }