RedEyeFilter.java revision 65953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9
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
17package android.filterpacks.imageproc;
18
19import android.filterfw.core.Filter;
20import android.filterfw.core.FilterContext;
21import android.filterfw.core.Frame;
22import android.filterfw.core.FrameFormat;
23import android.filterfw.core.GenerateFieldPort;
24import android.filterfw.core.KeyValueMap;
25import android.filterfw.core.NativeProgram;
26import android.filterfw.core.NativeFrame;
27import android.filterfw.core.Program;
28import android.filterfw.core.ShaderProgram;
29import android.filterfw.format.ImageFormat;
30import android.graphics.Bitmap;
31import android.graphics.Canvas;
32import android.graphics.Color;
33import android.graphics.Paint;
34import android.graphics.PointF;
35import android.util.Log;
36
37/**
38 * @hide
39 */
40public class RedEyeFilter extends Filter {
41
42    private static final float RADIUS_RATIO = 0.06f;
43    private static final float MIN_RADIUS = 10.0f;
44    private static final float DEFAULT_RED_INTENSITY = 1.30f;
45
46    @GenerateFieldPort(name = "centers")
47    private float[] mCenters;
48
49    @GenerateFieldPort(name = "tile_size", hasDefault = true)
50    private int mTileSize = 640;
51
52    private Frame mRedEyeFrame;
53    private Bitmap mRedEyeBitmap;
54
55    private final Canvas mCanvas = new Canvas();
56    private final Paint mPaint = new Paint();
57
58    private float mRadius;
59
60    private int mWidth = 0;
61    private int mHeight = 0;
62
63    private Program mProgram;
64    private int mTarget = FrameFormat.TARGET_UNSPECIFIED;
65
66    private final String mRedEyeShader =
67            "precision mediump float;\n" +
68            "uniform sampler2D tex_sampler_0;\n" +
69            "uniform sampler2D tex_sampler_1;\n" +
70            "uniform float intensity;\n" +
71            "varying vec2 v_texcoord;\n" +
72            "void main() {\n" +
73            "  vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" +
74            "  vec4 mask = texture2D(tex_sampler_1, v_texcoord);\n" +
75            "  gl_FragColor = vec4(mask.a, mask.a, mask.a, 1.0) * intensity + color * (1.0 - intensity);\n" +
76            "  if (mask.a > 0.0) {\n" +
77            "    gl_FragColor.r = 0.0;\n" +
78            "    float green_blue = color.g + color.b;\n" +
79            "    float red_intensity = color.r / green_blue;\n" +
80            "    if (red_intensity > intensity) {\n" +
81            "      color.r = 0.5 * green_blue;\n" +
82            "    }\n" +
83            "  }\n" +
84            "  gl_FragColor = color;\n" +
85            "}\n";
86
87    public RedEyeFilter(String name) {
88        super(name);
89    }
90
91    @Override
92    public void setupPorts() {
93        addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
94        addOutputBasedOnInput("image", "image");
95    }
96
97    @Override
98    public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
99        return inputFormat;
100    }
101
102    public void initProgram(FilterContext context, int target) {
103        switch (target) {
104            case FrameFormat.TARGET_GPU:
105                ShaderProgram shaderProgram = new ShaderProgram(context, mRedEyeShader);
106                shaderProgram.setMaximumTileSize(mTileSize);
107                mProgram = shaderProgram;
108                break;
109
110            default:
111                throw new RuntimeException("Filter RedEye does not support frames of " +
112                    "target " + target + "!");
113        }
114        mTarget = target;
115    }
116
117    @Override
118    public void tearDown(FilterContext context) {
119        if (mRedEyeBitmap != null) {
120            mRedEyeBitmap.recycle();
121            mRedEyeBitmap = null;
122        }
123    }
124
125    @Override
126    public void process(FilterContext context) {
127        // Get input frame
128        Frame input = pullInput("image");
129        FrameFormat inputFormat = input.getFormat();
130
131        // Create output frame
132        Frame output = context.getFrameManager().newFrame(inputFormat);
133
134        // Create program if not created already
135        if (mProgram == null || inputFormat.getTarget() != mTarget) {
136            initProgram(context, inputFormat.getTarget());
137        }
138
139        // Check if the frame size has changed
140        if (inputFormat.getWidth() != mWidth || inputFormat.getHeight() != mHeight) {
141            mWidth = inputFormat.getWidth();
142            mHeight = inputFormat.getHeight();
143
144            createRedEyeBitmap();
145        }
146
147        createRedEyeFrame(context);
148
149        // Process
150        Frame[] inputs = {input, mRedEyeFrame};
151        mProgram.process(inputs, output);
152
153        // Push output
154        pushOutput("image", output);
155
156        // Release pushed frame
157        output.release();
158
159        // Release unused frame
160        mRedEyeFrame.release();
161        mRedEyeFrame = null;
162    }
163
164    @Override
165    public void fieldPortValueUpdated(String name, FilterContext context) {
166         if (mProgram != null) {
167            updateProgramParams();
168        }
169    }
170
171    private void createRedEyeBitmap() {
172        if (mRedEyeBitmap != null) {
173            mRedEyeBitmap.recycle();
174        }
175
176        int bitmapWidth = mWidth / 2;
177        int bitmapHeight = mHeight / 2;
178
179        mRedEyeBitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
180        mCanvas.setBitmap(mRedEyeBitmap);
181        mPaint.setColor(Color.WHITE);
182        mRadius = Math.max(MIN_RADIUS, RADIUS_RATIO * Math.min(bitmapWidth, bitmapHeight));
183
184        updateProgramParams();
185    }
186
187    private void createRedEyeFrame(FilterContext context) {
188        FrameFormat format = ImageFormat.create(mRedEyeBitmap.getWidth() ,
189                                                mRedEyeBitmap.getHeight(),
190                                                ImageFormat.COLORSPACE_RGBA,
191                                                FrameFormat.TARGET_GPU);
192        mRedEyeFrame = context.getFrameManager().newFrame(format);
193        mRedEyeFrame.setBitmap(mRedEyeBitmap);
194    }
195
196    private void updateProgramParams() {
197        mProgram.setHostValue("intensity", DEFAULT_RED_INTENSITY);
198
199        if ( mCenters.length % 2 == 1) {
200            throw new RuntimeException("The size of center array must be even.");
201        }
202
203        if (mRedEyeBitmap != null) {
204            for (int i = 0; i < mCenters.length; i += 2) {
205                mCanvas.drawCircle(mCenters[i] * mRedEyeBitmap.getWidth(),
206                                   mCenters[i + 1] * mRedEyeBitmap.getHeight(),
207                                   mRadius, mPaint);
208            }
209        }
210    }
211}
212