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.GenerateFieldPort;
25import android.filterfw.core.GLFrame;
26import android.filterfw.core.MutableFrameFormat;
27import android.filterfw.core.Program;
28import android.filterfw.core.ShaderProgram;
29import android.filterfw.format.ImageFormat;
30
31import android.opengl.GLES20;
32
33/**
34 * @hide
35 */
36public class ResizeFilter extends Filter {
37
38    @GenerateFieldPort(name = "owidth")
39    private int mOWidth;
40    @GenerateFieldPort(name = "oheight")
41    private int mOHeight;
42    @GenerateFieldPort(name = "keepAspectRatio", hasDefault = true)
43    private boolean mKeepAspectRatio = false;
44    @GenerateFieldPort(name = "generateMipMap", hasDefault = true)
45    private boolean mGenerateMipMap = false;
46
47    private Program mProgram;
48    private FrameFormat mLastFormat = null;
49
50    private MutableFrameFormat mOutputFormat;
51    private int mInputChannels;
52
53    public ResizeFilter(String name) {
54        super(name);
55    }
56
57    @Override
58    public void setupPorts() {
59        addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
60        addOutputBasedOnInput("image", "image");
61    }
62
63    @Override
64    public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
65        return inputFormat;
66    }
67
68    protected void createProgram(FilterContext context, FrameFormat format) {
69        if (mLastFormat != null && mLastFormat.getTarget() == format.getTarget()) return;
70        mLastFormat = format;
71        switch (format.getTarget()) {
72            case FrameFormat.TARGET_NATIVE:
73                throw new RuntimeException("Native ResizeFilter not implemented yet!");
74
75
76            case FrameFormat.TARGET_GPU:
77                ShaderProgram prog = ShaderProgram.createIdentity(context);
78                mProgram = prog;
79                break;
80
81            default:
82                throw new RuntimeException("ResizeFilter could not create suitable program!");
83        }
84    }
85    @Override
86    public void process(FilterContext env) {
87        // Get input frame
88        Frame input = pullInput("image");
89        createProgram(env, input.getFormat());
90
91        // Create output frame
92        MutableFrameFormat outputFormat = input.getFormat().mutableCopy();
93        if (mKeepAspectRatio) {
94            FrameFormat inputFormat = input.getFormat();
95            mOHeight = mOWidth * inputFormat.getHeight() / inputFormat.getWidth();
96        }
97        outputFormat.setDimensions(mOWidth, mOHeight);
98        Frame output = env.getFrameManager().newFrame(outputFormat);
99
100        // Process
101        if (mGenerateMipMap) {
102            GLFrame mipmapped = (GLFrame)env.getFrameManager().newFrame(input.getFormat());
103            mipmapped.setTextureParameter(GLES20.GL_TEXTURE_MIN_FILTER,
104                                          GLES20.GL_LINEAR_MIPMAP_NEAREST);
105            mipmapped.setDataFromFrame(input);
106            mipmapped.generateMipMap();
107            mProgram.process(mipmapped, output);
108            mipmapped.release();
109        } else {
110            mProgram.process(input, output);
111        }
112
113        // Push output
114        pushOutput("image", output);
115
116        // Release pushed frame
117        output.release();
118    }
119
120
121}
122