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