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