1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.renderscript.cts.refocus;
18 
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.BitmapFactory;
22 import android.renderscript.cts.refocus.image.RangeInverseDepthTransform;
23 import android.net.Uri;
24 
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 
29 public class DepthImage {
30     private final String mFormat;
31     private final double mFar;
32     private final double mNear;
33     private final Bitmap mDepthBitmap;
34     private final double mBlurAtInfinity;
35     private final double mFocalDistance;
36     private final double mDepthOfFiled;
37     private final double mFocalPointX;
38     private final double mFocalPointY;
39     private final DepthTransform mDepthTransform;
40 
DepthImage(String format, double far, double near, Bitmap depthBitmap, double blurAtInfinity, double focalDistance, double depthOfField, double focalPointX, double focalPointY, DepthTransform depthTransform)41     public DepthImage(String format, double far, double near,
42                       Bitmap depthBitmap, double blurAtInfinity,
43                       double focalDistance, double depthOfField,
44                       double focalPointX, double focalPointY,
45                       DepthTransform depthTransform) {
46         mFormat = format;
47         mFar = far;
48         mNear = near;
49         mDepthBitmap = depthBitmap;
50         mBlurAtInfinity = blurAtInfinity;
51         mFocalDistance = focalDistance;
52         mDepthOfFiled = depthOfField;
53         mFocalPointX = focalPointX;
54         mFocalPointY = focalPointY;
55         mDepthTransform = depthTransform;
56     }
57 
createFromXMPMetadata(Context context, Uri image)58     public static DepthImage createFromXMPMetadata(Context context, Uri image)
59             throws IOException {
60         InputStream input = context.getContentResolver().openInputStream(image);
61         XmpDepthDecode decode = new XmpDepthDecode(input);
62         return new DepthImage(decode.getFormat(), decode.getFar(),
63                               decode.getNear(), decode.getDepthBitmap(),
64                               decode.getBlurAtInfinity(),
65                               decode.getFocalDistance(),
66                               decode.getDepthOfField(),
67                               decode.getFocalPointX(),
68                               decode.getFocalPointY(),
69                               decode.getDepthTransform());
70     }
71 
createFromDepthmap(Context context, Uri uriDepthmap)72     public static DepthImage createFromDepthmap(Context context, Uri uriDepthmap)
73             throws IOException {
74         Bitmap bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uriDepthmap));
75         if (bitmap == null) {
76             throw new FileNotFoundException(uriDepthmap.toString());
77         }
78 
79         double near = 12.0;
80         double far = 120.0;
81         DepthTransform transform = new RangeInverseDepthTransform((float)near, (float)far);
82         return new DepthImage(RangeInverseDepthTransform.FORMAT,
83                               far,
84                               near,
85                               bitmap, // depthmap
86                               5.0,    // blur at ininity
87                               15.0,   // focal distance
88                               0.1,    // depth of field
89                               0.5,    // x of focal point
90                               0.5,    // y of focla point
91                               transform);
92     }
93 
getDepthBitmap()94     public Bitmap getDepthBitmap() {
95         return mDepthBitmap;
96     }
97 
getDepthTransform()98     public DepthTransform getDepthTransform() { return mDepthTransform; }
99 
getFormat()100     public String getFormat() {
101         return mFormat;
102     }
103 
getFar()104     public double getFar() {
105         return mFar;
106     }
107 
getNear()108     public double getNear() {
109         return mNear;
110     }
111 
getBlurAtInfinity()112     public double getBlurAtInfinity() {
113         return mBlurAtInfinity;
114     }
115 
getFocalDistance()116     public double getFocalDistance() {
117         return mFocalDistance;
118     }
119 
getDepthOfField()120     public double getDepthOfField() {return mDepthOfFiled; }
121 
getFocalPointX()122     public double getFocalPointX() {
123         return mFocalPointX;
124     }
125 
getFocalPointY()126     public double getFocalPointY() {
127         return mFocalPointY;
128     }
129 }
130 
131