1/* 2 * Copyright 2013 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 androidx.media.filterfw.samples.simplecamera; 18 19 20import android.content.res.AssetManager; 21import android.graphics.Bitmap; 22import android.graphics.BitmapFactory; 23import android.net.Uri; 24import android.provider.MediaStore; 25import androidx.media.filterfw.Filter; 26import androidx.media.filterfw.FrameImage2D; 27import androidx.media.filterfw.FrameType; 28import androidx.media.filterfw.FrameValue; 29import androidx.media.filterfw.MffContext; 30import androidx.media.filterfw.MffFilterTestCase; 31 32import java.io.FileNotFoundException; 33import java.io.IOException; 34import java.util.concurrent.ExecutionException; 35import java.util.concurrent.TimeoutException; 36 37public class IfElseFilterTest extends MffFilterTestCase { 38 private final static int BIG_INPUT_WIDTH = 1536; 39 private final static int BIG_INPUT_HEIGHT = 2048; 40 private final static int SMALL_INPUT_WIDTH = 480; 41 private final static int SMALL_INPUT_HEIGHT = 640; 42 43 private AssetManager assetMgr = null; 44 @Override 45 protected Filter createFilter(MffContext mffContext) { 46 assetMgr = mffContext.getApplicationContext().getAssets(); 47 return new IfElseFilter(mffContext, "ifElseFilter"); 48 } 49 50 public void testIfElseFilterTrue() throws Exception { 51 FrameImage2D image = 52 createFrame(FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.READ_CPU), 53 new int[] {BIG_INPUT_WIDTH,BIG_INPUT_HEIGHT}).asFrameImage2D(); 54 FrameImage2D video = 55 createFrame(FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.READ_CPU), 56 new int[] {SMALL_INPUT_WIDTH,SMALL_INPUT_HEIGHT}).asFrameImage2D(); 57 58 // Image of legs 59 Bitmap videoBitmap = BitmapFactory.decodeStream(assetMgr.open("0002_000390.jpg")); 60 // Image of a face 61 Bitmap imageBitmap = BitmapFactory.decodeStream(assetMgr.open("XZZ019.jpg")); 62 63 image.setBitmap(imageBitmap); 64 injectInputFrame("falseResult", image); 65 video.setBitmap(videoBitmap); 66 injectInputFrame("trueResult", video); 67 68 FrameValue conditionFrame = createFrame(FrameType.single(boolean.class), new int[] {1}). 69 asFrameValue(); 70 conditionFrame.setValue(true); 71 injectInputFrame("condition", conditionFrame); 72 73 process(); 74 75 // Ensure that for true, we use the video input 76 FrameImage2D outputImage = getOutputFrame("output").asFrameImage2D(); 77 assertEquals(outputImage, video); 78 } 79 80 public void testIfElseFilterFalse() throws Exception { 81 82 FrameImage2D image = 83 createFrame(FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.READ_CPU), 84 new int[] {BIG_INPUT_WIDTH,BIG_INPUT_HEIGHT}).asFrameImage2D(); 85 FrameImage2D video = 86 createFrame(FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.READ_CPU), 87 new int[] {SMALL_INPUT_WIDTH,SMALL_INPUT_HEIGHT}).asFrameImage2D(); 88 89 // Image of legs 90 Bitmap videoBitmap = BitmapFactory.decodeStream(assetMgr.open("0002_000390.jpg")); 91 // Image of a face 92 Bitmap imageBitmap = BitmapFactory.decodeStream(assetMgr.open("XZZ019.jpg")); 93 94 image.setBitmap(imageBitmap); 95 injectInputFrame("falseResult", image); 96 video.setBitmap(videoBitmap); 97 injectInputFrame("trueResult", video); 98 99 100 FrameValue conditionFrame = createFrame(FrameType.single(boolean.class), new int[] {1}). 101 asFrameValue(); 102 conditionFrame.setValue(false); 103 injectInputFrame("condition", conditionFrame); 104 105 process(); 106 107 // Ensure that for true, we use the video input 108 FrameImage2D outputImage = getOutputFrame("output").asFrameImage2D(); 109 assertEquals(outputImage, image); 110 } 111} 112