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