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.Bitmap; 28 29/** 30 * @hide 31 */ 32public class BitmapOverlayFilter extends Filter { 33 34 @GenerateFieldPort(name = "bitmap") 35 private Bitmap mBitmap; 36 37 @GenerateFieldPort(name = "tile_size", hasDefault = true) 38 private int mTileSize = 640; 39 40 private Program mProgram; 41 private Frame mFrame; 42 43 private int mTarget = FrameFormat.TARGET_UNSPECIFIED; 44 45 private final String mOverlayShader = 46 "precision mediump float;\n" + 47 "uniform sampler2D tex_sampler_0;\n" + 48 "uniform sampler2D tex_sampler_1;\n" + 49 "varying vec2 v_texcoord;\n" + 50 "void main() {\n" + 51 " vec4 original = texture2D(tex_sampler_0, v_texcoord);\n" + 52 " vec4 mask = texture2D(tex_sampler_1, v_texcoord);\n" + 53 " gl_FragColor = vec4(original.rgb * (1.0 - mask.a) + mask.rgb, 1.0);\n" + 54 "}\n"; 55 56 public BitmapOverlayFilter(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, mOverlayShader); 75 shaderProgram.setMaximumTileSize(mTileSize); 76 mProgram = shaderProgram; 77 break; 78 79 default: 80 throw new RuntimeException("Filter FisheyeFilter does not support frames of " + 81 "target " + target + "!"); 82 } 83 mTarget = target; 84 } 85 86 @Override 87 public void tearDown(FilterContext context) { 88 if (mFrame != null) { 89 mFrame.release(); 90 mFrame = null; 91 } 92 } 93 94 @Override 95 public void process(FilterContext context) { 96 // Get input frame 97 Frame input = pullInput("image"); 98 FrameFormat inputFormat = input.getFormat(); 99 100 // Create output frame 101 Frame output = context.getFrameManager().newFrame(inputFormat); 102 103 // Create program if not created already 104 if (mProgram == null || inputFormat.getTarget() != mTarget) { 105 initProgram(context, inputFormat.getTarget()); 106 } 107 108 if (mBitmap != null) { 109 Frame frame = createBitmapFrame(context); 110 // Process 111 Frame[] inputs = {input, frame}; 112 mProgram.process(inputs, output); 113 114 frame.release(); 115 } else { 116 output.setDataFromFrame(input); 117 } 118 119 // Push output 120 pushOutput("image", output); 121 122 // Release pushed frame 123 output.release(); 124 } 125 126 private Frame createBitmapFrame(FilterContext context) { 127 FrameFormat format = ImageFormat.create(mBitmap.getWidth(), 128 mBitmap.getHeight(), 129 ImageFormat.COLORSPACE_RGBA, 130 FrameFormat.TARGET_GPU); 131 132 Frame frame = context.getFrameManager().newFrame(format); 133 frame.setBitmap(mBitmap); 134 135 mBitmap.recycle(); 136 mBitmap = null; 137 138 return frame; 139 } 140} 141