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.Program;
25import android.filterfw.core.ShaderProgram;
26import android.filterfw.format.ImageFormat;
27
28/**
29 * @hide
30 */
31public class CropRectFilter extends Filter {
32
33    @GenerateFieldPort(name = "xorigin")
34    private int mXorigin;
35
36    @GenerateFieldPort(name = "yorigin")
37    private int mYorigin;
38
39    @GenerateFieldPort(name = "width")
40    private int mOutputWidth;
41
42    @GenerateFieldPort(name = "height")
43    private int mOutputHeight;
44
45    @GenerateFieldPort(name = "tile_size", hasDefault = true)
46    private int mTileSize = 640;
47
48    private Program mProgram;
49
50    private int mWidth = 0;
51    private int mHeight = 0;
52
53    private int mTarget = FrameFormat.TARGET_UNSPECIFIED;
54
55    public CropRectFilter(String name) {
56        super(name);
57    }
58
59    @Override
60    public void setupPorts() {
61        addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
62        addOutputBasedOnInput("image", "image");
63    }
64
65    public void initProgram(FilterContext context, int target) {
66        switch (target) {
67            case FrameFormat.TARGET_GPU:
68                ShaderProgram shaderProgram = ShaderProgram.createIdentity(context);
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 fieldPortValueUpdated(String name, FilterContext context) {
82        if (mProgram != null) {
83            updateSourceRect(mWidth, mHeight);
84        }
85    }
86
87    @Override
88    public void process(FilterContext context) {
89        // Get input frame
90        Frame input = pullInput("image");
91        FrameFormat inputFormat = input.getFormat();
92
93        // Create output frame
94        FrameFormat outputFormat = ImageFormat.create(mOutputWidth, mOutputHeight,
95                                                      ImageFormat.COLORSPACE_RGBA,
96                                                      FrameFormat.TARGET_GPU);
97        Frame output = context.getFrameManager().newFrame(outputFormat);
98
99        // Create program if not created already
100        if (mProgram == null || inputFormat.getTarget() != mTarget) {
101            initProgram(context, inputFormat.getTarget());
102        }
103
104        // Check if the frame size has changed
105        if (inputFormat.getWidth() != mWidth || inputFormat.getHeight() != mHeight) {
106            updateSourceRect(inputFormat.getWidth(), inputFormat.getHeight());
107        }
108
109        // Process
110        mProgram.process(input, output);
111
112        // Push output
113        pushOutput("image", output);
114
115        // Release pushed frame
116        output.release();
117    }
118
119    void updateSourceRect(int width, int height) {
120        mWidth = width;
121        mHeight = height;
122
123        /*
124        Log.e("CropFilter", mWidth + ", " + mHeight + ", " +
125                            (float) mXorigin / mWidth + ", " +
126                            (float) mYorigin / mHeight + ", " +
127                            (float) mOutputWidth / mWidth + ", " +
128                            (float) mOutputHeight / mHeight);
129        */
130
131        ((ShaderProgram) mProgram).setSourceRect((float) mXorigin / mWidth,
132                                                 (float) mYorigin / mHeight,
133                                                 (float) mOutputWidth / mWidth,
134                                                 (float) mOutputHeight / mHeight);
135    }
136}
137