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