Home | History | Annotate | Download | only in imageproc
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.filterpacks.imageproc;
     18 
     19 import android.filterfw.core.Filter;
     20 import android.filterfw.core.FilterContext;
     21 import android.filterfw.core.Frame;
     22 import android.filterfw.core.FrameFormat;
     23 import android.filterfw.core.GenerateFieldPort;
     24 import android.filterfw.core.MutableFrameFormat;
     25 import android.filterfw.core.Program;
     26 import android.filterfw.core.ShaderProgram;
     27 import android.filterfw.format.ImageFormat;
     28 
     29 public class ImageSlicer extends Filter {
     30 
     31     @GenerateFieldPort(name = "xSlices")
     32     private int mXSlices;
     33 
     34     @GenerateFieldPort(name = "ySlices")
     35     private int mYSlices;
     36 
     37     @GenerateFieldPort(name = "padSize")
     38     private int mPadSize;
     39 
     40     // The current slice index from 0 to xSlices * ySlices
     41     private int mSliceIndex;
     42 
     43     private Frame mOriginalFrame;
     44 
     45     private Program mProgram;
     46 
     47     private int mInputWidth;
     48     private int mInputHeight;
     49 
     50     private int mSliceWidth;
     51     private int mSliceHeight;
     52 
     53     private int mOutputWidth;
     54     private int mOutputHeight;
     55 
     56     public ImageSlicer(String name) {
     57         super(name);
     58         mSliceIndex = 0;
     59     }
     60 
     61     @Override
     62     public void setupPorts() {
     63         addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA,
     64                                                        FrameFormat.TARGET_GPU));
     65         addOutputBasedOnInput("image", "image");
     66     }
     67 
     68     @Override
     69     public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
     70         return inputFormat;
     71     }
     72 
     73     private void calcOutputFormatForInput(Frame frame) {
     74 
     75         // calculate the output size based on input size, xSlices, and ySlices
     76         mInputWidth = frame.getFormat().getWidth();
     77         mInputHeight = frame.getFormat().getHeight();
     78 
     79         mSliceWidth = (mInputWidth + mXSlices - 1) / mXSlices;
     80         mSliceHeight = (mInputHeight + mYSlices - 1)/ mYSlices;
     81 
     82         mOutputWidth = mSliceWidth + mPadSize * 2;
     83         mOutputHeight = mSliceHeight + mPadSize * 2;
     84     }
     85 
     86 
     87     @Override
     88     public void process(FilterContext context) {
     89 
     90         // Get input frame
     91         if (mSliceIndex == 0) {
     92             mOriginalFrame = pullInput("image");
     93             calcOutputFormatForInput(mOriginalFrame);
     94         }
     95 
     96         FrameFormat inputFormat = mOriginalFrame.getFormat();
     97         MutableFrameFormat outputFormat = inputFormat.mutableCopy();
     98         outputFormat.setDimensions(mOutputWidth, mOutputHeight);
     99 
    100         // Create output frame
    101         Frame output = context.getFrameManager().newFrame(outputFormat);
    102 
    103         // Create the program if not created already
    104         if (mProgram == null) {
    105             mProgram = ShaderProgram.createIdentity(context);
    106         }
    107 
    108         // Calculate the four corner of the source region
    109         int xSliceIndex = mSliceIndex % mXSlices;
    110         int ySliceIndex = mSliceIndex / mXSlices;
    111 
    112         // TODO(rslin) : not sure shifting by 0.5 is needed.
    113         float x0 = (xSliceIndex * mSliceWidth - mPadSize) / ((float) mInputWidth);
    114         float y0 = (ySliceIndex * mSliceHeight - mPadSize) / ((float) mInputHeight);
    115 
    116         ((ShaderProgram) mProgram).setSourceRect(x0, y0,
    117                                                  ((float) mOutputWidth) / mInputWidth,
    118                                                  ((float) mOutputHeight) / mInputHeight);
    119 
    120         // Process
    121         mProgram.process(mOriginalFrame, output);
    122         mSliceIndex++;
    123 
    124         if (mSliceIndex == mXSlices * mYSlices) {
    125             mSliceIndex = 0;
    126             mOriginalFrame.release();
    127             setWaitsOnInputPort("image", true);
    128         } else {
    129             // Retain the original frame so it can be used next time.
    130             mOriginalFrame.retain();
    131             setWaitsOnInputPort("image", false);
    132         }
    133 
    134         // Push output
    135         pushOutput("image", output);
    136 
    137         // Release pushed frame
    138         output.release();
    139     }
    140 }
    141