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;
27import android.graphics.Color;
28
29public class TintFilter extends Filter {
30
31    @GenerateFieldPort(name = "tint", hasDefault = true)
32    private int mTint = 0xFF0000FF;
33
34    @GenerateFieldPort(name = "tile_size", hasDefault = true)
35    private int mTileSize = 640;
36
37    private Program mProgram;
38    private int mTarget = FrameFormat.TARGET_UNSPECIFIED;
39
40    private final String mTintShader =
41            "precision mediump float;\n" +
42            "uniform sampler2D tex_sampler_0;\n" +
43            "uniform vec3 tint;\n" +
44            "uniform vec3 color_ratio;\n" +
45            "varying vec2 v_texcoord;\n" +
46            "void main() {\n" +
47            "  vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" +
48            "  float avg_color = dot(color_ratio, color.rgb);\n" +
49            "  vec3 new_color = min(0.8 * avg_color + 0.2 * tint, 1.0);\n" +
50            "  gl_FragColor = vec4(new_color.rgb, color.a);\n" +
51            "}\n";
52
53    public TintFilter(String name) {
54      super(name);
55    }
56
57    @Override
58    public void setupPorts() {
59        addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
60        addOutputBasedOnInput("image", "image");
61    }
62
63    @Override
64    public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
65        return inputFormat;
66    }
67
68    public void initProgram(FilterContext context, int target) {
69        switch (target) {
70            case FrameFormat.TARGET_GPU:
71                ShaderProgram shaderProgram = new ShaderProgram(context, mTintShader);
72                shaderProgram.setMaximumTileSize(mTileSize);
73                mProgram = shaderProgram;
74                break;
75
76            default:
77                throw new RuntimeException("Filter Sharpen does not support frames of " +
78                    "target " + target + "!");
79        }
80        mTarget = target;
81    }
82
83    @Override
84    public void fieldPortValueUpdated(String name, FilterContext context) {
85        if (mProgram != null) {
86            updateParameters();
87        }
88    }
89
90    @Override
91    public void process(FilterContext context) {
92        // Get input frame
93        Frame input = pullInput("image");
94        FrameFormat inputFormat = input.getFormat();
95
96        // Create program if not created already
97        if (mProgram == null || inputFormat.getTarget() != mTarget) {
98            initProgram(context, inputFormat.getTarget());
99            initParameters();
100        }
101
102        // Create output frame
103        Frame output = context.getFrameManager().newFrame(inputFormat);
104
105        // Process
106        mProgram.process(input, output);
107
108        // Push output
109        pushOutput("image", output);
110
111        // Release pushed frame
112        output.release();
113    }
114
115    private void initParameters() {
116        float color_ratio[] = {0.21f, 0.71f, 0.07f};
117        mProgram.setHostValue("color_ratio", color_ratio);
118
119        updateParameters();
120    }
121
122    private void updateParameters() {
123        float tint_color[] = {Color.red(mTint) / 255f,
124                              Color.green(mTint) / 255f,
125                              Color.blue(mTint) / 255f };
126
127        mProgram.setHostValue("tint", tint_color);
128    }
129
130}
131