Home | History | Annotate | Download | only in java
      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.KeyValueMap;
     25 import android.filterfw.core.MutableFrameFormat;
     26 import android.filterfw.core.NativeProgram;
     27 import android.filterfw.core.NativeFrame;
     28 import android.filterfw.core.Program;
     29 import android.filterfw.core.ShaderProgram;
     30 import android.filterfw.format.ImageFormat;
     31 
     32 /**
     33  * @hide
     34  */
     35 public class FlipFilter extends Filter {
     36 
     37     @GenerateFieldPort(name = "vertical", hasDefault = true)
     38     private boolean mVertical = false;
     39 
     40     @GenerateFieldPort(name = "horizontal", hasDefault = true)
     41     private boolean mHorizontal = false;
     42 
     43     @GenerateFieldPort(name = "tile_size", hasDefault = true)
     44     private int mTileSize = 640;
     45 
     46     private Program mProgram;
     47     private int mTarget = FrameFormat.TARGET_UNSPECIFIED;
     48 
     49     public FlipFilter(String name) {
     50         super(name);
     51     }
     52 
     53     @Override
     54     public void setupPorts() {
     55         addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
     56         addOutputBasedOnInput("image", "image");
     57     }
     58 
     59     @Override
     60     public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
     61         return inputFormat;
     62     }
     63 
     64     public void initProgram(FilterContext context, int target) {
     65         switch (target) {
     66             case FrameFormat.TARGET_GPU:
     67                 ShaderProgram shaderProgram = ShaderProgram.createIdentity(context);
     68                 shaderProgram.setMaximumTileSize(mTileSize);
     69                 mProgram = shaderProgram;
     70                 break;
     71 
     72             default:
     73                 throw new RuntimeException("Filter Sharpen does not support frames of " +
     74                     "target " + target + "!");
     75         }
     76         mTarget = target;
     77         updateParameters();
     78     }
     79 
     80     @Override
     81     public void fieldPortValueUpdated(String name, FilterContext context) {
     82         if (mProgram != null) {
     83             updateParameters();
     84         }
     85     }
     86 
     87     @Override
     88     public void process(FilterContext context) {
     89         // Get input frame
     90         Frame input = pullInput("image");
     91         FrameFormat inputFormat = input.getFormat();
     92 
     93         // Create program if not created already
     94         if (mProgram == null || inputFormat.getTarget() != mTarget) {
     95             initProgram(context, inputFormat.getTarget());
     96         }
     97 
     98         // Create output frame
     99         Frame output = context.getFrameManager().newFrame(inputFormat);
    100 
    101         // Process
    102         mProgram.process(input, output);
    103 
    104         // Push output
    105         pushOutput("image", output);
    106 
    107         // Release pushed frame
    108         output.release();
    109     }
    110 
    111     private void updateParameters() {
    112         float x_origin = (mHorizontal) ? 1.0f : 0.0f;
    113         float y_origin = (mVertical) ? 1.0f : 0.0f;
    114 
    115         float width  = (mHorizontal) ? -1.0f : 1.0f;
    116         float height  = (mVertical) ? -1.0f : 1.0f;
    117 
    118         ((ShaderProgram) mProgram).setSourceRect(x_origin, y_origin, width, height);
    119     }
    120 }
    121