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 public class PosterizeFilter extends Filter {
     29 
     30     @GenerateFieldPort(name = "tile_size", hasDefault = true)
     31     private int mTileSize = 640;
     32 
     33     private Program mProgram;
     34     private int mTarget = FrameFormat.TARGET_UNSPECIFIED;
     35 
     36     private final String mPosterizeShader =
     37             "precision mediump float;\n" +
     38             "uniform sampler2D tex_sampler_0;\n" +
     39             "varying vec2 v_texcoord;\n" +
     40             "void main() {\n" +
     41             "  vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" +
     42             "  vec3 pcolor;\n" +
     43             "  pcolor.r = (color.r >= 0.5) ? 0.75 : 0.25;\n" +
     44             "  pcolor.g = (color.g >= 0.5) ? 0.75 : 0.25;\n" +
     45             "  pcolor.b = (color.b >= 0.5) ? 0.75 : 0.25;\n" +
     46             "  gl_FragColor = vec4(pcolor, color.a);\n" +
     47             "}\n";
     48 
     49     public PosterizeFilter(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 = new ShaderProgram(context, mPosterizeShader);
     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     }
     78 
     79     @Override
     80     public void process(FilterContext context) {
     81         // Get input frame
     82         Frame input = pullInput("image");
     83         FrameFormat inputFormat = input.getFormat();
     84 
     85         // Create output frame
     86         Frame output = context.getFrameManager().newFrame(inputFormat);
     87 
     88         // Create program if not created already
     89         if (mProgram == null || inputFormat.getTarget() != mTarget) {
     90             initProgram(context, inputFormat.getTarget());
     91         }
     92 
     93         // Process
     94         mProgram.process(input, output);
     95 
     96         // Push output
     97         pushOutput("image", output);
     98 
     99         // Release pushed frame
    100         output.release();
    101     }
    102 
    103 }
    104