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.GenerateFinalPort; 25 import android.filterfw.core.KeyValueMap; 26 import android.filterfw.core.NativeProgram; 27 import android.filterfw.core.NativeFrame; 28 import android.filterfw.core.Program; 29 import android.filterfw.core.ShaderProgram; 30 import android.filterfw.format.ImageFormat; 31 32 import android.util.Log; 33 34 public class FillLightFilter extends Filter { 35 36 @GenerateFieldPort(name = "tile_size", hasDefault = true) 37 private int mTileSize = 640; 38 39 @GenerateFieldPort(name = "strength", hasDefault = true) 40 private float mBacklight = 0f; 41 42 private Program mProgram; 43 44 private int mTarget = FrameFormat.TARGET_UNSPECIFIED; 45 46 private final String mFillLightShader = 47 "precision mediump float;\n" + 48 "uniform sampler2D tex_sampler_0;\n" + 49 "uniform float mult;\n" + 50 "uniform float igamma;\n" + 51 "varying vec2 v_texcoord;\n" + 52 "void main()\n" + 53 "{\n" + 54 " const vec3 color_weights = vec3(0.25, 0.5, 0.25);\n" + 55 " vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" + 56 " float lightmask = dot(color.rgb, color_weights);\n" + 57 " float backmask = (1.0 - lightmask);\n" + 58 " vec3 ones = vec3(1.0, 1.0, 1.0);\n" + 59 " vec3 diff = pow(mult * color.rgb, igamma * ones) - color.rgb;\n" + 60 " diff = min(diff, 1.0);\n" + 61 " vec3 new_color = min(color.rgb + diff * backmask, 1.0);\n" + 62 " gl_FragColor = vec4(new_color, color.a);\n" + 63 "}\n"; 64 65 public FillLightFilter(String name) { 66 super(name); 67 } 68 69 @Override 70 public void setupPorts() { 71 addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA)); 72 addOutputBasedOnInput("image", "image"); 73 } 74 75 @Override 76 public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) { 77 return inputFormat; 78 } 79 80 public void initProgram(FilterContext context, int target) { 81 switch (target) { 82 case FrameFormat.TARGET_GPU: 83 ShaderProgram shaderProgram = new ShaderProgram(context, mFillLightShader); 84 Log.e("FillLight", "tile size: " + mTileSize); 85 shaderProgram.setMaximumTileSize(mTileSize); 86 mProgram = shaderProgram; 87 break; 88 89 default: 90 throw new RuntimeException("Filter FillLight does not support frames of " + 91 "target " + target + "!"); 92 } 93 mTarget = target; 94 } 95 96 97 @Override 98 public void process(FilterContext context) { 99 // Get input frame 100 Frame input = pullInput("image"); 101 FrameFormat inputFormat = input.getFormat(); 102 103 // Create output frame 104 Frame output = context.getFrameManager().newFrame(inputFormat); 105 106 // Create program if not created already 107 if (mProgram == null || inputFormat.getTarget() != mTarget) { 108 initProgram(context, inputFormat.getTarget()); 109 updateParameters(); 110 } 111 112 // Process 113 mProgram.process(input, output); 114 115 // Push output 116 pushOutput("image", output); 117 118 // Release pushed frame 119 output.release(); 120 } 121 122 123 @Override 124 public void fieldPortValueUpdated(String name, FilterContext context) { 125 if (mProgram != null) { 126 updateParameters(); 127 } 128 } 129 130 private void updateParameters() { 131 float fade_gamma = 0.3f; 132 float amt = 1.0f - mBacklight; 133 float mult = 1.0f / (amt * 0.7f + 0.3f); 134 float faded = fade_gamma + (1.0f -fade_gamma) *mult; 135 float igamma = 1.0f / faded; 136 137 mProgram.setHostValue("mult", mult); 138 mProgram.setHostValue("igamma", igamma); 139 } 140 } 141