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 28/** 29 * @hide 30 */ 31public class FlipFilter extends Filter { 32 33 @GenerateFieldPort(name = "vertical", hasDefault = true) 34 private boolean mVertical = false; 35 36 @GenerateFieldPort(name = "horizontal", hasDefault = true) 37 private boolean mHorizontal = false; 38 39 @GenerateFieldPort(name = "tile_size", hasDefault = true) 40 private int mTileSize = 640; 41 42 private Program mProgram; 43 private int mTarget = FrameFormat.TARGET_UNSPECIFIED; 44 45 public FlipFilter(String name) { 46 super(name); 47 } 48 49 @Override 50 public void setupPorts() { 51 addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA)); 52 addOutputBasedOnInput("image", "image"); 53 } 54 55 @Override 56 public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) { 57 return inputFormat; 58 } 59 60 public void initProgram(FilterContext context, int target) { 61 switch (target) { 62 case FrameFormat.TARGET_GPU: 63 ShaderProgram shaderProgram = ShaderProgram.createIdentity(context); 64 shaderProgram.setMaximumTileSize(mTileSize); 65 mProgram = shaderProgram; 66 break; 67 68 default: 69 throw new RuntimeException("Filter Sharpen does not support frames of " + 70 "target " + target + "!"); 71 } 72 mTarget = target; 73 updateParameters(); 74 } 75 76 @Override 77 public void fieldPortValueUpdated(String name, FilterContext context) { 78 if (mProgram != null) { 79 updateParameters(); 80 } 81 } 82 83 @Override 84 public void process(FilterContext context) { 85 // Get input frame 86 Frame input = pullInput("image"); 87 FrameFormat inputFormat = input.getFormat(); 88 89 // Create program if not created already 90 if (mProgram == null || inputFormat.getTarget() != mTarget) { 91 initProgram(context, inputFormat.getTarget()); 92 } 93 94 // Create output frame 95 Frame output = context.getFrameManager().newFrame(inputFormat); 96 97 // Process 98 mProgram.process(input, output); 99 100 // Push output 101 pushOutput("image", output); 102 103 // Release pushed frame 104 output.release(); 105 } 106 107 private void updateParameters() { 108 float x_origin = (mHorizontal) ? 1.0f : 0.0f; 109 float y_origin = (mVertical) ? 1.0f : 0.0f; 110 111 float width = (mHorizontal) ? -1.0f : 1.0f; 112 float height = (mVertical) ? -1.0f : 1.0f; 113 114 ((ShaderProgram) mProgram).setSourceRect(x_origin, y_origin, width, height); 115 } 116} 117