BlackWhiteFilter.java revision a75c7bbc633a6ebd35a0651be5c7a79b83d0c5c0
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 BlackWhiteFilter extends Filter {
35
36    @GenerateFieldPort(name = "black", hasDefault = true)
37    private float mBlack = 0f;
38
39    @GenerateFieldPort(name = "white", hasDefault = true)
40    private float mWhite = 1f;
41
42    @GenerateFieldPort(name = "tile_size", hasDefault = true)
43    private int mTileSize = 640;
44
45    private Program mProgram;
46    private Random mRandom;
47
48    private int mTarget = FrameFormat.TARGET_UNSPECIFIED;
49
50    private final String mBlackWhiteShader =
51            "precision mediump float;\n" +
52            "uniform sampler2D tex_sampler_0;\n" +
53            "uniform vec2 seed;\n" +
54            "uniform float black;\n" +
55            "uniform float scale;\n" +
56            "uniform float stepsize;\n" +
57            "varying vec2 v_texcoord;\n" +
58            "float rand(vec2 loc) {\n" +
59            "  const float divide = 0.00048828125;\n" +
60            "  const float factor = 2048.0;\n" +
61            "  float value = sin(dot(loc, vec2(12.9898, 78.233)));\n" +
62            "  float residual = mod(dot(mod(loc, divide), vec2(0.9898, 0.233)), divide);\n" +
63            "  float part2 = mod(value, divide);\n" +
64            "  float part1 = value - part2;\n" +
65            "  return fract(0.5453 * part1 + factor * (part2 + residual));\n" +
66            "}\n" +
67            "void main() {\n" +
68            "  vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" +
69            "  float dither = rand(v_texcoord + seed);\n" +
70            "  vec3 xform = clamp((color.rgb - black) * scale, 0.0, 1.0);\n" +
71            "  vec3 temp = clamp((color.rgb + stepsize - black) * scale, 0.0, 1.0);\n" +
72            "  vec3 new_color = clamp(xform + (temp - xform) * (dither - 0.5), 0.0, 1.0);\n" +
73            "  gl_FragColor = vec4(new_color, color.a);\n" +
74            "}\n";
75
76    public BlackWhiteFilter(String name) {
77        super(name);
78        mRandom = new Random(new Date().getTime());
79    }
80
81    @Override
82    public void setupPorts() {
83        addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
84        addOutputBasedOnInput("image", "image");
85    }
86
87    @Override
88    public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
89        return inputFormat;
90    }
91
92    public void initProgram(FilterContext context, int target) {
93        switch (target) {
94            case FrameFormat.TARGET_GPU:
95                ShaderProgram shaderProgram = new ShaderProgram(context, mBlackWhiteShader);
96                shaderProgram.setMaximumTileSize(mTileSize);
97                mProgram = shaderProgram;
98                updateParameters();
99                break;
100
101            default:
102                throw new RuntimeException("Filter Sharpen does not support frames of " +
103                    "target " + target + "!");
104        }
105        mTarget = target;
106    }
107
108    private void updateParameters() {
109        float scale = (mBlack != mWhite) ? 1.0f / (mWhite - mBlack) : 2000f;
110        float stepsize = 1.0f / 255.0f;
111        mProgram.setHostValue("black", mBlack);
112        mProgram.setHostValue("scale", scale);
113        mProgram.setHostValue("stepsize", stepsize);
114
115        float seed[] = { mRandom.nextFloat(), mRandom.nextFloat() };
116        mProgram.setHostValue("seed", seed);
117    }
118
119    @Override
120    public void fieldPortValueUpdated(String name, FilterContext context) {
121        if (mProgram != null) {
122            updateParameters();
123        }
124    }
125
126    @Override
127    public void process(FilterContext context) {
128        // Get input frame
129        Frame input = pullInput("image");
130        FrameFormat inputFormat = input.getFormat();
131
132        // Create program if not created already
133        if (mProgram == null || inputFormat.getTarget() != mTarget) {
134            initProgram(context, inputFormat.getTarget());
135        }
136
137        // Create output frame
138        Frame output = context.getFrameManager().newFrame(inputFormat);
139
140        // Process
141        mProgram.process(input, output);
142
143        // Push output
144        pushOutput("image", output);
145
146        // Release pushed frame
147        output.release();
148    }
149}
150