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