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 18 package android.filterpacks.imageproc; 19 20 import android.filterfw.core.Filter; 21 import android.filterfw.core.FilterContext; 22 import android.filterfw.core.Frame; 23 import android.filterfw.core.FrameFormat; 24 import android.filterfw.core.GenerateFieldPort; 25 import android.filterfw.core.Program; 26 import android.filterfw.core.ShaderProgram; 27 import android.filterfw.format.ImageFormat; 28 29 import java.lang.Math; 30 31 /** 32 * @hide 33 */ 34 public 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 // The constant min_dist, below, is an arbitrary number that gives good enough precision in 53 // the center of the picture without affecting the fisheye effect noticeably. 54 private static final String mFisheyeShader = 55 "precision mediump float;\n" + 56 "uniform sampler2D tex_sampler_0;\n" + 57 "uniform vec2 scale;\n" + 58 "uniform float alpha;\n" + 59 "uniform float radius2;\n" + 60 "uniform float factor;\n" + 61 "varying vec2 v_texcoord;\n" + 62 "void main() {\n" + 63 " const float m_pi_2 = 1.570963;\n" + 64 " const float min_dist = 0.01;\n" + 65 " vec2 coord = v_texcoord - vec2(0.5, 0.5);\n" + 66 " float dist = length(coord * scale);\n" + 67 " dist = max(dist, min_dist);\n" + 68 " float radian = m_pi_2 - atan(alpha * sqrt(radius2 - dist * dist), dist);\n" + 69 " float scalar = radian * factor / dist;\n" + 70 " vec2 new_coord = coord * scalar + vec2(0.5, 0.5);\n" + 71 " gl_FragColor = texture2D(tex_sampler_0, new_coord);\n" + 72 "}\n"; 73 FisheyeFilter(String name)74 public FisheyeFilter(String name) { 75 super(name); 76 } 77 78 @Override setupPorts()79 public void setupPorts() { 80 addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA)); 81 addOutputBasedOnInput("image", "image"); 82 } 83 84 @Override getOutputFormat(String portName, FrameFormat inputFormat)85 public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) { 86 return inputFormat; 87 } 88 initProgram(FilterContext context, int target)89 public void initProgram(FilterContext context, int target) { 90 switch (target) { 91 case FrameFormat.TARGET_GPU: 92 ShaderProgram shaderProgram = new ShaderProgram(context, mFisheyeShader); 93 shaderProgram.setMaximumTileSize(mTileSize); 94 mProgram = shaderProgram; 95 break; 96 97 default: 98 throw new RuntimeException("Filter FisheyeFilter does not support frames of " + 99 "target " + target + "!"); 100 } 101 mTarget = target; 102 } 103 104 @Override process(FilterContext context)105 public void process(FilterContext context) { 106 // Get input frame 107 Frame input = pullInput("image"); 108 FrameFormat inputFormat = input.getFormat(); 109 110 // Create output frame 111 Frame output = context.getFrameManager().newFrame(inputFormat); 112 113 // Create program if not created already 114 if (mProgram == null || inputFormat.getTarget() != mTarget) { 115 initProgram(context, inputFormat.getTarget()); 116 } 117 118 // Check if the frame size has changed 119 if (inputFormat.getWidth() != mWidth || inputFormat.getHeight() != mHeight) { 120 updateFrameSize(inputFormat.getWidth(), inputFormat.getHeight()); 121 } 122 123 // Process 124 mProgram.process(input, output); 125 126 // Push output 127 pushOutput("image", output); 128 129 // Release pushed frame 130 output.release(); 131 } 132 133 @Override fieldPortValueUpdated(String name, FilterContext context)134 public void fieldPortValueUpdated(String name, FilterContext context) { 135 if (mProgram != null) { 136 updateProgramParams(); 137 } 138 } 139 updateFrameSize(int width, int height)140 private void updateFrameSize(int width, int height) { 141 mWidth = width; 142 mHeight = height; 143 144 updateProgramParams(); 145 } 146 updateProgramParams()147 private void updateProgramParams() { 148 final float pi = 3.14159265f; 149 float scale[] = new float[2]; 150 if (mWidth > mHeight) { 151 scale[0] = 1f; 152 scale[1] = ((float) mHeight) / mWidth; 153 } else { 154 scale[0] = ((float) mWidth) / mHeight; 155 scale[1] = 1f; 156 } 157 float alpha = mScale * 2.0f + 0.75f; 158 float bound2 = 0.25f * (scale[0] * scale[0] + scale[1] * scale[1]); 159 float bound = (float) Math.sqrt(bound2); 160 float radius = 1.15f * bound; 161 float radius2 = radius * radius; 162 float max_radian = 0.5f * pi - 163 (float) Math.atan(alpha / bound * (float) Math.sqrt(radius2 - bound2)); 164 float factor = bound / max_radian; 165 166 mProgram.setHostValue("scale", scale); 167 mProgram.setHostValue("radius2",radius2); 168 mProgram.setHostValue("factor", factor); 169 mProgram.setHostValue("alpha", alpha); 170 } 171 } 172