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 package android.filterpacks.imageproc; 18 19 import android.filterfw.core.Filter; 20 import android.filterfw.core.FilterContext; 21 import android.filterfw.core.Frame; 22 import android.filterfw.core.FrameFormat; 23 import android.filterfw.core.GenerateFieldPort; 24 import android.filterfw.core.KeyValueMap; 25 import android.filterfw.core.NativeProgram; 26 import android.filterfw.core.NativeFrame; 27 import android.filterfw.core.Program; 28 import android.filterfw.core.ShaderProgram; 29 import android.filterfw.format.ImageFormat; 30 31 public class SaturateFilter extends Filter { 32 33 @GenerateFieldPort(name = "scale", hasDefault = true) 34 private float mScale = 0f; 35 36 @GenerateFieldPort(name = "tile_size", hasDefault = true) 37 private int mTileSize = 640; 38 39 private Program mBenProgram; 40 private Program mHerfProgram; 41 private int mTarget = FrameFormat.TARGET_UNSPECIFIED; 42 43 private final String mBenSaturateShader = 44 "precision mediump float;\n" + 45 "uniform sampler2D tex_sampler_0;\n" + 46 "uniform float scale;\n" + 47 "uniform float shift;\n" + 48 "uniform vec3 weights;\n" + 49 "varying vec2 v_texcoord;\n" + 50 "void main() {\n" + 51 " vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" + 52 " float kv = dot(color.rgb, weights) + shift;\n" + 53 " vec3 new_color = scale * color.rgb + (1.0 - scale) * kv;\n" + 54 " gl_FragColor = vec4(new_color, color.a);\n" + 55 "}\n"; 56 57 private final String mHerfSaturateShader = 58 "precision mediump float;\n" + 59 "uniform sampler2D tex_sampler_0;\n" + 60 "uniform vec3 weights;\n" + 61 "uniform vec3 exponents;\n" + 62 "varying vec2 v_texcoord;\n" + 63 "void main() {\n" + 64 " vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" + 65 " float de = dot(color.rgb, weights);\n" + 66 " float inv_de = 1.0 / de;\n" + 67 " vec3 new_color = de * pow(color.rgb * inv_de, exponents);\n" + 68 " float max_color = max(max(max(new_color.r, new_color.g), new_color.b), 1.0);\n" + 69 " gl_FragColor = vec4(new_color / max_color, color.a);\n" + 70 "}\n"; 71 72 73 public SaturateFilter(String name) { 74 super(name); 75 } 76 77 @Override 78 public void setupPorts() { 79 addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA)); 80 addOutputBasedOnInput("image", "image"); 81 } 82 83 @Override 84 public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) { 85 return inputFormat; 86 } 87 88 public void initProgram(FilterContext context, int target) { 89 switch (target) { 90 case FrameFormat.TARGET_GPU: 91 ShaderProgram shaderProgram = new ShaderProgram(context, mBenSaturateShader); 92 shaderProgram.setMaximumTileSize(mTileSize); 93 mBenProgram = shaderProgram; 94 95 shaderProgram = new ShaderProgram(context, mHerfSaturateShader); 96 shaderProgram.setMaximumTileSize(mTileSize); 97 mHerfProgram = shaderProgram; 98 99 break; 100 101 default: 102 throw new RuntimeException("Filter Sharpen does not support frames of " + 103 "target " + target + "!"); 104 } 105 mTarget = target; 106 } 107 108 @Override 109 public void fieldPortValueUpdated(String name, FilterContext context) { 110 if (mBenProgram != null && mHerfProgram != null) { 111 updateParameters(); 112 } 113 } 114 115 @Override 116 public void process(FilterContext context) { 117 // Get input frame 118 Frame input = pullInput("image"); 119 FrameFormat inputFormat = input.getFormat(); 120 121 // Create program if not created already 122 if (mBenProgram == null || inputFormat.getTarget() != mTarget) { 123 initProgram(context, inputFormat.getTarget()); 124 initParameters(); 125 } 126 127 // Create output frame 128 Frame output = context.getFrameManager().newFrame(inputFormat); 129 130 // Process 131 if (mScale > 0.0f) { 132 mHerfProgram.process(input, output); 133 } else { 134 mBenProgram.process(input, output); 135 } 136 // Push output 137 pushOutput("image", output); 138 139 // Release pushed frame 140 output.release(); 141 } 142 143 private void initParameters() { 144 float shift = 1.0f / 255.0f; 145 float weights[] = { 2f/8f, 5f/8f, 1f/8f}; 146 147 mBenProgram.setHostValue("weights", weights); 148 mBenProgram.setHostValue("shift", shift); 149 150 mHerfProgram.setHostValue("weights", weights); 151 152 updateParameters(); 153 } 154 155 private void updateParameters() { 156 157 if (mScale > 0.0f) { 158 float exponents[] = new float[3]; 159 160 exponents[0] = (0.9f * mScale) + 1.0f; 161 exponents[1] = (2.1f * mScale) + 1.0f; 162 exponents[2] = (2.7f * mScale) + 1.0f; 163 164 mHerfProgram.setHostValue("exponents", exponents); 165 } else { 166 mBenProgram.setHostValue("scale", 1.0f + mScale); 167 } 168 } 169 170 } 171