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