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
17
18package android.filterpacks.imageproc;
19
20import android.filterfw.core.Filter;
21import android.filterfw.core.FilterContext;
22import android.filterfw.core.Frame;
23import android.filterfw.core.FrameFormat;
24import android.filterfw.core.ShaderProgram;
25import android.filterfw.geometry.Quad;
26import android.filterfw.format.ImageFormat;
27import android.filterfw.format.ObjectFormat;
28
29/**
30 * @hide
31 */
32public class DrawOverlayFilter extends Filter {
33
34    private ShaderProgram mProgram;
35
36    public DrawOverlayFilter(String name) {
37        super(name);
38    }
39
40    @Override
41    public void setupPorts() {
42        FrameFormat imageFormatMask = ImageFormat.create(ImageFormat.COLORSPACE_RGBA,
43                                                         FrameFormat.TARGET_GPU);
44        addMaskedInputPort("source", imageFormatMask);
45        addMaskedInputPort("overlay", imageFormatMask);
46        addMaskedInputPort("box", ObjectFormat.fromClass(Quad.class, FrameFormat.TARGET_SIMPLE));
47        addOutputBasedOnInput("image", "source");
48    }
49
50    @Override
51    public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
52        return inputFormat;
53    }
54
55    @Override
56    public void prepare(FilterContext context) {
57        mProgram = ShaderProgram.createIdentity(context);
58    }
59
60    @Override
61    public void process(FilterContext env) {
62        // Get input frame
63        Frame sourceFrame = pullInput("source");
64        Frame overlayFrame = pullInput("overlay");
65        Frame boxFrame = pullInput("box");
66
67        // Get the box
68        Quad box = (Quad)boxFrame.getObjectValue();
69        box = box.translated(1.0f, 1.0f).scaled(2.0f);
70
71        mProgram.setTargetRegion(box);
72
73        // Create output frame with copy of input
74        Frame output = env.getFrameManager().newFrame(sourceFrame.getFormat());
75        output.setDataFromFrame(sourceFrame);
76
77        // Draw onto output
78        mProgram.process(overlayFrame, output);
79
80        // Push output
81        pushOutput("image", output);
82
83        // Release pushed frame
84        output.release();
85    }
86}
87