Binary3DImage.java

Go to the documentation of this file.
00001 package theba.core.gui;
00002 
00003 import java.io.FileInputStream;
00004 import java.io.RandomAccessFile;
00005 import java.nio.ByteBuffer;
00006 import java.nio.channels.FileChannel;
00007 import java.util.StringTokenizer;
00008 
00009 import theba.core.RegionMask;
00010 
00011 
00020 public class Binary3DImage {
00021         private int x;
00022 
00023         private int y;
00024 
00025         private int z;
00026 
00027         public byte[] image;
00028 
00029         public int[] image2;
00030 
00031         private String filepath;
00032 
00033         private String filepathRLC;
00034 
00035         private RegionMask regionMask;
00036 
00049         public Binary3DImage(String pathname, int x, int y, int z) {
00050                 this.x = x;
00051                 this.y = y;
00052                 this.z = z;
00053                 regionMask = null;
00054                 this.image = new byte[x * y * z / Byte.SIZE];
00055                 this.image2 = new int[x * y * z / Integer.SIZE];
00056                 this.filepath = pathname;
00057                 StringTokenizer stringTokenizer = new StringTokenizer(pathname, ".");
00058                 filepathRLC = new String(stringTokenizer.nextToken() + "_RLC."
00059                                 + stringTokenizer.nextToken());
00060                 System.out.println("image.length = " + image.length);
00061         }
00062 
00073         public Binary3DImage(RegionMask regionMask, int x, int y, int z) {
00074                 this.x = x;
00075                 this.y = y;
00076                 this.z = z;
00077                 this.regionMask = regionMask;
00078                 this.image = new byte[x * y * z / Byte.SIZE];
00079                 this.image2 = new int[x * y * z / Integer.SIZE];
00080                 // this.filepath = pathname;
00081                 // StringTokenizer stringTokenizer = new StringTokenizer(pathname, ".");
00082                 // filepathRLC = new String(stringTokenizer.nextToken() + "_RLC." +
00083                 // stringTokenizer.nextToken());
00084                 System.out.println("image.length = " + image.length);
00085         }
00086 
00093         public boolean readImageFile() {
00094                 ByteBuffer byteBuffer;
00095                 FileChannel fileChannel;
00096                 FileInputStream fileInputStream;
00097                 byte[] bufData;
00098 
00099                 try {
00100                         bufData = new byte[this.x * this.y / Byte.SIZE];
00101                         byteBuffer = ByteBuffer.wrap(bufData);
00102                         fileInputStream = new FileInputStream(filepath);
00103                         fileChannel = fileInputStream.getChannel();
00104                         int counter = 0;
00105                         for (int i = 0; i < this.z; i++) {
00106                                 fileChannel.position(i * this.x * this.y / Byte.SIZE);
00107                                 byteBuffer.position(0);
00108                                 // Save position
00109                                 int pos = byteBuffer.position();
00110                                 // Set position to zero
00111                                 byteBuffer.position(0);
00112                                 while (byteBuffer.hasRemaining()) {
00113                                         byte b = byteBuffer.get();
00114                                         image[counter++] = b;
00115                                         // System.out.println(b & 0xFF);
00116                                 }
00117                                 byteBuffer.position(pos);
00118                         }
00119                         fileChannel.close();
00120                         fileInputStream.close();
00121                         System.out.println("Voxels read = " + counter);
00122                 } catch (Exception e) {
00123                         e.printStackTrace();
00124                         return false;
00125                 }
00126                 return true;
00127         }
00128 
00134         public boolean readRLCCompressedImageFile() {
00135                 RandomAccessFile raf;
00136                 try {
00137                         raf = new RandomAccessFile(filepathRLC, "r");
00138                         int current = 0;
00139                         long size = raf.length() / 2;
00140                         int counter = 0;
00141                         int bitpos = 7;
00142                         byte b = 0;
00143                         for (int i = 0; i < size; i++) {
00144                                 int a = raf.readUnsignedShort();
00145                                 // System.out.println(a);
00146                                 for (int j = 0; j < a; j++) {
00147                                         b |= (current << bitpos--);
00148                                         if (bitpos == -1) {
00149                                                 image[counter++] = b;
00150                                                 // System.out.println(b & 0xFF);
00151                                                 // if (counter < 5230 && counter > 5220)
00152                                                 // System.out.println(b & 0xFF);
00153                                                 b = 0;
00154                                                 bitpos = 7;
00155                                         }
00156                                 }
00157                                 current = (current == 0) ? 1 : 0;
00158                         }
00159                         raf.close();
00160                         System.out.println("Voxels read (RLC) = " + counter);
00161                 } catch (Exception e) {
00162                         e.printStackTrace();
00163                         return false;
00164                 }
00165                 return true;
00166         }
00167 
00171         public void printImageArray() {
00172                 for (int i = 0; i < image.length; i++) {
00173                         printByteContent(image[i]);
00174                 }
00175         }
00176 
00183         public int compareByteArrays() {
00184                 int a = 0;
00185                 for (int i = 0; i < image.length; i++) {
00186                         if (image[i] != image2[i]) {
00187                                 a++;
00188                                 System.out.println(i + " " + image[i] + " " + image2[i]);
00189                         }
00190                 }
00191                 return a;
00192         }
00193 
00205         public byte getVoxelValue(int x, int y, int z) {
00206                 int a = (x + this.x * y + this.x * this.y * z);
00207                 // System.out.print( " " + a + " " );
00208                 byte b = image[a / Byte.SIZE];
00209                 // if (b > 0) System.out.println(b);
00210                 int i = a % Byte.SIZE;
00211                 byte c = (byte) (b >> (Byte.SIZE - 1 - i) & 0x01);
00212                 // if (c == en) System.out.println("yeah");
00213                 return c;
00214         }
00215 
00228         public int getVoxelValueInt(int x, int y, int z) {
00229                 int a = (x + this.x * y + this.x * this.y * z);
00230                 int b = image2[a / Integer.SIZE];
00231                 int i = a % Integer.SIZE;
00232                 int c = (b >> (Integer.SIZE - 1 - i) & 0x0001);
00233                 return c;
00234         }
00235 
00249         public int[] getCell(int x, int y, int z) {
00250                 int[] b = new int[8];
00251                 if (regionMask == null) {
00252                         b[0] = getVoxelValue(x, y, z);
00253                         b[1] = getVoxelValue(x + 1, y, z);
00254                         b[2] = getVoxelValue(x, y + 1, z);
00255                         b[3] = getVoxelValue(x + 1, y + 1, z);
00256                         b[4] = getVoxelValue(x, y, z + 1);
00257                         b[5] = getVoxelValue(x + 1, y, z + 1);
00258                         b[6] = getVoxelValue(x, y + 1, z + 1);
00259                         b[7] = getVoxelValue(x + 1, y + 1, z + 1);
00260                 } else {
00261                         b[0] = regionMask.isSet(x, y, z) ? 1 : 0;
00262                         b[1] = regionMask.isSet(x + 1, y, z) ? 1 : 0;
00263                         b[2] = regionMask.isSet(x, y + 1, z) ? 1 : 0;
00264                         b[3] = regionMask.isSet(x + 1, y + 1, z) ? 1 : 0;
00265                         b[4] = regionMask.isSet(x, y, z + 1) ? 1 : 0;
00266                         b[5] = regionMask.isSet(x + 1, y, z + 1) ? 1 : 0;
00267                         b[6] = regionMask.isSet(x, y + 1, z + 1) ? 1 : 0;
00268                         b[7] = regionMask.isSet(x + 1, y + 1, z + 1) ? 1 : 0;
00269                 }
00270                 return b;
00271         }
00272 
00281         public byte getByte(int position) {
00282                 if (position >= x * y * z / Byte.SIZE)
00283                         throw new ArrayIndexOutOfBoundsException(
00284                                         "Position greater than size of image: " + position);
00285                 // throw new ArrayIndexOutOfBoundsException();
00286                 return image[position];
00287         }
00288 
00296         public static byte[] getByteList(byte bits) {
00297                 byte[] b = new byte[Byte.SIZE];
00298                 for (int i = 0; i < Byte.SIZE; i++) {
00299                         b[i] = (byte) (bits >> (Byte.SIZE - 1 - i) & 0x01);
00300                 }
00301                 return b;
00302         }
00303 
00311         public static void printByteContent(byte b) {
00312                 for (int i = 0; i < Byte.SIZE; i++) {
00313                         System.out.print((b >> (Byte.SIZE - 1 - i) & 0x01) + " ");
00314                 }
00315                 System.out.println(" = " + (b & 0xFF));
00316         }
00317 
00323         public int getX() {
00324                 return x;
00325         }
00326 
00332         public int getY() {
00333                 return y;
00334         }
00335 
00341         public int getZ() {
00342                 return z;
00343         }
00344 }

Generated on Fri Nov 13 08:57:07 2009 for Theba by  doxygen 1.6.1