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