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 DuotoneFilter extends Filter { 33 34 @GenerateFieldPort(name = "first_color", hasDefault = true) 35 private int mFirstColor = 0xFFFF0000; 36 37 @GenerateFieldPort(name = "second_color", hasDefault = true) 38 private int mSecondColor = 0xFFFFFF00; 39 40 @GenerateFieldPort(name = "tile_size", hasDefault = true) 41 private int mTileSize = 640; 42 43 private Program mProgram; 44 private int mTarget = FrameFormat.TARGET_UNSPECIFIED; 45 46 private final String mDuotoneShader = 47 "precision mediump float;\n" + 48 "uniform sampler2D tex_sampler_0;\n" + 49 "uniform vec3 first;\n" + 50 "uniform vec3 second;\n" + 51 "varying vec2 v_texcoord;\n" + 52 "void main() {\n" + 53 " vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" + 54 " float energy = (color.r + color.g + color.b) * 0.3333;\n" + 55 " vec3 new_color = (1.0 - energy) * first + energy * second;\n" + 56 " gl_FragColor = vec4(new_color.rgb, color.a);\n" + 57 "}\n"; 58 59 public DuotoneFilter(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, mDuotoneShader); 78 shaderProgram.setMaximumTileSize(mTileSize); 79 mProgram = shaderProgram; 80 break; 81 82 default: 83 throw new RuntimeException("Filter Duotone 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 output frame 96 Frame output = context.getFrameManager().newFrame(inputFormat); 97 98 // Create program if not created already 99 if (mProgram == null || inputFormat.getTarget() != mTarget) { 100 initProgram(context, inputFormat.getTarget()); 101 } 102 updateParameters(); 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 float first[] = { Color.red(mFirstColor)/255f, 116 Color.green(mFirstColor)/255f, 117 Color.blue(mFirstColor)/255f }; 118 float second[] = { Color.red(mSecondColor)/255f, 119 Color.green(mSecondColor)/255f, 120 Color.blue(mSecondColor)/255f }; 121 122 mProgram.setHostValue("first", first); 123 mProgram.setHostValue("second", second); 124 } 125} 126