11 private int width, height;
12 private long skipCount;
13 private int bytesPerPixel, bufferSize, byteCount, nPixels;
14 private boolean showProgressBar=
true;
15 private int eofErrorCount;
32 byte[] read8bitImage(InputStream in)
throws IOException {
35 pixels =
new byte[nPixels];
36 int count, actuallyRead;
38 while (totalRead<byteCount) {
39 if (totalRead+bufferSize>byteCount)
40 count = byteCount-totalRead;
43 actuallyRead = in.read(pixels, totalRead, count);
44 if (actuallyRead==-1) {eofError();
break;}
45 totalRead += actuallyRead;
46 showProgress((
double)totalRead/byteCount);
51 private void showProgress(
double progress) {
53 IJ.showProgress(progress);
57 short[] read16bitImage(InputStream in)
throws IOException {
59 byte[] buffer =
new byte[bufferSize];
60 short[] pixels =
new short[nPixels];
66 while (totalRead<byteCount) {
67 if ((totalRead+bufferSize)>byteCount)
68 bufferSize = byteCount-totalRead;
70 while (bufferCount<bufferSize) {
71 count = in.read(buffer, bufferCount, bufferSize-bufferCount);
74 if (fi.fileType==FileInfo.GRAY16_SIGNED)
75 for (
int i=base; i<pixels.length; i++)
76 pixels[i] = (
short)32768;
81 totalRead += bufferSize;
82 showProgress((
double)totalRead/byteCount);
83 pixelsRead = bufferSize/bytesPerPixel;
84 if (fi.intelByteOrder) {
85 if (fi.fileType==FileInfo.GRAY16_SIGNED)
86 for (
int i=base,j=0; i<(base+pixelsRead); i++,j+=2)
87 pixels[i] = (
short)((((buffer[j+1]&0xff)<<8) | (buffer[j]&0xff))+32768);
89 for (
int i=base,j=0; i<(base+pixelsRead); i++,j+=2)
90 pixels[i] = (
short)(((buffer[j+1]&0xff)<<8) | (buffer[j]&0xff));
92 if (fi.fileType==FileInfo.GRAY16_SIGNED)
93 for (
int i=base,j=0; i<(base+pixelsRead); i++,j+=2)
94 pixels[i] = (
short)((((buffer[j]&0xff)<<8) | (buffer[j+1]&0xff))+32768);
96 for (
int i=base,j=0; i<(base+pixelsRead); i++,j+=2)
97 pixels[i] = (
short)(((buffer[j]&0xff)<<8) | (buffer[j+1]&0xff));
104 float[] read32bitImage(InputStream in)
throws IOException {
106 byte[] buffer =
new byte[bufferSize];
107 float[] pixels =
new float[nPixels];
114 while (totalRead<byteCount) {
115 if ((totalRead+bufferSize)>byteCount)
116 bufferSize = byteCount-totalRead;
118 while (bufferCount<bufferSize) {
119 count = in.read(buffer, bufferCount, bufferSize-bufferCount);
120 if (count==-1) {eofError();
return pixels;}
121 bufferCount += count;
123 totalRead += bufferSize;
124 showProgress((
double)totalRead/byteCount);
125 pixelsRead = bufferSize/bytesPerPixel;
127 if (fi.intelByteOrder)
128 for (
int i=base; i < (base+pixelsRead); i++) {
129 tmp = (int)(((buffer[j+3]&0xff)<<24) | ((buffer[j+2]&0xff)<<16) | ((buffer[j+1]&0xff)<<8) | (buffer[j]&0xff));
130 if (fi.fileType==FileInfo.GRAY32_FLOAT)
131 pixels[i] = Float.intBitsToFloat(tmp);
132 else if (fi.fileType==FileInfo.GRAY32_UNSIGNED)
133 pixels[i] = (float)(tmp&0xffffffffL);
139 for (
int i=base; i < (base+pixelsRead); i++) {
140 tmp = (int)(((buffer[j]&0xff)<<24) | ((buffer[j+1]&0xff)<<16) | ((buffer[j+2]&0xff)<<8) | (buffer[j+3]&0xff));
141 if (fi.fileType==FileInfo.GRAY32_FLOAT)
142 pixels[i] = Float.intBitsToFloat(tmp);
143 else if (fi.fileType==FileInfo.GRAY32_UNSIGNED)
144 pixels[i] = (float)(tmp&0xffffffffL);
154 int[] readChunkyRGB(InputStream in)
throws IOException {
156 bufferSize = 24*width;
157 byte[] buffer =
new byte[bufferSize];
158 int[] pixels =
new int[nPixels];
165 while (totalRead<byteCount) {
166 if ((totalRead+bufferSize)>byteCount)
167 bufferSize = byteCount-totalRead;
169 while (bufferCount<bufferSize) {
170 count = in.read(buffer, bufferCount, bufferSize-bufferCount);
171 if (count==-1) {eofError();
return pixels;}
172 bufferCount += count;
174 totalRead += bufferSize;
175 showProgress((
double)totalRead/byteCount);
176 pixelsRead = bufferSize/bytesPerPixel;
177 boolean bgr = fi.fileType==FileInfo.BGR;
179 for (
int i=base; i<(base+pixelsRead); i++) {
180 if (bytesPerPixel==4)
182 r = buffer[j++]&0xff;
183 g = buffer[j++]&0xff;
184 b = buffer[j++]&0xff;
186 pixels[i] = 0xff000000 | (b<<16) | (g<<8) | r;
188 pixels[i] = 0xff000000 | (r<<16) | (g<<8) | b;
195 int[] readPlanarRGB(InputStream in)
throws IOException {
196 int planeSize = nPixels;
197 byte[] buffer =
new byte[planeSize];
198 int[] pixels =
new int[nPixels];
204 bytesRead = in.read(buffer, 0, planeSize);
205 if (bytesRead==-1) {eofError();
return pixels;}
206 totalRead += bytesRead;
207 for (
int i=0; i < planeSize; i++) {
209 pixels[i] = 0xff000000 | (r<<16);
213 bytesRead = in.read(buffer, 0, planeSize);
214 if (bytesRead==-1) {eofError();
return pixels;}
215 totalRead += bytesRead;
216 for (
int i=0; i < planeSize; i++) {
222 bytesRead = in.read(buffer, 0, planeSize);
223 if (bytesRead==-1) {eofError();
return pixels;}
224 totalRead += bytesRead;
225 for (
int i=0; i < planeSize; i++) {
234 Object readRGB48(InputStream in)
throws IOException {
236 bufferSize = 24*width;
237 byte[] buffer =
new byte[bufferSize];
238 short[] red =
new short[nPixels];
239 short[] green =
new short[nPixels];
240 short[] blue =
new short[nPixels];
246 Object[] stack =
new Object[3];
250 while (totalRead<byteCount) {
251 if ((totalRead+bufferSize)>byteCount)
252 bufferSize = byteCount-totalRead;
254 while (bufferCount<bufferSize) {
255 count = in.read(buffer, bufferCount, bufferSize-bufferCount);
256 if (count==-1) {eofError();
return stack;}
257 bufferCount += count;
259 totalRead += bufferSize;
260 showProgress((
double)totalRead/byteCount);
261 pixelsRead = bufferSize/bytesPerPixel;
262 if (fi.intelByteOrder) {
263 for (
int i=base,j=0; i<(base+pixelsRead); i++) {
264 red[i] = (short)(((buffer[j+1]&0xff)<<8) | (buffer[j]&0xff)); j+=2;
265 green[i] = (short)(((buffer[j+1]&0xff)<<8) | (buffer[j]&0xff)); j+=2;
266 blue[i] = (short)(((buffer[j+1]&0xff)<<8) | (buffer[j]&0xff)); j+=2;
269 for (
int i=base,j=0; i<(base+pixelsRead); i++) {
270 red[i] = (short)(((buffer[j]&0xff)<<8) | (buffer[j+1]&0xff)); j+=2;
271 green[i] = (short)(((buffer[j]&0xff)<<8) | (buffer[j+1]&0xff)); j+=2;
272 blue[i] = (short)(((buffer[j]&0xff)<<8) | (buffer[j+1]&0xff)); j+=2;
280 void skip(InputStream in)
throws IOException {
283 int skipAttempts = 0;
285 while (bytesRead<skipCount) {
286 count = in.skip(skipCount-bytesRead);
288 if (count==-1 || skipAttempts>5)
break;
293 byteCount = width*height*bytesPerPixel;
294 if (fi.fileType==FileInfo.BITMAP) {
295 int scan=width/8, pad = width%8;
297 byteCount = scan*height;
299 nPixels = width*height;
300 bufferSize = byteCount/25;
304 bufferSize = (bufferSize/8192)*8192;
314 switch (fi.fileType) {
319 return (Object)read8bitImage(in);
324 return (Object)read16bitImage(in);
330 return (Object)read32bitImage(in);
336 return (Object)readChunkyRGB(in);
340 return (Object)readPlanarRGB(in);
344 byte[] bitmap = read8bitImage(in);
345 expandBitmap(bitmap);
346 return (Object)bitmap;
350 return (Object)readRGB48(in);
355 catch (IOException e) {
367 this.skipCount = skipCount;
368 showProgressBar =
false;
383 try {theURL =
new URL(url);}
384 catch (MalformedURLException e) {
IJ.
write(
""+e);
return null;}
385 try {is = theURL.openStream();}
386 catch (IOException e) {
IJ.
write(
""+e);
return null;}
390 void expandBitmap(byte[] pixels) {
394 int len = scan*height;
395 byte bitmap[] =
new byte [len];
396 System.arraycopy(pixels, 0, bitmap, 0, len);
397 int value1,value2, offset, index;
398 for (
int y=0; y<height; y++) {
401 for (
int x=0; x<scan; x++) {
402 value1 = bitmap[offset+x]&0xff;
403 for (
int i=7; i>=0; i--) {
404 value2 = (value1&(1<<i))!=0?255:0;
405 if (index<pixels.length)
406 pixels[index++] = (byte)value2;