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 28public class SepiaFilter extends Filter { 29 30 @GenerateFieldPort(name = "tile_size", hasDefault = true) 31 private int mTileSize = 640; 32 33 private Program mProgram; 34 private int mTarget = FrameFormat.TARGET_UNSPECIFIED; 35 36 private final String mSepiaShader = 37 "precision mediump float;\n" + 38 "uniform sampler2D tex_sampler_0;\n" + 39 "uniform mat3 matrix;\n" + 40 "varying vec2 v_texcoord;\n" + 41 "void main() {\n" + 42 " vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" + 43 " vec3 new_color = min(matrix * color.rgb, 1.0);\n" + 44 " gl_FragColor = vec4(new_color.rgb, color.a);\n" + 45 "}\n"; 46 47 public SepiaFilter(String name) { 48 super(name); 49 } 50 51 @Override 52 public void setupPorts() { 53 addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA)); 54 addOutputBasedOnInput("image", "image"); 55 } 56 57 @Override 58 public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) { 59 return inputFormat; 60 } 61 62 public void initProgram(FilterContext context, int target) { 63 switch (target) { 64 case FrameFormat.TARGET_GPU: 65 ShaderProgram shaderProgram = new ShaderProgram(context, mSepiaShader); 66 shaderProgram.setMaximumTileSize(mTileSize); 67 mProgram = shaderProgram; 68 break; 69 70 default: 71 throw new RuntimeException("Filter Sharpen does not support frames of " + 72 "target " + target + "!"); 73 } 74 mTarget = target; 75 } 76 77 @Override 78 public void process(FilterContext context) { 79 // Get input frame 80 Frame input = pullInput("image"); 81 FrameFormat inputFormat = input.getFormat(); 82 83 // Create output frame 84 Frame output = context.getFrameManager().newFrame(inputFormat); 85 86 // Create program if not created already 87 if (mProgram == null || inputFormat.getTarget() != mTarget) { 88 initProgram(context, inputFormat.getTarget()); 89 initParameters(); 90 } 91 92 // Process 93 mProgram.process(input, output); 94 95 // Push output 96 pushOutput("image", output); 97 98 // Release pushed frame 99 output.release(); 100 } 101 102 private void initParameters() { 103 float weights[] = { 805.0f / 2048.0f, 715.0f / 2048.0f, 557.0f / 2048.0f, 104 1575.0f / 2048.0f, 1405.0f / 2048.0f, 1097.0f / 2048.0f, 105 387.0f / 2048.0f, 344.0f / 2048.0f, 268.0f / 2048.0f }; 106 mProgram.setHostValue("matrix", weights); 107 } 108} 109