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