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 CrossProcessFilter extends Filter { 29 30 @GenerateFieldPort(name = "tile_size", hasDefault = true) 31 private int mTileSize = 640; 32 33 private Program mProgram; 34 35 private int mTarget = FrameFormat.TARGET_UNSPECIFIED; 36 37 private final String mCrossProcessShader = 38 "precision mediump float;\n" + 39 "uniform sampler2D tex_sampler_0;\n" + 40 "varying vec2 v_texcoord;\n" + 41 "void main() {\n" + 42 " vec4 color = texture2D(tex_sampler_0, v_texcoord);\n" + 43 " vec3 ncolor = vec3(0.0, 0.0, 0.0);\n" + 44 " float value;\n" + 45 " if (color.r < 0.5) {\n" + 46 " value = color.r;\n" + 47 " } else {\n" + 48 " value = 1.0 - color.r;\n" + 49 " }\n" + 50 " float red = 4.0 * value * value * value;\n" + 51 " if (color.r < 0.5) {\n" + 52 " ncolor.r = red;\n" + 53 " } else {\n" + 54 " ncolor.r = 1.0 - red;\n" + 55 " }\n" + 56 " if (color.g < 0.5) {\n" + 57 " value = color.g;\n" + 58 " } else {\n" + 59 " value = 1.0 - color.g;\n" + 60 " }\n" + 61 " float green = 2.0 * value * value;\n" + 62 " if (color.g < 0.5) {\n" + 63 " ncolor.g = green;\n" + 64 " } else {\n" + 65 " ncolor.g = 1.0 - green;\n" + 66 " }\n" + 67 " ncolor.b = color.b * 0.5 + 0.25;\n" + 68 " gl_FragColor = vec4(ncolor.rgb, color.a);\n" + 69 "}\n"; 70 71 public CrossProcessFilter(String name) { 72 super(name); 73 } 74 75 @Override 76 public void setupPorts() { 77 addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA)); 78 addOutputBasedOnInput("image", "image"); 79 } 80 81 @Override 82 public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) { 83 return inputFormat; 84 } 85 86 public void initProgram(FilterContext context, int target) { 87 switch (target) { 88 case FrameFormat.TARGET_GPU: 89 ShaderProgram shaderProgram = new ShaderProgram(context, mCrossProcessShader); 90 shaderProgram.setMaximumTileSize(mTileSize); 91 mProgram = shaderProgram; 92 break; 93 94 default: 95 throw new RuntimeException("Filter CrossProcess does not support frames of " + 96 "target " + target + "!"); 97 } 98 mTarget = target; 99 } 100 101 @Override 102 public void process(FilterContext context) { 103 // Get input frame 104 Frame input = pullInput("image"); 105 FrameFormat inputFormat = input.getFormat(); 106 107 // Create program if not created already 108 if (mProgram == null || inputFormat.getTarget() != mTarget) { 109 initProgram(context, inputFormat.getTarget()); 110 } 111 112 // Create output frame 113 Frame output = context.getFrameManager().newFrame(inputFormat); 114 115 // Process 116 mProgram.process(input, output); 117 118 // Push output 119 pushOutput("image", output); 120 121 // Release pushed frame 122 output.release(); 123 } 124} 125