FileDocCategorySizeDatePackage
ImageSlicer.javaAPI DocAndroid 5.1 API4491Thu Mar 12 22:22:30 GMT 2015android.filterpacks.imageproc

ImageSlicer

public class ImageSlicer extends android.filterfw.core.Filter

Fields Summary
private int
mXSlices
private int
mYSlices
private int
mPadSize
private int
mSliceIndex
private android.filterfw.core.Frame
mOriginalFrame
private android.filterfw.core.Program
mProgram
private int
mInputWidth
private int
mInputHeight
private int
mSliceWidth
private int
mSliceHeight
private int
mOutputWidth
private int
mOutputHeight
Constructors Summary
public ImageSlicer(String name)

        super(name);
        mSliceIndex = 0;
    
Methods Summary
private voidcalcOutputFormatForInput(android.filterfw.core.Frame frame)


        // calculate the output size based on input size, xSlices, and ySlices
        mInputWidth = frame.getFormat().getWidth();
        mInputHeight = frame.getFormat().getHeight();

        mSliceWidth = (mInputWidth + mXSlices - 1) / mXSlices;
        mSliceHeight = (mInputHeight + mYSlices - 1)/ mYSlices;

        mOutputWidth = mSliceWidth + mPadSize * 2;
        mOutputHeight = mSliceHeight + mPadSize * 2;
    
public android.filterfw.core.FrameFormatgetOutputFormat(java.lang.String portName, android.filterfw.core.FrameFormat inputFormat)

        return inputFormat;
    
public voidprocess(android.filterfw.core.FilterContext context)


        // Get input frame
        if (mSliceIndex == 0) {
            mOriginalFrame = pullInput("image");
            calcOutputFormatForInput(mOriginalFrame);
        }

        FrameFormat inputFormat = mOriginalFrame.getFormat();
        MutableFrameFormat outputFormat = inputFormat.mutableCopy();
        outputFormat.setDimensions(mOutputWidth, mOutputHeight);

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

        // Create the program if not created already
        if (mProgram == null) {
            mProgram = ShaderProgram.createIdentity(context);
        }

        // Calculate the four corner of the source region
        int xSliceIndex = mSliceIndex % mXSlices;
        int ySliceIndex = mSliceIndex / mXSlices;

        // TODO(rslin) : not sure shifting by 0.5 is needed.
        float x0 = (xSliceIndex * mSliceWidth - mPadSize) / ((float) mInputWidth);
        float y0 = (ySliceIndex * mSliceHeight - mPadSize) / ((float) mInputHeight);

        ((ShaderProgram) mProgram).setSourceRect(x0, y0,
                                                 ((float) mOutputWidth) / mInputWidth,
                                                 ((float) mOutputHeight) / mInputHeight);

        // Process
        mProgram.process(mOriginalFrame, output);
        mSliceIndex++;

        if (mSliceIndex == mXSlices * mYSlices) {
            mSliceIndex = 0;
            mOriginalFrame.release();
            setWaitsOnInputPort("image", true);
        } else {
            // Retain the original frame so it can be used next time.
            mOriginalFrame.retain();
            setWaitsOnInputPort("image", false);
        }

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

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

        addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA,
                                                       FrameFormat.TARGET_GPU));
        addOutputBasedOnInput("image", "image");