GrainFilterpublic 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 void | fieldPortValueUpdated(java.lang.String name, android.filterfw.core.FilterContext context)
if (mGrainProgram != null && mNoiseProgram != null) {
updateParameters();
}
| public android.filterfw.core.FrameFormat | getOutputFormat(java.lang.String portName, android.filterfw.core.FrameFormat inputFormat)
return inputFormat;
| public void | initProgram(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 void | process(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 void | setupPorts()
addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
addOutputBasedOnInput("image", "image");
| private void | updateFrameSize(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 void | updateParameters()
float seed[] = { mRandom.nextFloat(), mRandom.nextFloat() };
mNoiseProgram.setHostValue("seed", seed);
mGrainProgram.setHostValue("scale", mScale);
|
|