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
31public class SepiaFilter extends Filter {
32
33    @GenerateFieldPort(name = "tile_size", hasDefault = true)
34    private int mTileSize = 640;
35
36    private Program mProgram;
37    private int mTarget = FrameFormat.TARGET_UNSPECIFIED;
38
39    private final String mSepiaShader =
40            "precision mediump float;\n" +
41            "uniform sampler2D tex_sampler_0;\n" +
42            "uniform mat3 matrix;\n" +
43            "varying vec2 v_texcoord;\n" +
44            "void main() {\n" +
45            "  vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" +
46            "  vec3 new_color = min(matrix * color.rgb, 1.0);\n" +
47            "  gl_FragColor = vec4(new_color.rgb, color.a);\n" +
48            "}\n";
49
50    public SepiaFilter(String name) {
51      super(name);
52    }
53
54    @Override
55    public void setupPorts() {
56        addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
57        addOutputBasedOnInput("image", "image");
58    }
59
60    @Override
61    public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
62        return inputFormat;
63    }
64
65    public void initProgram(FilterContext context, int target) {
66        switch (target) {
67            case FrameFormat.TARGET_GPU:
68                ShaderProgram shaderProgram = new ShaderProgram(context, mSepiaShader);
69                shaderProgram.setMaximumTileSize(mTileSize);
70                mProgram = shaderProgram;
71                break;
72
73            default:
74                throw new RuntimeException("Filter Sharpen does not support frames of " +
75                    "target " + target + "!");
76        }
77        mTarget = target;
78    }
79
80   @Override
81    public void process(FilterContext context) {
82        // Get input frame
83        Frame input = pullInput("image");
84        FrameFormat inputFormat = input.getFormat();
85
86        // Create output frame
87        Frame output = context.getFrameManager().newFrame(inputFormat);
88
89        // Create program if not created already
90        if (mProgram == null || inputFormat.getTarget() != mTarget) {
91            initProgram(context, inputFormat.getTarget());
92            initParameters();
93        }
94
95        // Process
96        mProgram.process(input, output);
97
98        // Push output
99        pushOutput("image", output);
100
101        // Release pushed frame
102        output.release();
103    }
104
105    private void initParameters() {
106        float weights[] = { 805.0f / 2048.0f, 715.0f / 2048.0f, 557.0f / 2048.0f,
107                            1575.0f / 2048.0f, 1405.0f / 2048.0f, 1097.0f / 2048.0f,
108                            387.0f / 2048.0f, 344.0f / 2048.0f, 268.0f / 2048.0f };
109        mProgram.setHostValue("matrix", weights);
110    }
111}
112