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