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.Program;
26import android.filterfw.core.ShaderProgram;
27import android.filterfw.format.ImageFormat;
28
29import java.lang.Math;
30
31/**
32 * @hide
33 */
34public class FisheyeFilter extends Filter {
35    private static final String TAG = "FisheyeFilter";
36
37    // This parameter has range between 0 and 1. It controls the effect of radial distortion.
38    // The larger the value, the more prominent the distortion effect becomes (a straight line
39    // becomes a curve).
40    @GenerateFieldPort(name = "scale", hasDefault = true)
41    private float mScale = 0f;
42
43    @GenerateFieldPort(name = "tile_size", hasDefault = true)
44    private int mTileSize = 640;
45
46    private Program mProgram;
47
48    private int mWidth = 0;
49    private int mHeight = 0;
50    private int mTarget = FrameFormat.TARGET_UNSPECIFIED;
51
52    private static final String mFisheyeShader =
53            "precision mediump float;\n" +
54            "uniform sampler2D tex_sampler_0;\n" +
55            "uniform vec2 scale;\n" +
56            "uniform float alpha;\n" +
57            "uniform float radius2;\n" +
58            "uniform float factor;\n" +
59            "varying vec2 v_texcoord;\n" +
60            "void main() {\n" +
61            "  const float m_pi_2 = 1.570963;\n" +
62            "  vec2 coord = v_texcoord - vec2(0.5, 0.5);\n" +
63            "  float dist = length(coord * scale);\n" +
64            "  float radian = m_pi_2 - atan(alpha * sqrt(radius2 - dist * dist), dist);\n" +
65            "  float scalar = radian * factor / dist;\n" +
66            "  vec2 new_coord = coord * scalar + vec2(0.5, 0.5);\n" +
67            "  gl_FragColor = texture2D(tex_sampler_0, new_coord);\n" +
68            "}\n";
69
70    public FisheyeFilter(String name) {
71        super(name);
72    }
73
74    @Override
75    public void setupPorts() {
76        addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA));
77        addOutputBasedOnInput("image", "image");
78    }
79
80    @Override
81    public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) {
82        return inputFormat;
83    }
84
85    public void initProgram(FilterContext context, int target) {
86        switch (target) {
87            case FrameFormat.TARGET_GPU:
88                ShaderProgram shaderProgram = new ShaderProgram(context, mFisheyeShader);
89                shaderProgram.setMaximumTileSize(mTileSize);
90                mProgram = shaderProgram;
91                break;
92
93            default:
94                throw new RuntimeException("Filter FisheyeFilter does not support frames of " +
95                    "target " + target + "!");
96        }
97        mTarget = target;
98    }
99
100    @Override
101    public void process(FilterContext context) {
102        // Get input frame
103        Frame input = pullInput("image");
104        FrameFormat inputFormat = input.getFormat();
105
106        // Create output frame
107        Frame output = context.getFrameManager().newFrame(inputFormat);
108
109        // Create program if not created already
110        if (mProgram == null || inputFormat.getTarget() != mTarget) {
111            initProgram(context, inputFormat.getTarget());
112        }
113
114        // Check if the frame size has changed
115        if (inputFormat.getWidth() != mWidth || inputFormat.getHeight() != mHeight) {
116            updateFrameSize(inputFormat.getWidth(), inputFormat.getHeight());
117        }
118
119        // Process
120        mProgram.process(input, output);
121
122        // Push output
123        pushOutput("image", output);
124
125        // Release pushed frame
126        output.release();
127    }
128
129    @Override
130    public void fieldPortValueUpdated(String name, FilterContext context) {
131        if (mProgram != null) {
132            updateProgramParams();
133        }
134    }
135
136    private void updateFrameSize(int width, int height) {
137        mWidth = width;
138        mHeight = height;
139
140        updateProgramParams();
141    }
142
143    private void updateProgramParams() {
144        final float pi = 3.14159265f;
145        float scale[] = new float[2];
146        if (mWidth > mHeight) {
147          scale[0] = 1f;
148          scale[1] = ((float) mHeight) / mWidth;
149        } else {
150          scale[0] = ((float) mWidth) / mHeight;
151          scale[1] = 1f;
152        }
153        float alpha = mScale * 2.0f + 0.75f;
154        float bound2 = 0.25f * (scale[0] * scale[0] + scale[1] * scale[1]);
155        float bound = (float) Math.sqrt(bound2);
156        float radius = 1.15f * bound;
157        float radius2 = radius * radius;
158        float max_radian = 0.5f * pi -
159            (float) Math.atan(alpha / bound * (float) Math.sqrt(radius2 - bound2));
160        float factor = bound / max_radian;
161
162        mProgram.setHostValue("scale", scale);
163        mProgram.setHostValue("radius2",radius2);
164        mProgram.setHostValue("factor", factor);
165        mProgram.setHostValue("alpha", alpha);
166    }
167}
168