BlackWhiteFilter.java revision f4b403159f8a89eb2158bbb6c314de3bc90fee73
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; 30 31import java.util.Random; 32 33public class BlackWhiteFilter extends Filter { 34 35 @GenerateFieldPort(name = "black", hasDefault = true) 36 private float mBlack = 0f; 37 38 @GenerateFieldPort(name = "white", hasDefault = true) 39 private float mWhite = 1f; 40 41 @GenerateFieldPort(name = "tile_size", hasDefault = true) 42 private int mTileSize = 640; 43 44 private Program mProgram; 45 46 private int mWidth = 0; 47 private int mHeight = 0; 48 private int mTarget = FrameFormat.TARGET_UNSPECIFIED; 49 50 private final String mBlackWhiteShader = 51 "precision mediump float;\n" + 52 "uniform sampler2D tex_sampler_0;\n" + 53 "uniform float black;\n" + 54 "uniform float scale;\n" + 55 "uniform float stepsize;\n" + 56 "varying vec2 v_texcoord;\n" + 57 "float rand(vec2 loc) {\n" + 58 " return fract(sin(dot(loc, vec2(12.9898, 78.233))) * 43758.5453);\n" + 59 "}\n" + 60 "void main() {\n" + 61 " vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" + 62 " float dither = rand(v_texcoord);\n" + 63 " vec3 xform = clamp((color.rgb - black) * scale, 0.0, 1.0);\n" + 64 " vec3 temp = clamp((color.rgb + stepsize - black) * scale, 0.0, 1.0);\n" + 65 " vec3 new_color = clamp(xform + (temp - xform) * (dither - 0.5), 0.0, 1.0);\n" + 66 " gl_FragColor = vec4(new_color, color.a);\n" + 67 "}\n"; 68 69 public BlackWhiteFilter(String name) { 70 super(name); 71 } 72 73 @Override 74 public void setupPorts() { 75 addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA)); 76 addOutputBasedOnInput("image", "image"); 77 } 78 79 @Override 80 public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) { 81 return inputFormat; 82 } 83 84 public void initProgram(FilterContext context, int target) { 85 switch (target) { 86 case FrameFormat.TARGET_GPU: 87 ShaderProgram shaderProgram = new ShaderProgram(context, mBlackWhiteShader); 88 shaderProgram.setMaximumTileSize(mTileSize); 89 mProgram = shaderProgram; 90 updateParameters(); 91 break; 92 93 default: 94 throw new RuntimeException("Filter Sharpen does not support frames of " + 95 "target " + target + "!"); 96 } 97 mTarget = target; 98 } 99 100 private void updateParameters() { 101 float scale = (mBlack != mWhite) ? 1.0f / (mWhite - mBlack) : 2000f; 102 float stepsize = 1.0f / 255.0f; 103 104 mProgram.setHostValue("black", mBlack); 105 mProgram.setHostValue("scale", scale); 106 mProgram.setHostValue("stepsize", stepsize); 107 } 108 109 @Override 110 public void fieldPortValueUpdated(String name, FilterContext context) { 111 if (mProgram != null) { 112 updateParameters(); 113 } 114 } 115 116 @Override 117 public void process(FilterContext context) { 118 // Get input frame 119 Frame input = pullInput("image"); 120 FrameFormat inputFormat = input.getFormat(); 121 122 // Create program if not created already 123 if (mProgram == null || inputFormat.getTarget() != mTarget) { 124 initProgram(context, inputFormat.getTarget()); 125 } 126 127 // Check if the frame size has changed 128 if (inputFormat.getWidth() != mWidth || inputFormat.getHeight() != mHeight) { 129 mWidth = inputFormat.getWidth(); 130 mHeight = inputFormat.getHeight(); 131 } 132 133 // Create output frame 134 Frame output = context.getFrameManager().newFrame(inputFormat); 135 136 // Process 137 mProgram.process(input, output); 138 139 // Push output 140 pushOutput("image", output); 141 142 // Release pushed frame 143 output.release(); 144 } 145} 146