FileDocCategorySizeDatePackage
ToPackedGrayFilter.javaAPI DocAndroid 5.1 API4994Thu Mar 12 22:22:30 GMT 2015android.filterpacks.imageproc

ToPackedGrayFilter

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

Fields Summary
private int
mOWidth
private int
mOHeight
private boolean
mKeepAspectRatio
private android.filterfw.core.Program
mProgram
private final String
mColorToPackedGrayShader
Constructors Summary
public ToPackedGrayFilter(String name)


       
        super(name);
    
Methods Summary
private voidcheckOutputDimensions(int outputWidth, int outputHeight)

        if (outputWidth <= 0 || outputHeight <= 0) {
            throw new RuntimeException("Invalid output dimensions: " +
                                       outputWidth + " " + outputHeight);
        }
    
private android.filterfw.core.FrameFormatconvertInputFormat(android.filterfw.core.FrameFormat inputFormat)

        int ow = mOWidth;
        int oh = mOHeight;
        int w = inputFormat.getWidth();
        int h = inputFormat.getHeight();
        if (mOWidth == FrameFormat.SIZE_UNSPECIFIED) {
            ow = w;
        }
        if (mOHeight == FrameFormat.SIZE_UNSPECIFIED) {
            oh = h;
        }
        if (mKeepAspectRatio) {
            // if keep aspect ratio, use the bigger dimension to determine the
            // final output size
            if (w > h) {
                ow = Math.max(ow, oh);
                oh = ow * h / w;
            } else {
                oh = Math.max(ow, oh);
                ow = oh * w / h;
            }
        }
        ow = (ow > 0 && ow < 4) ? 4 : (ow / 4) * 4; // ensure width is multiple of 4
        return ImageFormat.create(ow, oh,
                                  ImageFormat.COLORSPACE_GRAY,
                                  FrameFormat.TARGET_NATIVE);
    
public android.filterfw.core.FrameFormatgetOutputFormat(java.lang.String portName, android.filterfw.core.FrameFormat inputFormat)

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

        mProgram = new ShaderProgram(context, mColorToPackedGrayShader);
    
public voidprocess(android.filterfw.core.FilterContext context)

        Frame input = pullInput("image");
        FrameFormat inputFormat = input.getFormat();
        FrameFormat outputFormat = convertInputFormat(inputFormat);
        int ow = outputFormat.getWidth();
        int oh = outputFormat.getHeight();
        checkOutputDimensions(ow, oh);
        mProgram.setHostValue("pix_stride", 1.0f / ow);

        // Do the RGBA to luminance conversion.
        MutableFrameFormat tempFrameFormat = inputFormat.mutableCopy();
        tempFrameFormat.setDimensions(ow / 4, oh);
        Frame temp = context.getFrameManager().newFrame(tempFrameFormat);
        mProgram.process(input, temp);

        // Read frame from GPU to CPU.
        Frame output = context.getFrameManager().newFrame(outputFormat);
        output.setDataFromFrame(temp);
        temp.release();

        // Push output and yield ownership.
        pushOutput("image", output);
        output.release();
    
public voidsetupPorts()

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