author | Claus Gittinger <cg@exept.de> |
Tue, 24 Feb 2004 11:35:25 +0100 | |
changeset 4033 | 9af502584519 |
parent 3965 | 8c9c9e08880c |
child 4055 | bb45fa7310eb |
permissions | -rw-r--r-- |
0 | 1 |
" |
6 | 2 |
COPYRIGHT (c) 1991 by Claus Gittinger |
77 | 3 |
All Rights Reserved |
0 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
3260 | 13 |
"{ Package: 'stx:libview' }" |
14 |
||
0 | 15 |
Object subclass:#ImageReader |
518 | 16 |
instanceVariableNames:'width height data byteOrder inStream outStream photometric |
1648
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
17 |
samplesPerPixel bitsPerSample colorMap mask maskPixel |
3955
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
18 |
dimensionCallBack dimensionHolder progressHolder imageSequence' |
518 | 19 |
classVariableNames:'ReverseBits' |
20 |
poolDictionaries:'' |
|
3843
57d7f752c63b
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
3839
diff
changeset
|
21 |
category:'Graphics-Images-Readers' |
0 | 22 |
! |
23 |
||
189 | 24 |
!ImageReader primitiveDefinitions! |
25 |
%{ |
|
1891 | 26 |
|
189 | 27 |
#ifndef _STDIO_H_INCLUDED_ |
28 |
# include <stdio.h> |
|
29 |
# define _STDIO_H_INCLUDED_ |
|
30 |
#endif |
|
2423
416882dbf8cf
changes for egcs (__new in stdio.h)
Claus Gittinger <cg@exept.de>
parents:
2261
diff
changeset
|
31 |
|
416882dbf8cf
changes for egcs (__new in stdio.h)
Claus Gittinger <cg@exept.de>
parents:
2261
diff
changeset
|
32 |
|
189 | 33 |
%} |
34 |
! ! |
|
35 |
||
89 | 36 |
!ImageReader primitiveFunctions! |
0 | 37 |
%{ |
38 |
||
39 |
/* |
|
40 |
* ccitt decompression |
|
41 |
*/ |
|
42 |
static short *whiteCountTable; |
|
43 |
static char *whiteShiftTable; |
|
44 |
static short *blackCountTable; |
|
45 |
static char *blackShiftTable; |
|
46 |
||
47 |
struct ccitt_def { |
|
48 |
unsigned short bits; |
|
49 |
short nBits; |
|
50 |
}; |
|
51 |
||
52 |
static struct ccitt_def |
|
53 |
whiteDef[] = { |
|
54 |
{ 0x3500, 8 }, /* 0 */ |
|
55 |
{ 0x1c00, 6 }, |
|
56 |
{ 0x7000, 4 }, |
|
57 |
{ 0x8000, 4 }, |
|
58 |
{ 0xb000, 4 }, |
|
59 |
{ 0xc000, 4 }, |
|
60 |
{ 0xe000, 4 }, |
|
61 |
{ 0xf000, 4 }, |
|
62 |
{ 0x9800, 5 }, |
|
63 |
{ 0xA000, 5 }, |
|
64 |
{ 0x3800, 5 }, /* 10 */ |
|
65 |
{ 0x4000, 5 }, |
|
66 |
{ 0x2000, 6 }, |
|
67 |
{ 0x0c00, 6 }, |
|
68 |
{ 0xd000, 6 }, |
|
69 |
{ 0xd400, 6 }, |
|
70 |
{ 0xa800, 6 }, |
|
71 |
{ 0xac00, 6 }, |
|
72 |
{ 0x4e00, 7 }, |
|
73 |
{ 0x1800, 7 }, |
|
74 |
{ 0x1000, 7 }, /* 20 */ |
|
75 |
{ 0x2e00, 7 }, |
|
76 |
{ 0x0600, 7 }, |
|
77 |
{ 0x0800, 7 }, |
|
78 |
{ 0x5000, 7 }, |
|
79 |
{ 0x5600, 7 }, |
|
80 |
{ 0x2600, 7 }, |
|
81 |
{ 0x4800, 7 }, |
|
82 |
{ 0x3000, 7 }, |
|
83 |
{ 0x0200, 8 }, |
|
84 |
{ 0x0300, 8 }, /* 30 */ |
|
85 |
{ 0x1a00, 8 }, |
|
86 |
{ 0x1b00, 8 }, |
|
87 |
{ 0x1200, 8 }, |
|
88 |
{ 0x1300, 8 }, |
|
89 |
{ 0x1400, 8 }, |
|
90 |
{ 0x1500, 8 }, |
|
91 |
{ 0x1600, 8 }, |
|
92 |
{ 0x1700, 8 }, |
|
93 |
{ 0x2800, 8 }, |
|
94 |
{ 0x2900, 8 }, /* 40 */ |
|
95 |
{ 0x2a00, 8 }, |
|
96 |
{ 0x2b00, 8 }, |
|
97 |
{ 0x2c00, 8 }, |
|
98 |
{ 0x2d00, 8 }, |
|
99 |
{ 0x0400, 8 }, |
|
100 |
{ 0x0500, 8 }, |
|
101 |
{ 0x0a00, 8 }, |
|
102 |
{ 0x0b00, 8 }, |
|
103 |
{ 0x5200, 8 }, |
|
104 |
{ 0x5300, 8 }, /* 50 */ |
|
105 |
{ 0x5400, 8 }, |
|
106 |
{ 0x5500, 8 }, |
|
107 |
{ 0x2400, 8 }, |
|
108 |
{ 0x2500, 8 }, |
|
109 |
{ 0x5800, 8 }, |
|
110 |
{ 0x5900, 8 }, |
|
111 |
{ 0x5a00, 8 }, |
|
112 |
{ 0x5b00, 8 }, |
|
113 |
{ 0x4a00, 8 }, |
|
114 |
{ 0x4b00, 8 }, /* 60 */ |
|
115 |
{ 0x3200, 8 }, |
|
116 |
{ 0x3300, 8 }, |
|
117 |
{ 0x3400, 8 }, |
|
118 |
/* ---------------- */ |
|
119 |
{ 0xd800, 5 }, /* 64 */ |
|
120 |
{ 0x9000, 5 }, /* 128 */ |
|
121 |
{ 0x5c00, 6 }, /* 192 */ |
|
122 |
{ 0x6e00, 7 }, /* 256 */ |
|
123 |
{ 0x3600, 8 }, /* 320 */ |
|
124 |
{ 0x3700, 8 }, |
|
125 |
{ 0x6400, 8 }, |
|
126 |
{ 0x6500, 8 }, |
|
127 |
{ 0x6800, 8 }, |
|
128 |
{ 0x6700, 8 }, /* 640 */ |
|
129 |
{ 0x6600, 9 }, /* 704 */ |
|
130 |
{ 0x6680, 9 }, |
|
131 |
{ 0x6900, 9 }, |
|
132 |
{ 0x6980, 9 }, |
|
133 |
{ 0x6a00, 9 }, |
|
134 |
{ 0x6a80, 9 }, |
|
135 |
{ 0x6b00, 9 }, |
|
136 |
{ 0x6b80, 9 }, |
|
137 |
{ 0x6c00, 9 }, |
|
138 |
{ 0x6c80, 9 }, |
|
139 |
{ 0x6d00, 9 }, |
|
140 |
{ 0x6d80, 9 }, |
|
141 |
{ 0x4c00, 9 }, |
|
142 |
{ 0x4c80, 9 }, |
|
143 |
{ 0x4d00, 9 }, /* 1600 */ |
|
144 |
{ 0x6000, 6 }, /* 1664 */ |
|
145 |
{ 0x4d80, 9 }, /* 1728 */ |
|
146 |
/* -------------------------------- */ |
|
147 |
{ 0x0100, 11 }, /* 1792 */ |
|
148 |
{ 0x0180, 11 }, |
|
149 |
{ 0x01a0, 11 }, /* 1920 */ |
|
150 |
{ 0x0120, 12 }, /* 1984 */ |
|
151 |
{ 0x0130, 12 }, |
|
152 |
{ 0x0140, 12 }, |
|
153 |
{ 0x0150, 12 }, |
|
154 |
{ 0x0160, 12 }, |
|
155 |
{ 0x0170, 12 }, |
|
156 |
{ 0x01c0, 12 }, |
|
157 |
{ 0x01d0, 12 }, |
|
158 |
{ 0x01e0, 12 }, |
|
159 |
{ 0x01f0, 12 }, /* 2560 */ |
|
160 |
/* -------------------------------- */ |
|
161 |
{ 0x0010, 12 }, /* EOL */ |
|
162 |
}; |
|
163 |
||
164 |
static struct ccitt_def |
|
165 |
blackDef[] = { |
|
166 |
{ 0x0dc0, 10 }, /* 0 */ |
|
167 |
{ 0x4000, 3 }, |
|
168 |
{ 0xc000, 2 }, |
|
169 |
{ 0x8000, 2 }, |
|
170 |
{ 0x6000, 3 }, |
|
171 |
{ 0x3000, 4 }, |
|
172 |
{ 0x2000, 4 }, |
|
173 |
{ 0x1800, 5 }, |
|
174 |
{ 0x1400, 6 }, |
|
175 |
{ 0x1000, 6 }, |
|
176 |
{ 0x0800, 7 }, /* 10 */ |
|
177 |
{ 0x0a00, 7 }, |
|
178 |
{ 0x0e00, 7 }, |
|
179 |
{ 0x0400, 8 }, |
|
180 |
{ 0x0700, 8 }, |
|
181 |
{ 0x0c00, 9 }, |
|
182 |
{ 0x05c0, 10 }, |
|
183 |
{ 0x0600, 10 }, |
|
184 |
{ 0x0200, 10 }, |
|
185 |
{ 0x0ce0, 11 }, |
|
186 |
{ 0x0d00, 11 }, /* 20 */ |
|
187 |
{ 0x0d80, 11 }, |
|
188 |
{ 0x06e0, 11 }, |
|
189 |
{ 0x0500, 11 }, |
|
190 |
{ 0x02e0, 11 }, |
|
191 |
{ 0x0300, 11 }, |
|
192 |
{ 0x0ca0, 12 }, |
|
193 |
{ 0x0cb0, 12 }, |
|
194 |
{ 0x0cc0, 12 }, |
|
195 |
{ 0x0cd0, 12 }, |
|
196 |
{ 0x0680, 12 }, /* 30 */ |
|
197 |
{ 0x0690, 12 }, |
|
198 |
{ 0x06a0, 12 }, |
|
199 |
{ 0x06b0, 12 }, |
|
200 |
{ 0x0d20, 12 }, |
|
201 |
{ 0x0d30, 12 }, |
|
202 |
{ 0x0d40, 12 }, |
|
203 |
{ 0x0d50, 12 }, |
|
204 |
{ 0x0d60, 12 }, |
|
205 |
{ 0x0d70, 12 }, |
|
206 |
{ 0x06c0, 12 }, /* 40 */ |
|
207 |
{ 0x06d0, 12 }, |
|
208 |
{ 0x0da0, 12 }, |
|
209 |
{ 0x0db0, 12 }, |
|
210 |
{ 0x0540, 12 }, |
|
211 |
{ 0x0550, 12 }, |
|
212 |
{ 0x0560, 12 }, |
|
213 |
{ 0x0570, 12 }, |
|
214 |
{ 0x0640, 12 }, |
|
215 |
{ 0x0650, 12 }, |
|
216 |
{ 0x0520, 12 }, /* 50 */ |
|
217 |
{ 0x0530, 12 }, |
|
218 |
{ 0x0240, 12 }, |
|
219 |
{ 0x0370, 12 }, |
|
220 |
{ 0x0380, 12 }, |
|
221 |
{ 0x0270, 12 }, |
|
222 |
{ 0x0280, 12 }, |
|
223 |
{ 0x0580, 12 }, |
|
224 |
{ 0x0590, 12 }, |
|
225 |
{ 0x02b0, 12 }, |
|
226 |
{ 0x02c0, 12 }, /* 60 */ |
|
227 |
{ 0x05a0, 12 }, |
|
228 |
{ 0x0660, 12 }, |
|
229 |
{ 0x0670, 12 }, |
|
230 |
/* ---------------- */ |
|
231 |
{ 0x03c0, 10 }, /* 64 */ |
|
232 |
{ 0x0c80, 12 }, /* 128 */ |
|
233 |
{ 0x0c90, 12 }, /* 192 */ |
|
234 |
{ 0x05b0, 12 }, /* 256 */ |
|
235 |
{ 0x0330, 12 }, /* 320 */ |
|
236 |
{ 0x0340, 12 }, |
|
237 |
{ 0x0350, 12 }, /* 448 */ |
|
238 |
{ 0x0360, 13 }, /* 512 */ |
|
239 |
{ 0x0368, 13 }, |
|
240 |
{ 0x0250, 13 }, /* 640 */ |
|
241 |
{ 0x0258, 13 }, /* 704 */ |
|
242 |
{ 0x0260, 13 }, |
|
243 |
{ 0x0268, 13 }, |
|
244 |
{ 0x0390, 13 }, |
|
245 |
{ 0x0398, 13 }, |
|
246 |
{ 0x03a0, 13 }, |
|
247 |
{ 0x03a8, 13 }, |
|
248 |
{ 0x03b0, 13 }, |
|
249 |
{ 0x03b8, 13 }, |
|
250 |
{ 0x0290, 13 }, |
|
251 |
{ 0x0298, 13 }, |
|
252 |
{ 0x02a0, 13 }, |
|
253 |
{ 0x02a8, 13 }, |
|
254 |
{ 0x02d0, 13 }, |
|
255 |
{ 0x02d8, 13 }, /* 1600 */ |
|
256 |
{ 0x0320, 13 }, /* 1664 */ |
|
257 |
{ 0x0328, 13 }, /* 1728 */ |
|
258 |
/* -------------------------------- */ |
|
259 |
}; |
|
260 |
||
261 |
static |
|
262 |
initCCITTTables() { |
|
263 |
register cnt, bits, value; |
|
264 |
int nBits, index; |
|
265 |
||
266 |
if (whiteCountTable != (short *)0) return; |
|
267 |
||
268 |
whiteCountTable = (short *) malloc(sizeof(short) * 8192); |
|
12 | 269 |
if (! whiteCountTable) return; |
0 | 270 |
whiteShiftTable = (char *) malloc(sizeof(char) * 8192); |
12 | 271 |
if (! whiteShiftTable) { |
95 | 272 |
goto fail1; |
12 | 273 |
} |
0 | 274 |
blackCountTable = (short *) malloc(sizeof(short) * 8192); |
12 | 275 |
if (! blackCountTable) { |
95 | 276 |
goto fail2; |
12 | 277 |
} |
0 | 278 |
blackShiftTable = (char *) malloc(sizeof(char) * 8192); |
12 | 279 |
if (! blackShiftTable) { |
95 | 280 |
free(blackCountTable); blackCountTable = (short *)0; |
281 |
fail2: |
|
77 | 282 |
free(whiteShiftTable); whiteShiftTable = (char *)0; |
95 | 283 |
fail1: |
77 | 284 |
free(whiteCountTable); whiteCountTable = (short *)0; |
285 |
return; |
|
12 | 286 |
} |
0 | 287 |
|
288 |
for (index = 0; index < 8192; index++) { |
|
77 | 289 |
whiteCountTable[index] = -1; |
290 |
blackCountTable[index] = -1; |
|
0 | 291 |
} |
292 |
||
293 |
for (value = 0; value <= 63; value++) { |
|
77 | 294 |
nBits = whiteDef[value].nBits; |
295 |
bits = whiteDef[value].bits >> 3; |
|
296 |
for (cnt = 1 << (13 - nBits); cnt; cnt--, bits++) { |
|
297 |
whiteCountTable[bits] = value; |
|
298 |
whiteShiftTable[bits] = nBits; |
|
299 |
} |
|
300 |
nBits = blackDef[value].nBits; |
|
301 |
bits = blackDef[value].bits >> 3; |
|
302 |
for (cnt = 1 << (13 - nBits); cnt; cnt--, bits++) { |
|
303 |
blackCountTable[bits] = value; |
|
304 |
blackShiftTable[bits] = nBits; |
|
305 |
} |
|
0 | 306 |
} |
307 |
index = value; |
|
308 |
||
309 |
for (; value <= 1728; value += 64) { |
|
77 | 310 |
nBits = whiteDef[index].nBits; |
311 |
bits = whiteDef[index].bits >> 3; |
|
312 |
for (cnt = 1 << (13 - nBits); cnt; cnt--, bits++) { |
|
313 |
whiteCountTable[bits] = value; |
|
314 |
whiteShiftTable[bits] = nBits; |
|
315 |
} |
|
316 |
nBits = blackDef[index].nBits; |
|
317 |
bits = blackDef[index].bits >> 3; |
|
318 |
for (cnt = 1 << (13 - nBits); cnt; cnt--, bits++) { |
|
319 |
blackCountTable[bits] = value; |
|
320 |
blackShiftTable[bits] = nBits; |
|
321 |
} |
|
322 |
index++; |
|
0 | 323 |
} |
324 |
||
325 |
for (; value <= 2560; value += 64) { |
|
77 | 326 |
nBits = whiteDef[index].nBits; |
327 |
bits = whiteDef[index].bits >> 3; |
|
328 |
for (cnt = 1 << (13 - nBits); cnt; cnt--, bits++) { |
|
329 |
whiteCountTable[bits] = value; |
|
330 |
whiteShiftTable[bits] = nBits; |
|
331 |
blackCountTable[bits] = value; |
|
332 |
blackShiftTable[bits] = nBits; |
|
333 |
} |
|
334 |
index++; |
|
0 | 335 |
} |
336 |
} |
|
337 |
||
338 |
static short |
|
339 |
leftBits[] = { |
|
340 |
0, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFF |
|
341 |
}; |
|
342 |
||
189 | 343 |
static int |
77 | 344 |
__decodeCCITTgroup3__(from, to, len) |
0 | 345 |
unsigned char *from; |
346 |
register unsigned char *to; |
|
347 |
{ |
|
348 |
register cnt; |
|
349 |
register short *countPtr; |
|
350 |
register char *shiftPtr; |
|
351 |
unsigned bits, bits13; |
|
352 |
int shift, outCount, nBitsLess13; |
|
353 |
int outBitOffset; |
|
354 |
int nLeft, t; |
|
355 |
||
356 |
if (! whiteCountTable) initCCITTTables(); |
|
46 | 357 |
if (! whiteCountTable) return 0; /* malloc failed */ |
0 | 358 |
|
359 |
countPtr = whiteCountTable; |
|
360 |
shiftPtr = whiteShiftTable; |
|
361 |
outCount = 0; |
|
362 |
outBitOffset = 0; |
|
363 |
bits = *from++; |
|
364 |
bits = (bits << 8) | *from++; |
|
365 |
nBitsLess13 = 3; |
|
366 |
for (;;) { |
|
77 | 367 |
bits13 = (bits >> nBitsLess13) & 0x1FFF; |
368 |
cnt = countPtr[bits13]; |
|
189 | 369 |
if (cnt < 0) return 1; |
77 | 370 |
shift = shiftPtr[bits13]; |
371 |
outCount += cnt; |
|
372 |
if (countPtr == blackCountTable) { |
|
373 |
/* toggle if it was a terminating code */ |
|
374 |
if (cnt < 64) { |
|
375 |
countPtr = whiteCountTable; |
|
376 |
shiftPtr = whiteShiftTable; |
|
377 |
} |
|
0 | 378 |
|
77 | 379 |
/* draw cnt black bits */ |
380 |
if (cnt) { |
|
381 |
if (outBitOffset) { |
|
382 |
nLeft = 8 - outBitOffset; |
|
383 |
if (cnt < nLeft) nLeft = cnt; |
|
384 |
t = leftBits[nLeft] >> outBitOffset; |
|
385 |
*to |= t; |
|
386 |
cnt -= nLeft; |
|
387 |
outBitOffset += nLeft; |
|
388 |
if (outBitOffset >= 8) { |
|
389 |
to++; |
|
390 |
outBitOffset -= 8; |
|
391 |
} |
|
392 |
} |
|
393 |
if (cnt > 256) { |
|
394 |
while ((int)to & 3) { |
|
395 |
*to++ = 0xFF; |
|
396 |
cnt -= 8; |
|
397 |
} |
|
398 |
while (cnt >= 32) { |
|
399 |
(*(long *)to) = 0xFFFFFFFF; |
|
400 |
to += 4; |
|
401 |
cnt -= 32; |
|
402 |
} |
|
403 |
} |
|
404 |
while (cnt >= 8) { |
|
405 |
*to++ = 0xFF; |
|
406 |
cnt -= 8; |
|
407 |
} |
|
408 |
*to |= leftBits[cnt]; |
|
409 |
outBitOffset += cnt; |
|
410 |
} |
|
411 |
} else { |
|
412 |
/* toggle if it was a terminating code */ |
|
413 |
if (cnt < 64) { |
|
414 |
countPtr = blackCountTable; |
|
415 |
shiftPtr = blackShiftTable; |
|
416 |
} |
|
0 | 417 |
|
77 | 418 |
/* skip cnt bits */ |
419 |
to += cnt >> 3; |
|
420 |
outBitOffset += cnt & 7; |
|
421 |
if (outBitOffset >= 8) { |
|
422 |
to++; |
|
423 |
outBitOffset -= 8; |
|
424 |
} |
|
425 |
} |
|
426 |
if (outCount >= len) return 1; |
|
0 | 427 |
|
77 | 428 |
nBitsLess13 -= shift; |
429 |
while (nBitsLess13 < 0) { |
|
430 |
bits = (bits << 8) | *from++; |
|
431 |
nBitsLess13 += 8; |
|
432 |
} |
|
0 | 433 |
} |
434 |
} |
|
435 |
||
436 |
/* |
|
437 |
* LZW decompression |
|
438 |
*/ |
|
439 |
struct buffer { |
|
77 | 440 |
struct buffer *prev; |
441 |
unsigned char chars[8192 - 4]; |
|
0 | 442 |
}; |
189 | 443 |
|
444 |
static int |
|
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
445 |
__decodeLZW__(from, to, inCount, fromSize, toSize) |
0 | 446 |
unsigned char *from; |
447 |
unsigned char *to; |
|
448 |
{ |
|
449 |
register unsigned code; |
|
12 | 450 |
unsigned char **strings; |
451 |
short *stringLen; |
|
0 | 452 |
struct buffer *scratchBuffer; |
453 |
struct buffer *newBuffer; |
|
454 |
unsigned char *scratchPtr; |
|
455 |
int nScratch; |
|
1262 | 456 |
unsigned nextCode; |
457 |
int oldCode = -1; |
|
0 | 458 |
register unsigned bits; |
459 |
int nBits, mask, shift; |
|
460 |
int i; |
|
461 |
int len; |
|
462 |
int codeLen = 9; |
|
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
463 |
unsigned char *fromEnd, *toEnd; |
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
464 |
int ret = 1; /* return success */ |
0 | 465 |
|
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
466 |
fromEnd = from + fromSize; |
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
467 |
toEnd = to + toSize; |
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
468 |
|
0 | 469 |
scratchBuffer = (struct buffer *)malloc(sizeof(struct buffer)); |
12 | 470 |
if (! scratchBuffer) return 0; |
46 | 471 |
|
12 | 472 |
strings = (unsigned char **)malloc(sizeof(unsigned char *) * 4096); |
473 |
if (! strings) { |
|
77 | 474 |
free(scratchBuffer); |
475 |
return 0; |
|
12 | 476 |
} |
477 |
stringLen = (short *)malloc(sizeof(short) * 4096); |
|
478 |
if (! stringLen) { |
|
77 | 479 |
free(strings); |
480 |
free(scratchBuffer); |
|
481 |
return 0; |
|
12 | 482 |
} |
483 |
||
0 | 484 |
scratchBuffer->prev = (struct buffer *)0; |
485 |
scratchPtr = scratchBuffer->chars; |
|
486 |
nScratch = sizeof(scratchBuffer->chars); |
|
487 |
||
488 |
for (i = 0; i < 256; i++) { |
|
77 | 489 |
*scratchPtr = i; |
490 |
strings[i] = scratchPtr++; |
|
491 |
stringLen[i] = 1; |
|
0 | 492 |
} |
493 |
||
494 |
nextCode = 258; |
|
495 |
nScratch -= 256; |
|
496 |
mask = 0x1FF; |
|
497 |
nBits = 0; |
|
498 |
bits = 0; |
|
499 |
while (inCount) { |
|
77 | 500 |
/* fetch code */ |
501 |
while (nBits < codeLen) { |
|
502 |
bits = (bits<<8) | *from++; |
|
503 |
inCount--; |
|
504 |
nBits += 8; |
|
505 |
} |
|
506 |
shift = nBits - codeLen; |
|
507 |
code = (bits >> shift) & mask; |
|
508 |
bits &= ~(mask << shift); |
|
509 |
nBits -= codeLen; |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
510 |
|
77 | 511 |
if (code == 257) break; |
512 |
if (code == 256) { |
|
513 |
if (! inCount) |
|
514 |
break; |
|
0 | 515 |
|
77 | 516 |
/* free stuff */ |
517 |
while (scratchBuffer->prev) { |
|
518 |
newBuffer = scratchBuffer; |
|
519 |
scratchBuffer = scratchBuffer->prev; |
|
520 |
free(newBuffer); |
|
521 |
} |
|
522 |
/* reset everything */ |
|
523 |
scratchPtr = scratchBuffer->chars + 256; |
|
524 |
nScratch = sizeof(scratchBuffer->chars) - 256; |
|
525 |
codeLen = 9; |
|
526 |
nextCode = 258; |
|
527 |
mask = 0x1FF; |
|
528 |
/* fetch code */ |
|
529 |
while (nBits < codeLen) { |
|
530 |
bits = (bits<<8) | *from++; |
|
531 |
inCount--; |
|
532 |
nBits += 8; |
|
533 |
} |
|
534 |
shift = nBits - codeLen; |
|
535 |
code = (bits >> shift) & mask; |
|
536 |
bits &= ~(mask << shift); |
|
537 |
nBits -= codeLen; |
|
538 |
if (code == 257) break; |
|
539 |
/* add to output */ |
|
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
540 |
if (to >= toEnd) { |
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
541 |
fprintf(stderr, "ImageReader [warning]: LZW outBuffer overrun\n"); |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
542 |
ret = 0; |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
543 |
break; |
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
544 |
} |
77 | 545 |
*to++ = code; |
546 |
oldCode = code; |
|
547 |
} else { |
|
548 |
if (code < nextCode) { |
|
549 |
/* writeString(string[code]) */ |
|
550 |
len = stringLen[code]; |
|
551 |
bcopy(strings[code], to, len); |
|
552 |
to += len; |
|
0 | 553 |
|
1262 | 554 |
if (oldCode != -1) { |
555 |
/* add( string[oldcode] + first(string[code]) ) */ |
|
556 |
len = stringLen[oldCode] + 1; |
|
557 |
if (nScratch < len) { |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
558 |
newBuffer = (struct buffer *)malloc(sizeof(struct buffer)); |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
559 |
if (! newBuffer) goto out; |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
560 |
newBuffer->prev = scratchBuffer; |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
561 |
scratchBuffer = newBuffer; |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
562 |
scratchPtr = scratchBuffer->chars; |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
563 |
nScratch = sizeof(scratchBuffer->chars); |
1262 | 564 |
} |
565 |
stringLen[nextCode] = len; |
|
566 |
strings[nextCode] = scratchPtr; |
|
567 |
bcopy(strings[oldCode], scratchPtr, len-1); |
|
568 |
scratchPtr += len-1; |
|
569 |
*scratchPtr++ = strings[code][0]; |
|
570 |
nScratch -= len; |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
571 |
nextCode++; |
77 | 572 |
} |
573 |
} else { |
|
1262 | 574 |
if (oldCode == -1) { |
575 |
/* bad input */ |
|
576 |
ret = 0; |
|
577 |
goto out; |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
578 |
} |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
579 |
|
77 | 580 |
/* writeString(string[oldCode] + first(string[oldCode]) ) */ |
581 |
len = stringLen[oldCode]; |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
582 |
if ((to+len) >= toEnd) { |
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
583 |
fprintf(stderr, "ImageReader [warning]: LZW outBuffer overrun\n"); |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
584 |
ret = 0; |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
585 |
goto out; |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
586 |
} |
77 | 587 |
bcopy(strings[oldCode], to, len); |
588 |
to += len; |
|
589 |
*to++ = strings[oldCode][0]; |
|
0 | 590 |
|
77 | 591 |
/* add( string[oldcode] + first(string[oldCode]) ) */ |
592 |
len++; |
|
593 |
if (nScratch < len) { |
|
594 |
newBuffer = (struct buffer *)malloc(sizeof(struct buffer)); |
|
595 |
if (! newBuffer) goto out; |
|
596 |
newBuffer->prev = scratchBuffer; |
|
597 |
scratchBuffer = newBuffer; |
|
598 |
scratchPtr = scratchBuffer->chars; |
|
599 |
nScratch = sizeof(scratchBuffer->chars); |
|
600 |
} |
|
601 |
stringLen[nextCode] = len; |
|
602 |
strings[nextCode] = scratchPtr; |
|
603 |
bcopy(strings[oldCode], scratchPtr, len-1); |
|
604 |
scratchPtr += len-1; |
|
605 |
*scratchPtr++ = strings[oldCode][0]; |
|
606 |
nScratch -= len; |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
607 |
nextCode++; |
77 | 608 |
} |
609 |
oldCode = code; |
|
610 |
if (nextCode >= 511) |
|
611 |
if (nextCode == 511) { |
|
612 |
codeLen = 10; |
|
613 |
mask = 0x3FF; |
|
614 |
} else if (nextCode >= 1023) |
|
615 |
if (nextCode == 1023) { |
|
616 |
codeLen = 11; |
|
617 |
mask = 0x7FF; |
|
618 |
} else |
|
619 |
if (nextCode == 2047) { |
|
620 |
codeLen = 12; |
|
621 |
mask = 0xFFF; |
|
622 |
} |
|
623 |
} |
|
0 | 624 |
} |
12 | 625 |
out: ; |
0 | 626 |
/* free stuff */ |
627 |
while (scratchBuffer) { |
|
77 | 628 |
newBuffer = scratchBuffer; |
629 |
scratchBuffer = scratchBuffer->prev; |
|
630 |
free(newBuffer); |
|
0 | 631 |
} |
46 | 632 |
|
12 | 633 |
free(strings); |
634 |
free(stringLen); |
|
46 | 635 |
|
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
636 |
if (from > fromEnd) { |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
637 |
fprintf(stderr, "ImageReader [warning]: LZW inBuffer overrun\n"); |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
638 |
ret = 0; |
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
639 |
} |
1262 | 640 |
return ret; |
0 | 641 |
} |
642 |
||
643 |
/* |
|
644 |
* delta decoding (TIFF predictor = 2) |
|
645 |
*/ |
|
189 | 646 |
static |
77 | 647 |
__decodeDelta__(bytes, width, height) |
0 | 648 |
register unsigned char *bytes; |
649 |
{ |
|
77 | 650 |
register w; |
651 |
unsigned char r, g, b; |
|
0 | 652 |
|
77 | 653 |
while (height--) { |
654 |
r = g = b = 0; |
|
655 |
for (w = width; w; w--) { |
|
656 |
r += *bytes; |
|
657 |
*bytes++ = r; |
|
658 |
g += *bytes; |
|
659 |
*bytes++ = g; |
|
660 |
b += *bytes; |
|
661 |
*bytes++ = b; |
|
662 |
} |
|
663 |
} |
|
0 | 664 |
} |
665 |
||
46 | 666 |
/* |
667 |
* GIF decompression |
|
668 |
*/ |
|
189 | 669 |
static int |
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
670 |
__decodeGIF__(from, to, inCount, initialCodeLen, fromSize, toSize) |
0 | 671 |
unsigned char *from; |
672 |
unsigned char *to; |
|
673 |
{ |
|
674 |
register unsigned code; |
|
12 | 675 |
unsigned short *prefix; |
676 |
unsigned short *suffix; |
|
677 |
unsigned short *outCode; |
|
0 | 678 |
int outCount; |
679 |
unsigned maxCode, oldCode, fin, inCode, curCode; |
|
680 |
register unsigned bits; |
|
681 |
register int nBits, mask, shift; |
|
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
682 |
int ret = 1; |
0 | 683 |
int i; |
684 |
int len; |
|
685 |
int endCode, clearCode, freeCode; |
|
686 |
int codeLen = initialCodeLen; |
|
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
687 |
unsigned char *fromEnd, *toEnd; |
12 | 688 |
static int ranges[] = {0, 1, 2, 4, 8, 16, 32, 64, |
77 | 689 |
128, 256, 512, 1024, 2048 }; |
0 | 690 |
|
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
691 |
fromEnd = from + fromSize; |
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
692 |
toEnd = to + toSize; |
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
693 |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
694 |
if ((unsigned)codeLen > 12) { |
2646 | 695 |
fprintf(stderr, "ImageReader [warning]: GIF bad codelen (>12)\n"); |
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
696 |
return 0; |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
697 |
} |
12 | 698 |
prefix = (unsigned short *)malloc(sizeof(short) * 4096); |
699 |
if (! prefix) return 0; |
|
700 |
suffix = (unsigned short *)malloc(sizeof(short) * 4096); |
|
701 |
if (! suffix) { |
|
77 | 702 |
free(prefix); |
703 |
return 0; |
|
12 | 704 |
} |
705 |
outCode = (unsigned short *)malloc(sizeof(short) * 4096); |
|
706 |
if (! outCode) { |
|
77 | 707 |
free(prefix); |
708 |
free(suffix); |
|
709 |
return 0; |
|
12 | 710 |
} |
0 | 711 |
clearCode = ranges[codeLen]; /* 256 */ |
712 |
endCode = clearCode + 1; /* 257 */ |
|
713 |
freeCode = clearCode + 2; /* 258 */ |
|
714 |
maxCode = clearCode << 1; /* 512 */ |
|
715 |
outCount = 0; |
|
716 |
||
717 |
mask = maxCode - 1; /* 1FF */ |
|
718 |
nBits = 0; |
|
719 |
bits = 0; |
|
720 |
while (inCount) { |
|
77 | 721 |
/* fetch code */ |
722 |
while (nBits < codeLen) { |
|
723 |
bits = bits | (*from++ << nBits); |
|
724 |
inCount--; |
|
725 |
nBits += 8; |
|
726 |
} |
|
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
727 |
if (inCount <= 0) |
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
728 |
break; |
77 | 729 |
code = bits & mask; |
730 |
bits >>= codeLen; |
|
731 |
nBits -= codeLen; |
|
732 |
if (code == endCode) break; |
|
733 |
if (code == clearCode) { |
|
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
734 |
if (inCount <= 0) |
77 | 735 |
break; |
0 | 736 |
|
77 | 737 |
codeLen = initialCodeLen; |
738 |
maxCode = clearCode<<1; |
|
739 |
mask = maxCode - 1; |
|
740 |
freeCode = clearCode + 2; |
|
0 | 741 |
|
77 | 742 |
/* fetch code */ |
743 |
while (nBits < codeLen) { |
|
744 |
bits = bits | (*from++ << nBits); |
|
745 |
inCount--; |
|
746 |
nBits += 8; |
|
747 |
} |
|
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
748 |
if (inCount <= 0) |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
749 |
break; |
77 | 750 |
code = bits & mask; |
751 |
bits >>= codeLen; |
|
752 |
nBits -= codeLen; |
|
753 |
if (code == endCode) break; |
|
754 |
/* add to output */ |
|
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
755 |
if (to >= toEnd) { |
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
756 |
fprintf(stderr, "ImageReader [warning]: GIF outBuffer overrun\n"); |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
757 |
ret = 0; |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
758 |
break; |
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
759 |
} |
77 | 760 |
*to++ = code; |
761 |
oldCode = fin = curCode = code; |
|
762 |
} else { |
|
763 |
curCode = inCode = code; |
|
764 |
if (curCode >= freeCode) { |
|
765 |
curCode = oldCode; |
|
766 |
outCode[outCount++] = fin; |
|
767 |
} |
|
0 | 768 |
|
77 | 769 |
while (curCode >= clearCode) { |
770 |
if (outCount > 1024) { |
|
771 |
goto out; |
|
772 |
} |
|
2261 | 773 |
|
2646 | 774 |
/* debugging and protecting myself from overwrites */ |
775 |
if (curCode >= 4096) { |
|
2261 | 776 |
fprintf(stderr, "ImageReader [warning]: GIF corrupted input (code > 4095)\n"); |
777 |
ret = 0; |
|
778 |
break; |
|
2646 | 779 |
} |
77 | 780 |
outCode[outCount++] = suffix[curCode]; |
781 |
curCode = prefix[curCode]; |
|
782 |
} |
|
0 | 783 |
|
77 | 784 |
fin = curCode; |
785 |
outCode[outCount++] = fin; |
|
0 | 786 |
|
2014
bcae1c2c32e4
fixed destination-array boundary check (in GIF-decode)
Claus Gittinger <cg@exept.de>
parents:
1988
diff
changeset
|
787 |
#if 0 |
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
788 |
if ((to+outCount) >= toEnd) { |
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
789 |
fprintf(stderr, "ImageReader [warning]: GIF outBuffer overrun\n"); |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
790 |
ret = 0; |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
791 |
break; |
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
792 |
} |
2014
bcae1c2c32e4
fixed destination-array boundary check (in GIF-decode)
Claus Gittinger <cg@exept.de>
parents:
1988
diff
changeset
|
793 |
#endif |
bcae1c2c32e4
fixed destination-array boundary check (in GIF-decode)
Claus Gittinger <cg@exept.de>
parents:
1988
diff
changeset
|
794 |
for (i = outCount - 1; i >= 0; i--) { |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
795 |
if (to >= toEnd) { |
2014
bcae1c2c32e4
fixed destination-array boundary check (in GIF-decode)
Claus Gittinger <cg@exept.de>
parents:
1988
diff
changeset
|
796 |
fprintf(stderr, "ImageReader [warning]: GIF outBuffer overrun\n"); |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
797 |
ret = 0; |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
798 |
break; |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
799 |
} |
77 | 800 |
*to++ = outCode[i]; |
2014
bcae1c2c32e4
fixed destination-array boundary check (in GIF-decode)
Claus Gittinger <cg@exept.de>
parents:
1988
diff
changeset
|
801 |
} |
77 | 802 |
outCount = 0; |
0 | 803 |
|
2646 | 804 |
/* debugging and protecting myself from overwrites */ |
805 |
if (freeCode >= 4096) { |
|
806 |
fprintf(stderr, "ImageReader [warning]: GIF corrupted input (freeCode > 4095)\n"); |
|
807 |
ret = 0; |
|
808 |
break; |
|
809 |
} |
|
2261 | 810 |
|
77 | 811 |
prefix[freeCode] = oldCode; |
812 |
suffix[freeCode] = fin; |
|
813 |
oldCode = inCode; |
|
0 | 814 |
|
77 | 815 |
freeCode++; |
816 |
if (freeCode >= maxCode) { |
|
817 |
if (codeLen < 12) { |
|
818 |
codeLen++; |
|
819 |
maxCode *= 2; |
|
820 |
mask = (1 << codeLen) - 1; |
|
821 |
} |
|
822 |
} |
|
823 |
} |
|
0 | 824 |
} |
12 | 825 |
out: ; |
826 |
free(prefix); |
|
827 |
free(suffix); |
|
828 |
free(outCode); |
|
46 | 829 |
|
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
830 |
if (from > fromEnd) { |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
831 |
fprintf(stderr, "ImageReader [warning]: GIF inBuffer overrun\n"); |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
832 |
ret = 0; |
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
833 |
} |
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
834 |
return ret; |
0 | 835 |
} |
836 |
||
189 | 837 |
/* |
838 |
* BMP file read/decompression |
|
3243 | 839 |
* |
840 |
* depth1 to 8bit ... |
|
189 | 841 |
*/ |
842 |
static int |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
843 |
loadBMP1to8(w, h, fp, dest, szDest) |
189 | 844 |
int w, h; |
845 |
FILE *fp; |
|
1819 | 846 |
unsigned char *dest; |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
847 |
int szDest; |
189 | 848 |
{ |
849 |
int i,j,c,bitnum,padw; |
|
850 |
unsigned char *pp; |
|
851 |
||
852 |
c = 0; |
|
853 |
padw = ((w + 31)/32) * 32; /* 'w', padded to be a multiple of 32 */ |
|
854 |
||
855 |
for (i=h-1; i>=0; i--) { |
|
856 |
pp = dest + (i * w); |
|
857 |
for (j=bitnum=0; j<padw; j++,bitnum++) { |
|
858 |
if ((bitnum&7) == 0) { /* read the next byte */ |
|
859 |
c = getc(fp); |
|
3243 | 860 |
if (c == EOF) { |
861 |
if (@global(InfoPrinting) == true) { |
|
862 |
fprintf(stderr, "ImageReader [warning]: premature EOF\n"); |
|
863 |
} |
|
864 |
return 0; |
|
865 |
} |
|
189 | 866 |
bitnum = 0; |
867 |
} |
|
868 |
if (j<w) { |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
869 |
if (pp >= (dest+szDest)) { |
3243 | 870 |
if (@global(InfoPrinting) == true) { |
871 |
fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n"); |
|
872 |
} |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
873 |
return 0; |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
874 |
} |
189 | 875 |
*pp++ = (c & 0x80) ? 1 : 0; |
876 |
c <<= 1; |
|
877 |
} |
|
878 |
} |
|
879 |
if (ferror(fp)) return 0; |
|
880 |
} |
|
881 |
if (ferror(fp)) return 0; |
|
882 |
return 1; |
|
883 |
} |
|
884 |
||
3243 | 885 |
/* |
886 |
* depth2 to 8bit ... |
|
887 |
*/ |
|
189 | 888 |
static int |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
889 |
loadBMP2to8(w, h, fp, dest, szDest) |
189 | 890 |
int w, h; |
891 |
FILE *fp; |
|
1819 | 892 |
unsigned char *dest; |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
893 |
int szDest; |
189 | 894 |
{ |
895 |
int i,j,c,bitnum,padw; |
|
896 |
unsigned char *pp; |
|
897 |
||
898 |
c = 0; |
|
899 |
padw = ((w + 31)/32) * 32; /* 'w', padded to be a multiple of 32 */ |
|
900 |
||
901 |
for (i=h-1; i>=0; i--) { |
|
902 |
pp = dest + (i * w); |
|
903 |
for (j=bitnum=0; j<padw; j++,bitnum++) { |
|
904 |
if ((bitnum&3) == 0) { /* read the next byte */ |
|
905 |
c = getc(fp); |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
906 |
if (c == EOF) return 0; |
189 | 907 |
bitnum = 0; |
908 |
} |
|
909 |
if (j<w) { |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
910 |
if (pp >= (dest+szDest)) { |
3243 | 911 |
if (@global(InfoPrinting) == true) { |
912 |
fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n"); |
|
913 |
} |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
914 |
return 0; |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
915 |
} |
189 | 916 |
*pp++ = (c & 0xC0) >> 6; |
917 |
c <<= 2; |
|
918 |
} |
|
919 |
} |
|
920 |
if (ferror(fp)) return 0; |
|
921 |
} |
|
922 |
if (ferror(fp)) return 0; |
|
923 |
return 1; |
|
924 |
} |
|
925 |
||
3243 | 926 |
/* |
927 |
* depth4 to 8bit ... |
|
928 |
*/ |
|
189 | 929 |
static int |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
930 |
loadBMP4to8(w, h, comp, fp, dest, szDest) |
189 | 931 |
int w, h; |
932 |
FILE *fp; |
|
1819 | 933 |
unsigned char *dest; |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
934 |
int szDest; |
189 | 935 |
{ |
936 |
int i,j,c,c1,padw,x,y,nybnum; |
|
937 |
unsigned char *pp; |
|
938 |
||
939 |
c = c1 = 0; |
|
940 |
||
941 |
if (comp == 0) { /* read uncompressed data */ |
|
942 |
padw = ((w + 7)/8) * 8; /* 'w' padded to a multiple of 8pix (32 bits) */ |
|
943 |
||
944 |
for (i=h-1; i>=0; i--) { |
|
945 |
pp = dest + (i * w); |
|
946 |
||
947 |
for (j=nybnum=0; j<padw; j++,nybnum++) { |
|
948 |
if ((nybnum & 1) == 0) { /* read next byte */ |
|
949 |
c = getc(fp); |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
950 |
if (c == EOF) return 0; |
189 | 951 |
nybnum = 0; |
952 |
} |
|
953 |
||
954 |
if (j<w) { |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
955 |
if (pp >= (dest+szDest)) { |
3243 | 956 |
if (@global(InfoPrinting) == true) { |
957 |
fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n"); |
|
958 |
} |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
959 |
return 0; |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
960 |
} |
189 | 961 |
*pp++ = (c & 0xf0) >> 4; |
962 |
c <<= 4; |
|
963 |
} |
|
964 |
} |
|
965 |
if (ferror(fp)) return 0; |
|
966 |
} |
|
3243 | 967 |
if (ferror(fp)) return 0; |
968 |
return 1; |
|
969 |
} |
|
970 |
if (comp == 2) { /* read RLE4 compressed data */ |
|
971 |
x = y = 0; |
|
972 |
pp = dest + x + (h-y-1)*w; |
|
189 | 973 |
|
3243 | 974 |
while (y<h) { |
975 |
c = getc(fp); if (c == EOF) return 0; |
|
189 | 976 |
|
3243 | 977 |
if (c) { /* encoded mode */ |
978 |
c &= 0xFF; |
|
979 |
c1 = getc(fp); |
|
980 |
if (c1 == EOF) return 0; |
|
981 |
for (i=0; i<c; i++,x++,pp++) { |
|
982 |
if (pp >= (dest+szDest)) { |
|
983 |
if (@global(InfoPrinting) == true) { |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
984 |
fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n"); |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
985 |
} |
3243 | 986 |
return 0; |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
987 |
} |
3243 | 988 |
*pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); |
989 |
} |
|
990 |
} else { |
|
991 |
/* c==0x00 : escape codes */ |
|
992 |
c = getc(fp); |
|
993 |
if (c == EOF) return 0; |
|
189 | 994 |
|
3243 | 995 |
if (c == 0x00) { /* end of line */ |
996 |
x=0; y++; pp = dest + x + (h-y-1)*w; |
|
997 |
} else |
|
998 |
if (c == 0x01) break; /* end of pic8 */ |
|
189 | 999 |
|
3243 | 1000 |
else if (c == 0x02) { /* delta */ |
1001 |
c = getc(fp); |
|
1002 |
if (c == EOF) return 0; |
|
1003 |
x += (c & 0xFF); |
|
1004 |
c = getc(fp); |
|
1005 |
if (c == EOF) return 0; |
|
1006 |
y += (c & 0xFF); |
|
1007 |
pp = dest + x + (h-y-1)*w; |
|
1008 |
} else { /* absolute mode */ |
|
1009 |
c &= 0xFF; |
|
1010 |
for (i=0; i<c; i++, x++, pp++) { |
|
1011 |
if ((i&1) == 0) { |
|
1012 |
c1 = getc(fp); |
|
1013 |
if (c1 == EOF) return 0; |
|
1014 |
} |
|
1015 |
if (pp >= (dest+szDest)) { |
|
1016 |
if (@global(InfoPrinting) == true) { |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1017 |
fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n"); |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1018 |
} |
3243 | 1019 |
return 0; |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1020 |
} |
3243 | 1021 |
*pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1022 |
} |
3243 | 1023 |
|
1024 |
if (((c&3)==1) || ((c&3)==2)) getc(fp); /* read pad byte */ |
|
1025 |
} |
|
1026 |
} /* escape processing */ |
|
1027 |
if (ferror(fp)) return 0; |
|
1028 |
} /* while */ |
|
1029 |
if (ferror(fp)) return 0; |
|
1030 |
return 1; |
|
189 | 1031 |
} |
3243 | 1032 |
|
1033 |
return 0; /* bad comp */ |
|
189 | 1034 |
} |
1035 |
||
3243 | 1036 |
/* |
1037 |
* depth8 to 8bit ... |
|
1038 |
*/ |
|
189 | 1039 |
static int |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1040 |
loadBMP8(w, h, comp, fp, dest, szDest) |
189 | 1041 |
int w, h; |
1042 |
FILE *fp; |
|
1819 | 1043 |
unsigned char *dest; |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1044 |
int szDest; |
189 | 1045 |
{ |
1046 |
int i,j,c,c1,padw,x,y; |
|
1047 |
unsigned char *pp; |
|
1048 |
||
1049 |
if (comp == 0) { /* uncompressed data */ |
|
1050 |
padw = ((w + 3)/4) * 4; |
|
1051 |
||
1052 |
for (i=h-1; i>=0; i--) { |
|
1053 |
pp = dest + (i * w); |
|
1054 |
||
1055 |
for (j=0; j<padw; j++) { |
|
1056 |
c = getc(fp); |
|
3243 | 1057 |
if (c==EOF) { |
1058 |
if (@global(InfoPrinting) == true) { |
|
1059 |
fprintf(stderr, "ImageReader [warning]: BMP premature EOF [%d]\n", __LINE__); |
|
1060 |
fprintf(stderr, "h=%d i=%d w=%d j=%d padw=%d\n", h, i, w, j, padw); |
|
1061 |
} |
|
1062 |
return 0; |
|
1063 |
} |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1064 |
if (j<w) { |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1065 |
if (pp >= (dest+szDest)) { |
3243 | 1066 |
if (@global(InfoPrinting) == true) { |
1067 |
fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n"); |
|
1068 |
} |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1069 |
return 0; |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1070 |
} |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1071 |
*pp++ = c; |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1072 |
} |
189 | 1073 |
} |
3243 | 1074 |
if (ferror(fp)) { |
1075 |
if (@global(InfoPrinting) == true) { |
|
1076 |
fprintf(stderr, "ImageReader [warning]: BMP ferror set\n"); |
|
1077 |
} |
|
1078 |
return 0; |
|
1079 |
} |
|
1080 |
} |
|
1081 |
if (ferror(fp)) { |
|
1082 |
if (@global(InfoPrinting) == true) { |
|
1083 |
fprintf(stderr, "ImageReader [warning]: BMP ferror set at end\n"); |
|
1084 |
} |
|
1085 |
return 0; |
|
189 | 1086 |
} |
3243 | 1087 |
return 1; |
1088 |
} |
|
1089 |
if (comp == 1) { /* RLE8 compressed */ |
|
1090 |
x = y = 0; |
|
1091 |
pp = dest + x + (h-y-1)*w; |
|
189 | 1092 |
|
3243 | 1093 |
while (y<h) { |
1094 |
c = getc(fp); |
|
1095 |
if (c == EOF) { |
|
1096 |
if (@global(InfoPrinting) == true) { |
|
1097 |
fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__); |
|
1098 |
} |
|
1099 |
return 0; |
|
1100 |
} |
|
1101 |
if (c) { /* encoded mode */ |
|
1102 |
c1 = getc(fp); |
|
1103 |
if (c1 == EOF) { |
|
1104 |
if (@global(InfoPrinting) == true) { |
|
1105 |
fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__); |
|
1106 |
} |
|
1107 |
return 0; |
|
1108 |
} |
|
1109 |
c &= 0xFF; |
|
1110 |
||
1111 |
for (i=0; i<c; i++,x++,pp++) { |
|
1112 |
if (pp >= (dest+szDest)) { |
|
1113 |
if (@global(InfoPrinting) == true) { |
|
1114 |
fprintf(stderr, "ImageReader [warning]: BMP/RLE8 outBuffer overrun\n"); |
|
1115 |
} |
|
1116 |
return 0; |
|
1117 |
} |
|
1118 |
*pp = c1; |
|
1119 |
} |
|
1120 |
} else { |
|
1121 |
/* c==0x00 : escape codes */ |
|
189 | 1122 |
c = getc(fp); |
3243 | 1123 |
if (c == EOF) { |
1124 |
if (@global(InfoPrinting) == true) { |
|
1125 |
fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__); |
|
1126 |
} |
|
1127 |
return 0; |
|
1128 |
} |
|
1129 |
if (c == 0x00) { /* end of line */ |
|
1130 |
x=0; y++; pp = dest + x + (h-y-1)*w; |
|
1131 |
} else |
|
1132 |
if (c == 0x01) break; /* end of pic8 */ |
|
1133 |
else if (c == 0x02) { /* delta */ |
|
1134 |
c = getc(fp); |
|
1135 |
if (c == EOF) { |
|
1136 |
if (@global(InfoPrinting) == true) { |
|
1137 |
fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__); |
|
1138 |
} |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1139 |
return 0; |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1140 |
} |
3243 | 1141 |
x += (c & 0xFF); |
1142 |
c = getc(fp); |
|
1143 |
if (c == EOF) { |
|
1144 |
if (@global(InfoPrinting) == true) { |
|
1145 |
fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__); |
|
1146 |
} |
|
1147 |
return 0; |
|
1148 |
} |
|
1149 |
y += (c & 0xFF); |
|
1150 |
pp = dest + x + (h-y-1)*w; |
|
1151 |
} else { /* absolute mode */ |
|
1152 |
c &= 0xFF; |
|
1153 |
for (i=0; i<c; i++, x++, pp++) { |
|
1154 |
c1 = getc(fp); |
|
1155 |
if (c1 == EOF) return 0; |
|
1156 |
if (pp >= (dest+szDest)) { |
|
1157 |
if (@global(InfoPrinting) == true) { |
|
1158 |
fprintf(stderr, "ImageReader [warning]: BMP/RLE8 outBuffer overrun\n"); |
|
1159 |
} |
|
1160 |
return 0; |
|
1161 |
} |
|
1162 |
*pp = c1; |
|
1163 |
} |
|
189 | 1164 |
|
3243 | 1165 |
if (c & 1) getc(fp); /* odd length run: read an extra pad byte */ |
1166 |
} |
|
1167 |
} |
|
1168 |
if (ferror(fp)) { |
|
1169 |
if (@global(InfoPrinting) == true) { |
|
1170 |
fprintf(stderr, "ImageReader [warning]: BMP/RLE8 ferror set\n"); |
|
1171 |
} |
|
1172 |
return 0; |
|
1173 |
} |
|
1174 |
} |
|
1175 |
if (ferror(fp)) { |
|
1176 |
if (@global(InfoPrinting) == true) { |
|
1177 |
fprintf(stderr, "ImageReader [warning]: BMP/RLE8 ferror set at end\n"); |
|
1178 |
} |
|
189 | 1179 |
return 0; |
1180 |
} |
|
3243 | 1181 |
return 1; |
189 | 1182 |
} |
3243 | 1183 |
if (@global(InfoPrinting) == true) { |
1184 |
fprintf(stderr, "ImageReader [warning]: BMP unhandled compression: %d\n", comp); |
|
1185 |
} |
|
1186 |
return 0; |
|
189 | 1187 |
} |
1188 |
||
0 | 1189 |
%} |
77 | 1190 |
! ! |
105 | 1191 |
|
1167 | 1192 |
!ImageReader class methodsFor:'documentation'! |
105 | 1193 |
|
1194 |
copyright |
|
1195 |
" |
|
1196 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
1197 |
All Rights Reserved |
|
1198 |
||
1199 |
This software is furnished under a license and may be used |
|
1200 |
only in accordance with the terms of that license and with the |
|
1201 |
inclusion of the above copyright notice. This software may not |
|
1202 |
be provided or otherwise made available to, or used by, any |
|
1203 |
other person. No title to or ownership of the software is |
|
1204 |
hereby transferred. |
|
1205 |
" |
|
1206 |
! |
|
1207 |
||
1208 |
documentation |
|
1209 |
" |
|
1210 |
Abstract class to provide common functions for image-readers |
|
1211 |
(i.e. TIFFReader, GIFReader etc.) |
|
1212 |
||
1213 |
ImageReaders are created temporary to read an image from a stream. |
|
618 | 1214 |
Normally, they are not directly used - instead, the image class is |
1215 |
asked to read some file, and return an instance for it: |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1216 |
Image fromFile:<someFileName> |
618 | 1217 |
The Image class will guess the images format and forward the task to |
1218 |
some concrete ImageReaderClass. |
|
1219 |
If that class finds, that the files format is incorrect, other readers |
|
1220 |
are tried until some finds the files format acceptable. |
|
1221 |
||
1222 |
Image readers read the stream and collect all relevant information internally. |
|
118 | 1223 |
Once done with reading, the actual image object is created and |
1224 |
data filled in from the imageReaders collected info. |
|
105 | 1225 |
|
618 | 1226 |
See the implementation of #fromStream: in concrete subclasses. |
105 | 1227 |
The public interfaces are: |
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1228 |
<ConcreteReaderClass> fromFile:aFilename |
105 | 1229 |
or: |
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1230 |
<ConcreteReaderClass> fromStream:aStream |
105 | 1231 |
|
121 | 1232 |
If you add a new reader, dont forget to add the method #isValidImageFile: |
618 | 1233 |
which should return true, if this reader supports reading a given file. |
1234 |
If your new reader class supports writing files, dont forget to add |
|
1235 |
#canRepresent:anImage and return true from this method. |
|
593 | 1236 |
|
1237 |
[See also:] |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1238 |
Image Icon Form |
611 | 1239 |
|
1240 |
[author:] |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1241 |
Claus Gittinger |
105 | 1242 |
" |
1243 |
! ! |
|
1244 |
||
1358 | 1245 |
!ImageReader class methodsFor:'instance creation'! |
1246 |
||
1247 |
new |
|
1248 |
^ self basicNew initialize |
|
1249 |
||
1250 |
"Created: 18.2.1997 / 17:08:45 / cg" |
|
1251 |
! ! |
|
1252 |
||
1167 | 1253 |
!ImageReader class methodsFor:'cleanup'! |
105 | 1254 |
|
285 | 1255 |
lowSpaceCleanup |
1256 |
"cleanup things we do not need" |
|
118 | 1257 |
|
285 | 1258 |
ReverseBits := nil |
105 | 1259 |
! ! |
1260 |
||
1167 | 1261 |
!ImageReader class methodsFor:'constants'! |
105 | 1262 |
|
1263 |
reverseBits |
|
1264 |
"return a table filled with bit reverse information. |
|
1265 |
To convert from msbit-first to lsbit-first bytes, use |
|
1266 |
the value as index into the table, retrieving the reverse |
|
1267 |
value. Since indexing must start at 1, use (value + 1) as |
|
1268 |
index." |
|
1269 |
||
1270 |
|val "{ Class: SmallInteger }" | |
|
1271 |
||
1272 |
ReverseBits isNil ifTrue:[ |
|
153 | 1273 |
ReverseBits := ByteArray uninitializedNew:256. |
105 | 1274 |
0 to:255 do:[:i | |
1275 |
val := 0. |
|
1276 |
(i bitTest:16r01) ifTrue:[val := val bitOr:16r80]. |
|
1277 |
(i bitTest:16r02) ifTrue:[val := val bitOr:16r40]. |
|
1278 |
(i bitTest:16r04) ifTrue:[val := val bitOr:16r20]. |
|
1279 |
(i bitTest:16r08) ifTrue:[val := val bitOr:16r10]. |
|
1280 |
(i bitTest:16r10) ifTrue:[val := val bitOr:16r08]. |
|
1281 |
(i bitTest:16r20) ifTrue:[val := val bitOr:16r04]. |
|
1282 |
(i bitTest:16r40) ifTrue:[val := val bitOr:16r02]. |
|
1283 |
(i bitTest:16r80) ifTrue:[val := val bitOr:16r01]. |
|
1284 |
ReverseBits at:(i + 1) put:val |
|
1285 |
] |
|
1286 |
]. |
|
1287 |
^ ReverseBits |
|
1288 |
! ! |
|
1289 |
||
1167 | 1290 |
!ImageReader class methodsFor:'decompression support'! |
105 | 1291 |
|
285 | 1292 |
decodeDelta:step in:data width:width height:height |
1293 |
"perform NeXT special predictor delta decoding inplace in data. |
|
1294 |
Calls primitive c function for speed" |
|
1295 |
||
1296 |
(step ~~ 3) ifTrue:[ |
|
1297 |
^ self error:'only rgb pictures supported' |
|
1298 |
]. |
|
105 | 1299 |
|
285 | 1300 |
%{ |
1301 |
if (__isByteArray(data) |
|
1302 |
&& __bothSmallInteger(width, height)) { |
|
2043
7b8d741538a0
single underscore-macros -> double underscores
Claus Gittinger <cg@exept.de>
parents:
2039
diff
changeset
|
1303 |
__decodeDelta__(__ByteArrayInstPtr(data)->ba_element, |
2044
9ea84b4ba249
more single-underline macros
Claus Gittinger <cg@exept.de>
parents:
2043
diff
changeset
|
1304 |
__intVal(width), __intVal(height)); |
285 | 1305 |
RETURN ( self ); |
1306 |
} |
|
1307 |
%} |
|
1308 |
. |
|
1309 |
self primitiveFailed |
|
1310 |
! |
|
105 | 1311 |
|
1312 |
decompressCCITT3From:srcBytes into:dstBytes startingAt:offset count:count |
|
1313 |
"decompress CCITT Group 3 compressed image data. |
|
1314 |
count bytes from srcBytes are decompressed into dstBytes. |
|
1315 |
Calls primitive c function for speed" |
|
1316 |
%{ |
|
1317 |
if (__isByteArray(srcBytes) |
|
1318 |
&& __isByteArray(dstBytes) |
|
1319 |
&& __bothSmallInteger(offset, count)) { |
|
2043
7b8d741538a0
single underscore-macros -> double underscores
Claus Gittinger <cg@exept.de>
parents:
2039
diff
changeset
|
1320 |
if (__decodeCCITTgroup3__(__ByteArrayInstPtr(srcBytes)->ba_element, |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1321 |
__ByteArrayInstPtr(dstBytes)->ba_element |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1322 |
+ __intVal(offset) - 1, |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1323 |
__intVal(count))) { |
105 | 1324 |
RETURN ( self ); |
1325 |
} |
|
1326 |
} |
|
1327 |
%} |
|
1328 |
. |
|
1329 |
self primitiveFailed |
|
1330 |
! |
|
1331 |
||
1332 |
decompressGIFFrom:srcBytes count:count into:dstBytes startingAt:offset codeLen:codeLen |
|
1333 |
"decompress GIF compressed image data. |
|
1334 |
count bytes from srcBytes are decompressed into dstBytes. |
|
1335 |
Calls primitive c function for speed" |
|
1336 |
%{ |
|
1337 |
if (__isByteArray(srcBytes) |
|
1338 |
&& __isByteArray(dstBytes) |
|
1339 |
&& __bothSmallInteger(codeLen, offset) |
|
1340 |
&& __isSmallInteger(count)) { |
|
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
1341 |
if (__decodeGIF__(__ByteArrayInstPtr(srcBytes)->ba_element, |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1342 |
__ByteArrayInstPtr(dstBytes)->ba_element |
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
1343 |
+__intVal(offset) - 1, |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1344 |
__intVal(count), |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1345 |
__intVal(codeLen), |
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
1346 |
__byteArraySize(srcBytes), |
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
1347 |
__byteArraySize(dstBytes) |
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
1348 |
)) { |
105 | 1349 |
RETURN ( self ); |
1350 |
} |
|
1351 |
} |
|
1352 |
%} |
|
1353 |
. |
|
1354 |
self primitiveFailed |
|
1355 |
! |
|
1356 |
||
285 | 1357 |
decompressLZWFrom:srcBytes count:count into:dstBytes startingAt:offset |
1358 |
"decompress LZW (tiff) compressed image data. |
|
1359 |
count bytes from srcBytes are decompressed into dstBytes. |
|
105 | 1360 |
Calls primitive c function for speed" |
1361 |
%{ |
|
285 | 1362 |
if (__isByteArray(srcBytes) |
1363 |
&& __isByteArray(dstBytes) |
|
1364 |
&& __bothSmallInteger(offset, count)) { |
|
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
1365 |
if (__decodeLZW__(__ByteArrayInstPtr(srcBytes)->ba_element, |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1366 |
__ByteArrayInstPtr(dstBytes)->ba_element |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1367 |
+ __intVal(offset) - 1, |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1368 |
__intVal(count), |
1988
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
1369 |
__byteArraySize(srcBytes), |
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
1370 |
__byteArraySize(dstBytes) |
528dbff6ca26
do not overwrite provided output buffer when decompressing
Claus Gittinger <cg@exept.de>
parents:
1983
diff
changeset
|
1371 |
)) { |
285 | 1372 |
RETURN ( self ); |
1373 |
} |
|
105 | 1374 |
} |
1375 |
%} |
|
1376 |
. |
|
1377 |
self primitiveFailed |
|
189 | 1378 |
! |
1379 |
||
1648
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1380 |
decompressRLEFrom:srcBytes at:srcStartIndex into:dstBytes at:dstStartIndex increment:dstIncrement |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1381 |
"common helper to expand RLE encoded data" |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1382 |
|
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1383 |
"/ the code below expands common RLE formats, where the high bit |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1384 |
"/ controls how the count in the low 7 bits is to be interpreted: |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1385 |
"/ 1 means: count verbatim bytes follow; |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1386 |
"/ 0 means: repeat the next byte count times. |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1387 |
"/ Decoding stops when a 0-count is encountered. |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1388 |
"/ srcBytes and dstBytes should not overlap. |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1389 |
"/ dstIncrement controls the stepping in the destination (usually: 1) |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1390 |
"/ |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1391 |
"/ this may be recoded in C in the future |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1392 |
|
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1393 |
|srcIdx "{Class: SmallInteger }" |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1394 |
dstIdx "{Class: SmallInteger }" |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1395 |
pixel "{Class: SmallInteger }" |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1396 |
count "{Class: SmallInteger }"| |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1397 |
|
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1398 |
srcIdx := srcStartIndex. |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1399 |
dstIdx := dstStartIndex. |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1400 |
[true] whileTrue:[ |
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1401 |
pixel := srcBytes at:srcIdx. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1402 |
count := pixel bitAnd:16r7F. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1403 |
count == 0 ifTrue:[ |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1404 |
^ self |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1405 |
]. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1406 |
srcIdx := srcIdx + 1. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1407 |
(pixel bitAnd:16r80) == 0 ifTrue:[ |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1408 |
"/ run bytes |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1409 |
pixel := srcBytes at:srcIdx. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1410 |
srcIdx := srcIdx + 1. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1411 |
dstIncrement == 1 ifTrue:[ |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1412 |
dstBytes from:dstIdx to:(dstIdx+count-1) put:pixel. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1413 |
dstIdx := dstIdx + count. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1414 |
] ifFalse:[ |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1415 |
1 to:count do:[:c | |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1416 |
dstBytes at:dstIdx put:pixel. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1417 |
dstIdx := dstIdx + dstIncrement. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1418 |
] |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1419 |
] |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1420 |
] ifFalse:[ |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1421 |
"/ verbatim bytes |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1422 |
dstIncrement == 1 ifTrue:[ |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1423 |
dstBytes replaceFrom:dstIdx to:(dstIdx+count-1) with:srcBytes startingAt:srcIdx. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1424 |
srcIdx := srcIdx + count. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1425 |
dstIdx := dstIdx + count. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1426 |
] ifFalse:[ |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1427 |
1 to:count do:[:c | |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1428 |
dstBytes at:dstIdx put:(srcBytes at:srcIdx). |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1429 |
srcIdx := srcIdx + 1. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1430 |
dstIdx := dstIdx + dstIncrement. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1431 |
] |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1432 |
] |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1433 |
] |
1648
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1434 |
]. |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1435 |
|
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1436 |
"Modified: 23.4.1997 / 18:54:05 / cg" |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1437 |
! |
cbb3f0d60617
prepare for RLE decompression code reuse
Claus Gittinger <cg@exept.de>
parents:
1633
diff
changeset
|
1438 |
|
189 | 1439 |
loadBMP1to8Width:width height:height from:aStream into:aByteArray |
583 | 1440 |
"load bmp-1 bit per pixel imagedata. A helper for BMP image reader. |
1441 |
Calls primitive c function for speed" |
|
1442 |
||
189 | 1443 |
|f| |
1444 |
||
1445 |
aStream isExternalStream ifFalse:[^ false]. |
|
1446 |
f := aStream filePointer. |
|
1447 |
f isNil ifTrue:[^ false]. |
|
1448 |
%{ |
|
1449 |
if (! (__bothSmallInteger(width, height) |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1450 |
&& __isByteArray(aByteArray))) { |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1451 |
RETURN (false); |
189 | 1452 |
} |
1453 |
if (loadBMP1to8(__intVal(width), __intVal(height), |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1454 |
__FILEVal(f), |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1455 |
__ByteArrayInstPtr(aByteArray)->ba_element, |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1456 |
__byteArraySize(aByteArray) )) { |
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1457 |
RETURN (true); |
189 | 1458 |
} |
1459 |
RETURN (false); |
|
1460 |
%} |
|
583 | 1461 |
|
1462 |
"Modified: 22.4.1996 / 19:14:32 / cg" |
|
189 | 1463 |
! |
1464 |
||
1465 |
loadBMP2to8Width:width height:height from:aStream into:aByteArray |
|
583 | 1466 |
"load bmp-2 bit per pixel imagedata. A helper for BMP image reader. |
1467 |
Calls primitive c function for speed" |
|
1468 |
||
189 | 1469 |
|f| |
1470 |
||
1471 |
aStream isExternalStream ifFalse:[^ false]. |
|
1472 |
f := aStream filePointer. |
|
1473 |
f isNil ifTrue:[^ false]. |
|
1474 |
%{ |
|
1475 |
if (! (__bothSmallInteger(width, height) |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1476 |
&& __isByteArray(aByteArray))) { |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1477 |
RETURN (false); |
189 | 1478 |
} |
1479 |
if (loadBMP2to8(__intVal(width), __intVal(height), |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1480 |
__FILEVal(f), |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1481 |
__ByteArrayInstPtr(aByteArray)->ba_element, |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1482 |
__byteArraySize(aByteArray) )) { |
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1483 |
RETURN (true); |
189 | 1484 |
} |
1485 |
RETURN (false); |
|
1486 |
%} |
|
583 | 1487 |
|
1488 |
"Modified: 22.4.1996 / 19:14:40 / cg" |
|
189 | 1489 |
! |
1490 |
||
1491 |
loadBMP4to8Width:width height:height compression:compression from:aStream into:aByteArray |
|
583 | 1492 |
"load bmp-4 bit per pixel imagedata. A helper for BMP image reader. |
1493 |
Calls primitive c function for speed" |
|
1494 |
||
189 | 1495 |
|f| |
1496 |
||
1497 |
aStream isExternalStream ifFalse:[^ false]. |
|
1498 |
f := aStream filePointer. |
|
1499 |
f isNil ifTrue:[^ false]. |
|
1500 |
%{ |
|
1501 |
if (! (__bothSmallInteger(width, height) |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1502 |
&& __isSmallInteger(compression) |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1503 |
&& __isByteArray(aByteArray))) { |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1504 |
RETURN (false); |
189 | 1505 |
} |
1506 |
if (loadBMP4to8(__intVal(width), __intVal(height), __intVal(compression), |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1507 |
__FILEVal(f), |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1508 |
__ByteArrayInstPtr(aByteArray)->ba_element, |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1509 |
__byteArraySize(aByteArray) )) { |
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1510 |
RETURN (true); |
189 | 1511 |
} |
1512 |
RETURN (false); |
|
1513 |
%} |
|
583 | 1514 |
|
1515 |
"Modified: 22.4.1996 / 19:14:46 / cg" |
|
285 | 1516 |
! |
1517 |
||
1518 |
loadBMP8Width:width height:height compression:compression from:aStream into:aByteArray |
|
583 | 1519 |
"load bmp-8 bit per pixel imagedata. A helper for BMP image reader. |
1520 |
Calls primitive c function for speed" |
|
1521 |
||
1522 |
|f| |
|
285 | 1523 |
|
1524 |
aStream isExternalStream ifFalse:[^ false]. |
|
1525 |
f := aStream filePointer. |
|
1526 |
f isNil ifTrue:[^ false]. |
|
1527 |
%{ |
|
1528 |
if (! (__bothSmallInteger(width, height) |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1529 |
&& __isSmallInteger(compression) |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1530 |
&& __isByteArray(aByteArray))) { |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1531 |
RETURN (false); |
285 | 1532 |
} |
1533 |
if (loadBMP8(__intVal(width), __intVal(height), __intVal(compression), |
|
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1534 |
__FILEVal(f), |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1535 |
__ByteArrayInstPtr(aByteArray)->ba_element, |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1536 |
__byteArraySize(aByteArray) )) { |
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1537 |
RETURN (true); |
285 | 1538 |
} |
1539 |
RETURN (false); |
|
1540 |
%} |
|
583 | 1541 |
|
1542 |
"Modified: 22.4.1996 / 19:14:54 / cg" |
|
3835 | 1543 |
! |
1544 |
||
1545 |
swap:nBytes bytesFromRGB_to_BGR_in:data |
|
1546 |
"swap bytes from RGB into BGR order. |
|
1547 |
The argument is a pixel data buffer (byteArray)" |
|
1548 |
||
3836 | 1549 |
|t| |
1550 |
||
3835 | 1551 |
%{ /* OPTIONAL */ |
1552 |
if (__isByteArray(data) && __isSmallInteger(nBytes)) { |
|
4033 | 1553 |
int __nBytes = __intVal(nBytes); |
1554 |
int __dataSize = __byteArraySize(data); |
|
3835 | 1555 |
|
4033 | 1556 |
if (__nBytes <= __dataSize) { |
1557 |
int __lastIndex = __nBytes - 2; |
|
1558 |
unsigned char *__cp = __ByteArrayInstPtr(data)->ba_element; |
|
1559 |
int __i; |
|
1560 |
unsigned char __t; |
|
3835 | 1561 |
|
4033 | 1562 |
for (__i=0; __i<__lastIndex; __i+=3, __cp+=3) { |
1563 |
__t = __cp[0]; |
|
1564 |
__cp[0] = __cp[2]; |
|
1565 |
__cp[2] = __t; |
|
1566 |
} |
|
1567 |
RETURN (true); |
|
1568 |
} |
|
3835 | 1569 |
} |
1570 |
%}. |
|
3836 | 1571 |
1 to:(nBytes - 2) by:3 do:[:i | |
4033 | 1572 |
t := data at:i. |
1573 |
data at:i put:(data at:i+2). |
|
1574 |
data at:i+2 put:t |
|
3836 | 1575 |
]. |
3835 | 1576 |
|
1577 |
" |
|
1578 |
|bytes| |
|
1579 |
||
1580 |
bytes := #[ 0 1 2 3 4 5 6 7 8 9 ]. |
|
1581 |
self swap:6 bytesFromRGB_to_BGR_in:bytes. |
|
1582 |
bytes. |
|
1583 |
" |
|
1584 |
" |
|
1585 |
|bytes| |
|
1586 |
||
1587 |
bytes := ByteArray new:1000000. |
|
1588 |
bytes replaceFrom:1 with:#[ 0 1 2 3 4 5 6 7 8 9 10 11 12]. |
|
1589 |
Time millisecondsToRun:[ |
|
4033 | 1590 |
self swap:1000000 bytesFromRGB_to_BGR_in:bytes. |
3835 | 1591 |
]. |
1592 |
bytes copyTo:10. |
|
1593 |
" |
|
285 | 1594 |
! ! |
1595 |
||
1167 | 1596 |
!ImageReader class methodsFor:'i/o support'! |
285 | 1597 |
|
1598 |
streamReadingFile:aFilename |
|
1599 |
"return a stream to read aFilename. |
|
1600 |
If the filename ends with '.Z' or '.gz', return a stream |
|
1601 |
to a pipe for the uncompressor. Otherwise, return a stream to read |
|
1602 |
the file directly." |
|
1603 |
||
1300 | 1604 |
|inStream name fn| |
285 | 1605 |
|
1606 |
name := aFilename asString. |
|
1607 |
((name endsWith:'.Z') or:[name endsWith:'.gz']) ifTrue:[ |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1608 |
fn := Smalltalk getSystemFileName:name. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1609 |
inStream := PipeStream readingFrom:'gunzip < ' , fn. |
285 | 1610 |
] ifFalse:[ |
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1611 |
inStream := Smalltalk systemFileStreamFor:aFilename. |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1612 |
inStream isNil ifTrue:[ |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1613 |
inStream := Smalltalk bitmapFileStreamFor:aFilename |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1614 |
] |
285 | 1615 |
]. |
1616 |
inStream isNil ifTrue:[ |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1617 |
'ImageReader [warning]: open error on: ' infoPrint. aFilename infoPrintCR. |
285 | 1618 |
]. |
1619 |
^ inStream |
|
518 | 1620 |
|
1300 | 1621 |
"Modified: 31.1.1997 / 10:37:24 / cg" |
285 | 1622 |
! ! |
1623 |
||
1167 | 1624 |
!ImageReader class methodsFor:'image reading'! |
285 | 1625 |
|
1626 |
fromFile:aFileName |
|
1627 |
"read an image (in my format) from aFileName. |
|
1628 |
Return the image or nil on error." |
|
1629 |
||
1551 | 1630 |
|reader| |
285 | 1631 |
|
1551 | 1632 |
reader := self readFile:aFileName. |
1633 |
reader notNil ifTrue:[ |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1634 |
^ reader image |
285 | 1635 |
]. |
1551 | 1636 |
^ nil |
285 | 1637 |
|
1638 |
" |
|
1639 |
XBMReader fromFile:'bitmaps/SBrowser.xbm' |
|
4033 | 1640 |
XPMReader fromFile:'bitmaps/xpmBitmaps/misc_icons/BOOK.xpm' |
1641 |
XBMReader fromFile:'bitmaps/xpmBitmaps/misc_icons/BOOK.xpm' |
|
518 | 1642 |
" |
1643 |
||
1551 | 1644 |
"Modified: 4.4.1997 / 22:09:49 / cg" |
285 | 1645 |
! |
1646 |
||
1647 |
fromStream:aStream |
|
583 | 1648 |
"read an image (in my format) from aStream. |
1551 | 1649 |
Return the image or nil (if unrecognized format or error). |
1650 |
The stream remains open." |
|
285 | 1651 |
|
1652 |
|reader| |
|
1653 |
||
3839 | 1654 |
reader := self readStream:aStream. |
1655 |
^ reader image |
|
1550
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1656 |
! |
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1657 |
|
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1658 |
imagesFromFile:aFileName |
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1659 |
"read all images (in my format) from aFileName. |
1551 | 1660 |
Return a collection of images or nil on error. |
1661 |
Not all reader may support multiple images." |
|
1662 |
||
1663 |
|reader| |
|
1664 |
||
1665 |
reader := self readFile:aFileName. |
|
1666 |
reader notNil ifTrue:[ |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1667 |
^ reader images |
1551 | 1668 |
]. |
1669 |
^ nil |
|
1670 |
||
1671 |
"Modified: 4.4.1997 / 22:11:01 / cg" |
|
1672 |
! |
|
1550
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1673 |
|
1551 | 1674 |
imagesFromStream:aStream |
1675 |
"read all images (in my format) from aStream. |
|
1676 |
Return a collection of images or nil (if unrecognized format or error). |
|
1677 |
The stream remains open. |
|
1678 |
Not all reader may support multiple images." |
|
1679 |
||
1680 |
|reader| |
|
1681 |
||
3839 | 1682 |
reader := self new. |
1683 |
reader inStream:aStream. |
|
1684 |
reader fromStream:aStream. |
|
1685 |
^ reader images |
|
1551 | 1686 |
|
1687 |
"Modified: 4.4.1997 / 22:11:40 / cg" |
|
1688 |
! |
|
1689 |
||
1690 |
readFile:aFileName |
|
1691 |
"create a reader and let it read an image (in my format) from aFileName. |
|
1692 |
Return the reader." |
|
1693 |
||
1694 |
|reader inStream| |
|
1550
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1695 |
|
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1696 |
inStream := self streamReadingFile:aFileName. |
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1697 |
inStream isNil ifTrue:[ |
4033 | 1698 |
'ImageReader [warning]: file open error' errorPrintCR. |
1699 |
^ nil |
|
1550
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1700 |
]. |
3839 | 1701 |
reader := self new. |
1702 |
reader inStream:inStream. |
|
3489 | 1703 |
[ |
4033 | 1704 |
reader fromStream:inStream. |
3605
b5c70808fc8c
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
3489
diff
changeset
|
1705 |
] ensure:[ |
4033 | 1706 |
inStream close. |
3489 | 1707 |
]. |
1551 | 1708 |
^ reader |
1550
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1709 |
|
1551 | 1710 |
" |
4033 | 1711 |
XPMReader readFile:'bitmaps/xpmBitmaps/misc_icons/BOOK.xpm' |
1551 | 1712 |
" |
1713 |
||
1714 |
"Modified: 4.4.1997 / 22:06:49 / cg" |
|
1550
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1715 |
! |
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1716 |
|
3839 | 1717 |
readStream:aStream |
1551 | 1718 |
"create a reader and let it read a stream (in my format). |
1719 |
Return the reader. The stream remains open." |
|
1550
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1720 |
|
3839 | 1721 |
|reader| |
1722 |
||
1723 |
reader := self new. |
|
1724 |
reader fromStream:aStream. |
|
1725 |
^ reader |
|
1550
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1726 |
|
1551 | 1727 |
"Created: 4.4.1997 / 22:08:13 / cg" |
285 | 1728 |
! ! |
1729 |
||
2039
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1730 |
!ImageReader class methodsFor:'image support'! |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1731 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1732 |
buildMaskFromColor:maskPixelValue for:pixels width:width height:height |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1733 |
"helper for image formats, where an individual pixel value |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1734 |
has been defined as a mask-pixel (i.e. GIF). |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1735 |
Creates a maskImage, with zeros at positions where the image |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1736 |
has the given pixelValue; all other mask pixels are set to 1." |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1737 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1738 |
|maskArray bytesPerMaskRow mask| |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1739 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1740 |
bytesPerMaskRow := (width+7) // 8. |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1741 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1742 |
maskArray := ByteArray uninitializedNew:bytesPerMaskRow * height. |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1743 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1744 |
%{ |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1745 |
int __w = __intVal(width); |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1746 |
int __h = __intVal(height); |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1747 |
int __x, __y; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1748 |
int __outBits, __nOut; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1749 |
unsigned char *__inP, *__outP, *__nextOutRow; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1750 |
int __bpr = __intVal(bytesPerMaskRow); |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1751 |
int __maskPixel = __intVal(maskPixelValue); |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1752 |
#ifdef DEBUG |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1753 |
unsigned char *outEnd; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1754 |
#endif |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1755 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1756 |
if (! __isByteArray(pixels)) goto fail; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1757 |
if (! __isByteArray(maskArray)) goto fail; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1758 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1759 |
__inP = __ByteArrayInstPtr(pixels)->ba_element; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1760 |
__outP = __ByteArrayInstPtr(maskArray)->ba_element; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1761 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1762 |
#ifdef DEBUG |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1763 |
outEnd = __outP + (__byteArraySize(maskArray)); |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1764 |
#endif |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1765 |
/* |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1766 |
printf("outP: %x outEnd: %x sz: %d\n", __outP, outEnd, __byteArraySize(maskArray)); |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1767 |
*/ |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1768 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1769 |
for (__y=__h; __y>0; __y--) { |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1770 |
__outBits = 0; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1771 |
__nOut = 8; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1772 |
__nextOutRow = __outP + __bpr; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1773 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1774 |
for (__x=__w; __x>=8; __x-=8) { |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1775 |
if (__inP[0] != __maskPixel) { __outBits |= 0x80; }; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1776 |
if (__inP[1] != __maskPixel) { __outBits |= 0x40; }; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1777 |
if (__inP[2] != __maskPixel) { __outBits |= 0x20; }; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1778 |
if (__inP[3] != __maskPixel) { __outBits |= 0x10; }; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1779 |
if (__inP[4] != __maskPixel) { __outBits |= 0x08; }; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1780 |
if (__inP[5] != __maskPixel) { __outBits |= 0x04; }; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1781 |
if (__inP[6] != __maskPixel) { __outBits |= 0x02; }; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1782 |
if (__inP[7] != __maskPixel) { __outBits |= 0x01; }; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1783 |
__inP += 8; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1784 |
*__outP++ = __outBits; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1785 |
__outBits = 0; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1786 |
} |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1787 |
for (; __x>0; __x--) { |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1788 |
__outBits <<= 1; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1789 |
if (*__inP != __maskPixel) { |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1790 |
__outBits |= 1; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1791 |
} |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1792 |
__inP++; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1793 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1794 |
/* |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1795 |
printf("x: %d bits: %x\n", __x, __outBits); |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1796 |
*/ |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1797 |
if (--__nOut == 0) { |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1798 |
#ifdef DEBUG |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1799 |
if (__outP >= outEnd) { |
3468 | 1800 |
fprintf(stderr, "oops %d\n", __LINE__); |
1801 |
goto fail; |
|
2039
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1802 |
} |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1803 |
#endif |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1804 |
*__outP = __outBits; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1805 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1806 |
__outP++; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1807 |
__nOut = 8; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1808 |
__outBits = 0; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1809 |
} |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1810 |
} |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1811 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1812 |
if (__nOut != 8) { |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1813 |
__outBits <<= __nOut; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1814 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1815 |
#ifdef DEBUG |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1816 |
if (__outP >= outEnd) { |
3468 | 1817 |
fprintf(stderr, "oops2 %d\n", __LINE__); |
4033 | 1818 |
goto fail; |
2039
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1819 |
} |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1820 |
#endif |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1821 |
*__outP = __outBits; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1822 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1823 |
} |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1824 |
__outP = __nextOutRow; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1825 |
} |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1826 |
fail: ; |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1827 |
%}. |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1828 |
mask := ImageMask width:width height:height fromArray:maskArray. |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1829 |
^ mask |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1830 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1831 |
"Created: 21.6.1996 / 11:43:47 / cg" |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1832 |
! ! |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
1833 |
|
1167 | 1834 |
!ImageReader class methodsFor:'image writing'! |
285 | 1835 |
|
1836 |
save:anImage onFile:aFileName |
|
1837 |
"save the image in my format on aFileName" |
|
1838 |
||
1839 |
^ (self basicNew) save:anImage onFile:aFileName |
|
1574 | 1840 |
|
1841 |
"Modified: 10.4.1997 / 17:42:57 / cg" |
|
3932 | 1842 |
! |
1843 |
||
1844 |
save:anImage onStream:aStream |
|
1845 |
"save the image in my format on a Stream" |
|
1846 |
||
1847 |
^ (self basicNew) save:anImage onStream:aStream |
|
1848 |
||
1849 |
"Modified: 10.4.1997 / 17:42:57 / cg" |
|
285 | 1850 |
! ! |
1851 |
||
1167 | 1852 |
!ImageReader class methodsFor:'testing'! |
285 | 1853 |
|
1854 |
canRepresent:anImage |
|
1855 |
"return true, if anImage can be represented in my file format. |
|
1856 |
must be redefined in concrete subclasses which support saving." |
|
1857 |
||
1858 |
^ false |
|
1859 |
! |
|
1860 |
||
1861 |
isValidImageFile:aFileName |
|
1862 |
"return true, if aFileName contains an image this |
|
1863 |
reader understands - must be redefined in concrete subclasses |
|
1864 |
which support reading." |
|
1865 |
||
1866 |
^ false |
|
105 | 1867 |
! ! |
1868 |
||
1869 |
!ImageReader methodsFor:'accessing'! |
|
1870 |
||
285 | 1871 |
bitsPerPixel |
1872 |
"return the number of bits per pixel" |
|
1873 |
||
3386
b9294699dbcf
care for nil-data when converting to an image
Claus Gittinger <cg@exept.de>
parents:
3263
diff
changeset
|
1874 |
bitsPerSample isNil ifTrue:[^ nil]. |
285 | 1875 |
^ (bitsPerSample inject:0 into:[:sum :i | sum + i]) |
1876 |
! |
|
1877 |
||
1300 | 1878 |
bitsPerRow |
1879 |
"return the number of bits in one scanline of the image" |
|
1880 |
||
3868 | 1881 |
^ width * (self bitsPerPixel). |
1300 | 1882 |
|
1883 |
"Created: 31.1.1997 / 10:38:42 / cg" |
|
1884 |
! |
|
1885 |
||
285 | 1886 |
bitsPerSample |
583 | 1887 |
"return the number of bits per sample" |
1888 |
||
285 | 1889 |
^ bitsPerSample |
583 | 1890 |
|
1891 |
"Modified: 22.4.1996 / 19:15:18 / cg" |
|
285 | 1892 |
! |
1893 |
||
1300 | 1894 |
bytesPerRow |
1895 |
"return the number of bytes in one scanline of the image" |
|
1896 |
||
1897 |
|bitsPerRow bytesPerRow| |
|
1898 |
||
1899 |
bitsPerRow := width * (self bitsPerPixel). |
|
1900 |
bytesPerRow := bitsPerRow // 8. |
|
1901 |
((bitsPerRow \\ 8) ~~ 0) ifTrue:[ |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
1902 |
bytesPerRow := bytesPerRow + 1 |
1300 | 1903 |
]. |
1904 |
^ bytesPerRow |
|
1905 |
||
1906 |
"Created: 31.1.1997 / 10:39:06 / cg" |
|
1907 |
! |
|
1908 |
||
285 | 1909 |
colorMap |
583 | 1910 |
"return the colormap" |
1911 |
||
285 | 1912 |
^ colorMap |
583 | 1913 |
|
1914 |
"Modified: 22.4.1996 / 19:15:24 / cg" |
|
285 | 1915 |
! |
1916 |
||
1917 |
data |
|
583 | 1918 |
"return the raw image data" |
1919 |
||
285 | 1920 |
^ data |
583 | 1921 |
|
1922 |
"Modified: 22.4.1996 / 19:15:31 / cg" |
|
285 | 1923 |
! |
1924 |
||
1550
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1925 |
hasMultipleImages |
1773 | 1926 |
^ imageSequence size > 1 |
1550
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1927 |
|
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1928 |
"Created: 4.4.1997 / 21:37:06 / cg" |
1773 | 1929 |
"Modified: 24.6.1997 / 15:33:43 / cg" |
1550
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1930 |
! |
afee7c032a57
rudimentary support for multiple images.
Claus Gittinger <cg@exept.de>
parents:
1358
diff
changeset
|
1931 |
|
285 | 1932 |
height |
583 | 1933 |
"return the height of the image" |
1934 |
||
285 | 1935 |
^ height |
583 | 1936 |
|
1937 |
"Modified: 22.4.1996 / 19:15:39 / cg" |
|
285 | 1938 |
! |
1939 |
||
105 | 1940 |
image |
1772
56b3ae478bbb
changes to support multiple images
Claus Gittinger <cg@exept.de>
parents:
1661
diff
changeset
|
1941 |
"return the image as represented by myself; |
56b3ae478bbb
changes to support multiple images
Claus Gittinger <cg@exept.de>
parents:
1661
diff
changeset
|
1942 |
If my file contained multiple images, return the first one." |
105 | 1943 |
|
1773 | 1944 |
imageSequence size > 0 ifTrue:[ |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1945 |
^ self images first. |
1772
56b3ae478bbb
changes to support multiple images
Claus Gittinger <cg@exept.de>
parents:
1661
diff
changeset
|
1946 |
]. |
56b3ae478bbb
changes to support multiple images
Claus Gittinger <cg@exept.de>
parents:
1661
diff
changeset
|
1947 |
|
2080 | 1948 |
^ self makeImage. |
583 | 1949 |
|
1980
5f99cc1ac111
be carefull when reading from non-fileStreams
Stefan Vogel <sv@exept.de>
parents:
1949
diff
changeset
|
1950 |
"Modified: / 15.1.1998 / 15:46:24 / stefan" |
2082 | 1951 |
"Modified: / 1.4.1998 / 14:31:35 / cg" |
1952 |
! |
|
1953 |
||
1954 |
imageFrames |
|
1955 |
"return a collection of all imageFrames as represented by myself. |
|
1956 |
ImageFrames are wrappers for individual images, which hold |
|
1957 |
additional information (such as image delay time). |
|
1958 |
Nil is return for single image formats/files." |
|
1959 |
||
1960 |
^ imageSequence |
|
1961 |
||
1962 |
"Created: / 1.4.1998 / 14:37:42 / cg" |
|
893
24dc4b26a486
prepare to read mask/alpha channel
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
1963 |
! |
24dc4b26a486
prepare to read mask/alpha channel
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
1964 |
|
1551 | 1965 |
images |
1966 |
"return a collection of all images as represented by myself. |
|
1967 |
For compatibility with single-image formats, return a collection |
|
1773 | 1968 |
containing my single image here if the reader does not support |
1969 |
multiple images. |
|
1970 |
Readers for formats with multiple images should leave the images |
|
1971 |
in the imageSequence instVar as a side effect of reading." |
|
1551 | 1972 |
|
1973 |
|img| |
|
1974 |
||
1773 | 1975 |
imageSequence notNil ifTrue:[ |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1976 |
^ (imageSequence collect:[:aFrame | aFrame image]) |
1773 | 1977 |
]. |
1978 |
||
2082 | 1979 |
img := self makeImage. |
1551 | 1980 |
img isNil ifTrue:[ |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
1981 |
^ img |
1551 | 1982 |
]. |
1983 |
^ Array with:img |
|
1984 |
||
2082 | 1985 |
"Modified: / 1.4.1998 / 14:36:23 / cg" |
1551 | 1986 |
! |
1987 |
||
2080 | 1988 |
makeImage |
2082 | 1989 |
"return the image as represented by the values found in my |
1990 |
instvars; these have been typically set as a side effect of |
|
1991 |
the fromStream:-method, which reads images." |
|
2080 | 1992 |
|
1993 |
|image depth| |
|
1994 |
||
1995 |
depth := self bitsPerPixel. |
|
3839 | 1996 |
(depth notNil and:[data notNil]) ifTrue:[ |
4033 | 1997 |
image := Image newForDepth:depth. |
1998 |
image depth:depth. |
|
1999 |
inStream isFileStream ifTrue:[ |
|
2000 |
image fileName:inStream pathName. |
|
2001 |
]. |
|
2002 |
image |
|
2003 |
width:width |
|
2004 |
height:height |
|
2005 |
photometric:photometric |
|
2006 |
samplesPerPixel:samplesPerPixel |
|
2007 |
bitsPerSample:bitsPerSample |
|
2008 |
colorMap:colorMap |
|
2009 |
bits:data |
|
2010 |
mask:mask. |
|
3386
b9294699dbcf
care for nil-data when converting to an image
Claus Gittinger <cg@exept.de>
parents:
3263
diff
changeset
|
2011 |
|
4033 | 2012 |
imageSequence notNil ifTrue:[ |
2013 |
image imageSequence:imageSequence. |
|
2014 |
]. |
|
2080 | 2015 |
]. |
2016 |
^ image |
|
2017 |
||
2018 |
"Modified: / 15.1.1998 / 15:46:24 / stefan" |
|
2019 |
"Created: / 1.4.1998 / 14:09:45 / cg" |
|
2082 | 2020 |
"Modified: / 1.4.1998 / 14:41:35 / cg" |
2080 | 2021 |
! |
2022 |
||
893
24dc4b26a486
prepare to read mask/alpha channel
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2023 |
mask |
24dc4b26a486
prepare to read mask/alpha channel
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2024 |
"return the image mask (or nil)" |
24dc4b26a486
prepare to read mask/alpha channel
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2025 |
|
24dc4b26a486
prepare to read mask/alpha channel
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2026 |
^ mask |
24dc4b26a486
prepare to read mask/alpha channel
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2027 |
|
24dc4b26a486
prepare to read mask/alpha channel
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2028 |
"Modified: 22.4.1996 / 19:15:31 / cg" |
24dc4b26a486
prepare to read mask/alpha channel
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
2029 |
"Created: 20.6.1996 / 17:08:29 / cg" |
105 | 2030 |
! |
2031 |
||
1772
56b3ae478bbb
changes to support multiple images
Claus Gittinger <cg@exept.de>
parents:
1661
diff
changeset
|
2032 |
numberOfImages |
56b3ae478bbb
changes to support multiple images
Claus Gittinger <cg@exept.de>
parents:
1661
diff
changeset
|
2033 |
"return the number of images as read. |
56b3ae478bbb
changes to support multiple images
Claus Gittinger <cg@exept.de>
parents:
1661
diff
changeset
|
2034 |
By default (here), I hold a single image; |
56b3ae478bbb
changes to support multiple images
Claus Gittinger <cg@exept.de>
parents:
1661
diff
changeset
|
2035 |
however, some fileFormats allow for multiple images" |
56b3ae478bbb
changes to support multiple images
Claus Gittinger <cg@exept.de>
parents:
1661
diff
changeset
|
2036 |
|
1773 | 2037 |
|n| |
2038 |
||
2039 |
(n := imageSequence size) > 0 ifTrue:[ |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
2040 |
^ n |
1773 | 2041 |
]. |
1772
56b3ae478bbb
changes to support multiple images
Claus Gittinger <cg@exept.de>
parents:
1661
diff
changeset
|
2042 |
^ 1 |
56b3ae478bbb
changes to support multiple images
Claus Gittinger <cg@exept.de>
parents:
1661
diff
changeset
|
2043 |
|
56b3ae478bbb
changes to support multiple images
Claus Gittinger <cg@exept.de>
parents:
1661
diff
changeset
|
2044 |
"Created: 21.6.1997 / 18:33:45 / cg" |
1773 | 2045 |
"Modified: 24.6.1997 / 15:33:26 / cg" |
1772
56b3ae478bbb
changes to support multiple images
Claus Gittinger <cg@exept.de>
parents:
1661
diff
changeset
|
2046 |
! |
56b3ae478bbb
changes to support multiple images
Claus Gittinger <cg@exept.de>
parents:
1661
diff
changeset
|
2047 |
|
105 | 2048 |
photometric |
583 | 2049 |
"return the photometric interpretation of the image data" |
2050 |
||
105 | 2051 |
^ photometric |
583 | 2052 |
|
2053 |
"Modified: 22.4.1996 / 19:15:57 / cg" |
|
105 | 2054 |
! |
2055 |
||
2056 |
samplesPerPixel |
|
583 | 2057 |
"return the number of samples per pixel" |
2058 |
||
105 | 2059 |
^ samplesPerPixel |
583 | 2060 |
|
2061 |
"Modified: 22.4.1996 / 19:16:03 / cg" |
|
105 | 2062 |
! |
2063 |
||
285 | 2064 |
width |
583 | 2065 |
"return the width of the image" |
2066 |
||
285 | 2067 |
^ width |
583 | 2068 |
|
2069 |
"Modified: 22.4.1996 / 19:16:10 / cg" |
|
105 | 2070 |
! ! |
2071 |
||
3263 | 2072 |
!ImageReader methodsFor:'accessing-private'! |
1355 | 2073 |
|
2074 |
byteOrder:aSymbol |
|
2075 |
"set the byte order - either #lsb or #msb" |
|
2076 |
||
2077 |
byteOrder := aSymbol |
|
2078 |
||
2079 |
"Created: 15.2.1997 / 13:58:50 / cg" |
|
2080 |
! |
|
2081 |
||
2082 |
inStream |
|
2083 |
^ inStream |
|
2084 |
||
2085 |
"Created: 15.2.1997 / 13:57:40 / cg" |
|
2086 |
! |
|
2087 |
||
2088 |
inStream:aStream |
|
2089 |
inStream := aStream |
|
2090 |
||
2091 |
"Created: 15.2.1997 / 13:57:35 / cg" |
|
2092 |
! ! |
|
2093 |
||
2039
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2094 |
!ImageReader methodsFor:'error reporting'! |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2095 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2096 |
fileFormatError:aMessage |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2097 |
"report a format error - no image could be read" |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2098 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2099 |
Image |
2152
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
2100 |
badImageFormatQuerySignal |
b071479dac57
check byteArray bounds in bmp-decompressors;
Claus Gittinger <cg@exept.de>
parents:
2082
diff
changeset
|
2101 |
raiseErrorString:(self class name , ' [info]: ' , aMessage). |
2039
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2102 |
^ nil |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2103 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2104 |
"Created: / 3.2.1998 / 17:50:06 / cg" |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2105 |
! ! |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2106 |
|
105 | 2107 |
!ImageReader methodsFor:'i/o support'! |
2108 |
||
2109 |
readLong |
|
2110 |
"return the next 4-byte long, honoring the byte-order" |
|
2111 |
||
2112 |
^ inStream nextLongMSB:(byteOrder ~~ #lsb) |
|
2113 |
! |
|
2114 |
||
2115 |
readShort |
|
2116 |
"return the next 2-byte short, honoring the byte-order" |
|
2117 |
||
2118 |
^ inStream nextUnsignedShortMSB:(byteOrder ~~ #lsb) |
|
2119 |
! |
|
2120 |
||
2121 |
readShortLong |
|
2122 |
"return the next 2-byte short, honoring the byte-order. |
|
2123 |
There are actually 4 bytes read, but only 2 looked at." |
|
2124 |
||
2125 |
|bytes val| |
|
2126 |
||
153 | 2127 |
bytes := ByteArray uninitializedNew:4. |
105 | 2128 |
inStream nextBytes:4 into:bytes. |
2129 |
(byteOrder == #lsb) ifTrue:[ |
|
2130 |
val := bytes at:2. |
|
2131 |
val := val * 256 + (bytes at:1) |
|
2132 |
] ifFalse:[ |
|
2133 |
val := bytes at:3. |
|
2134 |
val := val * 256 + (bytes at:4) |
|
2135 |
]. |
|
2136 |
^ val |
|
2137 |
! |
|
2138 |
||
2139 |
writeLong:anInteger |
|
2140 |
"write a 4-byte long, honoring the byte-order." |
|
2141 |
||
2142 |
outStream nextPutLong:anInteger MSB:(byteOrder ~~ #lsb) |
|
2143 |
! |
|
2144 |
||
2145 |
writeShort:anInteger |
|
2146 |
"write a 2-byte short, honoring the byte-order." |
|
2147 |
||
2148 |
outStream nextPutShort:anInteger MSB:(byteOrder ~~ #lsb) |
|
2149 |
! ! |
|
285 | 2150 |
|
2039
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2151 |
!ImageReader methodsFor:'image reading'! |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2152 |
|
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2153 |
fromStream:aStream |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2154 |
"read an image in my format from aStream. |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2155 |
Leave image description in instance variables." |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2156 |
|
3839 | 2157 |
self inStream:aStream. |
2158 |
self readImage. |
|
2159 |
! |
|
2039
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2160 |
|
3839 | 2161 |
readImage |
2162 |
"read an image in my format from my inStream. |
|
2163 |
Leave image description in instance variables." |
|
2039
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2164 |
|
3839 | 2165 |
self subclassResponsibility. |
2039
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2166 |
! ! |
00e10543cf57
added common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
2014
diff
changeset
|
2167 |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
2168 |
!ImageReader methodsFor:'image reading support'! |
285 | 2169 |
|
929
f792d9689def
Moved #buildMask from GIFReader to #buildMaskFromColor:
Stefan Vogel <sv@exept.de>
parents:
893
diff
changeset
|
2170 |
buildMaskFromColor:maskPixelValue |
1631
fc944d181796
use ImageMask instead of Depth1Image
Claus Gittinger <cg@exept.de>
parents:
1574
diff
changeset
|
2171 |
"helper for image formats, where an individual pixel value |
fc944d181796
use ImageMask instead of Depth1Image
Claus Gittinger <cg@exept.de>
parents:
1574
diff
changeset
|
2172 |
has been defined as a mask-pixel (i.e. GIF). |
fc944d181796
use ImageMask instead of Depth1Image
Claus Gittinger <cg@exept.de>
parents:
1574
diff
changeset
|
2173 |
Creates a maskImage, with zeros at positions where the image |
fc944d181796
use ImageMask instead of Depth1Image
Claus Gittinger <cg@exept.de>
parents:
1574
diff
changeset
|
2174 |
has the given pixelValue; all other mask pixels are set to 1." |
fc944d181796
use ImageMask instead of Depth1Image
Claus Gittinger <cg@exept.de>
parents:
1574
diff
changeset
|
2175 |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
2176 |
mask := self class |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
2177 |
buildMaskFromColor:maskPixelValue |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
2178 |
for:data |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
2179 |
width:width |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
2180 |
height:height |
929
f792d9689def
Moved #buildMask from GIFReader to #buildMaskFromColor:
Stefan Vogel <sv@exept.de>
parents:
893
diff
changeset
|
2181 |
|
1983
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
2182 |
"Modified: / 14.1.1998 / 14:48:33 / cg" |
03a93138c52a
made buildMask public and a class method (functionality also needed by JavaVM)
Claus Gittinger <cg@exept.de>
parents:
1980
diff
changeset
|
2183 |
! ! |
929
f792d9689def
Moved #buildMask from GIFReader to #buildMaskFromColor:
Stefan Vogel <sv@exept.de>
parents:
893
diff
changeset
|
2184 |
|
285 | 2185 |
!ImageReader methodsFor:'image writing'! |
2186 |
||
2187 |
save:image onFile:aFileName |
|
2188 |
"save image in my format on aFile" |
|
2189 |
||
3933
10147dc53aef
image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents:
3932
diff
changeset
|
2190 |
|stream| |
10147dc53aef
image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents:
3932
diff
changeset
|
2191 |
|
10147dc53aef
image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents:
3932
diff
changeset
|
2192 |
[ |
4033 | 2193 |
stream := aFileName asFilename newReadWriteStream. |
3933
10147dc53aef
image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents:
3932
diff
changeset
|
2194 |
] on:FileStream openErrorSignal do:[:ex| |
4033 | 2195 |
^ Image fileCreationErrorSignal |
2196 |
raiseWith:image |
|
2197 |
errorString:('file creation error: ' , aFileName asString). |
|
3933
10147dc53aef
image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents:
3932
diff
changeset
|
2198 |
]. |
1574 | 2199 |
|
3933
10147dc53aef
image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents:
3932
diff
changeset
|
2200 |
[ |
4033 | 2201 |
[ |
2202 |
self save:image onStream:stream. |
|
2203 |
] ifCurtailed:[ |
|
2204 |
aFileName asFilename delete. |
|
2205 |
]. |
|
3933
10147dc53aef
image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents:
3932
diff
changeset
|
2206 |
|
10147dc53aef
image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents:
3932
diff
changeset
|
2207 |
] ensure:[ |
4033 | 2208 |
stream close. |
3933
10147dc53aef
image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents:
3932
diff
changeset
|
2209 |
]. |
3932 | 2210 |
! |
2211 |
||
2212 |
save:image onStream:aStream |
|
2213 |
"save image in my format on a Stream" |
|
2214 |
||
2215 |
^ Image cannotRepresentImageSignal |
|
4033 | 2216 |
raiseWith:image |
2217 |
errorString:('image save not implemented for this format'). |
|
285 | 2218 |
! ! |
2219 |
||
1358 | 2220 |
!ImageReader methodsFor:'initialization'! |
2221 |
||
2222 |
initialize |
|
2223 |
||
2224 |
"Created: 18.2.1997 / 17:08:31 / cg" |
|
2225 |
! ! |
|
2226 |
||
3955
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2227 |
!ImageReader methodsFor:'progress reporting'! |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2228 |
|
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2229 |
dimensionCallBack:aBlock |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2230 |
"set the block, which is evaluated during the readProcess, as soon as |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2231 |
the images dimension is known. This is useful for background image reading, |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2232 |
if the size is need to be known (for example: for formatting purposes). |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2233 |
Obsoleted by dimensionHolder." |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2234 |
|
3965 | 2235 |
self obsoleteMethodWarning:'use #dimensionHolder'. |
3955
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2236 |
dimensionCallBack := aBlock |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2237 |
|
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2238 |
"Created: 14.9.1996 / 17:10:58 / cg" |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2239 |
! |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2240 |
|
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2241 |
dimensionHolder:aValueHolderOrBlock |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2242 |
"set the valueHolder or block, which is evaluated during the readProcess, |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2243 |
as soon as the images dimension is known. |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2244 |
Useful for background image reading, if the size is need to be known (for example: for formatting purposes)." |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2245 |
|
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2246 |
dimensionHolder := aValueHolderOrBlock |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2247 |
! |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2248 |
|
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2249 |
progressHolder:aValueHolderOrBlock |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2250 |
"set the valueHolder or block, which is evaluated during the readProcess, |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2251 |
and set with progress information (0..100 percent). |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2252 |
Useful for user feedback" |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2253 |
|
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2254 |
progressHolder := aValueHolderOrBlock |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2255 |
! |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2256 |
|
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2257 |
reportDimension |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2258 |
dimensionCallBack notNil ifTrue:[ |
4033 | 2259 |
dimensionCallBack value:self |
3955
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2260 |
]. |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2261 |
dimensionHolder notNil ifTrue:[ |
4033 | 2262 |
dimensionHolder value:(width @ height) |
3955
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2263 |
]. |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2264 |
! |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2265 |
|
3957 | 2266 |
reportProgress:fraction |
2267 |
"can be used by a GUI application to indicate loading progress (0..1)" |
|
2268 |
||
3955
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2269 |
progressHolder notNil ifTrue:[ |
4033 | 2270 |
progressHolder value:fraction |
3955
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2271 |
]. |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2272 |
! ! |
6f48b9693fe5
dimensionReport and progressReport hooks
Claus Gittinger <cg@exept.de>
parents:
3933
diff
changeset
|
2273 |
|
1167 | 2274 |
!ImageReader class methodsFor:'documentation'! |
285 | 2275 |
|
2276 |
version |
|
4033 | 2277 |
^ '$Header: /cvs/stx/stx/libview/ImageReader.st,v 1.87 2004-02-24 10:35:25 cg Exp $' |
285 | 2278 |
! ! |