summaryrefslogtreecommitdiff
path: root/java/tests/Refocus/src/com/android/rs/test/RenderScriptTask.java
blob: 6c932ba25b0b585499c78ececa7cfc99f43e25b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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.
   *
   * <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;

  /**
   * 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();
  }
}