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 DocumentaryFilter 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 mDocumentaryShader =
47            "precision mediump float;\n" +
48            "uniform sampler2D tex_sampler_0;\n" +
49            "uniform vec2 seed;\n" +
50            "uniform float stepsize;\n" +
51            "uniform float inv_max_dist;\n" +
52            "uniform vec2 scale;\n" +
53            "varying vec2 v_texcoord;\n" +
54            "float rand(vec2 loc) {\n" +
55            "  float theta1 = dot(loc, vec2(0.9898, 0.233));\n" +
56            "  float theta2 = dot(loc, vec2(12.0, 78.0));\n" +
57            "  float value = cos(theta1) * sin(theta2) + sin(theta1) * cos(theta2);\n" +
58            // keep value of part1 in range: (2^-14 to 2^14).
59            "  float temp = mod(197.0 * value, 1.0) + value;\n" +
60            "  float part1 = mod(220.0 * temp, 1.0) + temp;\n" +
61            "  float part2 = value * 0.5453;\n" +
62            "  float part3 = cos(theta1 + theta2) * 0.43758;\n" +
63            "  return fract(part1 + part2 + part3);\n" +
64            "}\n" +
65            "void main() {\n" +
66            // black white
67            "  vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" +
68            "  float dither = rand(v_texcoord + seed);\n" +
69            "  vec3 xform = clamp(2.0 * color.rgb, 0.0, 1.0);\n" +
70            "  vec3 temp = clamp(2.0 * (color.rgb + stepsize), 0.0, 1.0);\n" +
71            "  vec3 new_color = clamp(xform + (temp - xform) * (dither - 0.5), 0.0, 1.0);\n" +
72            // grayscale
73            "  float gray = dot(new_color, vec3(0.299, 0.587, 0.114));\n" +
74            "  new_color = vec3(gray, gray, gray);\n" +
75            // vignette
76            "  vec2 coord = v_texcoord - vec2(0.5, 0.5);\n" +
77            "  float dist = length(coord * scale);\n" +
78            "  float lumen = 0.85 / (1.0 + exp((dist * inv_max_dist - 0.83) * 20.0)) + 0.15;\n" +
79            "  gl_FragColor = vec4(new_color * lumen, color.a);\n" +
80            "}\n";
81
82    public DocumentaryFilter(String name) {
83        super(name);
84        Date date = new Date();
85        mRandom = new Random(new Date().getTime());
86    }
87
88    @Override
89    public void setupPorts() {
90        addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
91        addOutputBasedOnInput("image", "image");
92    }
93
94    @Override
95    public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
96        return inputFormat;
97    }
98
99    public void initProgram(FilterContext context, int target) {
100        switch (target) {
101            case FrameFormat.TARGET_GPU:
102                ShaderProgram shaderProgram = new ShaderProgram(context, mDocumentaryShader);
103                shaderProgram.setMaximumTileSize(mTileSize);
104                mProgram = shaderProgram;
105                break;
106
107            default:
108                throw new RuntimeException("Filter Sharpen does not support frames of " +
109                    "target " + target + "!");
110        }
111        mTarget = target;
112    }
113
114    @Override
115    public void process(FilterContext context) {
116        // Get input frame
117        Frame input = pullInput("image");
118        FrameFormat inputFormat = input.getFormat();
119
120        // Create program if not created already
121        if (mProgram == null || inputFormat.getTarget() != mTarget) {
122            initProgram(context, inputFormat.getTarget());
123        }
124
125        // Check if the frame size has changed
126        if (inputFormat.getWidth() != mWidth || inputFormat.getHeight() != mHeight) {
127            mWidth = inputFormat.getWidth();
128            mHeight = inputFormat.getHeight();
129            initParameters();
130        }
131
132        // Create output frame
133        Frame output = context.getFrameManager().newFrame(inputFormat);
134
135        // Process
136        mProgram.process(input, output);
137
138        // Push output
139        pushOutput("image", output);
140
141        // Release pushed frame
142        output.release();
143    }
144
145    private void initParameters() {
146        if (mProgram != null) {
147            float scale[] = new float[2];
148            if (mWidth > mHeight) {
149                scale[0] = 1f;
150                scale[1] = ((float) mHeight) / mWidth;
151            } else {
152                scale[0] = ((float) mWidth) / mHeight;
153                scale[1] = 1f;
154            }
155            float max_dist = ((float) Math.sqrt(scale[0] * scale[0] + scale[1] * scale[1])) * 0.5f;
156
157            mProgram.setHostValue("scale", scale);
158            mProgram.setHostValue("inv_max_dist", 1.0f / max_dist);
159            mProgram.setHostValue("stepsize", 1.0f / 255.0f);
160
161            float seed[] = { mRandom.nextFloat(), mRandom.nextFloat() };
162            mProgram.setHostValue("seed", seed);
163        }
164    }
165}
166