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 28public class VignetteFilter extends Filter { 29 30 @GenerateFieldPort(name = "scale", hasDefault = true) 31 private float mScale = 0f; 32 33 @GenerateFieldPort(name = "tile_size", hasDefault = true) 34 private int mTileSize = 640; 35 36 private Program mProgram; 37 38 private int mWidth = 0; 39 private int mHeight = 0; 40 private int mTarget = FrameFormat.TARGET_UNSPECIFIED; 41 42 private final float mSlope = 20.0f; 43 private final float mShade = 0.85f; 44 45 private final String mVignetteShader = 46 "precision mediump float;\n" + 47 "uniform sampler2D tex_sampler_0;\n" + 48 "uniform float range;\n" + 49 "uniform float inv_max_dist;\n" + 50 "uniform float shade;\n" + 51 "uniform vec2 scale;\n" + 52 "varying vec2 v_texcoord;\n" + 53 "void main() {\n" + 54 " const float slope = 20.0;\n" + 55 " vec2 coord = v_texcoord - vec2(0.5, 0.5);\n" + 56 " float dist = length(coord * scale);\n" + 57 " float lumen = shade / (1.0 + exp((dist * inv_max_dist - range) * slope)) + (1.0 - shade);\n" + 58 " vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" + 59 " gl_FragColor = vec4(color.rgb * lumen, color.a);\n" + 60 "}\n"; 61 62 public VignetteFilter(String name) { 63 super(name); 64 } 65 66 @Override 67 public void setupPorts() { 68 addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA)); 69 addOutputBasedOnInput("image", "image"); 70 } 71 72 @Override 73 public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) { 74 return inputFormat; 75 } 76 77 public void initProgram(FilterContext context, int target) { 78 switch (target) { 79 case FrameFormat.TARGET_GPU: 80 ShaderProgram shaderProgram = new ShaderProgram(context, mVignetteShader); 81 shaderProgram.setMaximumTileSize(mTileSize); 82 mProgram = shaderProgram; 83 break; 84 85 default: 86 throw new RuntimeException("Filter Sharpen does not support frames of " + 87 "target " + target + "!"); 88 } 89 mTarget = target; 90 } 91 92 private void initParameters() { 93 if (mProgram != null) { 94 float scale[] = new float[2]; 95 if (mWidth > mHeight) { 96 scale[0] = 1f; 97 scale[1] = ((float) mHeight) / mWidth; 98 } else { 99 scale[0] = ((float) mWidth) / mHeight; 100 scale[1] = 1f; 101 } 102 float max_dist = ((float) Math.sqrt(scale[0] * scale[0] + scale[1] * scale[1])) * 0.5f; 103 mProgram.setHostValue("scale", scale); 104 mProgram.setHostValue("inv_max_dist", 1f / max_dist); 105 mProgram.setHostValue("shade", mShade); 106 107 updateParameters(); 108 } 109 } 110 111 private void updateParameters() { 112 // The 'range' is between 1.3 to 0.6. When scale is zero then range is 1.3 113 // which means no vignette at all because the luminousity difference is 114 // less than 1/256 and will cause nothing. 115 mProgram.setHostValue("range", 1.30f - (float) Math.sqrt(mScale) * 0.7f); 116 } 117 118 @Override 119 public void fieldPortValueUpdated(String name, FilterContext context) { 120 if (mProgram != null) { 121 updateParameters(); 122 } 123 } 124 125 @Override 126 public void process(FilterContext context) { 127 // Get input frame 128 Frame input = pullInput("image"); 129 FrameFormat inputFormat = input.getFormat(); 130 131 // Create program if not created already 132 if (mProgram == null || inputFormat.getTarget() != mTarget) { 133 initProgram(context, inputFormat.getTarget()); 134 } 135 136 // Check if the frame size has changed 137 if (inputFormat.getWidth() != mWidth || inputFormat.getHeight() != mHeight) { 138 mWidth = inputFormat.getWidth(); 139 mHeight = inputFormat.getHeight(); 140 initParameters(); 141 } 142 143 // Create output frame 144 Frame output = context.getFrameManager().newFrame(inputFormat); 145 146 // Process 147 mProgram.process(input, output); 148 149 // Push output 150 pushOutput("image", output); 151 152 // Release pushed frame 153 output.release(); 154 } 155} 156