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.Program;
     25 import android.filterfw.core.ShaderProgram;
     26 import android.filterfw.format.ImageFormat;
     27 
     28 
     29 public class NegativeFilter extends Filter {
     30 
     31     @GenerateFieldPort(name = "tile_size", hasDefault = true)
     32     private int mTileSize = 640;
     33 
     34     private Program mProgram;
     35     private int mTarget = FrameFormat.TARGET_UNSPECIFIED;
     36 
     37     private final String mNegativeShader =
     38             "precision mediump float;\n" +
     39             "uniform sampler2D tex_sampler_0;\n" +
     40             "varying vec2 v_texcoord;\n" +
     41             "void main() {\n" +
     42             "  vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" +
     43             "  gl_FragColor = vec4(1.0 - color.rgb, color.a);\n" +
     44             "}\n";
     45 
     46     public NegativeFilter(String name) {
     47         super(name);
     48     }
     49 
     50     @Override
     51     public void setupPorts() {
     52         addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
     53         addOutputBasedOnInput("image", "image");
     54     }
     55 
     56     @Override
     57     public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
     58         return inputFormat;
     59     }
     60 
     61     public void initProgram(FilterContext context, int target) {
     62         switch (target) {
     63             case FrameFormat.TARGET_GPU:
     64                 ShaderProgram shaderProgram = new ShaderProgram(context, mNegativeShader);
     65                 shaderProgram.setMaximumTileSize(mTileSize);
     66                 mProgram = shaderProgram;
     67                 break;
     68 
     69             default:
     70                 throw new RuntimeException("Filter Sharpen does not support frames of " +
     71                     "target " + target + "!");
     72         }
     73         mTarget = target;
     74     }
     75 
     76     @Override
     77     public void process(FilterContext context) {
     78         // Get input frame
     79         Frame input = pullInput("image");
     80         FrameFormat inputFormat = input.getFormat();
     81 
     82         // Create output frame
     83         Frame output = context.getFrameManager().newFrame(inputFormat);
     84 
     85         // Create program if not created already
     86         if (mProgram == null || inputFormat.getTarget() != mTarget) {
     87             initProgram(context, inputFormat.getTarget());
     88         }
     89 
     90         // Process
     91         mProgram.process(input, output);
     92 
     93         // Push output
     94         pushOutput("image", output);
     95 
     96         // Release pushed frame
     97         output.release();
     98     }
     99 
    100 }
    101