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
31
32public class NegativeFilter extends Filter {
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 mNegativeShader =
41            "precision mediump float;\n" +
42            "uniform sampler2D tex_sampler_0;\n" +
43            "varying vec2 v_texcoord;\n" +
44            "void main() {\n" +
45            "  vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" +
46            "  gl_FragColor = vec4(1.0 - color.rgb, color.a);\n" +
47            "}\n";
48
49    public NegativeFilter(String name) {
50        super(name);
51    }
52
53    @Override
54    public void setupPorts() {
55        addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
56        addOutputBasedOnInput("image", "image");
57    }
58
59    @Override
60    public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
61        return inputFormat;
62    }
63
64    public void initProgram(FilterContext context, int target) {
65        switch (target) {
66            case FrameFormat.TARGET_GPU:
67                ShaderProgram shaderProgram = new ShaderProgram(context, mNegativeShader);
68                shaderProgram.setMaximumTileSize(mTileSize);
69                mProgram = shaderProgram;
70                break;
71
72            default:
73                throw new RuntimeException("Filter Sharpen does not support frames of " +
74                    "target " + target + "!");
75        }
76        mTarget = target;
77    }
78
79    @Override
80    public void process(FilterContext context) {
81        // Get input frame
82        Frame input = pullInput("image");
83        FrameFormat inputFormat = input.getFormat();
84
85        // Create output frame
86        Frame output = context.getFrameManager().newFrame(inputFormat);
87
88        // Create program if not created already
89        if (mProgram == null || inputFormat.getTarget() != mTarget) {
90            initProgram(context, inputFormat.getTarget());
91        }
92
93        // Process
94        mProgram.process(input, output);
95
96        // Push output
97        pushOutput("image", output);
98
99        // Release pushed frame
100        output.release();
101    }
102
103}
104