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            "  if (mask.a > 0.0) {\n" +
76            "    float green_blue = color.g + color.b;\n" +
77            "    float red_intensity = color.r / green_blue;\n" +
78            "    if (red_intensity > intensity) {\n" +
79            "      color.r = 0.5 * green_blue;\n" +
80            "    }\n" +
81            "  }\n" +
82            "  gl_FragColor = color;\n" +
83            "}\n";
84
85    public RedEyeFilter(String name) {
86        super(name);
87    }
88
89    @Override
90    public void setupPorts() {
91        addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
92        addOutputBasedOnInput("image", "image");
93    }
94
95    @Override
96    public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
97        return inputFormat;
98    }
99
100    public void initProgram(FilterContext context, int target) {
101        switch (target) {
102            case FrameFormat.TARGET_GPU:
103                ShaderProgram shaderProgram = new ShaderProgram(context, mRedEyeShader);
104                shaderProgram.setMaximumTileSize(mTileSize);
105                mProgram = shaderProgram;
106                mProgram.setHostValue("intensity", DEFAULT_RED_INTENSITY);
107                break;
108            default:
109                throw new RuntimeException("Filter RedEye does not support frames of " +
110                    "target " + target + "!");
111        }
112        mTarget = target;
113    }
114
115    @Override
116    public void process(FilterContext context) {
117        // Get input frame
118        Frame input = pullInput("image");
119        FrameFormat inputFormat = input.getFormat();
120
121        // Create output frame
122        Frame output = context.getFrameManager().newFrame(inputFormat);
123
124        // Create program if not created already
125        if (mProgram == null || inputFormat.getTarget() != mTarget) {
126            initProgram(context, inputFormat.getTarget());
127        }
128
129        // Check if the frame size has changed
130        if (inputFormat.getWidth() != mWidth || inputFormat.getHeight() != mHeight) {
131            mWidth = inputFormat.getWidth();
132            mHeight = inputFormat.getHeight();
133        }
134        createRedEyeFrame(context);
135
136        // Process
137        Frame[] inputs = {input, mRedEyeFrame};
138        mProgram.process(inputs, output);
139
140        // Push output
141        pushOutput("image", output);
142
143        // Release pushed frame
144        output.release();
145
146        // Release unused frame
147        mRedEyeFrame.release();
148        mRedEyeFrame = null;
149    }
150
151    @Override
152    public void fieldPortValueUpdated(String name, FilterContext context) {
153         if (mProgram != null) {
154            updateProgramParams();
155        }
156    }
157
158    private void createRedEyeFrame(FilterContext context) {
159        int bitmapWidth = mWidth / 2;
160        int bitmapHeight = mHeight / 2;
161
162        Bitmap redEyeBitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
163        mCanvas.setBitmap(redEyeBitmap);
164        mPaint.setColor(Color.WHITE);
165        mRadius = Math.max(MIN_RADIUS, RADIUS_RATIO * Math.min(bitmapWidth, bitmapHeight));
166
167        for (int i = 0; i < mCenters.length; i += 2) {
168            mCanvas.drawCircle(mCenters[i] * bitmapWidth, mCenters[i + 1] * bitmapHeight,
169                               mRadius, mPaint);
170        }
171
172        FrameFormat format = ImageFormat.create(bitmapWidth, bitmapHeight,
173                                                ImageFormat.COLORSPACE_RGBA,
174                                                FrameFormat.TARGET_GPU);
175        mRedEyeFrame = context.getFrameManager().newFrame(format);
176        mRedEyeFrame.setBitmap(redEyeBitmap);
177        redEyeBitmap.recycle();
178    }
179
180    private void updateProgramParams() {
181        if ( mCenters.length % 2 == 1) {
182            throw new RuntimeException("The size of center array must be even.");
183        }
184    }
185}
186