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