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 
     18 package android.filterpacks.imageproc;
     19 
     20 import android.filterfw.core.Filter;
     21 import android.filterfw.core.FilterContext;
     22 import android.filterfw.core.Frame;
     23 import android.filterfw.core.FrameFormat;
     24 import android.filterfw.core.GenerateFieldPort;
     25 import android.filterfw.core.GLFrame;
     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.geometry.Quad;
     31 import android.filterfw.format.ImageFormat;
     32 import android.filterfw.format.ObjectFormat;
     33 
     34 import android.opengl.GLES20;
     35 
     36 /**
     37  * @hide
     38  */
     39 public class DrawRectFilter extends Filter {
     40 
     41     @GenerateFieldPort(name = "colorRed",  hasDefault = true)
     42     private float mColorRed = 0.8f;
     43 
     44     @GenerateFieldPort(name = "colorGreen", hasDefault = true)
     45     private float mColorGreen = 0.8f;
     46 
     47     @GenerateFieldPort(name = "colorBlue", hasDefault = true)
     48     private float mColorBlue = 0.0f;
     49 
     50     private final String mVertexShader =
     51         "attribute vec4 aPosition;\n" +
     52         "void main() {\n" +
     53         "  gl_Position = aPosition;\n" +
     54         "}\n";
     55 
     56     private final String mFixedColorFragmentShader =
     57         "precision mediump float;\n" +
     58         "uniform vec4 color;\n" +
     59         "void main() {\n" +
     60         "  gl_FragColor = color;\n" +
     61         "}\n";
     62 
     63     private ShaderProgram mProgram;
     64 
     65 
     66     public DrawRectFilter(String name) {
     67         super(name);
     68     }
     69 
     70     @Override
     71     public void setupPorts() {
     72         addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA,
     73                                                        FrameFormat.TARGET_GPU));
     74         addMaskedInputPort("box", ObjectFormat.fromClass(Quad.class, FrameFormat.TARGET_SIMPLE));
     75         addOutputBasedOnInput("image", "image");
     76     }
     77 
     78     @Override
     79     public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
     80         return inputFormat;
     81     }
     82 
     83     @Override
     84     public void prepare(FilterContext context) {
     85         mProgram = new ShaderProgram(context, mVertexShader, mFixedColorFragmentShader);
     86     }
     87 
     88     @Override
     89     public void process(FilterContext env) {
     90         // Get input frame
     91         Frame imageFrame = pullInput("image");
     92         Frame boxFrame = pullInput("box");
     93 
     94         // Get the box
     95         Quad box = (Quad)boxFrame.getObjectValue();
     96         box = box.scaled(2.0f).translated(-1.0f, -1.0f);
     97 
     98         // Create output frame with copy of input
     99         GLFrame output = (GLFrame)env.getFrameManager().duplicateFrame(imageFrame);
    100 
    101         // Draw onto output
    102         output.focus();
    103         renderBox(box);
    104 
    105         // Push output
    106         pushOutput("image", output);
    107 
    108         // Release pushed frame
    109         output.release();
    110     }
    111 
    112     private void renderBox(Quad box) {
    113         final int FLOAT_SIZE = 4;
    114 
    115         // Get current values
    116         float[] color = {mColorRed, mColorGreen, mColorBlue, 1f};
    117         float[] vertexValues = { box.p0.x, box.p0.y,
    118                                  box.p1.x, box.p1.y,
    119                                  box.p3.x, box.p3.y,
    120                                  box.p2.x, box.p2.y };
    121 
    122         // Set the program variables
    123         mProgram.setHostValue("color", color);
    124         mProgram.setAttributeValues("aPosition", vertexValues, 2);
    125         mProgram.setVertexCount(4);
    126 
    127         // Draw
    128         mProgram.beginDrawing();
    129         GLES20.glLineWidth(1.0f);
    130         GLES20.glDrawArrays(GLES20.GL_LINE_LOOP, 0, 4);
    131     }
    132 }
    133