Changeset 1602 for branches/3.0-Beta2/components
- Timestamp:
- 06/07/07 18:20:04 (15 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
branches/3.0-Beta2/components/romio/src/ome/io/nio/DeltaVision.java
r1599 r1602 31 31 public class DeltaVision implements PixelBuffer { 32 32 33 private FileChannel channel; 34 35 private Integer rowSize; 36 37 private Integer planeSize; 38 39 private Integer stackSize; 40 41 private Integer timepointSize; 42 43 private Integer totalSize; 44 45 protected MappedByteBuffer buf; 46 47 protected int initExtHdrOffset = 1024; 48 49 protected final String currentOrder = "XYCZT"; 50 51 private OriginalFile originalFile; 52 53 public static final int NULL_PLANE_SIZE = 64; 54 55 public boolean little = false; 56 57 public DeltaVisionHeader header; 58 59 public static final byte[] nullPlane = new byte[] { -128, 127, -128, 127, 60 -128, 127, -128, 127, -128, 127, // 10 61 -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, // 20 62 -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, // 30 63 -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, // 40 64 -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, // 50 65 -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, // 60 66 -128, 127, -128, 127 }; 67 68 69 /** 70 * Constructor requires OriginalFile object 71 * 72 * @param originalFile 73 */ 74 public DeltaVision(OriginalFile originalFile, boolean little) { 75 76 this.little = little; 77 78 try { 79 this.originalFile = originalFile; 80 initFile(); 81 } catch (IOException ioex) { 82 throw new RuntimeException( 83 "Original file not available, Critical Error!"); 84 } catch (RuntimeException rtex) { 85 rtex.printStackTrace(); 86 throw new RuntimeException( 87 "The binary file is not a valid DeltaVision file!"); 88 89 } 90 91 } 92 93 /* 94 * (non-Javadoc) 95 * 96 * @see ome.io.nio.PixelBuffer#calculateMessageDigest() 97 */ 98 public byte[] calculateMessageDigest() throws IOException { 99 100 MessageDigest md; 101 102 try { 103 md = MessageDigest.getInstance("SHA-1"); 104 } catch (NoSuchAlgorithmException e) { 105 throw new RuntimeException( 106 "Required SHA-1 message digest algorithm unavailable."); 107 } 108 109 for (int t = 0; t < getSizeT(); t++) { 110 try { 111 MappedByteBuffer buffer = getTimepoint(t); 112 md.update(buffer); 113 } catch (DimensionsOutOfBoundsException e) { 114 throw new RuntimeException(e); 115 } 116 } 117 118 return md.digest(); 119 } 120 121 /* 122 * (non-Javadoc) 123 * 124 * @see ome.io.nio.PixelBuffer#close() 125 */ 126 public void close() throws IOException { 127 if (channel != null) { 128 channel.close(); 129 channel = null; 130 } 131 } 132 133 /* 134 * (non-Javadoc) 135 * 136 * @see ome.io.nio.PixelBuffer#getPlane(java.lang.Integer, 137 * java.lang.Integer, java.lang.Integer) 138 */ 139 public MappedByteBuffer getPlane(Integer z, Integer c, Integer t) 140 throws IOException, DimensionsOutOfBoundsException { 141 142 Long offset = getPlaneOffset(z, c, t); 143 Integer size = getPlaneSize(); 144 145 MappedByteBuffer region = getRegion(size, offset); 146 147 byte[] myNullPlane = nullPlane; 148 149 for (int i = 0; i < NULL_PLANE_SIZE; i++) { 150 if (region.get(i) != myNullPlane[i]) { 151 return region; 152 } 153 } 154 155 return null; // All of the nullPlane bytes match, non-filled plane 156 157 } 158 159 /* 160 * (non-Javadoc) 161 * 162 * @see ome.io.nio.PixelBuffer#getPlaneOffset(java.lang.Integer, 163 * java.lang.Integer, java.lang.Integer) 164 */ 165 public Long getPlaneOffset(Integer z, Integer c, Integer t) 166 throws DimensionsOutOfBoundsException { 167 168 checkBounds(null, z, c, t); 169 int planeNo = getAbsolutePlaneOffset(z, c, t); 170 171 return (long) getPlaneSize() * planeNo; 172 173 } 174 175 /** 176 * Privately returns the absolute plane number index in the set of X-Y 177 * planes (planes start with 1) 178 * 179 * @param z 180 * @param c 181 * @param t 182 * @return 183 */ 184 private int getAbsolutePlaneOffset(Integer z, Integer c, Integer t) { 185 186 int result = 0; 187 188 switch (getHeader().getSequence()) { 189 190 case 0: // ZTC 191 result = ((c * getSizeT() * getSizeZ()) + (t * getSizeZ()) + z); 192 break; 193 194 case 1: // CZT 195 result = ((t * getSizeC() * getSizeZ()) + (z * getSizeC()) + c); 196 break; 197 198 case 2: // ZCT 199 result = ((t * getSizeC() * getSizeZ()) + (c * getSizeZ()) + z); 200 break; 201 202 default: 203 result = -1; 204 } 205 206 return result; 207 } 208 209 /* 210 * (non-Javadoc) 211 * 212 * @see ome.io.nio.PixelBuffer#getPlaneSize() 213 */ 214 public Integer getPlaneSize() { 215 216 if (planeSize == null) { 217 planeSize = getSizeX() * getSizeY() * getByteWidth(); 218 } 219 return planeSize; 220 221 } 222 223 /* 224 * (non-Javadoc) 225 * 226 * @see ome.io.nio.PixelBuffer#getRegion(java.lang.Integer, java.lang.Long) 227 */ 228 public MappedByteBuffer getRegion(Integer size, Long offset) 229 throws IOException { 230 FileChannel fileChannel = getFileChannel(); 231 MappedByteBuffer buf = fileChannel.map(MapMode.READ_ONLY, offset, size); 232 buf.order(ByteOrder.BIG_ENDIAN); 233 return buf; 234 235 } 236 237 /* 238 * (non-Javadoc) 239 * 240 * @see ome.io.nio.PixelBuffer#getRow(java.lang.Integer, java.lang.Integer, 241 * java.lang.Integer, java.lang.Integer) 242 */ 243 public MappedByteBuffer getRow(Integer y, Integer z, Integer c, Integer t) 244 throws IOException, DimensionsOutOfBoundsException { 245 246 Long offset = getRowOffset(y, z, c, t); 247 Integer size = getRowSize(); 248 249 return getRegion(size, offset); 250 } 251 252 /** 253 * Privately returns the row number (specific). Starts with 1 254 * 255 * @param y 256 * @param z 257 * @param c 258 * @param t 259 * @return 260 */ 261 private int getAbsoluteRowOffset(Integer y, Integer z, Integer c, Integer t) { 262 263 int result = 0; 264 265 switch (getHeader().getSequence()) { 266 267 case 0: // (Y)ZTC 268 result = ((y * getRowSize()) + (c * getSizeT() * getSizeZ()) + (t * getSizeZ()) + z); 269 break; 270 271 case 1: // (Y)CZT 272 result = ((y * getRowSize()) + (t * getSizeC() * getSizeZ()) + (z * getSizeC()) + c); 273 break; 274 275 case 2: // (Y)ZCT 276 result = ((y * getRowSize()) + (t * getSizeC() * getSizeZ()) + (c * getSizeZ()) + z); 277 break; 278 279 default: 280 result = -1; 281 } 282 283 return result; 284 } 285 286 /* 287 * (non-Javadoc) 288 * 289 * @see ome.io.nio.PixelBuffer#getRowOffset(java.lang.Integer, 290 * java.lang.Integer, java.lang.Integer, java.lang.Integer) 291 */ 292 public Long getRowOffset(Integer y, Integer z, Integer c, Integer t) 293 throws DimensionsOutOfBoundsException { 294 295 checkBounds(y, z, c, t); 296 int rowNumber = getAbsoluteRowOffset(y, z, c, t); 297 298 Integer rowSize = getRowSize(); 299 300 return (long) rowNumber * rowSize; 301 } 302 303 /* 304 * (non-Javadoc) 305 * 306 * @see ome.io.nio.PixelBuffer#getRowSize() 307 */ 308 public Integer getRowSize() { 309 // row is X-wide 310 if (rowSize == null) { 311 rowSize = getSizeX() * getByteWidth(); 312 } 313 314 return rowSize; 315 } 316 317 /* 318 * (non-Javadoc) 319 * 320 * @see ome.io.nio.PixelBuffer#getSizeC() 321 */ 322 public int getSizeC() { 323 return header.getSizeC(); 324 } 325 326 /* 327 * (non-Javadoc) 328 * 329 * @see ome.io.nio.PixelBuffer#getSizeT() 330 */ 331 public int getSizeT() { 332 return header.getSizeT(); 333 } 334 335 /* 336 * (non-Javadoc) 337 * 338 * @see ome.io.nio.PixelBuffer#getSizeX() 339 */ 340 public int getSizeX() { 341 return header.getSizeX(); 342 } 343 344 /* 345 * (non-Javadoc) 346 * 347 * @see ome.io.nio.PixelBuffer#getSizeY() 348 */ 349 public int getSizeY() { 350 return header.getSizeY(); 351 } 352 353 /* 354 * (non-Javadoc) 355 * 356 * @see ome.io.nio.PixelBuffer#getSizeZ() 357 */ 358 public int getSizeZ() { 359 return header.getSizeZ(); 360 } 361 362 /* 363 * (non-Javadoc) 364 * 365 * @see ome.io.nio.PixelBuffer#getStack(java.lang.Integer, 366 * java.lang.Integer) 367 */ 368 public MappedByteBuffer getStack(Integer c, Integer t) throws IOException, 369 DimensionsOutOfBoundsException { 370 371 Long offset = getStackOffset(c, t); 372 Integer size = getStackSize(); 373 374 return getRegion(size, offset); 375 } 376 377 /** 378 * Privately returns the absolute stack number. Starts with 1 379 * 380 * @param c 381 * @param t 382 * @return 383 */ 384 private int getAbsoluteStackOffset(Integer c, Integer t) { 385 int result = 0; 386 387 switch (getHeader().getSequence()) { 388 389 case 0: // ZTC 390 result = ((c * getSizeT()) + t); 391 break; 392 393 case 1: // CZT 394 // TODO - tricky one 395 break; 396 397 case 2: // ZCT 398 result = ((t * getSizeC()) + c); 399 break; 400 401 default: 402 result = -1; 403 } 404 405 return result; 406 } 407 408 /* 409 * (non-Javadoc) 410 * 411 * @see ome.io.nio.PixelBuffer#getStackOffset(java.lang.Integer, 412 * java.lang.Integer) 413 */ 414 public Long getStackOffset(Integer c, Integer t) 415 throws DimensionsOutOfBoundsException { 416 417 checkBounds(null, null, c, t); 418 419 int stackNo = getAbsoluteStackOffset(c, t); 420 return (long) stackNo * getStackSize(); 421 422 } 423 424 /* 425 * (non-Javadoc) 426 * 427 * @see ome.io.nio.PixelBuffer#getStackSize() 428 */ 429 public Integer getStackSize() { 430 431 // stack is X-Y (plane) by Z (focalpoints) ... Z first 432 if (getHeader().getSequence() == 0 || getHeader().getSequence() == 2) { 433 if (stackSize == null) { 434 stackSize = getPlaneSize() * getSizeZ(); 435 } 436 } else { 437 // TODO - we need some new code 438 } 439 440 return stackSize; 441 } 442 443 /* 444 * (non-Javadoc) 445 * 446 * @see ome.io.nio.PixelBuffer#getTimepoint(java.lang.Integer) 447 */ 448 public MappedByteBuffer getTimepoint(Integer t) throws IOException, 449 DimensionsOutOfBoundsException { 450 // TODO - affected by ZCT ordering 451 Long offset = getTimepointOffset(t); 452 Integer size = getTimepointSize(); 453 454 return getRegion(size, offset); 455 } 456 457 /* 458 * (non-Javadoc) 459 * 460 * @see ome.io.nio.PixelBuffer#getTimepointOffset(java.lang.Integer) 461 */ 462 public Long getTimepointOffset(Integer t) 463 throws DimensionsOutOfBoundsException { 464 // TODO - affected by ZCT ordering 465 checkBounds(null, null, null, t); 466 467 Integer timepointSize = getTimepointSize(); 468 469 return (long) timepointSize * t; 470 } 471 472 /* 473 * (non-Javadoc) 474 * 475 * @see ome.io.nio.PixelBuffer#getTimepointSize() 476 */ 477 public Integer getTimepointSize() { 478 if (timepointSize == null) { 479 timepointSize = getStackSize() * getSizeC(); 480 } 481 482 return timepointSize; 483 } 484 485 /* 486 * (non-Javadoc) 487 * 488 * @see ome.io.nio.PixelBuffer#getTotalSize() 489 */ 490 public Integer getTotalSize() { 491 if (totalSize == null) { 492 totalSize = getTimepointSize() * getSizeT(); 493 } 494 495 return totalSize; 496 } 497 498 /* 499 * (non-Javadoc) 500 * 501 * @see ome.io.nio.PixelBuffer#checkBounds(java.lang.Integer, 502 * java.lang.Integer, java.lang.Integer, java.lang.Integer) 503 */ 504 public void checkBounds(Integer y, Integer z, Integer c, Integer t) 505 throws DimensionsOutOfBoundsException { 506 if (y != null && (y > getSizeY() - 1 || y < 0)) { 507 throw new DimensionsOutOfBoundsException("Y '" + y 508 + "' greater than sizeY '" + getSizeY() + "'."); 509 } 510 511 if (z != null && (z > getSizeZ() - 1 || z < 0)) { 512 throw new DimensionsOutOfBoundsException("Z '" + z 513 + "' greater than sizeZ '" + getSizeZ() + "'."); 514 } 515 516 if (c != null && (c > getSizeC() - 1 || c < 0)) { 517 throw new DimensionsOutOfBoundsException("C '" + c 518 + "' greater than sizeC '" + getSizeC() + "'."); 519 } 520 521 if (t != null && (t > getSizeT() - 1 || t < 0)) { 522 throw new DimensionsOutOfBoundsException("T '" + t 523 + "' greater than sizeT '" + getSizeT() + "'."); 524 } 525 } 526 527 /* 528 * (non-Javadoc) 529 * 530 * @see ome.io.nio.PixelBuffer#getId() 531 */ 532 public long getId() { 533 return 0; 534 } 535 536 /** 537 * Getter for Header 538 * 539 * @return 540 */ 541 public DeltaVisionHeader getHeader() { 542 return header; 543 } 544 545 /* 546 * (non-Javadoc) 547 * 548 * @see ome.io.nio.PixelBuffer#getPath() 549 */ 550 public String getPath() { 551 return originalFile.getPath(); 552 } 553 554 /** 555 * Setter for Header 556 * 557 * @param header 558 */ 559 public void setHeader(DeltaVisionHeader header) { 560 this.header = header; 561 } 562 563 // ---------- set or write methods ---------- 564 /** 565 * not implemented 566 */ 567 public void setRegion(Integer size, Long offset, byte[] buffer) 568 throws IOException, BufferOverflowException { 569 throw new UnsupportedOperationException("This method is not supported"); 570 } 571 572 /** 573 * not implemented 574 */ 575 public void setRegion(Integer size, Long offset, ByteBuffer buffer) 576 throws IOException, BufferOverflowException { 577 throw new UnsupportedOperationException("This method is not supported"); 578 } 579 580 /** 581 * not implemented 582 */ 583 public void setPlane(ByteBuffer buffer, Integer z, Integer c, Integer t) 584 throws IOException, DimensionsOutOfBoundsException, 585 BufferOverflowException { 586 throw new UnsupportedOperationException("This method is not supported"); 587 } 588 589 /** 590 * not implemented 591 */ 592 public void setPlane(byte[] buffer, Integer z, Integer c, Integer t) 593 throws IOException, DimensionsOutOfBoundsException, 594 BufferOverflowException { 595 throw new UnsupportedOperationException("This method is not supported"); 596 } 597 598 /** 599 * not implemented 600 */ 601 public void setRow(ByteBuffer buffer, Integer y, Integer z, Integer c, 602 Integer t) throws IOException, DimensionsOutOfBoundsException, 603 BufferOverflowException { 604 throw new UnsupportedOperationException("This method is not supported"); 605 } 606 607 /** 608 * not implemented 609 */ 610 public void setStack(ByteBuffer buffer, Integer z, Integer c, Integer t) 611 throws IOException, DimensionsOutOfBoundsException, 612 BufferOverflowException { 613 throw new UnsupportedOperationException("This method is not supported"); 614 } 615 616 /** 617 * not implemented 618 */ 619 public void setStack(byte[] buffer, Integer z, Integer c, Integer t) 620 throws IOException, DimensionsOutOfBoundsException, 621 BufferOverflowException { 622 throw new UnsupportedOperationException("This method is not supported"); 623 } 624 625 /** 626 * not implemented 627 */ 628 public void setTimepoint(ByteBuffer buffer, Integer t) throws IOException, 629 DimensionsOutOfBoundsException, BufferOverflowException { 630 throw new UnsupportedOperationException("This method is not supported"); 631 } 632 633 /** 634 * not implemented 635 */ 636 public void setTimepoint(byte[] buffer, Integer t) throws IOException, 637 DimensionsOutOfBoundsException, BufferOverflowException { 638 throw new UnsupportedOperationException("This method is not supported"); 639 } 640 641 // ---------- private methods ---------------------- 642 643 /** 644 * utility method returns byte width, 1, 2, 4, etc 645 */ 646 private int getByteWidth() { 647 return header.getBytesPerPixel(); 648 } 649 650 /** 651 * This method is used to establish most of the DeltaVision data that the 652 * Pixels object once held. Key data is obtained from a random access data 653 * structure after the DeltaVision file header is read into memory. 654 * 655 * @throws IOException 656 */ 657 private void initFile() throws IOException, RuntimeException { 658 659 File file = new File(originalFile.getPath()); 660 RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); 661 662 channel = randomAccessFile.getChannel(); 663 664 buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, 1024); 665 666 if (header == null) { 667 header = new DeltaVisionHeader(buf, true); 668 } 669 } 670 671 /** 672 * Private access only to read-write file 673 * 674 * @return 675 */ 676 private FileChannel getFileChannel() { 677 return channel; 678 } 33 private FileChannel channel; 34 35 private Integer rowSize; 36 37 private Integer planeSize; 38 39 private Integer stackSize; 40 41 private Integer timepointSize; 42 43 private Integer totalSize; 44 45 protected MappedByteBuffer buf; 46 47 protected int initExtHdrOffset = 1024; 48 49 protected final String currentOrder = "XYCZT"; 50 51 private OriginalFile originalFile; 52 53 public static final int NULL_PLANE_SIZE = 64; 54 55 public boolean little = false; 56 57 public DeltaVisionHeader header; 58 59 public static final byte[] nullPlane = new byte[] { -128, 127, -128, 127, 60 -128, 127, -128, 127, -128, 127, // 10 61 -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, // 20 62 -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, // 30 63 -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, // 40 64 -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, // 50 65 -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, // 60 66 -128, 127, -128, 127 }; 67 68 /** 69 * Constructor requires OriginalFile object 70 * 71 * @param originalFile 72 */ 73 public DeltaVision(OriginalFile originalFile, boolean little) { 74 75 this.little = little; 76 77 try { 78 this.originalFile = originalFile; 79 initFile(); 80 } catch (IOException ioex) { 81 throw new RuntimeException( 82 "Original file not available, Critical Error!"); 83 } catch (RuntimeException rtex) { 84 rtex.printStackTrace(); 85 throw new RuntimeException( 86 "The binary file is not a valid DeltaVision file!"); 87 88 } 89 90 } 91 92 &n
