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