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; /** * 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. * * 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; /** * 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) { long startTime = System.currentTimeMillis(); // Generates {@code rgbdImage} and {@code blurStack}. prepareRefocusFilter(options); Bitmap outputImage = null; // Check which version of RenderScript code is used. switch (mScript) { case f32: RefocusFilterF32 rfFilterF32 = new RefocusFilterF32(renderScript); outputImage = rfFilterF32.compute(rgbdImage, blurStack); break; case d1new: RefocusFilterd1new rfFilterd1new = new RefocusFilterd1new(renderScript); outputImage = rfFilterd1new.compute(rgbdImage, blurStack); break; } long endTime = System.currentTimeMillis(); float duration = (endTime - startTime); Log.d(TAG, "applyRefocusFilter is finished in " + (duration / 1000.0f) + " seconds"); 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(); } }