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; 27import android.graphics.Color; 28 29public class DuotoneFilter extends Filter { 30 31 @GenerateFieldPort(name = "first_color", hasDefault = true) 32 private int mFirstColor = 0xFFFF0000; 33 34 @GenerateFieldPort(name = "second_color", hasDefault = true) 35 private int mSecondColor = 0xFFFFFF00; 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 mDuotoneShader = 44 "precision mediump float;\n" + 45 "uniform sampler2D tex_sampler_0;\n" + 46 "uniform vec3 first;\n" + 47 "uniform vec3 second;\n" + 48 "varying vec2 v_texcoord;\n" + 49 "void main() {\n" + 50 " vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" + 51 " float energy = (color.r + color.g + color.b) * 0.3333;\n" + 52 " vec3 new_color = (1.0 - energy) * first + energy * second;\n" + 53 " gl_FragColor = vec4(new_color.rgb, color.a);\n" + 54 "}\n"; 55 56 public DuotoneFilter(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, mDuotoneShader); 75 shaderProgram.setMaximumTileSize(mTileSize); 76 mProgram = shaderProgram; 77 break; 78 79 default: 80 throw new RuntimeException("Filter Duotone does not support frames of " + 81 "target " + target + "!"); 82 } 83 mTarget = target; 84 } 85 86 @Override 87 public void process(FilterContext context) { 88 // Get input frame 89 Frame input = pullInput("image"); 90 FrameFormat inputFormat = input.getFormat(); 91 92 // Create output frame 93 Frame output = context.getFrameManager().newFrame(inputFormat); 94 95 // Create program if not created already 96 if (mProgram == null || inputFormat.getTarget() != mTarget) { 97 initProgram(context, inputFormat.getTarget()); 98 } 99 updateParameters(); 100 101 // Process 102 mProgram.process(input, output); 103 104 // Push output 105 pushOutput("image", output); 106 107 // Release pushed frame 108 output.release(); 109 } 110 111 private void updateParameters() { 112 float first[] = { Color.red(mFirstColor)/255f, 113 Color.green(mFirstColor)/255f, 114 Color.blue(mFirstColor)/255f }; 115 float second[] = { Color.red(mSecondColor)/255f, 116 Color.green(mSecondColor)/255f, 117 Color.blue(mSecondColor)/255f }; 118 119 mProgram.setHostValue("first", first); 120 mProgram.setHostValue("second", second); 121 } 122} 123