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
|
|
15 |
samplesPerPixel bitsPerSample colorMap'
|
|
16 |
classVariableNames:'ReverseBits'
|
|
17 |
poolDictionaries:''
|
|
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;
|
|
459 |
unsigned nextCode, oldCode;
|
|
460 |
register unsigned bits;
|
|
461 |
int nBits, mask, shift;
|
|
462 |
int i;
|
|
463 |
int len;
|
|
464 |
int codeLen = 9;
|
|
465 |
|
|
466 |
scratchBuffer = (struct buffer *)malloc(sizeof(struct buffer));
|
12
|
467 |
if (! scratchBuffer) return 0;
|
46
|
468 |
|
12
|
469 |
strings = (unsigned char **)malloc(sizeof(unsigned char *) * 4096);
|
|
470 |
if (! strings) {
|
77
|
471 |
free(scratchBuffer);
|
|
472 |
return 0;
|
12
|
473 |
}
|
|
474 |
stringLen = (short *)malloc(sizeof(short) * 4096);
|
|
475 |
if (! stringLen) {
|
77
|
476 |
free(strings);
|
|
477 |
free(scratchBuffer);
|
|
478 |
return 0;
|
12
|
479 |
}
|
|
480 |
|
0
|
481 |
scratchBuffer->prev = (struct buffer *)0;
|
|
482 |
scratchPtr = scratchBuffer->chars;
|
|
483 |
nScratch = sizeof(scratchBuffer->chars);
|
|
484 |
|
|
485 |
for (i = 0; i < 256; i++) {
|
77
|
486 |
*scratchPtr = i;
|
|
487 |
strings[i] = scratchPtr++;
|
|
488 |
stringLen[i] = 1;
|
0
|
489 |
}
|
|
490 |
|
|
491 |
nextCode = 258;
|
|
492 |
nScratch -= 256;
|
|
493 |
mask = 0x1FF;
|
|
494 |
nBits = 0;
|
|
495 |
bits = 0;
|
|
496 |
while (inCount) {
|
77
|
497 |
/* fetch code */
|
|
498 |
while (nBits < codeLen) {
|
|
499 |
bits = (bits<<8) | *from++;
|
|
500 |
inCount--;
|
|
501 |
nBits += 8;
|
|
502 |
}
|
|
503 |
shift = nBits - codeLen;
|
|
504 |
code = (bits >> shift) & mask;
|
|
505 |
bits &= ~(mask << shift);
|
|
506 |
nBits -= codeLen;
|
|
507 |
if (code == 257) break;
|
|
508 |
if (code == 256) {
|
|
509 |
if (! inCount)
|
|
510 |
break;
|
0
|
511 |
|
77
|
512 |
/* free stuff */
|
|
513 |
while (scratchBuffer->prev) {
|
|
514 |
newBuffer = scratchBuffer;
|
|
515 |
scratchBuffer = scratchBuffer->prev;
|
|
516 |
free(newBuffer);
|
|
517 |
}
|
|
518 |
/* reset everything */
|
|
519 |
scratchPtr = scratchBuffer->chars + 256;
|
|
520 |
nScratch = sizeof(scratchBuffer->chars) - 256;
|
|
521 |
codeLen = 9;
|
|
522 |
nextCode = 258;
|
|
523 |
mask = 0x1FF;
|
|
524 |
/* fetch code */
|
|
525 |
while (nBits < codeLen) {
|
|
526 |
bits = (bits<<8) | *from++;
|
|
527 |
inCount--;
|
|
528 |
nBits += 8;
|
|
529 |
}
|
|
530 |
shift = nBits - codeLen;
|
|
531 |
code = (bits >> shift) & mask;
|
|
532 |
bits &= ~(mask << shift);
|
|
533 |
nBits -= codeLen;
|
|
534 |
if (code == 257) break;
|
|
535 |
/* add to output */
|
|
536 |
*to++ = code;
|
|
537 |
oldCode = code;
|
|
538 |
} else {
|
|
539 |
if (code < nextCode) {
|
|
540 |
/* writeString(string[code]) */
|
|
541 |
len = stringLen[code];
|
|
542 |
bcopy(strings[code], to, len);
|
|
543 |
to += len;
|
0
|
544 |
|
77
|
545 |
/* add( string[oldcode] + first(string[code]) ) */
|
|
546 |
len = stringLen[oldCode] + 1;
|
|
547 |
if (nScratch < len) {
|
|
548 |
newBuffer = (struct buffer *)malloc(sizeof(struct buffer));
|
|
549 |
if (! newBuffer) goto out;
|
|
550 |
newBuffer->prev = scratchBuffer;
|
|
551 |
scratchBuffer = newBuffer;
|
|
552 |
scratchPtr = scratchBuffer->chars;
|
|
553 |
nScratch = sizeof(scratchBuffer->chars);
|
|
554 |
}
|
|
555 |
stringLen[nextCode] = len;
|
|
556 |
strings[nextCode] = scratchPtr;
|
|
557 |
bcopy(strings[oldCode], scratchPtr, len-1);
|
|
558 |
scratchPtr += len-1;
|
|
559 |
*scratchPtr++ = strings[code][0];
|
|
560 |
nScratch -= len;
|
|
561 |
} else {
|
|
562 |
/* writeString(string[oldCode] + first(string[oldCode]) ) */
|
|
563 |
len = stringLen[oldCode];
|
|
564 |
bcopy(strings[oldCode], to, len);
|
|
565 |
to += len;
|
|
566 |
*to++ = strings[oldCode][0];
|
0
|
567 |
|
77
|
568 |
/* add( string[oldcode] + first(string[oldCode]) ) */
|
|
569 |
len++;
|
|
570 |
if (nScratch < len) {
|
|
571 |
newBuffer = (struct buffer *)malloc(sizeof(struct buffer));
|
|
572 |
if (! newBuffer) goto out;
|
|
573 |
newBuffer->prev = scratchBuffer;
|
|
574 |
scratchBuffer = newBuffer;
|
|
575 |
scratchPtr = scratchBuffer->chars;
|
|
576 |
nScratch = sizeof(scratchBuffer->chars);
|
|
577 |
}
|
|
578 |
stringLen[nextCode] = len;
|
|
579 |
strings[nextCode] = scratchPtr;
|
|
580 |
bcopy(strings[oldCode], scratchPtr, len-1);
|
|
581 |
scratchPtr += len-1;
|
|
582 |
*scratchPtr++ = strings[oldCode][0];
|
|
583 |
nScratch -= len;
|
|
584 |
}
|
|
585 |
oldCode = code;
|
|
586 |
nextCode++;
|
|
587 |
if (nextCode >= 511)
|
|
588 |
if (nextCode == 511) {
|
|
589 |
codeLen = 10;
|
|
590 |
mask = 0x3FF;
|
|
591 |
} else if (nextCode >= 1023)
|
|
592 |
if (nextCode == 1023) {
|
|
593 |
codeLen = 11;
|
|
594 |
mask = 0x7FF;
|
|
595 |
} else
|
|
596 |
if (nextCode == 2047) {
|
|
597 |
codeLen = 12;
|
|
598 |
mask = 0xFFF;
|
|
599 |
}
|
|
600 |
}
|
0
|
601 |
}
|
12
|
602 |
out: ;
|
0
|
603 |
/* free stuff */
|
|
604 |
while (scratchBuffer) {
|
77
|
605 |
newBuffer = scratchBuffer;
|
|
606 |
scratchBuffer = scratchBuffer->prev;
|
|
607 |
free(newBuffer);
|
0
|
608 |
}
|
46
|
609 |
|
12
|
610 |
free(strings);
|
|
611 |
free(stringLen);
|
46
|
612 |
|
12
|
613 |
return 1;
|
0
|
614 |
}
|
|
615 |
|
|
616 |
/*
|
|
617 |
* delta decoding (TIFF predictor = 2)
|
|
618 |
*/
|
189
|
619 |
static
|
77
|
620 |
__decodeDelta__(bytes, width, height)
|
0
|
621 |
register unsigned char *bytes;
|
|
622 |
{
|
77
|
623 |
register w;
|
|
624 |
unsigned char r, g, b;
|
0
|
625 |
|
77
|
626 |
while (height--) {
|
|
627 |
r = g = b = 0;
|
|
628 |
for (w = width; w; w--) {
|
|
629 |
r += *bytes;
|
|
630 |
*bytes++ = r;
|
|
631 |
g += *bytes;
|
|
632 |
*bytes++ = g;
|
|
633 |
b += *bytes;
|
|
634 |
*bytes++ = b;
|
|
635 |
}
|
|
636 |
}
|
0
|
637 |
}
|
|
638 |
|
46
|
639 |
/*
|
|
640 |
* GIF decompression
|
|
641 |
*/
|
189
|
642 |
static int
|
77
|
643 |
__decodeGIF__(from, to, inCount, initialCodeLen)
|
0
|
644 |
unsigned char *from;
|
|
645 |
unsigned char *to;
|
|
646 |
{
|
|
647 |
register unsigned code;
|
12
|
648 |
unsigned short *prefix;
|
|
649 |
unsigned short *suffix;
|
|
650 |
unsigned short *outCode;
|
0
|
651 |
int outCount;
|
|
652 |
unsigned maxCode, oldCode, fin, inCode, curCode;
|
|
653 |
register unsigned bits;
|
|
654 |
register int nBits, mask, shift;
|
|
655 |
int i;
|
|
656 |
int len;
|
|
657 |
int endCode, clearCode, freeCode;
|
|
658 |
int codeLen = initialCodeLen;
|
12
|
659 |
static int ranges[] = {0, 1, 2, 4, 8, 16, 32, 64,
|
77
|
660 |
128, 256, 512, 1024, 2048 };
|
0
|
661 |
|
12
|
662 |
prefix = (unsigned short *)malloc(sizeof(short) * 4096);
|
|
663 |
if (! prefix) return 0;
|
|
664 |
suffix = (unsigned short *)malloc(sizeof(short) * 4096);
|
|
665 |
if (! suffix) {
|
77
|
666 |
free(prefix);
|
|
667 |
return 0;
|
12
|
668 |
}
|
|
669 |
outCode = (unsigned short *)malloc(sizeof(short) * 4096);
|
|
670 |
if (! outCode) {
|
77
|
671 |
free(prefix);
|
|
672 |
free(suffix);
|
|
673 |
return 0;
|
12
|
674 |
}
|
0
|
675 |
clearCode = ranges[codeLen]; /* 256 */
|
|
676 |
endCode = clearCode + 1; /* 257 */
|
|
677 |
freeCode = clearCode + 2; /* 258 */
|
|
678 |
maxCode = clearCode << 1; /* 512 */
|
|
679 |
outCount = 0;
|
|
680 |
|
|
681 |
mask = maxCode - 1; /* 1FF */
|
|
682 |
nBits = 0;
|
|
683 |
bits = 0;
|
|
684 |
while (inCount) {
|
77
|
685 |
/* fetch code */
|
|
686 |
while (nBits < codeLen) {
|
|
687 |
bits = bits | (*from++ << nBits);
|
|
688 |
inCount--;
|
|
689 |
nBits += 8;
|
|
690 |
}
|
|
691 |
code = bits & mask;
|
|
692 |
bits >>= codeLen;
|
|
693 |
nBits -= codeLen;
|
|
694 |
if (code == endCode) break;
|
|
695 |
if (code == clearCode) {
|
|
696 |
if (! inCount)
|
|
697 |
break;
|
0
|
698 |
|
77
|
699 |
codeLen = initialCodeLen;
|
|
700 |
maxCode = clearCode<<1;
|
|
701 |
mask = maxCode - 1;
|
|
702 |
freeCode = clearCode + 2;
|
0
|
703 |
|
77
|
704 |
/* fetch code */
|
|
705 |
while (nBits < codeLen) {
|
|
706 |
bits = bits | (*from++ << nBits);
|
|
707 |
inCount--;
|
|
708 |
nBits += 8;
|
|
709 |
}
|
|
710 |
code = bits & mask;
|
|
711 |
bits >>= codeLen;
|
|
712 |
nBits -= codeLen;
|
|
713 |
if (code == endCode) break;
|
|
714 |
/* add to output */
|
|
715 |
*to++ = code;
|
|
716 |
oldCode = fin = curCode = code;
|
|
717 |
} else {
|
|
718 |
curCode = inCode = code;
|
|
719 |
if (curCode >= freeCode) {
|
|
720 |
curCode = oldCode;
|
|
721 |
outCode[outCount++] = fin;
|
|
722 |
}
|
0
|
723 |
|
77
|
724 |
while (curCode >= clearCode) {
|
|
725 |
if (outCount > 1024) {
|
|
726 |
goto out;
|
|
727 |
}
|
|
728 |
outCode[outCount++] = suffix[curCode];
|
|
729 |
curCode = prefix[curCode];
|
|
730 |
}
|
0
|
731 |
|
77
|
732 |
fin = curCode;
|
|
733 |
outCode[outCount++] = fin;
|
0
|
734 |
|
77
|
735 |
for (i = outCount - 1; i >= 0; i--)
|
|
736 |
*to++ = outCode[i];
|
|
737 |
outCount = 0;
|
0
|
738 |
|
77
|
739 |
prefix[freeCode] = oldCode;
|
|
740 |
suffix[freeCode] = fin;
|
|
741 |
oldCode = inCode;
|
0
|
742 |
|
77
|
743 |
freeCode++;
|
|
744 |
if (freeCode >= maxCode) {
|
|
745 |
if (codeLen < 12) {
|
|
746 |
codeLen++;
|
|
747 |
maxCode *= 2;
|
|
748 |
mask = (1 << codeLen) - 1;
|
|
749 |
}
|
|
750 |
}
|
|
751 |
}
|
0
|
752 |
}
|
12
|
753 |
out: ;
|
|
754 |
free(prefix);
|
|
755 |
free(suffix);
|
|
756 |
free(outCode);
|
46
|
757 |
|
12
|
758 |
return 1;
|
0
|
759 |
}
|
|
760 |
|
189
|
761 |
/*
|
|
762 |
* BMP file read/decompression
|
|
763 |
*/
|
|
764 |
static int
|
|
765 |
loadBMP1to8(w, h, fp, dest)
|
|
766 |
int w, h;
|
|
767 |
FILE *fp;
|
|
768 |
char *dest;
|
|
769 |
{
|
|
770 |
int i,j,c,bitnum,padw;
|
|
771 |
unsigned char *pp;
|
|
772 |
|
|
773 |
c = 0;
|
|
774 |
padw = ((w + 31)/32) * 32; /* 'w', padded to be a multiple of 32 */
|
|
775 |
|
|
776 |
for (i=h-1; i>=0; i--) {
|
|
777 |
pp = dest + (i * w);
|
|
778 |
for (j=bitnum=0; j<padw; j++,bitnum++) {
|
|
779 |
if ((bitnum&7) == 0) { /* read the next byte */
|
|
780 |
c = getc(fp);
|
|
781 |
bitnum = 0;
|
|
782 |
}
|
|
783 |
if (j<w) {
|
|
784 |
*pp++ = (c & 0x80) ? 1 : 0;
|
|
785 |
c <<= 1;
|
|
786 |
}
|
|
787 |
}
|
|
788 |
if (ferror(fp)) return 0;
|
|
789 |
}
|
|
790 |
if (ferror(fp)) return 0;
|
|
791 |
return 1;
|
|
792 |
}
|
|
793 |
|
|
794 |
static int
|
|
795 |
loadBMP2to8(w, h, fp, dest)
|
|
796 |
int w, h;
|
|
797 |
FILE *fp;
|
|
798 |
char *dest;
|
|
799 |
{
|
|
800 |
int i,j,c,bitnum,padw;
|
|
801 |
unsigned char *pp;
|
|
802 |
|
|
803 |
c = 0;
|
|
804 |
padw = ((w + 31)/32) * 32; /* 'w', padded to be a multiple of 32 */
|
|
805 |
|
|
806 |
for (i=h-1; i>=0; i--) {
|
|
807 |
pp = dest + (i * w);
|
|
808 |
for (j=bitnum=0; j<padw; j++,bitnum++) {
|
|
809 |
if ((bitnum&3) == 0) { /* read the next byte */
|
|
810 |
c = getc(fp);
|
|
811 |
bitnum = 0;
|
|
812 |
}
|
|
813 |
if (j<w) {
|
|
814 |
*pp++ = (c & 0xC0) >> 6;
|
|
815 |
c <<= 2;
|
|
816 |
}
|
|
817 |
}
|
|
818 |
if (ferror(fp)) return 0;
|
|
819 |
}
|
|
820 |
if (ferror(fp)) return 0;
|
|
821 |
return 1;
|
|
822 |
}
|
|
823 |
|
|
824 |
static int
|
|
825 |
loadBMP4to8(w, h, comp, fp, dest)
|
|
826 |
int w, h;
|
|
827 |
FILE *fp;
|
|
828 |
char *dest;
|
|
829 |
{
|
|
830 |
int i,j,c,c1,padw,x,y,nybnum;
|
|
831 |
unsigned char *pp;
|
|
832 |
|
|
833 |
c = c1 = 0;
|
|
834 |
|
|
835 |
if (comp == 0) { /* read uncompressed data */
|
|
836 |
padw = ((w + 7)/8) * 8; /* 'w' padded to a multiple of 8pix (32 bits) */
|
|
837 |
|
|
838 |
for (i=h-1; i>=0; i--) {
|
|
839 |
pp = dest + (i * w);
|
|
840 |
|
|
841 |
for (j=nybnum=0; j<padw; j++,nybnum++) {
|
|
842 |
if ((nybnum & 1) == 0) { /* read next byte */
|
|
843 |
c = getc(fp);
|
|
844 |
nybnum = 0;
|
|
845 |
}
|
|
846 |
|
|
847 |
if (j<w) {
|
|
848 |
*pp++ = (c & 0xf0) >> 4;
|
|
849 |
c <<= 4;
|
|
850 |
}
|
|
851 |
}
|
|
852 |
if (ferror(fp)) return 0;
|
|
853 |
}
|
|
854 |
} else {
|
|
855 |
if (comp == 2) { /* read RLE4 compressed data */
|
|
856 |
x = y = 0;
|
|
857 |
pp = dest + x + (h-y-1)*w;
|
|
858 |
|
|
859 |
while (y<h) {
|
|
860 |
c = getc(fp); if (c == EOF) return 0;
|
|
861 |
|
|
862 |
if (c) { /* encoded mode */
|
|
863 |
c1 = getc(fp);
|
|
864 |
for (i=0; i<c; i++,x++,pp++)
|
|
865 |
*pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f);
|
|
866 |
} else {
|
|
867 |
/* c==0x00 : escape codes */
|
|
868 |
c = getc(fp); if (c == EOF) return 0;
|
|
869 |
|
|
870 |
if (c == 0x00) { /* end of line */
|
|
871 |
x=0; y++; pp = dest + x + (h-y-1)*w;
|
|
872 |
} else
|
|
873 |
if (c == 0x01) break; /* end of pic8 */
|
|
874 |
|
|
875 |
else
|
|
876 |
if (c == 0x02) { /* delta */
|
|
877 |
c = getc(fp); x += c;
|
|
878 |
c = getc(fp); y += c;
|
|
879 |
pp = dest + x + (h-y-1)*w;
|
|
880 |
} else { /* absolute mode */
|
|
881 |
for (i=0; i<c; i++, x++, pp++) {
|
|
882 |
if ((i&1) == 0) c1 = getc(fp);
|
|
883 |
*pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f);
|
|
884 |
}
|
|
885 |
|
|
886 |
if (((c&3)==1) || ((c&3)==2)) getc(fp); /* read pad byte */
|
|
887 |
}
|
|
888 |
} /* escape processing */
|
|
889 |
if (ferror(fp)) return 0;
|
|
890 |
} /* while */
|
|
891 |
} else {
|
|
892 |
return 0;
|
|
893 |
}
|
|
894 |
}
|
|
895 |
if (ferror(fp)) return 0;
|
|
896 |
return 1;
|
|
897 |
}
|
|
898 |
|
|
899 |
static int
|
|
900 |
loadBMP8(w, h, comp, fp, dest)
|
|
901 |
int w, h;
|
|
902 |
FILE *fp;
|
|
903 |
char *dest;
|
|
904 |
{
|
|
905 |
int i,j,c,c1,padw,x,y;
|
|
906 |
unsigned char *pp;
|
|
907 |
|
|
908 |
if (comp == 0) { /* uncompressed data */
|
|
909 |
padw = ((w + 3)/4) * 4;
|
|
910 |
|
|
911 |
for (i=h-1; i>=0; i--) {
|
|
912 |
pp = dest + (i * w);
|
|
913 |
|
|
914 |
for (j=0; j<padw; j++) {
|
|
915 |
c = getc(fp);
|
|
916 |
if (c==EOF) return 0;
|
|
917 |
|
|
918 |
if (j<w) *pp++ = c;
|
|
919 |
}
|
|
920 |
if (ferror(fp)) return 0;
|
|
921 |
}
|
|
922 |
} else {
|
|
923 |
if (comp == 1) { /* RLE8 compressed */
|
|
924 |
x = y = 0;
|
|
925 |
pp = dest + x + (h-y-1)*w;
|
|
926 |
|
|
927 |
while (y<h) {
|
|
928 |
c = getc(fp);
|
|
929 |
if (c == EOF) return 0;
|
|
930 |
|
|
931 |
if (c) { /* encoded mode */
|
|
932 |
c1 = getc(fp);
|
|
933 |
for (i=0; i<c; i++,x++,pp++) *pp = c1;
|
|
934 |
} else {
|
|
935 |
/* c==0x00 : escape codes */
|
|
936 |
c = getc(fp);
|
|
937 |
if (c == EOF) return 0;
|
|
938 |
|
|
939 |
if (c == 0x00) { /* end of line */
|
|
940 |
x=0; y++; pp = dest + x + (h-y-1)*w;
|
|
941 |
} else
|
|
942 |
if (c == 0x01) break; /* end of pic8 */
|
|
943 |
|
|
944 |
else if (c == 0x02) { /* delta */
|
|
945 |
c = getc(fp); x += c;
|
|
946 |
c = getc(fp); y += c;
|
|
947 |
pp = dest + x + (h-y-1)*w;
|
|
948 |
} else { /* absolute mode */
|
|
949 |
for (i=0; i<c; i++, x++, pp++) {
|
|
950 |
c1 = getc(fp);
|
|
951 |
*pp = c1;
|
|
952 |
}
|
|
953 |
|
|
954 |
if (c & 1) getc(fp); /* odd length run: read an extra pad byte */
|
|
955 |
}
|
|
956 |
}
|
|
957 |
if (ferror(fp)) return 0;
|
|
958 |
}
|
|
959 |
} else {
|
|
960 |
return 0;
|
|
961 |
}
|
|
962 |
}
|
|
963 |
if (ferror(fp)) return 0;
|
|
964 |
return 1;
|
|
965 |
}
|
|
966 |
|
0
|
967 |
%}
|
77
|
968 |
! !
|
105
|
969 |
|
|
970 |
!ImageReader class methodsFor:'documentation'!
|
|
971 |
|
|
972 |
copyright
|
|
973 |
"
|
|
974 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
|
975 |
All Rights Reserved
|
|
976 |
|
|
977 |
This software is furnished under a license and may be used
|
|
978 |
only in accordance with the terms of that license and with the
|
|
979 |
inclusion of the above copyright notice. This software may not
|
|
980 |
be provided or otherwise made available to, or used by, any
|
|
981 |
other person. No title to or ownership of the software is
|
|
982 |
hereby transferred.
|
|
983 |
"
|
|
984 |
!
|
|
985 |
|
|
986 |
documentation
|
|
987 |
"
|
|
988 |
Abstract class to provide common functions for image-readers
|
|
989 |
(i.e. TIFFReader, GIFReader etc.)
|
|
990 |
|
|
991 |
ImageReaders are created temporary to read an image from a stream.
|
618
|
992 |
Normally, they are not directly used - instead, the image class is
|
|
993 |
asked to read some file, and return an instance for it:
|
|
994 |
Image fromFile:<someFileName>
|
|
995 |
The Image class will guess the images format and forward the task to
|
|
996 |
some concrete ImageReaderClass.
|
|
997 |
If that class finds, that the files format is incorrect, other readers
|
|
998 |
are tried until some finds the files format acceptable.
|
|
999 |
|
|
1000 |
Image readers read the stream and collect all relevant information internally.
|
118
|
1001 |
Once done with reading, the actual image object is created and
|
|
1002 |
data filled in from the imageReaders collected info.
|
105
|
1003 |
|
618
|
1004 |
See the implementation of #fromStream: in concrete subclasses.
|
105
|
1005 |
The public interfaces are:
|
593
|
1006 |
<ConcreteReaderClass> fromFile:aFilename
|
105
|
1007 |
or:
|
593
|
1008 |
<ConcreteReaderClass> fromStream:aStream
|
105
|
1009 |
|
121
|
1010 |
If you add a new reader, dont forget to add the method #isValidImageFile:
|
618
|
1011 |
which should return true, if this reader supports reading a given file.
|
|
1012 |
If your new reader class supports writing files, dont forget to add
|
|
1013 |
#canRepresent:anImage and return true from this method.
|
593
|
1014 |
|
|
1015 |
[See also:]
|
|
1016 |
Image Icon Form
|
611
|
1017 |
|
|
1018 |
[author:]
|
|
1019 |
Claus Gittinger
|
105
|
1020 |
"
|
|
1021 |
! !
|
|
1022 |
|
285
|
1023 |
!ImageReader class methodsFor:'cleanup'!
|
105
|
1024 |
|
285
|
1025 |
lowSpaceCleanup
|
|
1026 |
"cleanup things we do not need"
|
118
|
1027 |
|
285
|
1028 |
ReverseBits := nil
|
105
|
1029 |
! !
|
|
1030 |
|
|
1031 |
!ImageReader class methodsFor:'constants'!
|
|
1032 |
|
|
1033 |
reverseBits
|
|
1034 |
"return a table filled with bit reverse information.
|
|
1035 |
To convert from msbit-first to lsbit-first bytes, use
|
|
1036 |
the value as index into the table, retrieving the reverse
|
|
1037 |
value. Since indexing must start at 1, use (value + 1) as
|
|
1038 |
index."
|
|
1039 |
|
|
1040 |
|val "{ Class: SmallInteger }" |
|
|
1041 |
|
|
1042 |
ReverseBits isNil ifTrue:[
|
153
|
1043 |
ReverseBits := ByteArray uninitializedNew:256.
|
105
|
1044 |
0 to:255 do:[:i |
|
|
1045 |
val := 0.
|
|
1046 |
(i bitTest:16r01) ifTrue:[val := val bitOr:16r80].
|
|
1047 |
(i bitTest:16r02) ifTrue:[val := val bitOr:16r40].
|
|
1048 |
(i bitTest:16r04) ifTrue:[val := val bitOr:16r20].
|
|
1049 |
(i bitTest:16r08) ifTrue:[val := val bitOr:16r10].
|
|
1050 |
(i bitTest:16r10) ifTrue:[val := val bitOr:16r08].
|
|
1051 |
(i bitTest:16r20) ifTrue:[val := val bitOr:16r04].
|
|
1052 |
(i bitTest:16r40) ifTrue:[val := val bitOr:16r02].
|
|
1053 |
(i bitTest:16r80) ifTrue:[val := val bitOr:16r01].
|
|
1054 |
ReverseBits at:(i + 1) put:val
|
|
1055 |
]
|
|
1056 |
].
|
|
1057 |
^ ReverseBits
|
|
1058 |
! !
|
|
1059 |
|
285
|
1060 |
!ImageReader class methodsFor:'decompression support'!
|
105
|
1061 |
|
285
|
1062 |
decodeDelta:step in:data width:width height:height
|
|
1063 |
"perform NeXT special predictor delta decoding inplace in data.
|
|
1064 |
Calls primitive c function for speed"
|
|
1065 |
|
|
1066 |
(step ~~ 3) ifTrue:[
|
|
1067 |
^ self error:'only rgb pictures supported'
|
|
1068 |
].
|
105
|
1069 |
|
285
|
1070 |
%{
|
|
1071 |
if (__isByteArray(data)
|
|
1072 |
&& __bothSmallInteger(width, height)) {
|
|
1073 |
__decodeDelta__(_ByteArrayInstPtr(data)->ba_element,
|
|
1074 |
_intVal(width), _intVal(height));
|
|
1075 |
RETURN ( self );
|
|
1076 |
}
|
|
1077 |
%}
|
|
1078 |
.
|
|
1079 |
self primitiveFailed
|
|
1080 |
!
|
105
|
1081 |
|
|
1082 |
decompressCCITT3From:srcBytes into:dstBytes startingAt:offset count:count
|
|
1083 |
"decompress CCITT Group 3 compressed image data.
|
|
1084 |
count bytes from srcBytes are decompressed into dstBytes.
|
|
1085 |
Calls primitive c function for speed"
|
|
1086 |
%{
|
|
1087 |
if (__isByteArray(srcBytes)
|
|
1088 |
&& __isByteArray(dstBytes)
|
|
1089 |
&& __bothSmallInteger(offset, count)) {
|
|
1090 |
if (__decodeCCITTgroup3__(_ByteArrayInstPtr(srcBytes)->ba_element,
|
|
1091 |
_ByteArrayInstPtr(dstBytes)->ba_element
|
|
1092 |
+ _intVal(offset) - 1,
|
|
1093 |
_intVal(count))) {
|
|
1094 |
RETURN ( self );
|
|
1095 |
}
|
|
1096 |
}
|
|
1097 |
%}
|
|
1098 |
.
|
|
1099 |
self primitiveFailed
|
|
1100 |
!
|
|
1101 |
|
|
1102 |
decompressGIFFrom:srcBytes count:count into:dstBytes startingAt:offset codeLen:codeLen
|
|
1103 |
"decompress GIF 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(codeLen, offset)
|
|
1110 |
&& __isSmallInteger(count)) {
|
|
1111 |
if (__decodeGIF__(_ByteArrayInstPtr(srcBytes)->ba_element,
|
|
1112 |
_ByteArrayInstPtr(dstBytes)->ba_element
|
|
1113 |
+ _intVal(offset) - 1,
|
|
1114 |
_intVal(count),
|
|
1115 |
_intVal(codeLen))) {
|
|
1116 |
RETURN ( self );
|
|
1117 |
}
|
|
1118 |
}
|
|
1119 |
%}
|
|
1120 |
.
|
|
1121 |
self primitiveFailed
|
|
1122 |
!
|
|
1123 |
|
285
|
1124 |
decompressLZWFrom:srcBytes count:count into:dstBytes startingAt:offset
|
|
1125 |
"decompress LZW (tiff) compressed image data.
|
|
1126 |
count bytes from srcBytes are decompressed into dstBytes.
|
105
|
1127 |
Calls primitive c function for speed"
|
|
1128 |
%{
|
285
|
1129 |
if (__isByteArray(srcBytes)
|
|
1130 |
&& __isByteArray(dstBytes)
|
|
1131 |
&& __bothSmallInteger(offset, count)) {
|
|
1132 |
if (__decodeLZW__(_ByteArrayInstPtr(srcBytes)->ba_element,
|
|
1133 |
_ByteArrayInstPtr(dstBytes)->ba_element
|
|
1134 |
+ _intVal(offset) - 1,
|
|
1135 |
_intVal(count))) {
|
|
1136 |
RETURN ( self );
|
|
1137 |
}
|
105
|
1138 |
}
|
|
1139 |
%}
|
|
1140 |
.
|
|
1141 |
self primitiveFailed
|
189
|
1142 |
!
|
|
1143 |
|
|
1144 |
loadBMP1to8Width:width height:height from:aStream into:aByteArray
|
583
|
1145 |
"load bmp-1 bit per pixel imagedata. A helper for BMP image reader.
|
|
1146 |
Calls primitive c function for speed"
|
|
1147 |
|
189
|
1148 |
|f|
|
|
1149 |
|
|
1150 |
aStream isExternalStream ifFalse:[^ false].
|
|
1151 |
f := aStream filePointer.
|
|
1152 |
f isNil ifTrue:[^ false].
|
|
1153 |
%{
|
|
1154 |
if (! (__bothSmallInteger(width, height)
|
583
|
1155 |
&& __isByteArray(aByteArray))) {
|
|
1156 |
RETURN (false);
|
189
|
1157 |
}
|
|
1158 |
if (loadBMP1to8(__intVal(width), __intVal(height),
|
583
|
1159 |
__FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
|
|
1160 |
RETURN (true);
|
189
|
1161 |
}
|
|
1162 |
RETURN (false);
|
|
1163 |
%}
|
583
|
1164 |
|
|
1165 |
"Modified: 22.4.1996 / 19:14:32 / cg"
|
189
|
1166 |
!
|
|
1167 |
|
|
1168 |
loadBMP2to8Width:width height:height from:aStream into:aByteArray
|
583
|
1169 |
"load bmp-2 bit per pixel imagedata. A helper for BMP image reader.
|
|
1170 |
Calls primitive c function for speed"
|
|
1171 |
|
189
|
1172 |
|f|
|
|
1173 |
|
|
1174 |
aStream isExternalStream ifFalse:[^ false].
|
|
1175 |
f := aStream filePointer.
|
|
1176 |
f isNil ifTrue:[^ false].
|
|
1177 |
%{
|
|
1178 |
if (! (__bothSmallInteger(width, height)
|
583
|
1179 |
&& __isByteArray(aByteArray))) {
|
|
1180 |
RETURN (false);
|
189
|
1181 |
}
|
|
1182 |
if (loadBMP2to8(__intVal(width), __intVal(height),
|
583
|
1183 |
__FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
|
|
1184 |
RETURN (true);
|
189
|
1185 |
}
|
|
1186 |
RETURN (false);
|
|
1187 |
%}
|
583
|
1188 |
|
|
1189 |
"Modified: 22.4.1996 / 19:14:40 / cg"
|
189
|
1190 |
!
|
|
1191 |
|
|
1192 |
loadBMP4to8Width:width height:height compression:compression from:aStream into:aByteArray
|
583
|
1193 |
"load bmp-4 bit per pixel imagedata. A helper for BMP image reader.
|
|
1194 |
Calls primitive c function for speed"
|
|
1195 |
|
189
|
1196 |
|f|
|
|
1197 |
|
|
1198 |
aStream isExternalStream ifFalse:[^ false].
|
|
1199 |
f := aStream filePointer.
|
|
1200 |
f isNil ifTrue:[^ false].
|
|
1201 |
%{
|
|
1202 |
if (! (__bothSmallInteger(width, height)
|
583
|
1203 |
&& __isSmallInteger(compression)
|
|
1204 |
&& __isByteArray(aByteArray))) {
|
|
1205 |
RETURN (false);
|
189
|
1206 |
}
|
|
1207 |
if (loadBMP4to8(__intVal(width), __intVal(height), __intVal(compression),
|
583
|
1208 |
__FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
|
|
1209 |
RETURN (true);
|
189
|
1210 |
}
|
|
1211 |
RETURN (false);
|
|
1212 |
%}
|
583
|
1213 |
|
|
1214 |
"Modified: 22.4.1996 / 19:14:46 / cg"
|
285
|
1215 |
!
|
|
1216 |
|
|
1217 |
loadBMP8Width:width height:height compression:compression from:aStream into:aByteArray
|
583
|
1218 |
"load bmp-8 bit per pixel imagedata. A helper for BMP image reader.
|
|
1219 |
Calls primitive c function for speed"
|
|
1220 |
|
|
1221 |
|f|
|
285
|
1222 |
|
|
1223 |
aStream isExternalStream ifFalse:[^ false].
|
|
1224 |
f := aStream filePointer.
|
|
1225 |
f isNil ifTrue:[^ false].
|
|
1226 |
%{
|
|
1227 |
if (! (__bothSmallInteger(width, height)
|
583
|
1228 |
&& __isSmallInteger(compression)
|
|
1229 |
&& __isByteArray(aByteArray))) {
|
|
1230 |
RETURN (false);
|
285
|
1231 |
}
|
|
1232 |
if (loadBMP8(__intVal(width), __intVal(height), __intVal(compression),
|
583
|
1233 |
__FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
|
|
1234 |
RETURN (true);
|
285
|
1235 |
}
|
|
1236 |
RETURN (false);
|
|
1237 |
%}
|
583
|
1238 |
|
|
1239 |
"Modified: 22.4.1996 / 19:14:54 / cg"
|
285
|
1240 |
! !
|
|
1241 |
|
|
1242 |
!ImageReader class methodsFor:'i/o support'!
|
|
1243 |
|
|
1244 |
streamReadingFile:aFilename
|
|
1245 |
"return a stream to read aFilename.
|
|
1246 |
If the filename ends with '.Z' or '.gz', return a stream
|
|
1247 |
to a pipe for the uncompressor. Otherwise, return a stream to read
|
|
1248 |
the file directly."
|
|
1249 |
|
|
1250 |
|inStream name|
|
|
1251 |
|
|
1252 |
name := aFilename asString.
|
|
1253 |
((name endsWith:'.Z') or:[name endsWith:'.gz']) ifTrue:[
|
518
|
1254 |
inStream := PipeStream readingFrom:'gunzip < ' , name.
|
|
1255 |
inStream isNil ifTrue:[
|
|
1256 |
inStream := PipeStream readingFrom:'uncompress < ' , name.
|
|
1257 |
]
|
285
|
1258 |
] ifFalse:[
|
518
|
1259 |
inStream := Smalltalk systemFileStreamFor:aFilename.
|
|
1260 |
inStream isNil ifTrue:[
|
|
1261 |
inStream := Smalltalk bitmapFileStreamFor:aFilename
|
|
1262 |
]
|
285
|
1263 |
].
|
|
1264 |
inStream isNil ifTrue:[
|
518
|
1265 |
'IMGREADER: open error on: ' infoPrint. aFilename infoPrintNL.
|
285
|
1266 |
].
|
|
1267 |
^ inStream
|
518
|
1268 |
|
|
1269 |
"Modified: 7.3.1996 / 19:18:04 / cg"
|
285
|
1270 |
! !
|
|
1271 |
|
|
1272 |
!ImageReader class methodsFor:'image reading'!
|
|
1273 |
|
|
1274 |
fromFile:aFileName
|
|
1275 |
"read an image (in my format) from aFileName.
|
|
1276 |
Return the image or nil on error."
|
|
1277 |
|
|
1278 |
|result inStream|
|
|
1279 |
|
|
1280 |
inStream := self streamReadingFile:aFileName.
|
|
1281 |
inStream isNil ifTrue:[
|
518
|
1282 |
'IMGREADER: file open error' infoPrintNL.
|
|
1283 |
^ nil
|
285
|
1284 |
].
|
|
1285 |
result := self fromStream:inStream.
|
|
1286 |
inStream close.
|
|
1287 |
^ result
|
|
1288 |
|
|
1289 |
"
|
|
1290 |
XPMReader fromFile:'bitmaps/ljet.xpm'
|
|
1291 |
XBMReader fromFile:'bitmaps/SBrowser.xbm'
|
518
|
1292 |
"
|
|
1293 |
|
|
1294 |
"Modified: 7.3.1996 / 19:17:56 / cg"
|
285
|
1295 |
!
|
|
1296 |
|
|
1297 |
fromStream:aStream
|
583
|
1298 |
"read an image (in my format) from aStream.
|
|
1299 |
Return the image or nil (if unrecognized format or error)"
|
285
|
1300 |
|
|
1301 |
|reader|
|
|
1302 |
|
|
1303 |
reader := self new fromStream:aStream.
|
|
1304 |
reader notNil ifTrue:[
|
583
|
1305 |
^ reader image
|
285
|
1306 |
].
|
|
1307 |
^ nil
|
583
|
1308 |
|
|
1309 |
"Modified: 22.4.1996 / 19:11:58 / cg"
|
285
|
1310 |
! !
|
|
1311 |
|
|
1312 |
!ImageReader class methodsFor:'image writing'!
|
|
1313 |
|
|
1314 |
save:anImage onFile:aFileName
|
|
1315 |
"save the image in my format on aFileName"
|
|
1316 |
|
|
1317 |
^ (self basicNew) save:anImage onFile:aFileName
|
|
1318 |
! !
|
|
1319 |
|
|
1320 |
!ImageReader class methodsFor:'testing'!
|
|
1321 |
|
|
1322 |
canRepresent:anImage
|
|
1323 |
"return true, if anImage can be represented in my file format.
|
|
1324 |
must be redefined in concrete subclasses which support saving."
|
|
1325 |
|
|
1326 |
^ false
|
|
1327 |
!
|
|
1328 |
|
|
1329 |
isValidImageFile:aFileName
|
|
1330 |
"return true, if aFileName contains an image this
|
|
1331 |
reader understands - must be redefined in concrete subclasses
|
|
1332 |
which support reading."
|
|
1333 |
|
|
1334 |
^ false
|
105
|
1335 |
! !
|
|
1336 |
|
|
1337 |
!ImageReader methodsFor:'accessing'!
|
|
1338 |
|
285
|
1339 |
bitsPerPixel
|
|
1340 |
"return the number of bits per pixel"
|
|
1341 |
|
|
1342 |
^ (bitsPerSample inject:0 into:[:sum :i | sum + i])
|
|
1343 |
!
|
|
1344 |
|
|
1345 |
bitsPerSample
|
583
|
1346 |
"return the number of bits per sample"
|
|
1347 |
|
285
|
1348 |
^ bitsPerSample
|
583
|
1349 |
|
|
1350 |
"Modified: 22.4.1996 / 19:15:18 / cg"
|
285
|
1351 |
!
|
|
1352 |
|
|
1353 |
colorMap
|
583
|
1354 |
"return the colormap"
|
|
1355 |
|
285
|
1356 |
^ colorMap
|
583
|
1357 |
|
|
1358 |
"Modified: 22.4.1996 / 19:15:24 / cg"
|
285
|
1359 |
!
|
|
1360 |
|
|
1361 |
data
|
583
|
1362 |
"return the raw image data"
|
|
1363 |
|
285
|
1364 |
^ data
|
583
|
1365 |
|
|
1366 |
"Modified: 22.4.1996 / 19:15:31 / cg"
|
285
|
1367 |
!
|
|
1368 |
|
|
1369 |
height
|
583
|
1370 |
"return the height of the image"
|
|
1371 |
|
285
|
1372 |
^ height
|
583
|
1373 |
|
|
1374 |
"Modified: 22.4.1996 / 19:15:39 / cg"
|
285
|
1375 |
!
|
|
1376 |
|
105
|
1377 |
image
|
583
|
1378 |
"return the image as represented by myself"
|
105
|
1379 |
|
|
1380 |
|image depth|
|
|
1381 |
|
|
1382 |
depth := self bitsPerPixel.
|
|
1383 |
image := (Image implementorForDepth:depth) new.
|
593
|
1384 |
image
|
|
1385 |
width:width
|
|
1386 |
height:height
|
|
1387 |
photometric:photometric
|
|
1388 |
samplesPerPixel:samplesPerPixel
|
|
1389 |
bitsPerSample:bitsPerSample
|
|
1390 |
colorMap:colorMap
|
|
1391 |
bits:data.
|
105
|
1392 |
^ image
|
583
|
1393 |
|
593
|
1394 |
"Modified: 23.4.1996 / 11:07:04 / cg"
|
105
|
1395 |
!
|
|
1396 |
|
|
1397 |
photometric
|
583
|
1398 |
"return the photometric interpretation of the image data"
|
|
1399 |
|
105
|
1400 |
^ photometric
|
583
|
1401 |
|
|
1402 |
"Modified: 22.4.1996 / 19:15:57 / cg"
|
105
|
1403 |
!
|
|
1404 |
|
|
1405 |
samplesPerPixel
|
583
|
1406 |
"return the number of samples per pixel"
|
|
1407 |
|
105
|
1408 |
^ samplesPerPixel
|
583
|
1409 |
|
|
1410 |
"Modified: 22.4.1996 / 19:16:03 / cg"
|
105
|
1411 |
!
|
|
1412 |
|
285
|
1413 |
width
|
583
|
1414 |
"return the width of the image"
|
|
1415 |
|
285
|
1416 |
^ width
|
583
|
1417 |
|
|
1418 |
"Modified: 22.4.1996 / 19:16:10 / cg"
|
105
|
1419 |
! !
|
|
1420 |
|
|
1421 |
!ImageReader methodsFor:'i/o support'!
|
|
1422 |
|
|
1423 |
readLong
|
|
1424 |
"return the next 4-byte long, honoring the byte-order"
|
|
1425 |
|
|
1426 |
^ inStream nextLongMSB:(byteOrder ~~ #lsb)
|
|
1427 |
!
|
|
1428 |
|
|
1429 |
readShort
|
|
1430 |
"return the next 2-byte short, honoring the byte-order"
|
|
1431 |
|
|
1432 |
^ inStream nextUnsignedShortMSB:(byteOrder ~~ #lsb)
|
|
1433 |
!
|
|
1434 |
|
|
1435 |
readShortLong
|
|
1436 |
"return the next 2-byte short, honoring the byte-order.
|
|
1437 |
There are actually 4 bytes read, but only 2 looked at."
|
|
1438 |
|
|
1439 |
|bytes val|
|
|
1440 |
|
153
|
1441 |
bytes := ByteArray uninitializedNew:4.
|
105
|
1442 |
inStream nextBytes:4 into:bytes.
|
|
1443 |
(byteOrder == #lsb) ifTrue:[
|
|
1444 |
val := bytes at:2.
|
|
1445 |
val := val * 256 + (bytes at:1)
|
|
1446 |
] ifFalse:[
|
|
1447 |
val := bytes at:3.
|
|
1448 |
val := val * 256 + (bytes at:4)
|
|
1449 |
].
|
|
1450 |
^ val
|
|
1451 |
!
|
|
1452 |
|
|
1453 |
writeLong:anInteger
|
|
1454 |
"write a 4-byte long, honoring the byte-order."
|
|
1455 |
|
|
1456 |
outStream nextPutLong:anInteger MSB:(byteOrder ~~ #lsb)
|
|
1457 |
!
|
|
1458 |
|
|
1459 |
writeShort:anInteger
|
|
1460 |
"write a 2-byte short, honoring the byte-order."
|
|
1461 |
|
|
1462 |
outStream nextPutShort:anInteger MSB:(byteOrder ~~ #lsb)
|
|
1463 |
! !
|
285
|
1464 |
|
|
1465 |
!ImageReader methodsFor:'image reading'!
|
|
1466 |
|
|
1467 |
fromStream:aStream
|
583
|
1468 |
"read an image in my format from aStream.
|
|
1469 |
Leave image description in instance variables."
|
|
1470 |
|
285
|
1471 |
|
|
1472 |
^ self subclassResponsibility
|
583
|
1473 |
|
|
1474 |
"Modified: 22.4.1996 / 19:11:31 / cg"
|
285
|
1475 |
! !
|
|
1476 |
|
|
1477 |
!ImageReader methodsFor:'image writing'!
|
|
1478 |
|
|
1479 |
save:image onFile:aFileName
|
|
1480 |
"save image in my format on aFile"
|
|
1481 |
|
|
1482 |
^ self subclassResponsibility
|
|
1483 |
! !
|
|
1484 |
|
|
1485 |
!ImageReader class methodsFor:'documentation'!
|
|
1486 |
|
|
1487 |
version
|
618
|
1488 |
^ '$Header: /cvs/stx/stx/libview/ImageReader.st,v 1.29 1996-04-26 14:57:23 cg Exp $'
|
285
|
1489 |
! !
|