1 /* 2 * Copyright 2014 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 package android.hardware.camera2.cts.rs; 18 19 import static android.hardware.camera2.cts.helpers.Preconditions.*; 20 21 import android.graphics.ImageFormat; 22 import android.graphics.RectF; 23 import android.util.Size; 24 import android.hardware.camera2.cts.ScriptC_crop_yuvf_420_to_yuvx_444; 25 import android.hardware.camera2.cts.rs.AllocationInfo.ElementInfo; 26 import android.renderscript.Element; 27 import android.util.Log; 28 29 /** 30 * Crop {@link ImageFormat#YUV_420_888 flexible-YUV} {@link Allocation allocations} into 31 * a {@link ElementInfo#U8_3 U8_3} {@link Allocation allocation}. 32 * 33 * <p>Users of this script must configure it with the 34 * {@link ScriptYuvCrop#CROP_WINDOW crop window} parameter.</p> 35 * 36 */ 37 public class ScriptYuvCrop extends Script<ScriptC_crop_yuvf_420_to_yuvx_444> { 38 private static final String TAG = "ScriptYuvCrop"; 39 private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE); 40 41 /** 42 * A rectangle holding the top,left,right,bottom normalized coordinates each within [0,1]. 43 * 44 * <p>The output will be a cropped copy of the input to only this crop window.</p> 45 */ 46 // TODO: Change this to use Patch 47 public static final Script.ScriptParameter<ScriptYuvCrop, RectF> CROP_WINDOW = 48 new Script.ScriptParameter<ScriptYuvCrop, RectF>(ScriptYuvCrop.class, 49 RectF.class); 50 51 private final RectF mCropWindow; 52 createOutputInfo(AllocationInfo inputInfo, ParameterMap<ScriptYuvCrop> parameters)53 private static AllocationInfo createOutputInfo(AllocationInfo inputInfo, 54 ParameterMap<ScriptYuvCrop> parameters) { 55 checkNotNull("inputInfo", inputInfo); 56 checkNotNull("parameters", parameters); 57 58 if (!parameters.contains(CROP_WINDOW)) { 59 throw new IllegalArgumentException("Script's CROP_WINDOW was not set"); 60 } 61 62 Size inputSize = inputInfo.getSize(); 63 RectF crop = parameters.get(CROP_WINDOW); 64 Size outputSize = new Size( 65 (int)(crop.width() * inputSize.getWidth()), 66 (int)(crop.height() * inputSize.getHeight())); 67 68 if (VERBOSE) Log.v(TAG, String.format("createOutputInfo - outputSize is %s", outputSize)); 69 70 /** 71 * Input YUV W x H 72 * Output U8_3 CropW x CropH 73 */ 74 return AllocationInfo.newInstance(Element.U8_3(getRS()), outputSize); 75 } 76 ScriptYuvCrop(AllocationInfo inputInfo, ParameterMap<ScriptYuvCrop> parameterMap)77 public ScriptYuvCrop(AllocationInfo inputInfo, 78 ParameterMap<ScriptYuvCrop> parameterMap) { 79 super(inputInfo, 80 createOutputInfo(inputInfo, parameterMap), 81 new ScriptC_crop_yuvf_420_to_yuvx_444(getRS())); 82 83 // YUV_420_888 is the only supported format here 84 if (!inputInfo.isElementEqualTo(ElementInfo.YUV)) { 85 throw new UnsupportedOperationException("Unsupported element " 86 + inputInfo.getElement()); 87 } 88 89 mCropWindow = parameterMap.get(CROP_WINDOW); 90 } 91 92 @Override executeUnchecked()93 protected void executeUnchecked() { 94 mScript.forEach_crop(mOutputAllocation); 95 96 if (VERBOSE) { Log.v(TAG, "executeUnchecked - forEach_crop done"); } 97 } 98 99 @Override updateScriptInput()100 protected void updateScriptInput() { 101 int x = (int)(mCropWindow.left * mInputInfo.getSize().getWidth()); 102 int y = (int)(mCropWindow.top * mInputInfo.getSize().getHeight()); 103 104 mScript.set_src_x(x); 105 mScript.set_src_y(y); 106 107 mScript.set_mInput(mInputAllocation); 108 } 109 } 110