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;
30
31public class VignetteFilter extends Filter {
32
33    @GenerateFieldPort(name = "scale", hasDefault = true)
34    private float mScale = 0f;
35
36    @GenerateFieldPort(name = "tile_size", hasDefault = true)
37    private int mTileSize = 640;
38
39    private Program mProgram;
40
41    private int mWidth = 0;
42    private int mHeight = 0;
43    private int mTarget = FrameFormat.TARGET_UNSPECIFIED;
44
45    private final float mSlope = 20.0f;
46    private final float mShade = 0.85f;
47
48    private final String mVignetteShader =
49            "precision mediump float;\n" +
50            "uniform sampler2D tex_sampler_0;\n" +
51            "uniform float range;\n" +
52            "uniform float inv_max_dist;\n" +
53            "uniform float shade;\n" +
54            "uniform vec2 scale;\n" +
55            "varying vec2 v_texcoord;\n" +
56            "void main() {\n" +
57            "  const float slope = 20.0;\n" +
58            "  vec2 coord = v_texcoord - vec2(0.5, 0.5);\n" +
59            "  float dist = length(coord * scale);\n" +
60            "  float lumen = shade / (1.0 + exp((dist * inv_max_dist - range) * slope)) + (1.0 - shade);\n" +
61            "  vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" +
62            "  gl_FragColor = vec4(color.rgb * lumen, color.a);\n" +
63            "}\n";
64
65    public VignetteFilter(String name) {
66        super(name);
67    }
68
69    @Override
70    public void setupPorts() {
71        addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
72        addOutputBasedOnInput("image", "image");
73    }
74
75    @Override
76    public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
77        return inputFormat;
78    }
79
80    public void initProgram(FilterContext context, int target) {
81        switch (target) {
82            case FrameFormat.TARGET_GPU:
83                ShaderProgram shaderProgram = new ShaderProgram(context, mVignetteShader);
84                shaderProgram.setMaximumTileSize(mTileSize);
85                mProgram = shaderProgram;
86                break;
87
88            default:
89                throw new RuntimeException("Filter Sharpen does not support frames of " +
90                    "target " + target + "!");
91        }
92        mTarget = target;
93    }
94
95    private void initParameters() {
96        if (mProgram != null) {
97            float scale[] = new float[2];
98            if (mWidth > mHeight) {
99                scale[0] = 1f;
100                scale[1] = ((float) mHeight) / mWidth;
101            } else {
102                scale[0] = ((float) mWidth) / mHeight;
103                scale[1] = 1f;
104            }
105            float max_dist = ((float) Math.sqrt(scale[0] * scale[0] + scale[1] * scale[1])) * 0.5f;
106            mProgram.setHostValue("scale", scale);
107            mProgram.setHostValue("inv_max_dist", 1f / max_dist);
108            mProgram.setHostValue("shade", mShade);
109
110            updateParameters();
111        }
112    }
113
114    private void updateParameters() {
115        // The 'range' is between 1.3 to 0.6. When scale is zero then range is 1.3
116        // which means no vignette at all because the luminousity difference is
117        // less than 1/256 and will cause nothing.
118        mProgram.setHostValue("range", 1.30f - (float) Math.sqrt(mScale) * 0.7f);
119    }
120
121    @Override
122    public void fieldPortValueUpdated(String name, FilterContext context) {
123        if (mProgram != null) {
124            updateParameters();
125        }
126    }
127
128    @Override
129    public void process(FilterContext context) {
130        // Get input frame
131        Frame input = pullInput("image");
132        FrameFormat inputFormat = input.getFormat();
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            initParameters();
144        }
145
146        // Create output frame
147        Frame output = context.getFrameManager().newFrame(inputFormat);
148
149        // Process
150        mProgram.process(input, output);
151
152        // Push output
153        pushOutput("image", output);
154
155        // Release pushed frame
156        output.release();
157    }
158}
159