FileDocCategorySizeDatePackage
GrainFilter.javaAPI DocAndroid 5.1 API7168Thu Mar 12 22:22:30 GMT 2015android.filterpacks.imageproc

GrainFilter

public class GrainFilter extends android.filterfw.core.Filter

Fields Summary
private static final int
RAND_THRESHOLD
private float
mScale
private int
mTileSize
private android.filterfw.core.Program
mGrainProgram
private android.filterfw.core.Program
mNoiseProgram
private int
mWidth
private int
mHeight
private int
mTarget
private Random
mRandom
private final String
mNoiseShader
private final String
mGrainShader
Constructors Summary
public GrainFilter(String name)


       
        super(name);
        mRandom = new Random(new Date().getTime());
    
Methods Summary
public voidfieldPortValueUpdated(java.lang.String name, android.filterfw.core.FilterContext context)

        if (mGrainProgram != null && mNoiseProgram != null) {
            updateParameters();
        }
    
public android.filterfw.core.FrameFormatgetOutputFormat(java.lang.String portName, android.filterfw.core.FrameFormat inputFormat)

        return inputFormat;
    
public voidinitProgram(android.filterfw.core.FilterContext context, int target)

        switch (target) {
            case FrameFormat.TARGET_GPU:
                ShaderProgram shaderProgram = new ShaderProgram(context, mNoiseShader);
                shaderProgram.setMaximumTileSize(mTileSize);
                mNoiseProgram = shaderProgram;

                shaderProgram = new ShaderProgram(context, mGrainShader);
                shaderProgram.setMaximumTileSize(mTileSize);
                mGrainProgram = shaderProgram;
                break;

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

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

        FrameFormat noiseFormat = ImageFormat.create(inputFormat.getWidth() / 2,
                                                     inputFormat.getHeight() / 2,
                                                     ImageFormat.COLORSPACE_RGBA,
                                                     FrameFormat.TARGET_GPU);

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

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

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

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

        Frame[] empty = {};
        mNoiseProgram.process(empty, noiseFrame);

        // Process
        Frame[] inputs = {input, noiseFrame};
        mGrainProgram.process(inputs, output);

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

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

        addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
        addOutputBasedOnInput("image", "image");
    
private voidupdateFrameSize(int width, int height)

        mWidth = width;
        mHeight = height;

        if (mGrainProgram != null) {
            mGrainProgram.setHostValue("stepX", 0.5f / mWidth);
            mGrainProgram.setHostValue("stepY", 0.5f / mHeight);
            updateParameters();
        }
    
private voidupdateParameters()

        float seed[] = { mRandom.nextFloat(), mRandom.nextFloat() };
        mNoiseProgram.setHostValue("seed", seed);

        mGrainProgram.setHostValue("scale", mScale);