1 /* 2 * Copyright (C) 2015 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.renderscript.cts.refocus; 18 19 import android.graphics.Bitmap; 20 import android.renderscript.RenderScript; 21 import android.renderscript.cts.refocus.d1new.RefocusFilterd1new; 22 import android.renderscript.cts.refocus.f32.RefocusFilterF32; 23 import android.util.Log; 24 25 /** 26 * An wrapper class that calls the refocus filtering function in 27 * {@code RefocusFilter} class. The class also contains several default 28 * parameters that are used in calling the refocus filtering function. 29 * 30 * Example usage: 31 * 32 * {@code DepthOfFieldOptions options;} 33 * {@code RenderScriptTask renderScriptTask;} 34 * {@code Bitmap result = renderScriptTask.applyRefocusFilter(options);} 35 * 36 */ 37 public class RenderScriptTask { 38 enum script{f32, d1new}; 39 /** 40 * A flag to choose the version of RenderScript. 41 */ 42 private script mScript = script.d1new; 43 44 /** 45 * An enum for the different types of Render Script tasks. (generated by zhl) 46 */ 47 public enum Purpose { 48 VIEWER, SERVICE 49 } 50 51 //private static final Log.Tag TAG = new Log.Tag("RenderScriptTask"); 52 private static final String TAG = "RenderScriptTask"; 53 /** 54 * Number of blending layers in which the quantized depth levels are grouped. 55 */ 56 private static final int NUM_BLENDING_LAYERS = 8; 57 58 /** 59 * An object that records the blur disk radius for each quantized inverse 60 * depth level and how all the depth levels are grouped into blending layers. 61 */ 62 public BlurStack blurStack; 63 64 /** 65 * An image in which each pixel has red, green, blue, and quantized inverse 66 * depth level. The quantized inverse depth levels range from 1 to 67 * {@code BlurStack.MAX_DEPTH}. 0 is reserved for padding pixels. 68 * 69 * <b> The pixels with larger depth values are closer to the camera. 70 */ 71 private Bitmap rgbdImage; 72 73 /** 74 * The Render Script context that is required to construct the filter. 75 */ 76 private RenderScript renderScript; 77 78 /** 79 * A constructor of render script context. 80 * 81 * @param renderScript RenderScript context. 82 */ RenderScriptTask(RenderScript renderScript, script sChoice)83 public RenderScriptTask(RenderScript renderScript, script sChoice) { 84 this.renderScript = renderScript; 85 this.mScript = sChoice; 86 } 87 88 /** 89 * A function that computes a refocused image from an instance of 90 * {@code DepthOfFieldOptions}. 91 * 92 * @param options an object contains color image, depth map, focal depth, and 93 * the amount of desired blur ({@code blurInfinity}) 94 * @return the refocus filtering result 95 */ applyRefocusFilter(DepthOfFieldOptions options)96 public Bitmap applyRefocusFilter(DepthOfFieldOptions options) { 97 long startTime = System.currentTimeMillis(); 98 99 // Generates {@code rgbdImage} and {@code blurStack}. 100 prepareRefocusFilter(options); 101 Bitmap outputImage = null; 102 // Check which version of RenderScript code is used. 103 switch (mScript) { 104 case f32: 105 RefocusFilterF32 rfFilterF32 = new RefocusFilterF32(renderScript); 106 outputImage = 107 rfFilterF32.compute(rgbdImage, blurStack); 108 rfFilterF32.destroy(); 109 break; 110 case d1new: 111 RefocusFilterd1new rfFilterd1new = new RefocusFilterd1new(renderScript); 112 outputImage = 113 rfFilterd1new.compute(rgbdImage, blurStack); 114 rfFilterd1new.destroy(); 115 break; 116 } 117 118 long endTime = System.currentTimeMillis(); 119 float duration = (endTime - startTime); 120 Log.d(TAG, "applyRefocusFilter is finished in " + (duration / 1000.0f) 121 + " seconds"); 122 123 return outputImage; 124 } 125 126 /** 127 * A function that computes {@code rgbdImage} and {@code blurStack} from an 128 * instance of {@code DepthOfFieldOptions}. 129 * 130 * @param options an object contains color image, depth map, focal depth, and 131 * the amount of desired blur ({@code blurInfinity}). 132 */ prepareRefocusFilter(DepthOfFieldOptions options)133 private void prepareRefocusFilter(DepthOfFieldOptions options) { 134 blurStack = BlurStack.createFromDepthTransform( 135 options.rgbz.getDepthTransform(), options.focalDepth, 136 options.depthOfField, options.blurInfinity, NUM_BLENDING_LAYERS); 137 138 rgbdImage = options.rgbz.getBitmap(); 139 } 140 } 141