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