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.KeyValueMap;
24import android.filterfw.core.NativeProgram;
25import android.filterfw.core.NativeFrame;
26import android.filterfw.core.Program;
27import android.filterfw.core.ShaderProgram;
28import android.filterfw.format.ImageFormat;
29
30import java.util.Set;
31
32/**
33 * The filter linearly blends "left" and "right" frames. The blending weight is
34 * the multiplication of parameter "blend" and the alpha value in "right" frame.
35 * @hide
36 */
37public class BlendFilter extends ImageCombineFilter {
38
39    private final String mBlendShader =
40            "precision mediump float;\n" +
41            "uniform sampler2D tex_sampler_0;\n" +
42            "uniform sampler2D tex_sampler_1;\n" +
43            "uniform float blend;\n" +
44            "varying vec2 v_texcoord;\n" +
45            "void main() {\n" +
46            "  vec4 colorL = texture2D(tex_sampler_0, v_texcoord);\n" +
47            "  vec4 colorR = texture2D(tex_sampler_1, v_texcoord);\n" +
48            "  float weight = colorR.a * blend;\n" +
49            "  gl_FragColor = mix(colorL, colorR, weight);\n" +
50            "}\n";
51
52    public BlendFilter(String name) {
53        super(name, new String[] { "left", "right" }, "blended", "blend");
54    }
55
56    @Override
57    protected Program getNativeProgram(FilterContext context) {
58        throw new RuntimeException("TODO: Write native implementation for Blend!");
59    }
60
61    @Override
62    protected Program getShaderProgram(FilterContext context) {
63        return new ShaderProgram(context, mBlendShader);
64    }
65}
66