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.KeyValueMap; 25import android.filterfw.core.NativeProgram; 26import android.filterfw.core.NativeFrame; 27import android.filterfw.core.Program; 28import android.filterfw.core.ShaderProgram; 29import android.filterfw.format.ImageFormat; 30import android.graphics.Color; 31 32public class TintFilter extends Filter { 33 34 @GenerateFieldPort(name = "tint", hasDefault = true) 35 private int mTint = 0xFF0000FF; 36 37 @GenerateFieldPort(name = "tile_size", hasDefault = true) 38 private int mTileSize = 640; 39 40 private Program mProgram; 41 private int mTarget = FrameFormat.TARGET_UNSPECIFIED; 42 43 private final String mTintShader = 44 "precision mediump float;\n" + 45 "uniform sampler2D tex_sampler_0;\n" + 46 "uniform vec3 tint;\n" + 47 "uniform vec3 color_ratio;\n" + 48 "varying vec2 v_texcoord;\n" + 49 "void main() {\n" + 50 " vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" + 51 " float avg_color = dot(color_ratio, color.rgb);\n" + 52 " vec3 new_color = min(0.8 * avg_color + 0.2 * tint, 1.0);\n" + 53 " gl_FragColor = vec4(new_color.rgb, color.a);\n" + 54 "}\n"; 55 56 public TintFilter(String name) { 57 super(name); 58 } 59 60 @Override 61 public void setupPorts() { 62 addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA)); 63 addOutputBasedOnInput("image", "image"); 64 } 65 66 @Override 67 public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) { 68 return inputFormat; 69 } 70 71 public void initProgram(FilterContext context, int target) { 72 switch (target) { 73 case FrameFormat.TARGET_GPU: 74 ShaderProgram shaderProgram = new ShaderProgram(context, mTintShader); 75 shaderProgram.setMaximumTileSize(mTileSize); 76 mProgram = shaderProgram; 77 break; 78 79 default: 80 throw new RuntimeException("Filter Sharpen does not support frames of " + 81 "target " + target + "!"); 82 } 83 mTarget = target; 84 } 85 86 @Override 87 public void fieldPortValueUpdated(String name, FilterContext context) { 88 if (mProgram != null) { 89 updateParameters(); 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 program if not created already 100 if (mProgram == null || inputFormat.getTarget() != mTarget) { 101 initProgram(context, inputFormat.getTarget()); 102 initParameters(); 103 } 104 105 // Create output frame 106 Frame output = context.getFrameManager().newFrame(inputFormat); 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 private void initParameters() { 119 float color_ratio[] = {0.21f, 0.71f, 0.07f}; 120 mProgram.setHostValue("color_ratio", color_ratio); 121 122 updateParameters(); 123 } 124 125 private void updateParameters() { 126 float tint_color[] = {Color.red(mTint) / 255f, 127 Color.green(mTint) / 255f, 128 Color.blue(mTint) / 255f }; 129 130 mProgram.setHostValue("tint", tint_color); 131 } 132 133} 134