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 17 18package android.filterpacks.imageproc; 19 20import android.filterfw.core.Filter; 21import android.filterfw.core.FilterContext; 22import android.filterfw.core.Frame; 23import android.filterfw.core.FrameFormat; 24import android.filterfw.core.KeyValueMap; 25import android.filterfw.core.MutableFrameFormat; 26import android.filterfw.core.NativeProgram; 27import android.filterfw.core.NativeFrame; 28import android.filterfw.core.Program; 29import android.filterfw.core.ShaderProgram; 30import android.filterfw.format.ImageFormat; 31 32import android.util.Log; 33 34/** 35 * @hide 36 */ 37public class ToRGBAFilter extends Filter { 38 39 private int mInputBPP; 40 private Program mProgram; 41 private FrameFormat mLastFormat = null; 42 43 public ToRGBAFilter(String name) { 44 super(name); 45 } 46 47 @Override 48 public void setupPorts() { 49 MutableFrameFormat mask = new MutableFrameFormat(FrameFormat.TYPE_BYTE, 50 FrameFormat.TARGET_NATIVE); 51 mask.setDimensionCount(2); 52 addMaskedInputPort("image", mask); 53 addOutputBasedOnInput("image", "image"); 54 } 55 56 @Override 57 public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) { 58 return getConvertedFormat(inputFormat); 59 } 60 61 public FrameFormat getConvertedFormat(FrameFormat format) { 62 MutableFrameFormat result = format.mutableCopy(); 63 result.setMetaValue(ImageFormat.COLORSPACE_KEY, ImageFormat.COLORSPACE_RGBA); 64 result.setBytesPerSample(4); 65 return result; 66 } 67 68 public void createProgram(FilterContext context, FrameFormat format) { 69 mInputBPP = format.getBytesPerSample(); 70 if (mLastFormat != null && mLastFormat.getBytesPerSample() == mInputBPP) return; 71 mLastFormat = format; 72 switch (mInputBPP) { 73 case 1: 74 mProgram = new NativeProgram("filterpack_imageproc", "gray_to_rgba"); 75 break; 76 case 3: 77 mProgram = new NativeProgram("filterpack_imageproc", "rgb_to_rgba"); 78 break; 79 default: 80 throw new RuntimeException("Unsupported BytesPerPixel: " + mInputBPP + "!"); 81 } 82 } 83 84 @Override 85 public void process(FilterContext context) { 86 // Get input frame 87 Frame input = pullInput("image"); 88 createProgram(context, input.getFormat()); 89 90 // Create output frame 91 Frame output = context.getFrameManager().newFrame(getConvertedFormat(input.getFormat())); 92 93 // Process 94 mProgram.process(input, output); 95 96 // Push output 97 pushOutput("image", output); 98 99 // Release pushed frame 100 output.release(); 101 } 102 103} 104