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