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 java.util.Date; 29import java.util.Random; 30 31public class DocumentaryFilter extends Filter { 32 33 @GenerateFieldPort(name = "tile_size", hasDefault = true) 34 private int mTileSize = 640; 35 36 private Program mProgram; 37 private Random mRandom; 38 39 private int mWidth = 0; 40 private int mHeight = 0; 41 private int mTarget = FrameFormat.TARGET_UNSPECIFIED; 42 43 private final String mDocumentaryShader = 44 "precision mediump float;\n" + 45 "uniform sampler2D tex_sampler_0;\n" + 46 "uniform vec2 seed;\n" + 47 "uniform float stepsize;\n" + 48 "uniform float inv_max_dist;\n" + 49 "uniform vec2 scale;\n" + 50 "varying vec2 v_texcoord;\n" + 51 "float rand(vec2 loc) {\n" + 52 " float theta1 = dot(loc, vec2(0.9898, 0.233));\n" + 53 " float theta2 = dot(loc, vec2(12.0, 78.0));\n" + 54 " float value = cos(theta1) * sin(theta2) + sin(theta1) * cos(theta2);\n" + 55 // keep value of part1 in range: (2^-14 to 2^14). 56 " float temp = mod(197.0 * value, 1.0) + value;\n" + 57 " float part1 = mod(220.0 * temp, 1.0) + temp;\n" + 58 " float part2 = value * 0.5453;\n" + 59 " float part3 = cos(theta1 + theta2) * 0.43758;\n" + 60 " return fract(part1 + part2 + part3);\n" + 61 "}\n" + 62 "void main() {\n" + 63 // black white 64 " vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" + 65 " float dither = rand(v_texcoord + seed);\n" + 66 " vec3 xform = clamp(2.0 * color.rgb, 0.0, 1.0);\n" + 67 " vec3 temp = clamp(2.0 * (color.rgb + stepsize), 0.0, 1.0);\n" + 68 " vec3 new_color = clamp(xform + (temp - xform) * (dither - 0.5), 0.0, 1.0);\n" + 69 // grayscale 70 " float gray = dot(new_color, vec3(0.299, 0.587, 0.114));\n" + 71 " new_color = vec3(gray, gray, gray);\n" + 72 // vignette 73 " vec2 coord = v_texcoord - vec2(0.5, 0.5);\n" + 74 " float dist = length(coord * scale);\n" + 75 " float lumen = 0.85 / (1.0 + exp((dist * inv_max_dist - 0.83) * 20.0)) + 0.15;\n" + 76 " gl_FragColor = vec4(new_color * lumen, color.a);\n" + 77 "}\n"; 78 79 public DocumentaryFilter(String name) { 80 super(name); 81 Date date = new Date(); 82 mRandom = new Random(new Date().getTime()); 83 } 84 85 @Override 86 public void setupPorts() { 87 addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA)); 88 addOutputBasedOnInput("image", "image"); 89 } 90 91 @Override 92 public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) { 93 return inputFormat; 94 } 95 96 public void initProgram(FilterContext context, int target) { 97 switch (target) { 98 case FrameFormat.TARGET_GPU: 99 ShaderProgram shaderProgram = new ShaderProgram(context, mDocumentaryShader); 100 shaderProgram.setMaximumTileSize(mTileSize); 101 mProgram = shaderProgram; 102 break; 103 104 default: 105 throw new RuntimeException("Filter Sharpen does not support frames of " + 106 "target " + target + "!"); 107 } 108 mTarget = target; 109 } 110 111 @Override 112 public void process(FilterContext context) { 113 // Get input frame 114 Frame input = pullInput("image"); 115 FrameFormat inputFormat = input.getFormat(); 116 117 // Create program if not created already 118 if (mProgram == null || inputFormat.getTarget() != mTarget) { 119 initProgram(context, inputFormat.getTarget()); 120 } 121 122 // Check if the frame size has changed 123 if (inputFormat.getWidth() != mWidth || inputFormat.getHeight() != mHeight) { 124 mWidth = inputFormat.getWidth(); 125 mHeight = inputFormat.getHeight(); 126 initParameters(); 127 } 128 129 // Create output frame 130 Frame output = context.getFrameManager().newFrame(inputFormat); 131 132 // Process 133 mProgram.process(input, output); 134 135 // Push output 136 pushOutput("image", output); 137 138 // Release pushed frame 139 output.release(); 140 } 141 142 private void initParameters() { 143 if (mProgram != null) { 144 float scale[] = new float[2]; 145 if (mWidth > mHeight) { 146 scale[0] = 1f; 147 scale[1] = ((float) mHeight) / mWidth; 148 } else { 149 scale[0] = ((float) mWidth) / mHeight; 150 scale[1] = 1f; 151 } 152 float max_dist = ((float) Math.sqrt(scale[0] * scale[0] + scale[1] * scale[1])) * 0.5f; 153 154 mProgram.setHostValue("scale", scale); 155 mProgram.setHostValue("inv_max_dist", 1.0f / max_dist); 156 mProgram.setHostValue("stepsize", 1.0f / 255.0f); 157 158 float seed[] = { mRandom.nextFloat(), mRandom.nextFloat() }; 159 mProgram.setHostValue("seed", seed); 160 } 161 } 162} 163