SliceReader.java
Go to the documentation of this file.00001 package theba.core.io;
00002
00003 import java.io.FileNotFoundException;
00004 import java.io.IOException;
00005 import java.io.RandomAccessFile;
00006 import java.util.ArrayList;
00007 import java.util.HashMap;
00008 import java.util.LinkedList;
00009
00017 public class SliceReader implements VolumeReader {
00018 public int width, height, depth;
00019
00020 protected HashMap<Integer, short[]> cache = new HashMap<Integer, short[]>();
00021
00022 protected int cacheSize = 1;
00023
00024 protected RandomAccessFile file;
00025
00026 protected String filename;
00027
00028 protected ArrayList<short[]> preallocatedDataArrays = new ArrayList<short[]> ();
00029
00030 protected LinkedList<Integer> queue = new LinkedList<Integer>();
00031
00032 byte[] readAhead;
00033
00040 byte[] tmp = null;;
00041
00042 public SliceReader(int width, int height, int depth, int cacheSIze) {
00043 this.width = width;
00044 this.height = height;
00045 this.depth = depth;
00046 readAhead = new byte[width * height * 2 * 5];
00047 this.cacheSize = cacheSIze;
00048 }
00049
00050 public SliceReader(final String filename, int width, int height, int depth,
00051 int cacheSize) {
00052 this.width = width;
00053 this.height = height;
00054 this.depth = depth;
00055 this.filename = filename;
00056 readAhead = new byte[width * height * 2 * 5];
00057
00058 try {
00059 file = new RandomAccessFile(filename, "r");
00060 } catch (FileNotFoundException e) {
00061 e.printStackTrace();
00062 }
00063 this.cacheSize = cacheSize;
00064 }
00065
00066 public synchronized void close() {
00067 try {
00068 file.close();
00069 } catch (IOException e) {
00070 e.printStackTrace();
00071 }
00072 }
00073
00074 public void flush() {
00075
00076 }
00077
00078 public RandomAccessFile getFile() {
00079 return file;
00080 }
00081
00082 public String getFileName() {
00083 return filename;
00084 }
00085
00086 public synchronized short[] getSlice(int sliceNo) {
00087 short[] sliceCache = cache.get(sliceNo);
00088 if (sliceCache != null) {
00089 return sliceCache;
00090 }
00091
00092 final short[] output;
00093 if (preallocatedDataArrays.size() > 0)
00094 output = preallocatedDataArrays.remove(0);
00095 else
00096 output = new short[width * height];
00097 getSlice(sliceNo, output);
00098 queue.add(sliceNo);
00099 cache.put(sliceNo, output);
00100 updateCache();
00101 return output;
00102 }
00103
00104 public synchronized void putSlice(short[] data, int pos) {
00105 System.err.print("Warning, attempting to write to a sliceReader");
00106 }
00107
00108 public synchronized void setFile(RandomAccessFile file) {
00109 this.file = file;
00110 }
00111
00112 public synchronized void updateCache() {
00113 while (queue.size() > cacheSize) {
00114 int deletedSlice = queue.removeFirst();
00115 short[] sliceCache = cache.remove(deletedSlice);
00116 preallocatedDataArrays.add(sliceCache);
00117 }
00118 }
00119
00120 public synchronized void getSlice(int sliceNo, short[] output) {
00121 if (tmp == null)
00122 tmp = new byte[output.length * 2];
00123 try {
00124 file.seek(output.length * 2 * sliceNo);
00125 file.readFully(tmp);
00126 for (int i = 0; i < width * height; i++) {
00127 output[i] = (short) ((tmp[i * 2] & 0xff) | ((tmp[i * 2 + 1] & 0xff) << 8));
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 } catch (Exception e) {
00143
00144
00145 }
00146 }
00147
00148 public int getWidth() {
00149 return width;
00150 }
00151
00152 public int getHeight() {
00153 return height;
00154 }
00155
00156 public int getDepth() {
00157 return depth;
00158 }
00159
00160 public void destroy() {
00161 flush();
00162 try {
00163 file.close();
00164 } catch (IOException e) {
00165 e.printStackTrace();
00166 }
00167
00168 }
00169 }