author | Claus Gittinger <cg@exept.de> |
Sat, 11 Nov 1995 16:54:10 +0100 | |
changeset 219 | 9ff0660f447f |
parent 207 | 9124817bbb03 |
child 285 | 1effedd62f30 |
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 |
||
105 | 13 |
'From Smalltalk/X, Version:2.10.4 on 18-feb-1995 at 2:18:34 am'! |
14 |
||
0 | 15 |
Object subclass:#ImageReader |
105 | 16 |
instanceVariableNames:'width height data byteOrder inStream outStream photometric |
17 |
samplesPerPixel bitsPerSample colorMap' |
|
77 | 18 |
classVariableNames:'ReverseBits' |
19 |
poolDictionaries:'' |
|
81 | 20 |
category:'Graphics-Images support' |
0 | 21 |
! |
22 |
||
189 | 23 |
!ImageReader primitiveDefinitions! |
24 |
%{ |
|
25 |
#ifndef _STDIO_H_INCLUDED_ |
|
26 |
# include <stdio.h> |
|
27 |
# define _STDIO_H_INCLUDED_ |
|
28 |
#endif |
|
29 |
%} |
|
30 |
! ! |
|
31 |
||
89 | 32 |
!ImageReader primitiveFunctions! |
0 | 33 |
%{ |
34 |
||
35 |
/* |
|
36 |
* ccitt decompression |
|
37 |
*/ |
|
38 |
static short *whiteCountTable; |
|
39 |
static char *whiteShiftTable; |
|
40 |
static short *blackCountTable; |
|
41 |
static char *blackShiftTable; |
|
42 |
||
43 |
struct ccitt_def { |
|
44 |
unsigned short bits; |
|
45 |
short nBits; |
|
46 |
}; |
|
47 |
||
48 |
static struct ccitt_def |
|
49 |
whiteDef[] = { |
|
50 |
{ 0x3500, 8 }, /* 0 */ |
|
51 |
{ 0x1c00, 6 }, |
|
52 |
{ 0x7000, 4 }, |
|
53 |
{ 0x8000, 4 }, |
|
54 |
{ 0xb000, 4 }, |
|
55 |
{ 0xc000, 4 }, |
|
56 |
{ 0xe000, 4 }, |
|
57 |
{ 0xf000, 4 }, |
|
58 |
{ 0x9800, 5 }, |
|
59 |
{ 0xA000, 5 }, |
|
60 |
{ 0x3800, 5 }, /* 10 */ |
|
61 |
{ 0x4000, 5 }, |
|
62 |
{ 0x2000, 6 }, |
|
63 |
{ 0x0c00, 6 }, |
|
64 |
{ 0xd000, 6 }, |
|
65 |
{ 0xd400, 6 }, |
|
66 |
{ 0xa800, 6 }, |
|
67 |
{ 0xac00, 6 }, |
|
68 |
{ 0x4e00, 7 }, |
|
69 |
{ 0x1800, 7 }, |
|
70 |
{ 0x1000, 7 }, /* 20 */ |
|
71 |
{ 0x2e00, 7 }, |
|
72 |
{ 0x0600, 7 }, |
|
73 |
{ 0x0800, 7 }, |
|
74 |
{ 0x5000, 7 }, |
|
75 |
{ 0x5600, 7 }, |
|
76 |
{ 0x2600, 7 }, |
|
77 |
{ 0x4800, 7 }, |
|
78 |
{ 0x3000, 7 }, |
|
79 |
{ 0x0200, 8 }, |
|
80 |
{ 0x0300, 8 }, /* 30 */ |
|
81 |
{ 0x1a00, 8 }, |
|
82 |
{ 0x1b00, 8 }, |
|
83 |
{ 0x1200, 8 }, |
|
84 |
{ 0x1300, 8 }, |
|
85 |
{ 0x1400, 8 }, |
|
86 |
{ 0x1500, 8 }, |
|
87 |
{ 0x1600, 8 }, |
|
88 |
{ 0x1700, 8 }, |
|
89 |
{ 0x2800, 8 }, |
|
90 |
{ 0x2900, 8 }, /* 40 */ |
|
91 |
{ 0x2a00, 8 }, |
|
92 |
{ 0x2b00, 8 }, |
|
93 |
{ 0x2c00, 8 }, |
|
94 |
{ 0x2d00, 8 }, |
|
95 |
{ 0x0400, 8 }, |
|
96 |
{ 0x0500, 8 }, |
|
97 |
{ 0x0a00, 8 }, |
|
98 |
{ 0x0b00, 8 }, |
|
99 |
{ 0x5200, 8 }, |
|
100 |
{ 0x5300, 8 }, /* 50 */ |
|
101 |
{ 0x5400, 8 }, |
|
102 |
{ 0x5500, 8 }, |
|
103 |
{ 0x2400, 8 }, |
|
104 |
{ 0x2500, 8 }, |
|
105 |
{ 0x5800, 8 }, |
|
106 |
{ 0x5900, 8 }, |
|
107 |
{ 0x5a00, 8 }, |
|
108 |
{ 0x5b00, 8 }, |
|
109 |
{ 0x4a00, 8 }, |
|
110 |
{ 0x4b00, 8 }, /* 60 */ |
|
111 |
{ 0x3200, 8 }, |
|
112 |
{ 0x3300, 8 }, |
|
113 |
{ 0x3400, 8 }, |
|
114 |
/* ---------------- */ |
|
115 |
{ 0xd800, 5 }, /* 64 */ |
|
116 |
{ 0x9000, 5 }, /* 128 */ |
|
117 |
{ 0x5c00, 6 }, /* 192 */ |
|
118 |
{ 0x6e00, 7 }, /* 256 */ |
|
119 |
{ 0x3600, 8 }, /* 320 */ |
|
120 |
{ 0x3700, 8 }, |
|
121 |
{ 0x6400, 8 }, |
|
122 |
{ 0x6500, 8 }, |
|
123 |
{ 0x6800, 8 }, |
|
124 |
{ 0x6700, 8 }, /* 640 */ |
|
125 |
{ 0x6600, 9 }, /* 704 */ |
|
126 |
{ 0x6680, 9 }, |
|
127 |
{ 0x6900, 9 }, |
|
128 |
{ 0x6980, 9 }, |
|
129 |
{ 0x6a00, 9 }, |
|
130 |
{ 0x6a80, 9 }, |
|
131 |
{ 0x6b00, 9 }, |
|
132 |
{ 0x6b80, 9 }, |
|
133 |
{ 0x6c00, 9 }, |
|
134 |
{ 0x6c80, 9 }, |
|
135 |
{ 0x6d00, 9 }, |
|
136 |
{ 0x6d80, 9 }, |
|
137 |
{ 0x4c00, 9 }, |
|
138 |
{ 0x4c80, 9 }, |
|
139 |
{ 0x4d00, 9 }, /* 1600 */ |
|
140 |
{ 0x6000, 6 }, /* 1664 */ |
|
141 |
{ 0x4d80, 9 }, /* 1728 */ |
|
142 |
/* -------------------------------- */ |
|
143 |
{ 0x0100, 11 }, /* 1792 */ |
|
144 |
{ 0x0180, 11 }, |
|
145 |
{ 0x01a0, 11 }, /* 1920 */ |
|
146 |
{ 0x0120, 12 }, /* 1984 */ |
|
147 |
{ 0x0130, 12 }, |
|
148 |
{ 0x0140, 12 }, |
|
149 |
{ 0x0150, 12 }, |
|
150 |
{ 0x0160, 12 }, |
|
151 |
{ 0x0170, 12 }, |
|
152 |
{ 0x01c0, 12 }, |
|
153 |
{ 0x01d0, 12 }, |
|
154 |
{ 0x01e0, 12 }, |
|
155 |
{ 0x01f0, 12 }, /* 2560 */ |
|
156 |
/* -------------------------------- */ |
|
157 |
{ 0x0010, 12 }, /* EOL */ |
|
158 |
}; |
|
159 |
||
160 |
static struct ccitt_def |
|
161 |
blackDef[] = { |
|
162 |
{ 0x0dc0, 10 }, /* 0 */ |
|
163 |
{ 0x4000, 3 }, |
|
164 |
{ 0xc000, 2 }, |
|
165 |
{ 0x8000, 2 }, |
|
166 |
{ 0x6000, 3 }, |
|
167 |
{ 0x3000, 4 }, |
|
168 |
{ 0x2000, 4 }, |
|
169 |
{ 0x1800, 5 }, |
|
170 |
{ 0x1400, 6 }, |
|
171 |
{ 0x1000, 6 }, |
|
172 |
{ 0x0800, 7 }, /* 10 */ |
|
173 |
{ 0x0a00, 7 }, |
|
174 |
{ 0x0e00, 7 }, |
|
175 |
{ 0x0400, 8 }, |
|
176 |
{ 0x0700, 8 }, |
|
177 |
{ 0x0c00, 9 }, |
|
178 |
{ 0x05c0, 10 }, |
|
179 |
{ 0x0600, 10 }, |
|
180 |
{ 0x0200, 10 }, |
|
181 |
{ 0x0ce0, 11 }, |
|
182 |
{ 0x0d00, 11 }, /* 20 */ |
|
183 |
{ 0x0d80, 11 }, |
|
184 |
{ 0x06e0, 11 }, |
|
185 |
{ 0x0500, 11 }, |
|
186 |
{ 0x02e0, 11 }, |
|
187 |
{ 0x0300, 11 }, |
|
188 |
{ 0x0ca0, 12 }, |
|
189 |
{ 0x0cb0, 12 }, |
|
190 |
{ 0x0cc0, 12 }, |
|
191 |
{ 0x0cd0, 12 }, |
|
192 |
{ 0x0680, 12 }, /* 30 */ |
|
193 |
{ 0x0690, 12 }, |
|
194 |
{ 0x06a0, 12 }, |
|
195 |
{ 0x06b0, 12 }, |
|
196 |
{ 0x0d20, 12 }, |
|
197 |
{ 0x0d30, 12 }, |
|
198 |
{ 0x0d40, 12 }, |
|
199 |
{ 0x0d50, 12 }, |
|
200 |
{ 0x0d60, 12 }, |
|
201 |
{ 0x0d70, 12 }, |
|
202 |
{ 0x06c0, 12 }, /* 40 */ |
|
203 |
{ 0x06d0, 12 }, |
|
204 |
{ 0x0da0, 12 }, |
|
205 |
{ 0x0db0, 12 }, |
|
206 |
{ 0x0540, 12 }, |
|
207 |
{ 0x0550, 12 }, |
|
208 |
{ 0x0560, 12 }, |
|
209 |
{ 0x0570, 12 }, |
|
210 |
{ 0x0640, 12 }, |
|
211 |
{ 0x0650, 12 }, |
|
212 |
{ 0x0520, 12 }, /* 50 */ |
|
213 |
{ 0x0530, 12 }, |
|
214 |
{ 0x0240, 12 }, |
|
215 |
{ 0x0370, 12 }, |
|
216 |
{ 0x0380, 12 }, |
|
217 |
{ 0x0270, 12 }, |
|
218 |
{ 0x0280, 12 }, |
|
219 |
{ 0x0580, 12 }, |
|
220 |
{ 0x0590, 12 }, |
|
221 |
{ 0x02b0, 12 }, |
|
222 |
{ 0x02c0, 12 }, /* 60 */ |
|
223 |
{ 0x05a0, 12 }, |
|
224 |
{ 0x0660, 12 }, |
|
225 |
{ 0x0670, 12 }, |
|
226 |
/* ---------------- */ |
|
227 |
{ 0x03c0, 10 }, /* 64 */ |
|
228 |
{ 0x0c80, 12 }, /* 128 */ |
|
229 |
{ 0x0c90, 12 }, /* 192 */ |
|
230 |
{ 0x05b0, 12 }, /* 256 */ |
|
231 |
{ 0x0330, 12 }, /* 320 */ |
|
232 |
{ 0x0340, 12 }, |
|
233 |
{ 0x0350, 12 }, /* 448 */ |
|
234 |
{ 0x0360, 13 }, /* 512 */ |
|
235 |
{ 0x0368, 13 }, |
|
236 |
{ 0x0250, 13 }, /* 640 */ |
|
237 |
{ 0x0258, 13 }, /* 704 */ |
|
238 |
{ 0x0260, 13 }, |
|
239 |
{ 0x0268, 13 }, |
|
240 |
{ 0x0390, 13 }, |
|
241 |
{ 0x0398, 13 }, |
|
242 |
{ 0x03a0, 13 }, |
|
243 |
{ 0x03a8, 13 }, |
|
244 |
{ 0x03b0, 13 }, |
|
245 |
{ 0x03b8, 13 }, |
|
246 |
{ 0x0290, 13 }, |
|
247 |
{ 0x0298, 13 }, |
|
248 |
{ 0x02a0, 13 }, |
|
249 |
{ 0x02a8, 13 }, |
|
250 |
{ 0x02d0, 13 }, |
|
251 |
{ 0x02d8, 13 }, /* 1600 */ |
|
252 |
{ 0x0320, 13 }, /* 1664 */ |
|
253 |
{ 0x0328, 13 }, /* 1728 */ |
|
254 |
/* -------------------------------- */ |
|
255 |
}; |
|
256 |
||
257 |
static |
|
258 |
initCCITTTables() { |
|
259 |
register cnt, bits, value; |
|
260 |
int nBits, index; |
|
261 |
||
262 |
if (whiteCountTable != (short *)0) return; |
|
263 |
||
264 |
whiteCountTable = (short *) malloc(sizeof(short) * 8192); |
|
12 | 265 |
if (! whiteCountTable) return; |
0 | 266 |
whiteShiftTable = (char *) malloc(sizeof(char) * 8192); |
12 | 267 |
if (! whiteShiftTable) { |
95 | 268 |
goto fail1; |
269 |
/* |
|
77 | 270 |
free(whiteCountTable); whiteCountTable = (short *)0; |
271 |
return; |
|
95 | 272 |
*/ |
12 | 273 |
} |
0 | 274 |
blackCountTable = (short *) malloc(sizeof(short) * 8192); |
12 | 275 |
if (! blackCountTable) { |
95 | 276 |
goto fail2; |
277 |
/* |
|
77 | 278 |
free(whiteShiftTable); whiteShiftTable = (char *)0; |
279 |
free(whiteCountTable); whiteCountTable = (short *)0; |
|
280 |
return; |
|
95 | 281 |
*/ |
12 | 282 |
} |
0 | 283 |
blackShiftTable = (char *) malloc(sizeof(char) * 8192); |
12 | 284 |
if (! blackShiftTable) { |
95 | 285 |
free(blackCountTable); blackCountTable = (short *)0; |
286 |
fail2: |
|
77 | 287 |
free(whiteShiftTable); whiteShiftTable = (char *)0; |
95 | 288 |
fail1: |
77 | 289 |
free(whiteCountTable); whiteCountTable = (short *)0; |
290 |
return; |
|
12 | 291 |
} |
0 | 292 |
|
293 |
for (index = 0; index < 8192; index++) { |
|
77 | 294 |
whiteCountTable[index] = -1; |
295 |
blackCountTable[index] = -1; |
|
0 | 296 |
} |
297 |
||
298 |
for (value = 0; value <= 63; value++) { |
|
77 | 299 |
nBits = whiteDef[value].nBits; |
300 |
bits = whiteDef[value].bits >> 3; |
|
301 |
for (cnt = 1 << (13 - nBits); cnt; cnt--, bits++) { |
|
302 |
whiteCountTable[bits] = value; |
|
303 |
whiteShiftTable[bits] = nBits; |
|
304 |
} |
|
305 |
nBits = blackDef[value].nBits; |
|
306 |
bits = blackDef[value].bits >> 3; |
|
307 |
for (cnt = 1 << (13 - nBits); cnt; cnt--, bits++) { |
|
308 |
blackCountTable[bits] = value; |
|
309 |
blackShiftTable[bits] = nBits; |
|
310 |
} |
|
0 | 311 |
} |
312 |
index = value; |
|
313 |
||
314 |
for (; value <= 1728; value += 64) { |
|
77 | 315 |
nBits = whiteDef[index].nBits; |
316 |
bits = whiteDef[index].bits >> 3; |
|
317 |
for (cnt = 1 << (13 - nBits); cnt; cnt--, bits++) { |
|
318 |
whiteCountTable[bits] = value; |
|
319 |
whiteShiftTable[bits] = nBits; |
|
320 |
} |
|
321 |
nBits = blackDef[index].nBits; |
|
322 |
bits = blackDef[index].bits >> 3; |
|
323 |
for (cnt = 1 << (13 - nBits); cnt; cnt--, bits++) { |
|
324 |
blackCountTable[bits] = value; |
|
325 |
blackShiftTable[bits] = nBits; |
|
326 |
} |
|
327 |
index++; |
|
0 | 328 |
} |
329 |
||
330 |
for (; value <= 2560; value += 64) { |
|
77 | 331 |
nBits = whiteDef[index].nBits; |
332 |
bits = whiteDef[index].bits >> 3; |
|
333 |
for (cnt = 1 << (13 - nBits); cnt; cnt--, bits++) { |
|
334 |
whiteCountTable[bits] = value; |
|
335 |
whiteShiftTable[bits] = nBits; |
|
336 |
blackCountTable[bits] = value; |
|
337 |
blackShiftTable[bits] = nBits; |
|
338 |
} |
|
339 |
index++; |
|
0 | 340 |
} |
341 |
} |
|
342 |
||
343 |
static short |
|
344 |
leftBits[] = { |
|
345 |
0, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFF |
|
346 |
}; |
|
347 |
||
189 | 348 |
static int |
77 | 349 |
__decodeCCITTgroup3__(from, to, len) |
0 | 350 |
unsigned char *from; |
351 |
register unsigned char *to; |
|
352 |
{ |
|
353 |
register cnt; |
|
354 |
register short *countPtr; |
|
355 |
register char *shiftPtr; |
|
356 |
unsigned bits, bits13; |
|
357 |
int shift, outCount, nBitsLess13; |
|
358 |
int outBitOffset; |
|
359 |
int nLeft, t; |
|
360 |
||
361 |
if (! whiteCountTable) initCCITTTables(); |
|
46 | 362 |
if (! whiteCountTable) return 0; /* malloc failed */ |
0 | 363 |
|
364 |
countPtr = whiteCountTable; |
|
365 |
shiftPtr = whiteShiftTable; |
|
366 |
outCount = 0; |
|
367 |
outBitOffset = 0; |
|
368 |
bits = *from++; |
|
369 |
bits = (bits << 8) | *from++; |
|
370 |
nBitsLess13 = 3; |
|
371 |
for (;;) { |
|
77 | 372 |
bits13 = (bits >> nBitsLess13) & 0x1FFF; |
373 |
cnt = countPtr[bits13]; |
|
189 | 374 |
if (cnt < 0) return 1; |
77 | 375 |
shift = shiftPtr[bits13]; |
376 |
outCount += cnt; |
|
377 |
if (countPtr == blackCountTable) { |
|
378 |
/* toggle if it was a terminating code */ |
|
379 |
if (cnt < 64) { |
|
380 |
countPtr = whiteCountTable; |
|
381 |
shiftPtr = whiteShiftTable; |
|
382 |
} |
|
0 | 383 |
|
77 | 384 |
/* draw cnt black bits */ |
385 |
if (cnt) { |
|
386 |
if (outBitOffset) { |
|
387 |
nLeft = 8 - outBitOffset; |
|
388 |
if (cnt < nLeft) nLeft = cnt; |
|
389 |
t = leftBits[nLeft] >> outBitOffset; |
|
390 |
*to |= t; |
|
391 |
cnt -= nLeft; |
|
392 |
outBitOffset += nLeft; |
|
393 |
if (outBitOffset >= 8) { |
|
394 |
to++; |
|
395 |
outBitOffset -= 8; |
|
396 |
} |
|
397 |
} |
|
398 |
if (cnt > 256) { |
|
399 |
while ((int)to & 3) { |
|
400 |
*to++ = 0xFF; |
|
401 |
cnt -= 8; |
|
402 |
} |
|
403 |
while (cnt >= 32) { |
|
404 |
(*(long *)to) = 0xFFFFFFFF; |
|
405 |
to += 4; |
|
406 |
cnt -= 32; |
|
407 |
} |
|
408 |
} |
|
409 |
while (cnt >= 8) { |
|
410 |
*to++ = 0xFF; |
|
411 |
cnt -= 8; |
|
412 |
} |
|
413 |
*to |= leftBits[cnt]; |
|
414 |
outBitOffset += cnt; |
|
415 |
} |
|
416 |
} else { |
|
417 |
/* toggle if it was a terminating code */ |
|
418 |
if (cnt < 64) { |
|
419 |
countPtr = blackCountTable; |
|
420 |
shiftPtr = blackShiftTable; |
|
421 |
} |
|
0 | 422 |
|
77 | 423 |
/* skip cnt bits */ |
424 |
to += cnt >> 3; |
|
425 |
outBitOffset += cnt & 7; |
|
426 |
if (outBitOffset >= 8) { |
|
427 |
to++; |
|
428 |
outBitOffset -= 8; |
|
429 |
} |
|
430 |
} |
|
431 |
if (outCount >= len) return 1; |
|
0 | 432 |
|
77 | 433 |
nBitsLess13 -= shift; |
434 |
while (nBitsLess13 < 0) { |
|
435 |
bits = (bits << 8) | *from++; |
|
436 |
nBitsLess13 += 8; |
|
437 |
} |
|
0 | 438 |
} |
439 |
} |
|
440 |
||
441 |
/* |
|
442 |
* LZW decompression |
|
443 |
*/ |
|
444 |
struct buffer { |
|
77 | 445 |
struct buffer *prev; |
446 |
unsigned char chars[8192 - 4]; |
|
0 | 447 |
}; |
189 | 448 |
|
449 |
static int |
|
77 | 450 |
__decodeLZW__(from, to, inCount) |
0 | 451 |
unsigned char *from; |
452 |
unsigned char *to; |
|
453 |
{ |
|
454 |
register unsigned code; |
|
12 | 455 |
unsigned char **strings; |
456 |
short *stringLen; |
|
0 | 457 |
struct buffer *scratchBuffer; |
458 |
struct buffer *newBuffer; |
|
459 |
unsigned char *scratchPtr; |
|
460 |
int nScratch; |
|
461 |
unsigned nextCode, oldCode; |
|
462 |
register unsigned bits; |
|
463 |
int nBits, mask, shift; |
|
464 |
int i; |
|
465 |
int len; |
|
466 |
int codeLen = 9; |
|
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; |
|
509 |
if (code == 257) break; |
|
510 |
if (code == 256) { |
|
511 |
if (! inCount) |
|
512 |
break; |
|
0 | 513 |
|
77 | 514 |
/* free stuff */ |
515 |
while (scratchBuffer->prev) { |
|
516 |
newBuffer = scratchBuffer; |
|
517 |
scratchBuffer = scratchBuffer->prev; |
|
518 |
free(newBuffer); |
|
519 |
} |
|
520 |
/* reset everything */ |
|
521 |
scratchPtr = scratchBuffer->chars + 256; |
|
522 |
nScratch = sizeof(scratchBuffer->chars) - 256; |
|
523 |
codeLen = 9; |
|
524 |
nextCode = 258; |
|
525 |
mask = 0x1FF; |
|
526 |
/* fetch code */ |
|
527 |
while (nBits < codeLen) { |
|
528 |
bits = (bits<<8) | *from++; |
|
529 |
inCount--; |
|
530 |
nBits += 8; |
|
531 |
} |
|
532 |
shift = nBits - codeLen; |
|
533 |
code = (bits >> shift) & mask; |
|
534 |
bits &= ~(mask << shift); |
|
535 |
nBits -= codeLen; |
|
536 |
if (code == 257) break; |
|
537 |
/* add to output */ |
|
538 |
*to++ = code; |
|
539 |
oldCode = code; |
|
540 |
} else { |
|
541 |
if (code < nextCode) { |
|
542 |
/* writeString(string[code]) */ |
|
543 |
len = stringLen[code]; |
|
544 |
bcopy(strings[code], to, len); |
|
545 |
to += len; |
|
0 | 546 |
|
77 | 547 |
/* add( string[oldcode] + first(string[code]) ) */ |
548 |
len = stringLen[oldCode] + 1; |
|
549 |
if (nScratch < len) { |
|
550 |
newBuffer = (struct buffer *)malloc(sizeof(struct buffer)); |
|
551 |
if (! newBuffer) goto out; |
|
552 |
newBuffer->prev = scratchBuffer; |
|
553 |
scratchBuffer = newBuffer; |
|
554 |
scratchPtr = scratchBuffer->chars; |
|
555 |
nScratch = sizeof(scratchBuffer->chars); |
|
556 |
} |
|
557 |
stringLen[nextCode] = len; |
|
558 |
strings[nextCode] = scratchPtr; |
|
559 |
bcopy(strings[oldCode], scratchPtr, len-1); |
|
560 |
scratchPtr += len-1; |
|
561 |
*scratchPtr++ = strings[code][0]; |
|
562 |
nScratch -= len; |
|
563 |
} else { |
|
564 |
/* writeString(string[oldCode] + first(string[oldCode]) ) */ |
|
565 |
len = stringLen[oldCode]; |
|
566 |
bcopy(strings[oldCode], to, len); |
|
567 |
to += len; |
|
568 |
*to++ = strings[oldCode][0]; |
|
0 | 569 |
|
77 | 570 |
/* add( string[oldcode] + first(string[oldCode]) ) */ |
571 |
len++; |
|
572 |
if (nScratch < len) { |
|
573 |
newBuffer = (struct buffer *)malloc(sizeof(struct buffer)); |
|
574 |
if (! newBuffer) goto out; |
|
575 |
newBuffer->prev = scratchBuffer; |
|
576 |
scratchBuffer = newBuffer; |
|
577 |
scratchPtr = scratchBuffer->chars; |
|
578 |
nScratch = sizeof(scratchBuffer->chars); |
|
579 |
} |
|
580 |
stringLen[nextCode] = len; |
|
581 |
strings[nextCode] = scratchPtr; |
|
582 |
bcopy(strings[oldCode], scratchPtr, len-1); |
|
583 |
scratchPtr += len-1; |
|
584 |
*scratchPtr++ = strings[oldCode][0]; |
|
585 |
nScratch -= len; |
|
586 |
} |
|
587 |
oldCode = code; |
|
588 |
nextCode++; |
|
589 |
if (nextCode >= 511) |
|
590 |
if (nextCode == 511) { |
|
591 |
codeLen = 10; |
|
592 |
mask = 0x3FF; |
|
593 |
} else if (nextCode >= 1023) |
|
594 |
if (nextCode == 1023) { |
|
595 |
codeLen = 11; |
|
596 |
mask = 0x7FF; |
|
597 |
} else |
|
598 |
if (nextCode == 2047) { |
|
599 |
codeLen = 12; |
|
600 |
mask = 0xFFF; |
|
601 |
} |
|
602 |
} |
|
0 | 603 |
} |
12 | 604 |
out: ; |
0 | 605 |
/* free stuff */ |
606 |
while (scratchBuffer) { |
|
77 | 607 |
newBuffer = scratchBuffer; |
608 |
scratchBuffer = scratchBuffer->prev; |
|
609 |
free(newBuffer); |
|
0 | 610 |
} |
46 | 611 |
|
12 | 612 |
free(strings); |
613 |
free(stringLen); |
|
46 | 614 |
|
12 | 615 |
return 1; |
0 | 616 |
} |
617 |
||
618 |
/* |
|
619 |
* delta decoding (TIFF predictor = 2) |
|
620 |
*/ |
|
189 | 621 |
static |
77 | 622 |
__decodeDelta__(bytes, width, height) |
0 | 623 |
register unsigned char *bytes; |
624 |
{ |
|
77 | 625 |
register w; |
626 |
unsigned char r, g, b; |
|
0 | 627 |
|
77 | 628 |
while (height--) { |
629 |
r = g = b = 0; |
|
630 |
for (w = width; w; w--) { |
|
631 |
r += *bytes; |
|
632 |
*bytes++ = r; |
|
633 |
g += *bytes; |
|
634 |
*bytes++ = g; |
|
635 |
b += *bytes; |
|
636 |
*bytes++ = b; |
|
637 |
} |
|
638 |
} |
|
0 | 639 |
} |
640 |
||
46 | 641 |
/* |
642 |
* GIF decompression |
|
643 |
*/ |
|
189 | 644 |
static int |
77 | 645 |
__decodeGIF__(from, to, inCount, initialCodeLen) |
0 | 646 |
unsigned char *from; |
647 |
unsigned char *to; |
|
648 |
{ |
|
649 |
register unsigned code; |
|
12 | 650 |
unsigned short *prefix; |
651 |
unsigned short *suffix; |
|
652 |
unsigned short *outCode; |
|
0 | 653 |
int outCount; |
654 |
unsigned maxCode, oldCode, fin, inCode, curCode; |
|
655 |
register unsigned bits; |
|
656 |
register int nBits, mask, shift; |
|
657 |
int i; |
|
658 |
int len; |
|
659 |
int endCode, clearCode, freeCode; |
|
660 |
int codeLen = initialCodeLen; |
|
12 | 661 |
static int ranges[] = {0, 1, 2, 4, 8, 16, 32, 64, |
77 | 662 |
128, 256, 512, 1024, 2048 }; |
0 | 663 |
|
12 | 664 |
prefix = (unsigned short *)malloc(sizeof(short) * 4096); |
665 |
if (! prefix) return 0; |
|
666 |
suffix = (unsigned short *)malloc(sizeof(short) * 4096); |
|
667 |
if (! suffix) { |
|
77 | 668 |
free(prefix); |
669 |
return 0; |
|
12 | 670 |
} |
671 |
outCode = (unsigned short *)malloc(sizeof(short) * 4096); |
|
672 |
if (! outCode) { |
|
77 | 673 |
free(prefix); |
674 |
free(suffix); |
|
675 |
return 0; |
|
12 | 676 |
} |
0 | 677 |
clearCode = ranges[codeLen]; /* 256 */ |
678 |
endCode = clearCode + 1; /* 257 */ |
|
679 |
freeCode = clearCode + 2; /* 258 */ |
|
680 |
maxCode = clearCode << 1; /* 512 */ |
|
681 |
outCount = 0; |
|
682 |
||
683 |
mask = maxCode - 1; /* 1FF */ |
|
684 |
nBits = 0; |
|
685 |
bits = 0; |
|
686 |
while (inCount) { |
|
77 | 687 |
/* fetch code */ |
688 |
while (nBits < codeLen) { |
|
689 |
bits = bits | (*from++ << nBits); |
|
690 |
inCount--; |
|
691 |
nBits += 8; |
|
692 |
} |
|
693 |
code = bits & mask; |
|
694 |
bits >>= codeLen; |
|
695 |
nBits -= codeLen; |
|
696 |
if (code == endCode) break; |
|
697 |
if (code == clearCode) { |
|
698 |
if (! inCount) |
|
699 |
break; |
|
0 | 700 |
|
77 | 701 |
codeLen = initialCodeLen; |
702 |
maxCode = clearCode<<1; |
|
703 |
mask = maxCode - 1; |
|
704 |
freeCode = clearCode + 2; |
|
0 | 705 |
|
77 | 706 |
/* fetch code */ |
707 |
while (nBits < codeLen) { |
|
708 |
bits = bits | (*from++ << nBits); |
|
709 |
inCount--; |
|
710 |
nBits += 8; |
|
711 |
} |
|
712 |
code = bits & mask; |
|
713 |
bits >>= codeLen; |
|
714 |
nBits -= codeLen; |
|
715 |
if (code == endCode) break; |
|
716 |
/* add to output */ |
|
717 |
*to++ = code; |
|
718 |
oldCode = fin = curCode = code; |
|
719 |
} else { |
|
720 |
curCode = inCode = code; |
|
721 |
if (curCode >= freeCode) { |
|
722 |
curCode = oldCode; |
|
723 |
outCode[outCount++] = fin; |
|
724 |
} |
|
0 | 725 |
|
77 | 726 |
while (curCode >= clearCode) { |
727 |
if (outCount > 1024) { |
|
728 |
goto out; |
|
729 |
} |
|
730 |
outCode[outCount++] = suffix[curCode]; |
|
731 |
curCode = prefix[curCode]; |
|
732 |
} |
|
0 | 733 |
|
77 | 734 |
fin = curCode; |
735 |
outCode[outCount++] = fin; |
|
0 | 736 |
|
77 | 737 |
for (i = outCount - 1; i >= 0; i--) |
738 |
*to++ = outCode[i]; |
|
739 |
outCount = 0; |
|
0 | 740 |
|
77 | 741 |
prefix[freeCode] = oldCode; |
742 |
suffix[freeCode] = fin; |
|
743 |
oldCode = inCode; |
|
0 | 744 |
|
77 | 745 |
freeCode++; |
746 |
if (freeCode >= maxCode) { |
|
747 |
if (codeLen < 12) { |
|
748 |
codeLen++; |
|
749 |
maxCode *= 2; |
|
750 |
mask = (1 << codeLen) - 1; |
|
751 |
} |
|
752 |
} |
|
753 |
} |
|
0 | 754 |
} |
12 | 755 |
out: ; |
756 |
free(prefix); |
|
757 |
free(suffix); |
|
758 |
free(outCode); |
|
46 | 759 |
|
12 | 760 |
return 1; |
0 | 761 |
} |
762 |
||
189 | 763 |
/* |
764 |
* BMP file read/decompression |
|
765 |
*/ |
|
766 |
static int |
|
767 |
loadBMP1to8(w, h, fp, dest) |
|
768 |
int w, h; |
|
769 |
FILE *fp; |
|
770 |
char *dest; |
|
771 |
{ |
|
772 |
int i,j,c,bitnum,padw; |
|
773 |
unsigned char *pp; |
|
774 |
||
775 |
c = 0; |
|
776 |
padw = ((w + 31)/32) * 32; /* 'w', padded to be a multiple of 32 */ |
|
777 |
||
778 |
for (i=h-1; i>=0; i--) { |
|
779 |
pp = dest + (i * w); |
|
780 |
for (j=bitnum=0; j<padw; j++,bitnum++) { |
|
781 |
if ((bitnum&7) == 0) { /* read the next byte */ |
|
782 |
c = getc(fp); |
|
783 |
bitnum = 0; |
|
784 |
} |
|
785 |
if (j<w) { |
|
786 |
*pp++ = (c & 0x80) ? 1 : 0; |
|
787 |
c <<= 1; |
|
788 |
} |
|
789 |
} |
|
790 |
if (ferror(fp)) return 0; |
|
791 |
} |
|
792 |
if (ferror(fp)) return 0; |
|
793 |
return 1; |
|
794 |
} |
|
795 |
||
796 |
static int |
|
797 |
loadBMP2to8(w, h, fp, dest) |
|
798 |
int w, h; |
|
799 |
FILE *fp; |
|
800 |
char *dest; |
|
801 |
{ |
|
802 |
int i,j,c,bitnum,padw; |
|
803 |
unsigned char *pp; |
|
804 |
||
805 |
c = 0; |
|
806 |
padw = ((w + 31)/32) * 32; /* 'w', padded to be a multiple of 32 */ |
|
807 |
||
808 |
for (i=h-1; i>=0; i--) { |
|
809 |
pp = dest + (i * w); |
|
810 |
for (j=bitnum=0; j<padw; j++,bitnum++) { |
|
811 |
if ((bitnum&3) == 0) { /* read the next byte */ |
|
812 |
c = getc(fp); |
|
813 |
bitnum = 0; |
|
814 |
} |
|
815 |
if (j<w) { |
|
816 |
*pp++ = (c & 0xC0) >> 6; |
|
817 |
c <<= 2; |
|
818 |
} |
|
819 |
} |
|
820 |
if (ferror(fp)) return 0; |
|
821 |
} |
|
822 |
if (ferror(fp)) return 0; |
|
823 |
return 1; |
|
824 |
} |
|
825 |
||
826 |
static int |
|
827 |
loadBMP4to8(w, h, comp, fp, dest) |
|
828 |
int w, h; |
|
829 |
FILE *fp; |
|
830 |
char *dest; |
|
831 |
{ |
|
832 |
int i,j,c,c1,padw,x,y,nybnum; |
|
833 |
unsigned char *pp; |
|
834 |
||
835 |
c = c1 = 0; |
|
836 |
||
837 |
if (comp == 0) { /* read uncompressed data */ |
|
838 |
padw = ((w + 7)/8) * 8; /* 'w' padded to a multiple of 8pix (32 bits) */ |
|
839 |
||
840 |
for (i=h-1; i>=0; i--) { |
|
841 |
pp = dest + (i * w); |
|
842 |
||
843 |
for (j=nybnum=0; j<padw; j++,nybnum++) { |
|
844 |
if ((nybnum & 1) == 0) { /* read next byte */ |
|
845 |
c = getc(fp); |
|
846 |
nybnum = 0; |
|
847 |
} |
|
848 |
||
849 |
if (j<w) { |
|
850 |
*pp++ = (c & 0xf0) >> 4; |
|
851 |
c <<= 4; |
|
852 |
} |
|
853 |
} |
|
854 |
if (ferror(fp)) return 0; |
|
855 |
} |
|
856 |
} else { |
|
857 |
if (comp == 2) { /* read RLE4 compressed data */ |
|
858 |
x = y = 0; |
|
859 |
pp = dest + x + (h-y-1)*w; |
|
860 |
||
861 |
while (y<h) { |
|
862 |
c = getc(fp); if (c == EOF) return 0; |
|
863 |
||
864 |
if (c) { /* encoded mode */ |
|
865 |
c1 = getc(fp); |
|
866 |
for (i=0; i<c; i++,x++,pp++) |
|
867 |
*pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); |
|
868 |
} else { |
|
869 |
/* c==0x00 : escape codes */ |
|
870 |
c = getc(fp); if (c == EOF) return 0; |
|
871 |
||
872 |
if (c == 0x00) { /* end of line */ |
|
873 |
x=0; y++; pp = dest + x + (h-y-1)*w; |
|
874 |
} else |
|
875 |
if (c == 0x01) break; /* end of pic8 */ |
|
876 |
||
877 |
else |
|
878 |
if (c == 0x02) { /* delta */ |
|
879 |
c = getc(fp); x += c; |
|
880 |
c = getc(fp); y += c; |
|
881 |
pp = dest + x + (h-y-1)*w; |
|
882 |
} else { /* absolute mode */ |
|
883 |
for (i=0; i<c; i++, x++, pp++) { |
|
884 |
if ((i&1) == 0) c1 = getc(fp); |
|
885 |
*pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); |
|
886 |
} |
|
887 |
||
888 |
if (((c&3)==1) || ((c&3)==2)) getc(fp); /* read pad byte */ |
|
889 |
} |
|
890 |
} /* escape processing */ |
|
891 |
if (ferror(fp)) return 0; |
|
892 |
} /* while */ |
|
893 |
} else { |
|
894 |
return 0; |
|
895 |
} |
|
896 |
} |
|
897 |
if (ferror(fp)) return 0; |
|
898 |
return 1; |
|
899 |
} |
|
900 |
||
901 |
static int |
|
902 |
loadBMP8(w, h, comp, fp, dest) |
|
903 |
int w, h; |
|
904 |
FILE *fp; |
|
905 |
char *dest; |
|
906 |
{ |
|
907 |
int i,j,c,c1,padw,x,y; |
|
908 |
unsigned char *pp; |
|
909 |
||
910 |
if (comp == 0) { /* uncompressed data */ |
|
911 |
padw = ((w + 3)/4) * 4; |
|
912 |
||
913 |
for (i=h-1; i>=0; i--) { |
|
914 |
pp = dest + (i * w); |
|
915 |
||
916 |
for (j=0; j<padw; j++) { |
|
917 |
c = getc(fp); |
|
918 |
if (c==EOF) return 0; |
|
919 |
||
920 |
if (j<w) *pp++ = c; |
|
921 |
} |
|
922 |
if (ferror(fp)) return 0; |
|
923 |
} |
|
924 |
} else { |
|
925 |
if (comp == 1) { /* RLE8 compressed */ |
|
926 |
x = y = 0; |
|
927 |
pp = dest + x + (h-y-1)*w; |
|
928 |
||
929 |
while (y<h) { |
|
930 |
c = getc(fp); |
|
931 |
if (c == EOF) return 0; |
|
932 |
||
933 |
if (c) { /* encoded mode */ |
|
934 |
c1 = getc(fp); |
|
935 |
for (i=0; i<c; i++,x++,pp++) *pp = c1; |
|
936 |
} else { |
|
937 |
/* c==0x00 : escape codes */ |
|
938 |
c = getc(fp); |
|
939 |
if (c == EOF) return 0; |
|
940 |
||
941 |
if (c == 0x00) { /* end of line */ |
|
942 |
x=0; y++; pp = dest + x + (h-y-1)*w; |
|
943 |
} else |
|
944 |
if (c == 0x01) break; /* end of pic8 */ |
|
945 |
||
946 |
else if (c == 0x02) { /* delta */ |
|
947 |
c = getc(fp); x += c; |
|
948 |
c = getc(fp); y += c; |
|
949 |
pp = dest + x + (h-y-1)*w; |
|
950 |
} else { /* absolute mode */ |
|
951 |
for (i=0; i<c; i++, x++, pp++) { |
|
952 |
c1 = getc(fp); |
|
953 |
*pp = c1; |
|
954 |
} |
|
955 |
||
956 |
if (c & 1) getc(fp); /* odd length run: read an extra pad byte */ |
|
957 |
} |
|
958 |
} |
|
959 |
if (ferror(fp)) return 0; |
|
960 |
} |
|
961 |
} else { |
|
962 |
return 0; |
|
963 |
} |
|
964 |
} |
|
965 |
if (ferror(fp)) return 0; |
|
966 |
return 1; |
|
967 |
} |
|
968 |
||
0 | 969 |
%} |
77 | 970 |
! ! |
105 | 971 |
|
972 |
!ImageReader class methodsFor:'documentation'! |
|
973 |
||
974 |
copyright |
|
975 |
" |
|
976 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
977 |
All Rights Reserved |
|
978 |
||
979 |
This software is furnished under a license and may be used |
|
980 |
only in accordance with the terms of that license and with the |
|
981 |
inclusion of the above copyright notice. This software may not |
|
982 |
be provided or otherwise made available to, or used by, any |
|
983 |
other person. No title to or ownership of the software is |
|
984 |
hereby transferred. |
|
985 |
" |
|
986 |
! |
|
987 |
||
988 |
version |
|
219
9ff0660f447f
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
207
diff
changeset
|
989 |
^ '$Header: /cvs/stx/stx/libview/ImageReader.st,v 1.23 1995-11-11 15:51:31 cg Exp $' |
105 | 990 |
! |
991 |
||
992 |
documentation |
|
993 |
" |
|
994 |
Abstract class to provide common functions for image-readers |
|
995 |
(i.e. TIFFReader, GIFReader etc.) |
|
996 |
||
997 |
ImageReaders are created temporary to read an image from a stream. |
|
998 |
They read the stream and collect all relevant information internally. |
|
118 | 999 |
Once done with reading, the actual image object is created and |
1000 |
data filled in from the imageReaders collected info. |
|
105 | 1001 |
|
1002 |
See implementation of fromStream: in concrete subclasses. |
|
1003 |
The public interfaces are: |
|
1004 |
<ConcreteReaderClass> fromFile:aFilename |
|
1005 |
or: |
|
1006 |
<ConcreteReaderClass> fromStream:aStream |
|
1007 |
||
1008 |
However, usually this is done indirectly through |
|
1009 |
Image fromFile:aFileName |
|
118 | 1010 |
which tries to find an appropriate readerClass by the fileNames |
1011 |
extension (i.e. .tiff, .gif etc.) and/or by asking the readers |
|
1012 |
if they know about the format of the file (see #isValidImageFile:). |
|
121 | 1013 |
|
1014 |
If you add a new reader, dont forget to add the method #isValidImageFile: |
|
1015 |
if this reader supports reading image files. |
|
1016 |
And/or if it supports writing files, dont forget to add #canRepresent:. |
|
105 | 1017 |
" |
1018 |
! ! |
|
1019 |
||
1020 |
!ImageReader class methodsFor:'i/o support'! |
|
1021 |
||
1022 |
streamReadingFile:aFilename |
|
1023 |
"return a stream to read aFilename. |
|
1024 |
If the filename ends with '.Z' or '.gz', return a stream |
|
1025 |
to a pipe for the uncompressor. Otherwise, return a stream to read |
|
1026 |
the file directly." |
|
1027 |
||
1028 |
|inStream name| |
|
1029 |
||
1030 |
name := aFilename asString. |
|
1031 |
((name endsWith:'.Z') or:[name endsWith:'.gz']) ifTrue:[ |
|
1032 |
inStream := PipeStream readingFrom:'gunzip < ' , name. |
|
1033 |
inStream isNil ifTrue:[ |
|
1034 |
inStream := PipeStream readingFrom:'uncompress < ' , name. |
|
1035 |
] |
|
1036 |
] ifFalse:[ |
|
120 | 1037 |
inStream := Smalltalk systemFileStreamFor:aFilename. |
1038 |
inStream isNil ifTrue:[ |
|
134 | 1039 |
inStream := Smalltalk bitmapFileStreamFor:aFilename |
120 | 1040 |
] |
105 | 1041 |
]. |
1042 |
inStream isNil ifTrue:[ |
|
1043 |
'IMGREADER: open error on: ' errorPrint. aFilename errorPrintNL. |
|
1044 |
]. |
|
1045 |
^ inStream |
|
1046 |
! ! |
|
1047 |
||
118 | 1048 |
!ImageReader class methodsFor:'image reading'! |
105 | 1049 |
|
1050 |
fromStream:aStream |
|
1051 |
"read an image (in my format) from aStream" |
|
1052 |
||
1053 |
|reader| |
|
1054 |
||
1055 |
reader := self new fromStream:aStream. |
|
1056 |
reader notNil ifTrue:[ |
|
1057 |
^ reader image |
|
1058 |
]. |
|
1059 |
^ nil |
|
1060 |
! |
|
1061 |
||
1062 |
fromFile:aFileName |
|
1063 |
"read an image (in my format) from aFileName. |
|
1064 |
Return the image or nil on error." |
|
1065 |
||
1066 |
|result inStream| |
|
1067 |
||
1068 |
inStream := self streamReadingFile:aFileName. |
|
1069 |
inStream isNil ifTrue:[ |
|
1070 |
'IMGREADER: file open error' errorPrintNL. |
|
1071 |
^ nil |
|
1072 |
]. |
|
1073 |
result := self fromStream:inStream. |
|
1074 |
inStream close. |
|
1075 |
^ result |
|
1076 |
||
1077 |
" |
|
1078 |
XPMReader fromFile:'bitmaps/ljet.xpm' |
|
1079 |
XBMReader fromFile:'bitmaps/SBrowser.xbm' |
|
1080 |
" |
|
118 | 1081 |
! ! |
1082 |
||
1083 |
!ImageReader class methodsFor:'image writing'! |
|
105 | 1084 |
|
1085 |
save:anImage onFile:aFileName |
|
1086 |
"save the image in my format on aFileName" |
|
1087 |
||
1088 |
^ (self basicNew) save:anImage onFile:aFileName |
|
1089 |
! ! |
|
1090 |
||
1091 |
!ImageReader class methodsFor:'testing'! |
|
1092 |
||
1093 |
isValidImageFile:aFileName |
|
1094 |
"return true, if aFileName contains an image this |
|
121 | 1095 |
reader understands - must be redefined in concrete subclasses |
1096 |
which support reading." |
|
1097 |
||
1098 |
^ false |
|
1099 |
! |
|
1100 |
||
1101 |
canRepresent:anImage |
|
1102 |
"return true, if anImage can be represented in my file format. |
|
1103 |
must be redefined in concrete subclasses which support saving." |
|
105 | 1104 |
|
1105 |
^ false |
|
1106 |
! ! |
|
1107 |
||
1108 |
!ImageReader class methodsFor:'constants'! |
|
1109 |
||
1110 |
reverseBits |
|
1111 |
"return a table filled with bit reverse information. |
|
1112 |
To convert from msbit-first to lsbit-first bytes, use |
|
1113 |
the value as index into the table, retrieving the reverse |
|
1114 |
value. Since indexing must start at 1, use (value + 1) as |
|
1115 |
index." |
|
1116 |
||
1117 |
|val "{ Class: SmallInteger }" | |
|
1118 |
||
1119 |
ReverseBits isNil ifTrue:[ |
|
153 | 1120 |
ReverseBits := ByteArray uninitializedNew:256. |
105 | 1121 |
0 to:255 do:[:i | |
1122 |
val := 0. |
|
1123 |
(i bitTest:16r01) ifTrue:[val := val bitOr:16r80]. |
|
1124 |
(i bitTest:16r02) ifTrue:[val := val bitOr:16r40]. |
|
1125 |
(i bitTest:16r04) ifTrue:[val := val bitOr:16r20]. |
|
1126 |
(i bitTest:16r08) ifTrue:[val := val bitOr:16r10]. |
|
1127 |
(i bitTest:16r10) ifTrue:[val := val bitOr:16r08]. |
|
1128 |
(i bitTest:16r20) ifTrue:[val := val bitOr:16r04]. |
|
1129 |
(i bitTest:16r40) ifTrue:[val := val bitOr:16r02]. |
|
1130 |
(i bitTest:16r80) ifTrue:[val := val bitOr:16r01]. |
|
1131 |
ReverseBits at:(i + 1) put:val |
|
1132 |
] |
|
1133 |
]. |
|
1134 |
^ ReverseBits |
|
1135 |
! ! |
|
1136 |
||
1137 |
!ImageReader class methodsFor:'cleanup'! |
|
1138 |
||
1139 |
lowSpaceCleanup |
|
1140 |
"cleanup things we do not need" |
|
1141 |
||
1142 |
ReverseBits := nil |
|
1143 |
! ! |
|
1144 |
||
1145 |
!ImageReader class methodsFor:'decompression support'! |
|
1146 |
||
1147 |
decompressCCITT3From:srcBytes into:dstBytes startingAt:offset count:count |
|
1148 |
"decompress CCITT Group 3 compressed image data. |
|
1149 |
count bytes from srcBytes are decompressed into dstBytes. |
|
1150 |
Calls primitive c function for speed" |
|
1151 |
%{ |
|
1152 |
if (__isByteArray(srcBytes) |
|
1153 |
&& __isByteArray(dstBytes) |
|
1154 |
&& __bothSmallInteger(offset, count)) { |
|
1155 |
if (__decodeCCITTgroup3__(_ByteArrayInstPtr(srcBytes)->ba_element, |
|
1156 |
_ByteArrayInstPtr(dstBytes)->ba_element |
|
1157 |
+ _intVal(offset) - 1, |
|
1158 |
_intVal(count))) { |
|
1159 |
RETURN ( self ); |
|
1160 |
} |
|
1161 |
} |
|
1162 |
%} |
|
1163 |
. |
|
1164 |
self primitiveFailed |
|
1165 |
! |
|
1166 |
||
1167 |
decompressLZWFrom:srcBytes count:count into:dstBytes startingAt:offset |
|
1168 |
"decompress LZW (tiff) compressed image data. |
|
1169 |
count bytes from srcBytes are decompressed into dstBytes. |
|
1170 |
Calls primitive c function for speed" |
|
1171 |
%{ |
|
1172 |
if (__isByteArray(srcBytes) |
|
1173 |
&& __isByteArray(dstBytes) |
|
1174 |
&& __bothSmallInteger(offset, count)) { |
|
1175 |
if (__decodeLZW__(_ByteArrayInstPtr(srcBytes)->ba_element, |
|
1176 |
_ByteArrayInstPtr(dstBytes)->ba_element |
|
1177 |
+ _intVal(offset) - 1, |
|
1178 |
_intVal(count))) { |
|
1179 |
RETURN ( self ); |
|
1180 |
} |
|
1181 |
} |
|
1182 |
%} |
|
1183 |
. |
|
1184 |
self primitiveFailed |
|
1185 |
! |
|
1186 |
||
1187 |
decompressGIFFrom:srcBytes count:count into:dstBytes startingAt:offset codeLen:codeLen |
|
1188 |
"decompress GIF compressed image data. |
|
1189 |
count bytes from srcBytes are decompressed into dstBytes. |
|
1190 |
Calls primitive c function for speed" |
|
1191 |
%{ |
|
1192 |
if (__isByteArray(srcBytes) |
|
1193 |
&& __isByteArray(dstBytes) |
|
1194 |
&& __bothSmallInteger(codeLen, offset) |
|
1195 |
&& __isSmallInteger(count)) { |
|
1196 |
if (__decodeGIF__(_ByteArrayInstPtr(srcBytes)->ba_element, |
|
1197 |
_ByteArrayInstPtr(dstBytes)->ba_element |
|
1198 |
+ _intVal(offset) - 1, |
|
1199 |
_intVal(count), |
|
1200 |
_intVal(codeLen))) { |
|
1201 |
RETURN ( self ); |
|
1202 |
} |
|
1203 |
} |
|
1204 |
%} |
|
1205 |
. |
|
1206 |
self primitiveFailed |
|
1207 |
! |
|
1208 |
||
1209 |
decodeDelta:step in:data width:width height:height |
|
1210 |
"perform NeXT special predictor delta decoding inplace in data. |
|
1211 |
Calls primitive c function for speed" |
|
1212 |
||
1213 |
(step ~~ 3) ifTrue:[ |
|
1214 |
^ self error:'only rgb pictures supported' |
|
1215 |
]. |
|
1216 |
||
1217 |
%{ |
|
1218 |
if (__isByteArray(data) |
|
1219 |
&& __bothSmallInteger(width, height)) { |
|
1220 |
__decodeDelta__(_ByteArrayInstPtr(data)->ba_element, |
|
1221 |
_intVal(width), _intVal(height)); |
|
1222 |
RETURN ( self ); |
|
1223 |
} |
|
1224 |
%} |
|
1225 |
. |
|
1226 |
self primitiveFailed |
|
189 | 1227 |
! |
1228 |
||
1229 |
loadBMP1to8Width:width height:height from:aStream into:aByteArray |
|
1230 |
|f| |
|
1231 |
||
1232 |
aStream isExternalStream ifFalse:[^ false]. |
|
1233 |
f := aStream filePointer. |
|
1234 |
f isNil ifTrue:[^ false]. |
|
1235 |
%{ |
|
1236 |
if (! (__bothSmallInteger(width, height) |
|
1237 |
&& __isByteArray(aByteArray))) { |
|
1238 |
RETURN (false); |
|
1239 |
} |
|
1240 |
if (loadBMP1to8(__intVal(width), __intVal(height), |
|
207
9124817bbb03
use new c-pointer wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
189
diff
changeset
|
1241 |
__FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) { |
189 | 1242 |
RETURN (true); |
1243 |
} |
|
1244 |
RETURN (false); |
|
1245 |
%} |
|
1246 |
! |
|
1247 |
||
1248 |
loadBMP2to8Width:width height:height from:aStream into:aByteArray |
|
1249 |
|f| |
|
1250 |
||
1251 |
aStream isExternalStream ifFalse:[^ false]. |
|
1252 |
f := aStream filePointer. |
|
1253 |
f isNil ifTrue:[^ false]. |
|
1254 |
%{ |
|
1255 |
if (! (__bothSmallInteger(width, height) |
|
1256 |
&& __isByteArray(aByteArray))) { |
|
1257 |
RETURN (false); |
|
1258 |
} |
|
1259 |
if (loadBMP2to8(__intVal(width), __intVal(height), |
|
207
9124817bbb03
use new c-pointer wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
189
diff
changeset
|
1260 |
__FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) { |
189 | 1261 |
RETURN (true); |
1262 |
} |
|
1263 |
RETURN (false); |
|
1264 |
%} |
|
1265 |
! |
|
1266 |
||
1267 |
loadBMP8Width:width height:height compression:compression from:aStream into:aByteArray |
|
1268 |
|f| |
|
1269 |
||
1270 |
aStream isExternalStream ifFalse:[^ false]. |
|
1271 |
f := aStream filePointer. |
|
1272 |
f isNil ifTrue:[^ false]. |
|
1273 |
%{ |
|
1274 |
if (! (__bothSmallInteger(width, height) |
|
1275 |
&& __isSmallInteger(compression) |
|
1276 |
&& __isByteArray(aByteArray))) { |
|
1277 |
RETURN (false); |
|
1278 |
} |
|
1279 |
if (loadBMP8(__intVal(width), __intVal(height), __intVal(compression), |
|
207
9124817bbb03
use new c-pointer wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
189
diff
changeset
|
1280 |
__FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) { |
189 | 1281 |
RETURN (true); |
1282 |
} |
|
1283 |
RETURN (false); |
|
1284 |
%} |
|
1285 |
! |
|
1286 |
||
1287 |
loadBMP4to8Width:width height:height compression:compression from:aStream into:aByteArray |
|
1288 |
|f| |
|
1289 |
||
1290 |
aStream isExternalStream ifFalse:[^ false]. |
|
1291 |
f := aStream filePointer. |
|
1292 |
f isNil ifTrue:[^ false]. |
|
1293 |
%{ |
|
1294 |
if (! (__bothSmallInteger(width, height) |
|
1295 |
&& __isSmallInteger(compression) |
|
1296 |
&& __isByteArray(aByteArray))) { |
|
1297 |
RETURN (false); |
|
1298 |
} |
|
1299 |
if (loadBMP4to8(__intVal(width), __intVal(height), __intVal(compression), |
|
207
9124817bbb03
use new c-pointer wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
189
diff
changeset
|
1300 |
__FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) { |
189 | 1301 |
RETURN (true); |
1302 |
} |
|
1303 |
RETURN (false); |
|
1304 |
%} |
|
105 | 1305 |
! ! |
1306 |
||
1307 |
!ImageReader methodsFor:'accessing'! |
|
1308 |
||
1309 |
image |
|
1310 |
"return the image represented by myself" |
|
1311 |
||
1312 |
|image depth| |
|
1313 |
||
1314 |
depth := self bitsPerPixel. |
|
1315 |
image := (Image implementorForDepth:depth) new. |
|
1316 |
image width:width. |
|
1317 |
image height:height. |
|
1318 |
image photometric:photometric. |
|
1319 |
image samplesPerPixel:samplesPerPixel. |
|
1320 |
image bitsPerSample:bitsPerSample. |
|
1321 |
image colorMap:colorMap. |
|
1322 |
image data:data. |
|
1323 |
^ image |
|
1324 |
! |
|
1325 |
||
1326 |
height |
|
1327 |
^ height |
|
1328 |
! |
|
1329 |
||
1330 |
photometric |
|
1331 |
^ photometric |
|
1332 |
! |
|
1333 |
||
1334 |
width |
|
1335 |
^ width |
|
1336 |
! |
|
1337 |
||
1338 |
samplesPerPixel |
|
1339 |
^ samplesPerPixel |
|
1340 |
! |
|
1341 |
||
1342 |
data |
|
1343 |
^ data |
|
1344 |
! |
|
1345 |
||
1346 |
colorMap |
|
1347 |
^ colorMap |
|
1348 |
! |
|
1349 |
||
1350 |
bitsPerSample |
|
1351 |
^ bitsPerSample |
|
1352 |
! |
|
1353 |
||
1354 |
bitsPerPixel |
|
1355 |
"return the number of bits per pixel" |
|
1356 |
||
1357 |
^ (bitsPerSample inject:0 into:[:sum :i | sum + i]) |
|
1358 |
! ! |
|
1359 |
||
118 | 1360 |
!ImageReader methodsFor:'image writing'! |
105 | 1361 |
|
1362 |
save:image onFile:aFileName |
|
1363 |
"save image in my format on aFile" |
|
1364 |
||
1365 |
^ self subclassResponsibility |
|
118 | 1366 |
! ! |
1367 |
||
1368 |
!ImageReader methodsFor:'image reading'! |
|
105 | 1369 |
|
1370 |
fromStream:aStream |
|
1371 |
"read imagedata in my format from aStream" |
|
1372 |
||
1373 |
^ self subclassResponsibility |
|
1374 |
! ! |
|
1375 |
||
1376 |
!ImageReader methodsFor:'i/o support'! |
|
1377 |
||
1378 |
readLong |
|
1379 |
"return the next 4-byte long, honoring the byte-order" |
|
1380 |
||
1381 |
^ inStream nextLongMSB:(byteOrder ~~ #lsb) |
|
1382 |
! |
|
1383 |
||
1384 |
readShort |
|
1385 |
"return the next 2-byte short, honoring the byte-order" |
|
1386 |
||
1387 |
^ inStream nextUnsignedShortMSB:(byteOrder ~~ #lsb) |
|
1388 |
! |
|
1389 |
||
1390 |
readShortLong |
|
1391 |
"return the next 2-byte short, honoring the byte-order. |
|
1392 |
There are actually 4 bytes read, but only 2 looked at." |
|
1393 |
||
1394 |
|bytes val| |
|
1395 |
||
153 | 1396 |
bytes := ByteArray uninitializedNew:4. |
105 | 1397 |
inStream nextBytes:4 into:bytes. |
1398 |
(byteOrder == #lsb) ifTrue:[ |
|
1399 |
val := bytes at:2. |
|
1400 |
val := val * 256 + (bytes at:1) |
|
1401 |
] ifFalse:[ |
|
1402 |
val := bytes at:3. |
|
1403 |
val := val * 256 + (bytes at:4) |
|
1404 |
]. |
|
1405 |
^ val |
|
1406 |
! |
|
1407 |
||
1408 |
writeLong:anInteger |
|
1409 |
"write a 4-byte long, honoring the byte-order." |
|
1410 |
||
1411 |
outStream nextPutLong:anInteger MSB:(byteOrder ~~ #lsb) |
|
1412 |
! |
|
1413 |
||
1414 |
writeShort:anInteger |
|
1415 |
"write a 2-byte short, honoring the byte-order." |
|
1416 |
||
1417 |
outStream nextPutShort:anInteger MSB:(byteOrder ~~ #lsb) |
|
1418 |
! ! |