1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
|
package ij.io;
import ij.*;
import ij.process.*;
import java.io.*;
import java.net.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.zip.Inflater;
import java.util.zip.DataFormatException;
/** Reads raw 8-bit, 16-bit or 32-bit (float or RGB)
images from a stream or URL. */
public class ImageReader {
private static final int CLEAR_CODE = 256;
private static final int EOI_CODE = 257;
private FileInfo fi;
private int width, height;
private long skipCount;
private int bytesPerPixel, bufferSize, nPixels;
private long byteCount;
private boolean showProgressBar=true;
private int eofErrorCount;
private long startTime;
public double min, max; // readRGB48() calculates min/max pixel values
/**
Constructs a new ImageReader using a FileInfo object to describe the file to be read.
@see ij.io.FileInfo
*/
public ImageReader (FileInfo fi) {
this.fi = fi;
width = fi.width;
height = fi.height;
skipCount = fi.getOffset();
}
void eofError() {
eofErrorCount++;
}
byte[] read8bitImage(InputStream in) throws IOException {
if (fi.compression>FileInfo.COMPRESSION_NONE)
return readCompressed8bitImage(in);
byte[] pixels = new byte[nPixels];
// assume contiguous strips
int count, actuallyRead;
int totalRead = 0;
while (totalRead<byteCount) {
if (totalRead+bufferSize>byteCount)
count = (int)(byteCount-totalRead);
else
count = bufferSize;
actuallyRead = in.read(pixels, totalRead, count);
if (actuallyRead==-1) {eofError(); break;}
totalRead += actuallyRead;
showProgress(totalRead, byteCount);
}
return pixels;
}
byte[] readCompressed8bitImage(InputStream in) throws IOException {
byte[] pixels = new byte[nPixels];
int current = 0;
byte last = 0;
for (int i=0; i<fi.stripOffsets.length; i++) {
if (in instanceof RandomAccessStream)
((RandomAccessStream)in).seek(fi.stripOffsets[i]);
else if (i > 0) {
long skip = (fi.stripOffsets[i]&0xffffffffL) - (fi.stripOffsets[i-1]&0xffffffffL) - fi.stripLengths[i-1];
if (skip > 0L) in.skip(skip);
}
byte[] byteArray = new byte[fi.stripLengths[i]];
int read = 0, left = byteArray.length;
while (left > 0) {
int r = in.read(byteArray, read, left);
if (r == -1) {eofError(); break;}
read += r;
left -= r;
}
byteArray = uncompress(byteArray);
int length = byteArray.length;
length = length - (length%fi.width);
if (fi.compression==FileInfo.LZW_WITH_DIFFERENCING) {
for (int b=0; b<length; b++) {
byteArray[b] += last;
last = b % fi.width == fi.width - 1 ? 0 : byteArray[b];
}
}
if (current+length>pixels.length) length = pixels.length-current;
System.arraycopy(byteArray, 0, pixels, current, length);
current += length;
showProgress(i+1, fi.stripOffsets.length);
}
return pixels;
}
/** Reads a 16-bit image. Signed pixels are converted to unsigned by adding 32768. */
short[] read16bitImage(InputStream in) throws IOException {
if (fi.compression>FileInfo.COMPRESSION_NONE || (fi.stripOffsets!=null&&fi.stripOffsets.length>1))
return readCompressed16bitImage(in);
int pixelsRead;
byte[] buffer = new byte[bufferSize];
short[] pixels = new short[nPixels];
long totalRead = 0L;
int base = 0;
int count, value;
int bufferCount;
while (totalRead<byteCount) {
if ((totalRead+bufferSize)>byteCount)
bufferSize = (int)(byteCount-totalRead);
bufferCount = 0;
while (bufferCount<bufferSize) { // fill the buffer
count = in.read(buffer, bufferCount, bufferSize-bufferCount);
if (count==-1) {
if (bufferCount>0)
for (int i=bufferCount; i<bufferSize; i++) buffer[i] = 0;
totalRead = byteCount;
eofError();
break;
}
bufferCount += count;
}
totalRead += bufferSize;
showProgress(totalRead, byteCount);
pixelsRead = bufferSize/bytesPerPixel;
if (fi.intelByteOrder) {
if (fi.fileType==FileInfo.GRAY16_SIGNED)
for (int i=base,j=0; i<(base+pixelsRead); i++,j+=2)
pixels[i] = (short)((((buffer[j+1]&0xff)<<8) | (buffer[j]&0xff))+32768);
else
for (int i=base,j=0; i<(base+pixelsRead); i++,j+=2)
pixels[i] = (short)(((buffer[j+1]&0xff)<<8) | (buffer[j]&0xff));
} else {
if (fi.fileType==FileInfo.GRAY16_SIGNED)
for (int i=base,j=0; i<(base+pixelsRead); i++,j+=2)
pixels[i] = (short)((((buffer[j]&0xff)<<8) | (buffer[j+1]&0xff))+32768);
else
for (int i=base,j=0; i<(base+pixelsRead); i++,j+=2)
pixels[i] = (short)(((buffer[j]&0xff)<<8) | (buffer[j+1]&0xff));
}
base += pixelsRead;
}
return pixels;
}
short[] readCompressed16bitImage(InputStream in) throws IOException {
short[] pixels = new short[nPixels];
int base = 0;
short last = 0;
for (int k=0; k<fi.stripOffsets.length; k++) {
//IJ.log("seek: "+fi.stripOffsets[k]+" "+fi.stripLengths[k]+" "+(in instanceof RandomAccessStream));
if (in instanceof RandomAccessStream)
((RandomAccessStream)in).seek(fi.stripOffsets[k]);
else if (k > 0) {
long skip = (fi.stripOffsets[k]&0xffffffffL) - (fi.stripOffsets[k-1]&0xffffffffL) - fi.stripLengths[k-1];
if (skip > 0L) in.skip(skip);
}
byte[] byteArray = new byte[fi.stripLengths[k]];
int read = 0, left = byteArray.length;
while (left > 0) {
int r = in.read(byteArray, read, left);
if (r == -1) {eofError(); break;}
read += r;
left -= r;
}
byteArray = uncompress(byteArray);
int pixelsRead = byteArray.length/bytesPerPixel;
pixelsRead = pixelsRead - (pixelsRead%fi.width);
int pmax = base+pixelsRead;
if (pmax > nPixels) pmax = nPixels;
if (fi.intelByteOrder) {
for (int i=base,j=0; i<pmax; i++,j+=2)
pixels[i] = (short)(((byteArray[j+1]&0xff)<<8) | (byteArray[j]&0xff));
} else {
for (int i=base,j=0; i<pmax; i++,j+=2)
pixels[i] = (short)(((byteArray[j]&0xff)<<8) | (byteArray[j+1]&0xff));
}
if (fi.compression==FileInfo.LZW_WITH_DIFFERENCING) {
for (int b=base; b<pmax; b++) {
pixels[b] += last;
last = b % fi.width == fi.width - 1 ? 0 : pixels[b];
}
}
base += pixelsRead;
showProgress(k+1, fi.stripOffsets.length);
}
if (fi.fileType==FileInfo.GRAY16_SIGNED) {
// convert to unsigned
for (int i=0; i<nPixels; i++)
pixels[i] = (short)(pixels[i]+32768);
}
return pixels;
}
float[] read32bitImage(InputStream in) throws IOException {
if (fi.compression>FileInfo.COMPRESSION_NONE)
return readCompressed32bitImage(in);
int pixelsRead;
byte[] buffer = new byte[bufferSize];
float[] pixels = new float[nPixels];
long totalRead = 0L;
int base = 0;
int count, value;
int bufferCount;
int tmp;
while (totalRead<byteCount) {
if ((totalRead+bufferSize)>byteCount)
bufferSize = (int)(byteCount-totalRead);
bufferCount = 0;
while (bufferCount<bufferSize) { // fill the buffer
count = in.read(buffer, bufferCount, bufferSize-bufferCount);
if (count==-1) {
if (bufferCount>0)
for (int i=bufferCount; i<bufferSize; i++) buffer[i] = 0;
totalRead = byteCount;
eofError();
break;
}
bufferCount += count;
}
totalRead += bufferSize;
showProgress(totalRead, byteCount);
pixelsRead = bufferSize/bytesPerPixel;
int pmax = base+pixelsRead;
if (pmax>nPixels) pmax = nPixels;
int j = 0;
if (fi.intelByteOrder)
for (int i=base; i<pmax; i++) {
tmp = (int)(((buffer[j+3]&0xff)<<24) | ((buffer[j+2]&0xff)<<16) | ((buffer[j+1]&0xff)<<8) | (buffer[j]&0xff));
if (fi.fileType==FileInfo.GRAY32_FLOAT)
pixels[i] = Float.intBitsToFloat(tmp);
else if (fi.fileType==FileInfo.GRAY32_UNSIGNED)
pixels[i] = (float)(tmp&0xffffffffL);
else
pixels[i] = tmp;
j += 4;
}
else
for (int i=base; i<pmax; i++) {
tmp = (int)(((buffer[j]&0xff)<<24) | ((buffer[j+1]&0xff)<<16) | ((buffer[j+2]&0xff)<<8) | (buffer[j+3]&0xff));
if (fi.fileType==FileInfo.GRAY32_FLOAT)
pixels[i] = Float.intBitsToFloat(tmp);
else if (fi.fileType==FileInfo.GRAY32_UNSIGNED)
pixels[i] = (float)(tmp&0xffffffffL);
else
pixels[i] = tmp;
j += 4;
}
base += pixelsRead;
}
return pixels;
}
float[] readCompressed32bitImage(InputStream in) throws IOException {
float[] pixels = new float[nPixels];
int base = 0;
float last = 0;
for (int k=0; k<fi.stripOffsets.length; k++) {
//IJ.log("seek: "+fi.stripOffsets[k]+" "+(in instanceof RandomAccessStream));
if (in instanceof RandomAccessStream)
((RandomAccessStream)in).seek(fi.stripOffsets[k]);
else if (k > 0) {
long skip = (fi.stripOffsets[k]&0xffffffffL) - (fi.stripOffsets[k-1]&0xffffffffL) - fi.stripLengths[k-1];
if (skip > 0L) in.skip(skip);
}
byte[] byteArray = new byte[fi.stripLengths[k]];
int read = 0, left = byteArray.length;
while (left > 0) {
int r = in.read(byteArray, read, left);
if (r == -1) {eofError(); break;}
read += r;
left -= r;
}
byteArray = uncompress(byteArray);
int pixelsRead = byteArray.length/bytesPerPixel;
pixelsRead = pixelsRead - (pixelsRead%fi.width);
int pmax = base+pixelsRead;
if (pmax > nPixels) pmax = nPixels;
int tmp;
if (fi.intelByteOrder) {
for (int i=base,j=0; i<pmax; i++,j+=4) {
tmp = (int)(((byteArray[j+3]&0xff)<<24) | ((byteArray[j+2]&0xff)<<16) | ((byteArray[j+1]&0xff)<<8) | (byteArray[j]&0xff));
pixels[i] = Float.intBitsToFloat(tmp);
}
} else {
for (int i=base,j=0; i<pmax; i++,j+=4) {
tmp = (int)(((byteArray[j]&0xff)<<24) | ((byteArray[j+1]&0xff)<<16) | ((byteArray[j+2]&0xff)<<8) | (byteArray[j+3]&0xff));
pixels[i] = Float.intBitsToFloat(tmp);
}
}
if (fi.compression==FileInfo.LZW_WITH_DIFFERENCING) {
for (int b=base; b<pmax; b++) {
pixels[b] += last;
last = b % fi.width == fi.width - 1 ? 0 : pixels[b];
}
}
base += pixelsRead;
showProgress(k+1, fi.stripOffsets.length);
}
return pixels;
}
float[] read64bitImage(InputStream in) throws IOException {
int pixelsRead;
byte[] buffer = new byte[bufferSize];
float[] pixels = new float[nPixels];
long totalRead = 0L;
int base = 0;
int count, value;
int bufferCount;
long tmp;
long b1, b2, b3, b4, b5, b6, b7, b8;
while (totalRead<byteCount) {
if ((totalRead+bufferSize)>byteCount)
bufferSize = (int)(byteCount-totalRead);
bufferCount = 0;
while (bufferCount<bufferSize) { // fill the buffer
count = in.read(buffer, bufferCount, bufferSize-bufferCount);
if (count==-1) {
if (bufferCount>0)
for (int i=bufferCount; i<bufferSize; i++) buffer[i] = 0;
totalRead = byteCount;
eofError();
break;
}
bufferCount += count;
}
totalRead += bufferSize;
showProgress(totalRead, byteCount);
pixelsRead = bufferSize/bytesPerPixel;
int j = 0;
for (int i=base; i < (base+pixelsRead); i++) {
b1 = buffer[j+7]&0xff; b2 = buffer[j+6]&0xff; b3 = buffer[j+5]&0xff; b4 = buffer[j+4]&0xff;
b5 = buffer[j+3]&0xff; b6 = buffer[j+2]&0xff; b7 = buffer[j+1]&0xff; b8 = buffer[j]&0xff;
if (fi.intelByteOrder)
tmp = (long)((b1<<56)|(b2<<48)|(b3<<40)|(b4<<32)|(b5<<24)|(b6<<16)|(b7<<8)|b8);
else
tmp = (long)((b8<<56)|(b7<<48)|(b6<<40)|(b5<<32)|(b4<<24)|(b3<<16)|(b2<<8)|b1);
pixels[i] = (float)Double.longBitsToDouble(tmp);
j += 8;
}
base += pixelsRead;
}
return pixels;
}
int[] readChunkyRGB(InputStream in) throws IOException {
if (fi.compression==FileInfo.JPEG)
return readJPEG(in);
else if (fi.compression>FileInfo.COMPRESSION_NONE)
return readCompressedChunkyRGB(in);
int pixelsRead;
bufferSize = 24*width;
byte[] buffer = new byte[bufferSize];
int[] pixels = new int[nPixels];
long totalRead = 0L;
int base = 0;
int count, value;
int bufferCount;
int r, g, b;
while (totalRead<byteCount) {
if ((totalRead+bufferSize)>byteCount)
bufferSize = (int)(byteCount-totalRead);
bufferCount = 0;
while (bufferCount<bufferSize) { // fill the buffer
count = in.read(buffer, bufferCount, bufferSize-bufferCount);
if (count==-1) {
if (bufferCount>0)
for (int i=bufferCount; i<bufferSize; i++) buffer[i] = 0;
totalRead = byteCount;
eofError();
break;
}
bufferCount += count;
}
totalRead += bufferSize;
showProgress(totalRead, byteCount);
pixelsRead = bufferSize/bytesPerPixel;
boolean bgr = fi.fileType==FileInfo.BGR;
int j = 0;
for (int i=base; i<(base+pixelsRead); i++) {
if (bytesPerPixel==4) {
if (fi.fileType==FileInfo.BARG) { // MCID
b = buffer[j++]&0xff;
j++; // ignore alfa byte
r = buffer[j++]&0xff;
g = buffer[j++]&0xff;
} else if (fi.fileType==FileInfo.ABGR) {
b = buffer[j++]&0xff;
g = buffer[j++]&0xff;
r = buffer[j++]&0xff;
j++; // ignore alfa byte
} else { // ARGB
r = buffer[j++]&0xff;
g = buffer[j++]&0xff;
b = buffer[j++]&0xff;
j++; // ignore alfa byte
}
} else {
r = buffer[j++]&0xff;
g = buffer[j++]&0xff;
b = buffer[j++]&0xff;
}
if (bgr)
pixels[i] = 0xff000000 | (b<<16) | (g<<8) | r;
else
pixels[i] = 0xff000000 | (r<<16) | (g<<8) | b;
}
base += pixelsRead;
}
return pixels;
}
int[] readCompressedChunkyRGB(InputStream in) throws IOException {
int[] pixels = new int[nPixels];
int base = 0;
int lastRed=0, lastGreen=0, lastBlue=0;
int nextByte;
int red=0, green=0, blue=0;
boolean bgr = fi.fileType==FileInfo.BGR;
boolean differencing = fi.compression == FileInfo.LZW_WITH_DIFFERENCING;
for (int i=0; i<fi.stripOffsets.length; i++) {
if (i > 0) {
long skip = (fi.stripOffsets[i]&0xffffffffL) - (fi.stripOffsets[i-1]&0xffffffffL) - fi.stripLengths[i-1];
if (skip > 0L) in.skip(skip);
}
byte[] byteArray = new byte[fi.stripLengths[i]];
int read = 0, left = byteArray.length;
while (left > 0) {
int r = in.read(byteArray, read, left);
if (r == -1) {eofError(); break;}
read += r;
left -= r;
}
byteArray = uncompress(byteArray);
if (differencing) {
for (int b=0; b<byteArray.length; b++) {
if (b / bytesPerPixel % fi.width == 0) continue;
byteArray[b] += byteArray[b - bytesPerPixel];
}
}
int k = 0;
int pixelsRead = byteArray.length/bytesPerPixel;
pixelsRead = pixelsRead - (pixelsRead%fi.width);
int pmax = base+pixelsRead;
if (pmax > nPixels) pmax = nPixels;
for (int j=base; j<pmax; j++) {
if (bytesPerPixel==4) {
red = byteArray[k++]&0xff;
green = byteArray[k++]&0xff;
blue = byteArray[k++]&0xff;
k++; // ignore alfa byte
} else {
red = byteArray[k++]&0xff;
green = byteArray[k++]&0xff;
blue = byteArray[k++]&0xff;
}
if (bgr)
pixels[j] = 0xff000000 | (blue<<16) | (green<<8) | red;
else
pixels[j] = 0xff000000 | (red<<16) | (green<<8) | blue;
}
base += pixelsRead;
showProgress(i+1, fi.stripOffsets.length);
}
return pixels;
}
int[] readJPEG(InputStream in) throws IOException {
BufferedImage bi = ImageIO.read(in);
ImageProcessor ip = new ColorProcessor(bi);
return (int[])ip.getPixels();
}
int[] readPlanarRGB(InputStream in) throws IOException {
if (fi.compression>FileInfo.COMPRESSION_NONE)
return readCompressedPlanarRGBImage(in);
DataInputStream dis = new DataInputStream(in);
int planeSize = nPixels; // 1/3 image size
byte[] buffer = new byte[planeSize];
int[] pixels = new int[nPixels];
int r, g, b;
startTime = 0L;
showProgress(10, 100);
dis.readFully(buffer);
for (int i=0; i < planeSize; i++) {
r = buffer[i]&0xff;
pixels[i] = 0xff000000 | (r<<16);
}
showProgress(40, 100);
dis.readFully(buffer);
for (int i=0; i < planeSize; i++) {
g = buffer[i]&0xff;
pixels[i] |= g<<8;
}
showProgress(70, 100);
dis.readFully(buffer);
for (int i=0; i < planeSize; i++) {
b = buffer[i]&0xff;
pixels[i] |= b;
}
showProgress(90, 100);
return pixels;
}
int[] readCompressedPlanarRGBImage(InputStream in) throws IOException {
int[] pixels = new int[nPixels];
int r, g, b;
nPixels *= 3; // read all 3 planes
byte[] buffer = readCompressed8bitImage(in);
nPixels /= 3;
for (int i=0; i<nPixels; i++) {
r = buffer[i]&0xff;
pixels[i] = 0xff000000 | (r<<16);
}
for (int i=0; i<nPixels; i++) {
g = buffer[nPixels+i]&0xff;
pixels[i] |= g<<8;
}
for (int i=0; i<nPixels; i++) {
b = buffer[nPixels*2+i]&0xff;
pixels[i] |= b;
}
return pixels;
}
private void showProgress(int current, int last) {
if (showProgressBar && (System.currentTimeMillis()-startTime)>500L)
IJ.showProgress(current, last);
}
private void showProgress(long current, long last) {
showProgress((int)(current/10L), (int)(last/10L));
}
Object readRGB48(InputStream in) throws IOException {
if (fi.compression>FileInfo.COMPRESSION_NONE)
return readCompressedRGB48(in);
int channels = 3;
short[][] stack = new short[channels][nPixels];
DataInputStream dis = new DataInputStream(in);
int pixel = 0;
int min=65535, max=0;
if (fi.stripLengths==null) {
fi.stripLengths = new int[fi.stripOffsets.length];
fi.stripLengths[0] = width*height*bytesPerPixel;
}
for (int i=0; i<fi.stripOffsets.length; i++) {
if (i>0) {
long skip = (fi.stripOffsets[i]&0xffffffffL) - (fi.stripOffsets[i-1]&0xffffffffL) - fi.stripLengths[i-1];
if (skip>0L) dis.skip(skip);
}
int len = fi.stripLengths[i];
int bytesToGo = (nPixels-pixel)*channels*2;
if (len>bytesToGo) len = bytesToGo;
byte[] buffer = new byte[len];
dis.readFully(buffer);
int value;
int channel=0;
boolean intel = fi.intelByteOrder;
for (int base=0; base<len; base+=2) {
if (intel)
value = ((buffer[base+1]&0xff)<<8) | (buffer[base]&0xff);
else
value = ((buffer[base]&0xff)<<8) | (buffer[base+1]&0xff);
if (value<min) min = value;
if (value>max) max = value;
stack[channel][pixel] = (short)(value);
channel++;
if (channel==channels) {
channel = 0;
pixel++;
}
}
showProgress(i+1, fi.stripOffsets.length);
}
this.min=min; this.max=max;
return stack;
}
Object readCompressedRGB48(InputStream in) throws IOException {
if (fi.compression==FileInfo.LZW_WITH_DIFFERENCING)
throw new IOException("ImageJ cannot open 48-bit LZW compressed TIFFs with predictor");
int channels = 3;
short[][] stack = new short[channels][nPixels];
DataInputStream dis = new DataInputStream(in);
int pixel = 0;
int min=65535, max=0;
for (int i=0; i<fi.stripOffsets.length; i++) {
if (i>0) {
long skip = (fi.stripOffsets[i]&0xffffffffL) - (fi.stripOffsets[i-1]&0xffffffffL) - fi.stripLengths[i-1];
if (skip>0L) dis.skip(skip);
}
int len = fi.stripLengths[i];
byte[] buffer = new byte[len];
dis.readFully(buffer);
buffer = uncompress(buffer);
len = buffer.length;
if (len % 2 != 0) len--;
int value;
int channel=0;
boolean intel = fi.intelByteOrder;
for (int base=0; base<len && pixel<nPixels; base+=2) {
if (intel)
value = ((buffer[base+1]&0xff)<<8) | (buffer[base]&0xff);
else
value = ((buffer[base]&0xff)<<8) | (buffer[base+1]&0xff);
if (value<min) min = value;
if (value>max) max = value;
stack[channel][pixel] = (short)(value);
channel++;
if (channel==channels) {
channel = 0;
pixel++;
}
}
showProgress(i+1, fi.stripOffsets.length);
}
this.min=min; this.max=max;
return stack;
}
Object readRGB48Planar(InputStream in) throws IOException {
short[] red = read16bitImage(in);
short[] green = read16bitImage(in);
short[] blue = read16bitImage(in);
Object[] stack = new Object[3];
stack[0] = red;
stack[1] = green;
stack[2] = blue;
return stack;
}
short[] read12bitImage(InputStream in) throws IOException {
int bytesPerLine = (int)(width*1.5);
if ((width&1)==1) bytesPerLine++; // add 1 if odd
byte[] buffer = new byte[bytesPerLine*height];
short[] pixels = new short[nPixels];
DataInputStream dis = new DataInputStream(in);
dis.readFully(buffer);
for (int y=0; y<height; y++) {
int index1 = y*bytesPerLine;
int index2 = y*width;
int count = 0;
while (count<width) {
pixels[index2+count] = (short)(((buffer[index1]&0xff)*16) + ((buffer[index1+1]>>4)&0xf));
count++;
if (count==width) break;
pixels[index2+count] = (short)(((buffer[index1+1]&0xf)*256) + (buffer[index1+2]&0xff));
count++; index1+=3;
}
}
return pixels;
}
float[] read24bitImage(InputStream in) throws IOException {
byte[] buffer = new byte[width*3];
float[] pixels = new float[nPixels];
int b1, b2, b3;
DataInputStream dis = new DataInputStream(in);
for (int y=0; y<height; y++) {
//IJ.log("read24bitImage: ");
dis.readFully(buffer);
int b = 0;
for (int x=0; x<width; x++) {
b1 = buffer[b++]&0xff;
b2 = buffer[b++]&0xff;
b3 = buffer[b++]&0xff;
pixels[x+y*width] = (b3<<16) | (b2<<8) | b1;
}
}
return pixels;
}
byte[] read1bitImage(InputStream in) throws IOException {
if (fi.compression==FileInfo.LZW)
throw new IOException("ImageJ cannot open 1-bit LZW compressed TIFFs");
int scan=(int)Math.ceil(width/8.0);
int len = scan*height;
byte[] buffer = new byte[len];
byte[] pixels = new byte[nPixels];
DataInputStream dis = new DataInputStream(in);
dis.readFully(buffer);
int value1,value2, offset, index;
for (int y=0; y<height; y++) {
offset = y*scan;
index = y*width;
for (int x=0; x<scan; x++) {
value1 = buffer[offset+x]&0xff;
for (int i=7; i>=0; i--) {
value2 = (value1&(1<<i))!=0?255:0;
if (index<pixels.length)
pixels[index++] = (byte)value2;
}
}
}
return pixels;
}
void skip(InputStream in) throws IOException {
if (skipCount>0) {
long bytesRead = 0;
int skipAttempts = 0;
long count;
while (bytesRead<skipCount) {
count = in.skip(skipCount-bytesRead);
skipAttempts++;
if (count==-1 || skipAttempts>5) break;
bytesRead += count;
//IJ.log("skip: "+skipCount+" "+count+" "+bytesRead+" "+skipAttempts);
}
}
byteCount = ((long)width)*height*bytesPerPixel;
if (fi.fileType==FileInfo.BITMAP) {
int scan=width/8, pad = width%8;
if (pad>0) scan++;
byteCount = scan*height;
}
nPixels = width*height;
bufferSize = (int)(byteCount/25L);
if (bufferSize<8192)
bufferSize = 8192;
else
bufferSize = (bufferSize/8192)*8192;
}
/**
Reads the image from the InputStream and returns the pixel
array (byte, short, int or float). Returns null if there
was an IO exception. Does not close the InputStream.
*/
public Object readPixels(InputStream in) {
Object pixels;
startTime = System.currentTimeMillis();
try {
switch (fi.fileType) {
case FileInfo.GRAY8:
case FileInfo.COLOR8:
bytesPerPixel = 1;
skip(in);
pixels = (Object)read8bitImage(in);
break;
case FileInfo.GRAY16_SIGNED:
case FileInfo.GRAY16_UNSIGNED:
bytesPerPixel = 2;
skip(in);
pixels = (Object)read16bitImage(in);
break;
case FileInfo.GRAY32_INT:
case FileInfo.GRAY32_UNSIGNED:
case FileInfo.GRAY32_FLOAT:
bytesPerPixel = 4;
skip(in);
pixels = (Object)read32bitImage(in);
break;
case FileInfo.GRAY64_FLOAT:
bytesPerPixel = 8;
skip(in);
pixels = (Object)read64bitImage(in);
break;
case FileInfo.RGB:
case FileInfo.BGR:
case FileInfo.ARGB:
case FileInfo.ABGR:
case FileInfo.BARG:
bytesPerPixel = fi.getBytesPerPixel();
skip(in);
pixels = (Object)readChunkyRGB(in);
break;
case FileInfo.RGB_PLANAR:
bytesPerPixel = 3;
skip(in);
pixels = (Object)readPlanarRGB(in);
break;
case FileInfo.BITMAP:
bytesPerPixel = 1;
skip(in);
pixels = (Object)read1bitImage(in);
break;
case FileInfo.RGB48:
bytesPerPixel = 6;
skip(in);
pixels = (Object)readRGB48(in);
break;
case FileInfo.RGB48_PLANAR:
bytesPerPixel = 2;
skip(in);
pixels = (Object)readRGB48Planar(in);
break;
case FileInfo.GRAY12_UNSIGNED:
skip(in);
short[] data = read12bitImage(in);
pixels = (Object)data;
break;
case FileInfo.GRAY24_UNSIGNED:
skip(in);
pixels = (Object)read24bitImage(in);
break;
default:
pixels = null;
}
showProgress(1, 1);
return pixels;
}
catch (IOException e) {
IJ.log("" + e);
return null;
}
}
/**
Skips the specified number of bytes, then reads an image and
returns the pixel array (byte, short, int or float). Returns
null if there was an IO exception. Does not close the InputStream.
*/
public Object readPixels(InputStream in, long skipCount) {
this.skipCount = skipCount;
showProgressBar = false;
Object pixels = readPixels(in);
if (eofErrorCount>0)
return null;
else
return pixels;
}
/**
Reads the image from a URL and returns the pixel array (byte,
short, int or float). Returns null if there was an IO exception.
*/
public Object readPixels(String url) {
java.net.URL theURL;
InputStream is;
try {theURL = new URL(url);}
catch (MalformedURLException e) {IJ.log(""+e); return null;}
try {is = theURL.openStream();}
catch (IOException e) {IJ.log(""+e); return null;}
return readPixels(is);
}
byte[] uncompress(byte[] input) {
if (fi.compression==FileInfo.PACK_BITS)
return packBitsUncompress(input, fi.rowsPerStrip*fi.width*fi.getBytesPerPixel());
else if (fi.compression==FileInfo.LZW || fi.compression==FileInfo.LZW_WITH_DIFFERENCING)
return lzwUncompress(input);
else if (fi.compression==FileInfo.ZIP)
return zipUncompress(input);
else
return input;
}
/** TIFF Adobe ZIP support contributed by Jason Newton. */
public byte[] zipUncompress(byte[] input) {
ByteArrayOutputStream imageBuffer = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
Inflater decompressor = new Inflater();
decompressor.setInput(input);
try {
while(!decompressor.finished()) {
int rlen = decompressor.inflate(buffer);
imageBuffer.write(buffer, 0, rlen);
}
} catch(DataFormatException e){
IJ.log(e.toString());
}
decompressor.end();
return imageBuffer.toByteArray();
}
/**
* Utility method for decoding an LZW-compressed image strip.
* Adapted from the TIFF 6.0 Specification:
* http://partners.adobe.com/asn/developer/pdfs/tn/TIFF6.pdf (page 61)
* @author Curtis Rueden (ctrueden at wisc.edu)
*/
public byte[] lzwUncompress(byte[] input) {
if (input==null || input.length==0)
return input;
byte[][] symbolTable = new byte[4096][1];
int bitsToRead = 9;
int nextSymbol = 258;
int code;
int oldCode = -1;
ByteVector out = new ByteVector(8192);
BitBuffer bb = new BitBuffer(input);
byte[] byteBuffer1 = new byte[16];
byte[] byteBuffer2 = new byte[16];
while (out.size()<byteCount) {
code = bb.getBits(bitsToRead);
if (code==EOI_CODE || code==-1)
break;
if (code==CLEAR_CODE) {
// initialize symbol table
for (int i = 0; i < 256; i++)
symbolTable[i][0] = (byte)i;
nextSymbol = 258;
bitsToRead = 9;
code = bb.getBits(bitsToRead);
if (code==EOI_CODE || code==-1)
break;
out.add(symbolTable[code]);
oldCode = code;
} else {
if (code<nextSymbol) {
// code is in table
out.add(symbolTable[code]);
// add string to table
ByteVector symbol = new ByteVector(byteBuffer1);
symbol.add(symbolTable[oldCode]);
symbol.add(symbolTable[code][0]);
symbolTable[nextSymbol] = symbol.toByteArray(); //**
oldCode = code;
nextSymbol++;
} else {
// out of table
ByteVector symbol = new ByteVector(byteBuffer2);
symbol.add(symbolTable[oldCode]);
symbol.add(symbolTable[oldCode][0]);
byte[] outString = symbol.toByteArray();
out.add(outString);
symbolTable[nextSymbol] = outString; //**
oldCode = code;
nextSymbol++;
}
if (nextSymbol == 511) { bitsToRead = 10; }
if (nextSymbol == 1023) { bitsToRead = 11; }
if (nextSymbol == 2047) { bitsToRead = 12; }
}
}
return out.toByteArray();
}
/** Based on the Bio-Formats PackbitsCodec written by Melissa Linkert. */
public byte[] packBitsUncompress(byte[] input, int expected) {
if (expected==0) expected = Integer.MAX_VALUE;
ByteVector output = new ByteVector(1024);
int index = 0;
while (output.size()<expected && index<input.length) {
byte n = input[index++];
if (n>=0) { // 0 <= n <= 127
byte[] b = new byte[n+1];
for (int i=0; i<n+1; i++)
b[i] = input[index++];
output.add(b);
b = null;
} else if (n != -128) { // -127 <= n <= -1
int len = -n + 1;
byte inp = input[index++];
for (int i=0; i<len; i++) output.add(inp);
}
}
return output.toByteArray();
}
/*
void debug(String label, InputStream in) {
int offset = -1;
if (in instanceof RandomAccessStream) {
try {
offset = ((RandomAccessStream)in).getFilePointer();
} catch(Exception e) {}
}
IJ.log(label+": debug: offset="+offset+", fi="+fi);
}
*/
}
/** A growable array of bytes. */
class ByteVector {
private byte[] data;
private int size;
public ByteVector() {
data = new byte[10];
size = 0;
}
public ByteVector(int initialSize) {
data = new byte[initialSize];
size = 0;
}
public ByteVector(byte[] byteBuffer) {
data = byteBuffer;
size = 0;
}
public void add(byte x) {
if (size>=data.length) {
doubleCapacity();
add(x);
} else
data[size++] = x;
}
public int size() {
return size;
}
public void add(byte[] array) {
int length = array.length;
while (data.length-size<length)
doubleCapacity();
System.arraycopy(array, 0, data, size, length);
size += length;
}
void doubleCapacity() {
//IJ.log("double: "+data.length*2);
byte[] tmp = new byte[data.length*2 + 1];
System.arraycopy(data, 0, tmp, 0, data.length);
data = tmp;
}
public void clear() {
size = 0;
}
public byte[] toByteArray() {
byte[] bytes = new byte[size];
System.arraycopy(data, 0, bytes, 0, size);
return bytes;
}
}
|