| package com.android.rs.refocus; |
| |
| import com.android.rs.refocus.d1new.RefocusFilterd1new; |
| import com.android.rs.refocus.f32.RefocusFilterF32; |
| |
| import android.graphics.Bitmap; |
| import android.support.v8.renderscript.RenderScript; |
| import android.util.Log; |
| import android.util.Pair; |
| import java.util.ArrayList; |
| |
| /** |
| * An wrapper class that calls the refocus filtering function in |
| * {@code RefocusFilter} class. The class also contains several default |
| * parameters that are used in calling the refocus filtering function. |
| * |
| * Example usage: |
| * |
| * {@code DepthOfFieldOptions options;} |
| * {@code RenderScriptTask renderScriptTask;} |
| * {@code Bitmap result = renderScriptTask.applyRefocusFilter(options);} |
| * |
| * @author zhl@google.com (Li Zhang) |
| */ |
| public class RenderScriptTask { |
| enum script{f32, d1new}; |
| /** |
| * A flag to choose the version of RenderScript. |
| */ |
| private script mScript = script.d1new; |
| |
| /** |
| * An enum for the different types of Render Script tasks. (generated by zhl) |
| */ |
| public enum Purpose { |
| VIEWER, SERVICE |
| } |
| |
| //private static final Log.Tag TAG = new Log.Tag("RenderScriptTask"); |
| private static final String TAG = "RenderScriptTask"; |
| /** |
| * Number of blending layers in which the quantized depth levels are grouped. |
| */ |
| private static final int NUM_BLENDING_LAYERS = 8; |
| |
| /** |
| * An object that records the blur disk radius for each quantized inverse |
| * depth level and how all the depth levels are grouped into blending layers. |
| */ |
| public BlurStack blurStack; |
| |
| /** |
| * An image in which each pixel has red, green, blue, and quantized inverse |
| * depth level. The quantized inverse depth levels range from 1 to |
| * {@code BlurStack.MAX_DEPTH}. 0 is reserved for padding pixels. |
| * |
| * <b> The pixels with larger depth values are closer to the camera. |
| */ |
| private Bitmap rgbdImage; |
| |
| /** |
| * The Render Script context that is required to construct the filter. |
| */ |
| private RenderScript renderScript; |
| |
| public ArrayList<Pair<String,Long>> timings; |
| |
| /** |
| * A constructor of render script context. |
| * |
| * @param renderScript RenderScript context. |
| */ |
| public RenderScriptTask(RenderScript renderScript, script sChoice) { |
| this.renderScript = renderScript; |
| this.mScript = sChoice; |
| } |
| |
| /** |
| * A function that computes a refocused image from an instance of |
| * {@code DepthOfFieldOptions}. |
| * |
| * @param options an object contains color image, depth map, focal depth, and |
| * the amount of desired blur ({@code blurInfinity}) |
| * @return the refocus filtering result |
| */ |
| public Bitmap applyRefocusFilter(DepthOfFieldOptions options) { |
| // Generates {@code rgbdImage} and {@code blurStack}. |
| prepareRefocusFilter(options); |
| |
| // Check which version of RenderScript code is used. |
| RefocusFilter filter; |
| switch (mScript) { |
| case f32: |
| filter = new RefocusFilterF32(renderScript); |
| break; |
| case d1new: |
| filter = new RefocusFilterd1new(renderScript); |
| break; |
| default: |
| filter = null; |
| } |
| |
| long startTime = System.currentTimeMillis(); |
| Bitmap outputImage = filter.compute(rgbdImage, blurStack); |
| long endTime = System.currentTimeMillis(); |
| filter.logTiming(TAG, "TOTAL", endTime - startTime, "ms"); |
| |
| timings = filter.timings; |
| |
| return outputImage; |
| } |
| |
| /** |
| * A function that computes {@code rgbdImage} and {@code blurStack} from an |
| * instance of {@code DepthOfFieldOptions}. |
| * |
| * @param options an object contains color image, depth map, focal depth, and |
| * the amount of desired blur ({@code blurInfinity}). |
| */ |
| private void prepareRefocusFilter(DepthOfFieldOptions options) { |
| blurStack = BlurStack.createFromDepthTransform( |
| options.rgbz.getDepthTransform(), options.focalDepth, |
| options.depthOfField, options.blurInfinity, NUM_BLENDING_LAYERS); |
| |
| rgbdImage = options.rgbz.getBitmap(); |
| } |
| } |