LomoishFilter.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;
30
31import java.util.Random;
32
33public class LomoishFilter extends Filter {
34
35    @GenerateFieldPort(name = "tile_size", hasDefault = true)
36    private int mTileSize = 640;
37
38    private Program mProgram;
39
40    private int mWidth = 0;
41    private int mHeight = 0;
42    private int mTarget = FrameFormat.TARGET_UNSPECIFIED;
43
44    private Frame mNoiseFrame;
45    private Random mRandom;
46
47    private final String mLomoishShader =
48            "precision mediump float;\n" +
49            "uniform sampler2D tex_sampler_0;\n" +
50            "uniform sampler2D tex_sampler_1;\n" +
51            "uniform float stepsizeX;\n" +
52            "uniform float stepsizeY;\n" +
53            "uniform float stepsize;\n" +
54            "uniform vec2 center;\n" +
55            "uniform float inv_max_dist;\n" +
56            "varying vec2 v_texcoord;\n" +
57            "void main() {\n" +
58            // sharpen
59            "  vec3 nbr_color = vec3(0.0, 0.0, 0.0);\n" +
60            "  vec2 coord;\n" +
61            "  vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" +
62            "  coord.x = v_texcoord.x - 0.5 * stepsizeX;\n" +
63            "  coord.y = v_texcoord.y - stepsizeY;\n" +
64            "  nbr_color += texture2D(tex_sampler_0, coord).rgb - color.rgb;\n" +
65            "  coord.x = v_texcoord.x - stepsizeX;\n" +
66            "  coord.y = v_texcoord.y + 0.5 * stepsizeY;\n" +
67            "  nbr_color += texture2D(tex_sampler_0, coord).rgb - color.rgb;\n" +
68            "  coord.x = v_texcoord.x + stepsizeX;\n" +
69            "  coord.y = v_texcoord.y - 0.5 * stepsizeY;\n" +
70            "  nbr_color += texture2D(tex_sampler_0, coord).rgb - color.rgb;\n" +
71            "  coord.x = v_texcoord.x + stepsizeX;\n" +
72            "  coord.y = v_texcoord.y + 0.5 * stepsizeY;\n" +
73            "  nbr_color += texture2D(tex_sampler_0, coord).rgb - color.rgb;\n" +
74            "  vec3 s_color = vec3(color.rgb + 0.3 * nbr_color);\n" +
75            // cross process
76            "  vec3 c_color = vec3(0.0, 0.0, 0.0);\n" +
77            "  float value;\n" +
78            "  if (s_color.r < 0.5) {\n" +
79            "    value = s_color.r;\n" +
80            "  } else {\n" +
81            "    value = 1.0 - s_color.r;\n" +
82            "  }\n" +
83            "  float red = 4.0 * value * value * value;\n" +
84            "  if (s_color.r < 0.5) {\n" +
85            "    c_color.r = red;\n" +
86            "  } else {\n" +
87            "    c_color.r = 1.0 - red;\n" +
88            "  }\n" +
89            "  if (s_color.g < 0.5) {\n" +
90            "    value = s_color.g;\n" +
91            "  } else {\n" +
92            "    value = 1.0 - s_color.g;\n" +
93            "  }\n" +
94            "  float green = 2.0 * value * value;\n" +
95            "  if (s_color.g < 0.5) {\n" +
96            "    c_color.g = green;\n" +
97            "  } else {\n" +
98            "    c_color.g = 1.0 - green;\n" +
99            "  }\n" +
100            "  c_color.b = s_color.b * 0.5 + 0.25;\n" +
101            // blackwhite
102            "  float dither = texture2D(tex_sampler_1, v_texcoord).r;\n" +
103            "  vec3 xform = clamp((c_color.rgb - 0.15) * 1.53846, 0.0, 1.0);\n" +
104            "  vec3 temp = clamp((color.rgb + stepsize - 0.15) * 1.53846, 0.0, 1.0);\n" +
105            "  vec3 bw_color = clamp(xform + (temp - xform) * (dither - 0.5), 0.0, 1.0);\n" +
106            // vignette
107            "  float dist = distance(gl_FragCoord.xy, center);\n" +
108            "  float lumen = 0.85 / (1.0 + exp((dist * inv_max_dist - 0.73) * 20.0)) + 0.15;\n" +
109            "  gl_FragColor = vec4(bw_color * lumen, color.a);\n" +
110            "}\n";
111
112    public LomoishFilter(String name) {
113        super(name);
114
115        mRandom = new Random();
116    }
117
118    @Override
119    public void setupPorts() {
120        addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
121        addOutputBasedOnInput("image", "image");
122    }
123
124    @Override
125    public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
126        return inputFormat;
127    }
128
129    @Override
130    public void tearDown(FilterContext context) {
131        if (mNoiseFrame != null) {
132            mNoiseFrame.release();
133            mNoiseFrame = null;
134        }
135    }
136
137    public void initProgram(FilterContext context, int target) {
138        switch (target) {
139            case FrameFormat.TARGET_GPU:
140                ShaderProgram shaderProgram = new ShaderProgram(context, mLomoishShader);
141                shaderProgram.setMaximumTileSize(mTileSize);
142                mProgram = shaderProgram;
143                break;
144
145            default:
146                throw new RuntimeException("Filter Sharpen does not support frames of " +
147                    "target " + target + "!");
148        }
149        mTarget = target;
150    }
151
152    private void initParameters() {
153        if (mProgram !=null) {
154            float centerX = (float) (0.5 * mWidth);
155            float centerY = (float) (0.5 * mHeight);
156            float center[] = {centerX, centerY};
157            float max_dist = (float) Math.sqrt(centerX * centerX + centerY * centerY);
158
159            mProgram.setHostValue("center", center);
160            mProgram.setHostValue("inv_max_dist", 1.0f / max_dist);
161
162            mProgram.setHostValue("stepsize", 1.0f / 255.0f);
163            mProgram.setHostValue("stepsizeX", 1.0f / mWidth);
164            mProgram.setHostValue("stepsizeY", 1.0f / mHeight);
165        }
166    }
167
168    @Override
169    public void process(FilterContext context) {
170        // Get input frame
171        Frame input = pullInput("image");
172        FrameFormat inputFormat = input.getFormat();
173
174        // Create program if not created already
175        if (mProgram == null || inputFormat.getTarget() != mTarget) {
176            initProgram(context, inputFormat.getTarget());
177        }
178
179        // Check if the frame size has changed
180        if (inputFormat.getWidth() != mWidth || inputFormat.getHeight() != mHeight) {
181            mWidth = inputFormat.getWidth();
182            mHeight = inputFormat.getHeight();
183
184            int[] buffer = new int[mWidth * mHeight];
185            for (int i = 0; i < mWidth * mHeight; ++i) {
186              buffer[i] = mRandom.nextInt(255);
187            }
188            FrameFormat format = ImageFormat.create(mWidth, mHeight,
189                                                    ImageFormat.COLORSPACE_RGBA,
190                                                    FrameFormat.TARGET_GPU);
191            if (mNoiseFrame != null) {
192                mNoiseFrame.release();
193            }
194            mNoiseFrame = context.getFrameManager().newFrame(format);
195            mNoiseFrame.setInts(buffer);
196
197            initParameters();
198        }
199
200        if (mNoiseFrame != null && (mNoiseFrame.getFormat().getWidth() != mWidth ||
201                                    mNoiseFrame.getFormat().getHeight() != mHeight)) {
202            throw new RuntimeException("Random map and imput image size mismatch!");
203        }
204
205        // Create output frame
206        Frame output = context.getFrameManager().newFrame(inputFormat);
207
208        // Process
209        Frame[] inputs = {input, mNoiseFrame};
210        mProgram.process(inputs, output);
211
212        // Push output
213        pushOutput("image", output);
214
215        // Release pushed frame
216        output.release();
217    }
218}
219