FileDocCategorySizeDatePackage
StraightenFilter.javaAPI DocAndroid 5.1 API5106Thu Mar 12 22:22:30 GMT 2015android.filterpacks.imageproc

StraightenFilter

public class StraightenFilter extends android.filterfw.core.Filter
hide

Fields Summary
private float
mAngle
private float
mMaxAngle
private int
mTileSize
private android.filterfw.core.Program
mProgram
private int
mWidth
private int
mHeight
private int
mTarget
private static final float
DEGREE_TO_RADIAN
Constructors Summary
public StraightenFilter(String name)


       
        super(name);
    
Methods Summary
public voidfieldPortValueUpdated(java.lang.String name, android.filterfw.core.FilterContext context)

        if (mProgram != null) {
            updateParameters();
        }
    
public voidinitProgram(android.filterfw.core.FilterContext context, int target)

        switch (target) {
            case FrameFormat.TARGET_GPU:
                ShaderProgram shaderProgram = ShaderProgram.createIdentity(context);
                shaderProgram.setMaximumTileSize(mTileSize);
                mProgram = 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();

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

        // Create output frame
        if (inputFormat.getWidth() != mWidth || inputFormat.getHeight() != mHeight) {
            mWidth = inputFormat.getWidth();
            mHeight = inputFormat.getHeight();
            updateParameters();
        }

        Frame output = context.getFrameManager().newFrame(inputFormat);

        // Process
        mProgram.process(input, output);

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

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

        addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
        addOutputBasedOnInput("image", "image");
    
private voidupdateParameters()

        float cosTheta = (float) Math.cos(mAngle * DEGREE_TO_RADIAN);
        float sinTheta = (float) Math.sin(mAngle * DEGREE_TO_RADIAN);

        if (mMaxAngle <= 0)
            throw new RuntimeException("Max angle is out of range (0-180).");
        mMaxAngle = (mMaxAngle > 90) ? 90 : mMaxAngle;

        Point p0 = new Point(-cosTheta * mWidth + sinTheta * mHeight,
                             -sinTheta * mWidth - cosTheta * mHeight);

        Point p1 = new Point(cosTheta * mWidth + sinTheta * mHeight,
                             sinTheta * mWidth - cosTheta * mHeight);

        Point p2 = new Point(-cosTheta * mWidth - sinTheta * mHeight,
                             -sinTheta * mWidth + cosTheta * mHeight);

        Point p3 = new Point(cosTheta * mWidth - sinTheta * mHeight,
                             sinTheta * mWidth + cosTheta * mHeight);

        float maxWidth = (float) Math.max(Math.abs(p0.x), Math.abs(p1.x));
        float maxHeight = (float) Math.max(Math.abs(p0.y), Math.abs(p1.y));

        float scale = 0.5f * Math.min( mWidth / maxWidth,
                                       mHeight / maxHeight);

        p0.set(scale * p0.x / mWidth + 0.5f, scale * p0.y / mHeight + 0.5f);
        p1.set(scale * p1.x / mWidth + 0.5f, scale * p1.y / mHeight + 0.5f);
        p2.set(scale * p2.x / mWidth + 0.5f, scale * p2.y / mHeight + 0.5f);
        p3.set(scale * p3.x / mWidth + 0.5f, scale * p3.y / mHeight + 0.5f);

        Quad quad = new Quad(p0, p1, p2, p3);
        ((ShaderProgram) mProgram).setSourceRegion(quad);