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