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.MutableFrameFormat; 25import android.filterfw.core.Program; 26import android.filterfw.core.ShaderProgram; 27import android.filterfw.format.ImageFormat; 28import android.filterfw.geometry.Point; 29import android.filterfw.geometry.Quad; 30 31/** 32 * The FixedRotationFilter rotates the input image clockwise, it only accepts 33 * 4 rotation angles: 0, 90, 180, 270 34 * @hide 35 */ 36public class FixedRotationFilter extends Filter { 37 38 @GenerateFieldPort(name = "rotation", hasDefault = true) 39 private int mRotation = 0; 40 41 private ShaderProgram mProgram = null; 42 43 public FixedRotationFilter(String name) { 44 super(name); 45 } 46 47 @Override 48 public void setupPorts() { 49 addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA, 50 FrameFormat.TARGET_GPU)); 51 addOutputBasedOnInput("image", "image"); 52 } 53 54 @Override 55 public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) { 56 return inputFormat; 57 } 58 59 @Override 60 public void process(FilterContext context) { 61 Frame input = pullInput("image"); 62 if (mRotation == 0) { 63 pushOutput("image", input); 64 return; 65 } 66 FrameFormat inputFormat = input.getFormat(); 67 68 // Create program if not created already 69 if (mProgram == null) { 70 mProgram = ShaderProgram.createIdentity(context); 71 } 72 MutableFrameFormat outputFormat = inputFormat.mutableCopy(); 73 int width = inputFormat.getWidth(); 74 int height = inputFormat.getHeight(); 75 Point p1 = new Point(0.0f, 0.0f); 76 Point p2 = new Point(1.0f, 0.0f); 77 Point p3 = new Point(0.0f, 1.0f); 78 Point p4 = new Point(1.0f, 1.0f); 79 Quad sourceRegion; 80 switch (((int)Math.round(mRotation / 90f)) % 4) { 81 case 1: 82 sourceRegion = new Quad(p3,p1,p4,p2); 83 outputFormat.setDimensions(height, width); 84 break; 85 case 2: 86 sourceRegion = new Quad(p4,p3,p2,p1); 87 break; 88 case 3: 89 sourceRegion = new Quad(p2,p4,p1,p3); 90 outputFormat.setDimensions(height, width); 91 break; 92 case 0: 93 default: 94 sourceRegion = new Quad(p1,p2,p3,p4); 95 break; 96 } 97 // Create output frame 98 Frame output = context.getFrameManager().newFrame(outputFormat); 99 100 // Set the source region 101 mProgram.setSourceRegion(sourceRegion); 102 103 // Process 104 mProgram.process(input, output); 105 106 // Push output 107 pushOutput("image", output); 108 109 // Release pushed frame 110 output.release(); 111 } 112} 113