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 com.example.android.rs.vr.loaders;
18 
19 import android.renderscript.RenderScript;
20 import android.util.Log;
21 
22 import com.example.android.rs.vr.engine.Volume;
23 
24 import java.io.File;
25 import java.io.FileReader;
26 import java.io.FilenameFilter;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.Properties;
30 
31 public class VolumeLoader {
32     private static final String LOGTAG = "VolumeLoader";
33     HashMap<String, Properties> map = new HashMap<String, Properties>();
34     File baseDir;
35     ProgressListener mListener;
36 
VolumeLoader(String dir)37     public VolumeLoader(String dir) {
38         map.put(Mandelbulb.NAME,null);
39         map.put(Droid.NAME,null);
40         baseDir = new File(dir);
41         if (!baseDir.exists()) {
42             Log.e(LOGTAG, "Directory: \""+dir+"\" does not exist ");
43             return;
44         }
45         Properties[] prop = getPropertyFiles(baseDir);
46         for (int i = 0; i < prop.length; i++) {
47             map.put(prop[i].getProperty("name"), prop[i]);
48         }
49 
50     }
51 
getNames()52     public String[] getNames() {
53        String [] ret = map.keySet().toArray(new String[map.size()]);
54         Arrays.sort(ret);
55         return ret;
56     }
57 
getVolume(RenderScript rs, String name)58     public Volume getVolume(RenderScript rs, String name) {
59         if (name.equals(Mandelbulb.NAME)) {
60             return  Mandelbulb.buildRSVolume(rs,mListener);
61         }
62         if (name.equals(Droid.NAME)) {
63             return  Droid.buildRSVolume(rs,mListener);
64         }
65         Properties p = map.get(name);
66         if (p == null) {
67             Log.v(LOGTAG,"Could not find "+name);
68             return null;
69         }
70         String dir = p.getProperty("dir");
71         Log.v(LOGTAG,"dir ="+dir);
72 
73         if ("dicom".equalsIgnoreCase(p.getProperty("format"))) {
74             Log.v(LOGTAG,"processing dicom");
75             Volume v = LoaderDicom.buildRSVolume(rs, new File(baseDir, dir), mListener);
76             String [] looks = p.getProperty("looks").split(",");
77             for (int j = 0; j < looks.length; j++) {
78                 String look_color = p.getProperty(looks[j]+".color");
79                 String look_opacity = p.getProperty(looks[j]+".opacity");
80                 v.addLook(looks[j],look_color,look_opacity);
81             }
82             return v;
83         } else if ("raw".equalsIgnoreCase(p.getProperty("format"))) {
84             Log.v(LOGTAG,"processing dicom");
85             Volume v = LoaderRaw.buildRSVolume(rs, new File(baseDir, dir), p, mListener);
86             String [] looks = p.getProperty("looks").split(",");
87             for (int j = 0; j < looks.length; j++) {
88                 String look_color = p.getProperty(looks[j]+".color");
89                 String look_opacity = p.getProperty(looks[j]+".opacity");
90                 v.addLook(looks[j],look_color,look_opacity);
91             }
92             return v;
93         }
94         Log.v(LOGTAG,"could recognize format");
95         return null;
96     }
97 
getPropertyFiles(File dir)98     static Properties[] getPropertyFiles(File dir) {
99 
100         File[] f = dir.listFiles(new FilenameFilter() {
101 
102             @Override
103             public boolean accept(File dir, String name) {
104                 Log.v(LOGTAG, name);
105                 return name.endsWith(".prop");
106             }
107         });
108         Properties[]ret = new Properties[f.length];
109         for (int i = 0; i < f.length; i++) {
110             Properties prop = new Properties();
111             ret[i] = prop;
112             try {
113                 prop.load(new FileReader(f[i]));
114 
115             } catch (Exception e) {
116                 e.printStackTrace();
117             }
118         }
119         return ret;
120     }
121 
setProgressListener(ProgressListener listener)122     public void setProgressListener(ProgressListener listener){
123         mListener = listener;
124     }
125 
126     public static interface ProgressListener {
progress(int n, int total)127         public void progress(int n, int total);
128     }
129 }
130