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 |
|
|
23 |
ImageReader comment:'
|
46
|
24 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
77
|
25 |
All Rights Reserved
|
54
|
26 |
|
105
|
27 |
$Header: /cvs/stx/stx/libview/ImageReader.st,v 1.15 1995-02-18 15:57:27 claus Exp $
|
46
|
28 |
'!
|
0
|
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 |
|
77
|
346 |
__decodeCCITTgroup3__(from, to, len)
|
0
|
347 |
unsigned char *from;
|
|
348 |
register unsigned char *to;
|
|
349 |
{
|
|
350 |
register cnt;
|
|
351 |
register short *countPtr;
|
|
352 |
register char *shiftPtr;
|
|
353 |
unsigned bits, bits13;
|
|
354 |
int shift, outCount, nBitsLess13;
|
|
355 |
int outBitOffset;
|
|
356 |
int nLeft, t;
|
|
357 |
|
|
358 |
if (! whiteCountTable) initCCITTTables();
|
46
|
359 |
if (! whiteCountTable) return 0; /* malloc failed */
|
0
|
360 |
|
|
361 |
countPtr = whiteCountTable;
|
|
362 |
shiftPtr = whiteShiftTable;
|
|
363 |
outCount = 0;
|
|
364 |
outBitOffset = 0;
|
|
365 |
bits = *from++;
|
|
366 |
bits = (bits << 8) | *from++;
|
|
367 |
nBitsLess13 = 3;
|
|
368 |
for (;;) {
|
77
|
369 |
bits13 = (bits >> nBitsLess13) & 0x1FFF;
|
|
370 |
cnt = countPtr[bits13];
|
|
371 |
if (cnt < 0) return;
|
|
372 |
shift = shiftPtr[bits13];
|
|
373 |
outCount += cnt;
|
|
374 |
if (countPtr == blackCountTable) {
|
|
375 |
/* toggle if it was a terminating code */
|
|
376 |
if (cnt < 64) {
|
|
377 |
countPtr = whiteCountTable;
|
|
378 |
shiftPtr = whiteShiftTable;
|
|
379 |
}
|
0
|
380 |
|
77
|
381 |
/* draw cnt black bits */
|
|
382 |
if (cnt) {
|
|
383 |
if (outBitOffset) {
|
|
384 |
nLeft = 8 - outBitOffset;
|
|
385 |
if (cnt < nLeft) nLeft = cnt;
|
|
386 |
t = leftBits[nLeft] >> outBitOffset;
|
|
387 |
*to |= t;
|
|
388 |
cnt -= nLeft;
|
|
389 |
outBitOffset += nLeft;
|
|
390 |
if (outBitOffset >= 8) {
|
|
391 |
to++;
|
|
392 |
outBitOffset -= 8;
|
|
393 |
}
|
|
394 |
}
|
|
395 |
if (cnt > 256) {
|
|
396 |
while ((int)to & 3) {
|
|
397 |
*to++ = 0xFF;
|
|
398 |
cnt -= 8;
|
|
399 |
}
|
|
400 |
while (cnt >= 32) {
|
|
401 |
(*(long *)to) = 0xFFFFFFFF;
|
|
402 |
to += 4;
|
|
403 |
cnt -= 32;
|
|
404 |
}
|
|
405 |
}
|
|
406 |
while (cnt >= 8) {
|
|
407 |
*to++ = 0xFF;
|
|
408 |
cnt -= 8;
|
|
409 |
}
|
|
410 |
*to |= leftBits[cnt];
|
|
411 |
outBitOffset += cnt;
|
|
412 |
}
|
|
413 |
} else {
|
|
414 |
/* toggle if it was a terminating code */
|
|
415 |
if (cnt < 64) {
|
|
416 |
countPtr = blackCountTable;
|
|
417 |
shiftPtr = blackShiftTable;
|
|
418 |
}
|
0
|
419 |
|
77
|
420 |
/* skip cnt bits */
|
|
421 |
to += cnt >> 3;
|
|
422 |
outBitOffset += cnt & 7;
|
|
423 |
if (outBitOffset >= 8) {
|
|
424 |
to++;
|
|
425 |
outBitOffset -= 8;
|
|
426 |
}
|
|
427 |
}
|
|
428 |
if (outCount >= len) return 1;
|
0
|
429 |
|
77
|
430 |
nBitsLess13 -= shift;
|
|
431 |
while (nBitsLess13 < 0) {
|
|
432 |
bits = (bits << 8) | *from++;
|
|
433 |
nBitsLess13 += 8;
|
|
434 |
}
|
0
|
435 |
}
|
|
436 |
}
|
|
437 |
|
|
438 |
/*
|
|
439 |
* LZW decompression
|
|
440 |
*/
|
|
441 |
struct buffer {
|
77
|
442 |
struct buffer *prev;
|
|
443 |
unsigned char chars[8192 - 4];
|
0
|
444 |
};
|
|
445 |
|
77
|
446 |
__decodeLZW__(from, to, inCount)
|
0
|
447 |
unsigned char *from;
|
|
448 |
unsigned char *to;
|
|
449 |
{
|
|
450 |
register unsigned code;
|
12
|
451 |
unsigned char **strings;
|
|
452 |
short *stringLen;
|
0
|
453 |
struct buffer *scratchBuffer;
|
|
454 |
struct buffer *newBuffer;
|
|
455 |
unsigned char *scratchPtr;
|
|
456 |
int nScratch;
|
|
457 |
unsigned nextCode, oldCode;
|
|
458 |
register unsigned bits;
|
|
459 |
int nBits, mask, shift;
|
|
460 |
int i;
|
|
461 |
int len;
|
|
462 |
int codeLen = 9;
|
|
463 |
|
|
464 |
scratchBuffer = (struct buffer *)malloc(sizeof(struct buffer));
|
12
|
465 |
if (! scratchBuffer) return 0;
|
46
|
466 |
|
12
|
467 |
strings = (unsigned char **)malloc(sizeof(unsigned char *) * 4096);
|
|
468 |
if (! strings) {
|
77
|
469 |
free(scratchBuffer);
|
|
470 |
return 0;
|
12
|
471 |
}
|
|
472 |
stringLen = (short *)malloc(sizeof(short) * 4096);
|
|
473 |
if (! stringLen) {
|
77
|
474 |
free(strings);
|
|
475 |
free(scratchBuffer);
|
|
476 |
return 0;
|
12
|
477 |
}
|
|
478 |
|
0
|
479 |
scratchBuffer->prev = (struct buffer *)0;
|
|
480 |
scratchPtr = scratchBuffer->chars;
|
|
481 |
nScratch = sizeof(scratchBuffer->chars);
|
|
482 |
|
|
483 |
for (i = 0; i < 256; i++) {
|
77
|
484 |
*scratchPtr = i;
|
|
485 |
strings[i] = scratchPtr++;
|
|
486 |
stringLen[i] = 1;
|
0
|
487 |
}
|
|
488 |
|
|
489 |
nextCode = 258;
|
|
490 |
nScratch -= 256;
|
|
491 |
mask = 0x1FF;
|
|
492 |
nBits = 0;
|
|
493 |
bits = 0;
|
|
494 |
while (inCount) {
|
77
|
495 |
/* fetch code */
|
|
496 |
while (nBits < codeLen) {
|
|
497 |
bits = (bits<<8) | *from++;
|
|
498 |
inCount--;
|
|
499 |
nBits += 8;
|
|
500 |
}
|
|
501 |
shift = nBits - codeLen;
|
|
502 |
code = (bits >> shift) & mask;
|
|
503 |
bits &= ~(mask << shift);
|
|
504 |
nBits -= codeLen;
|
|
505 |
if (code == 257) break;
|
|
506 |
if (code == 256) {
|
|
507 |
if (! inCount)
|
|
508 |
break;
|
0
|
509 |
|
77
|
510 |
/* free stuff */
|
|
511 |
while (scratchBuffer->prev) {
|
|
512 |
newBuffer = scratchBuffer;
|
|
513 |
scratchBuffer = scratchBuffer->prev;
|
|
514 |
free(newBuffer);
|
|
515 |
}
|
|
516 |
/* reset everything */
|
|
517 |
scratchPtr = scratchBuffer->chars + 256;
|
|
518 |
nScratch = sizeof(scratchBuffer->chars) - 256;
|
|
519 |
codeLen = 9;
|
|
520 |
nextCode = 258;
|
|
521 |
mask = 0x1FF;
|
|
522 |
/* fetch code */
|
|
523 |
while (nBits < codeLen) {
|
|
524 |
bits = (bits<<8) | *from++;
|
|
525 |
inCount--;
|
|
526 |
nBits += 8;
|
|
527 |
}
|
|
528 |
shift = nBits - codeLen;
|
|
529 |
code = (bits >> shift) & mask;
|
|
530 |
bits &= ~(mask << shift);
|
|
531 |
nBits -= codeLen;
|
|
532 |
if (code == 257) break;
|
|
533 |
/* add to output */
|
|
534 |
*to++ = code;
|
|
535 |
oldCode = code;
|
|
536 |
} else {
|
|
537 |
if (code < nextCode) {
|
|
538 |
/* writeString(string[code]) */
|
|
539 |
len = stringLen[code];
|
|
540 |
bcopy(strings[code], to, len);
|
|
541 |
to += len;
|
0
|
542 |
|
77
|
543 |
/* add( string[oldcode] + first(string[code]) ) */
|
|
544 |
len = stringLen[oldCode] + 1;
|
|
545 |
if (nScratch < len) {
|
|
546 |
newBuffer = (struct buffer *)malloc(sizeof(struct buffer));
|
|
547 |
if (! newBuffer) goto out;
|
|
548 |
newBuffer->prev = scratchBuffer;
|
|
549 |
scratchBuffer = newBuffer;
|
|
550 |
scratchPtr = scratchBuffer->chars;
|
|
551 |
nScratch = sizeof(scratchBuffer->chars);
|
|
552 |
}
|
|
553 |
stringLen[nextCode] = len;
|
|
554 |
strings[nextCode] = scratchPtr;
|
|
555 |
bcopy(strings[oldCode], scratchPtr, len-1);
|
|
556 |
scratchPtr += len-1;
|
|
557 |
*scratchPtr++ = strings[code][0];
|
|
558 |
nScratch -= len;
|
|
559 |
} else {
|
|
560 |
/* writeString(string[oldCode] + first(string[oldCode]) ) */
|
|
561 |
len = stringLen[oldCode];
|
|
562 |
bcopy(strings[oldCode], to, len);
|
|
563 |
to += len;
|
|
564 |
*to++ = strings[oldCode][0];
|
0
|
565 |
|
77
|
566 |
/* add( string[oldcode] + first(string[oldCode]) ) */
|
|
567 |
len++;
|
|
568 |
if (nScratch < len) {
|
|
569 |
newBuffer = (struct buffer *)malloc(sizeof(struct buffer));
|
|
570 |
if (! newBuffer) goto out;
|
|
571 |
newBuffer->prev = scratchBuffer;
|
|
572 |
scratchBuffer = newBuffer;
|
|
573 |
scratchPtr = scratchBuffer->chars;
|
|
574 |
nScratch = sizeof(scratchBuffer->chars);
|
|
575 |
}
|
|
576 |
stringLen[nextCode] = len;
|
|
577 |
strings[nextCode] = scratchPtr;
|
|
578 |
bcopy(strings[oldCode], scratchPtr, len-1);
|
|
579 |
scratchPtr += len-1;
|
|
580 |
*scratchPtr++ = strings[oldCode][0];
|
|
581 |
nScratch -= len;
|
|
582 |
}
|
|
583 |
oldCode = code;
|
|
584 |
nextCode++;
|
|
585 |
if (nextCode >= 511)
|
|
586 |
if (nextCode == 511) {
|
|
587 |
codeLen = 10;
|
|
588 |
mask = 0x3FF;
|
|
589 |
} else if (nextCode >= 1023)
|
|
590 |
if (nextCode == 1023) {
|
|
591 |
codeLen = 11;
|
|
592 |
mask = 0x7FF;
|
|
593 |
} else
|
|
594 |
if (nextCode == 2047) {
|
|
595 |
codeLen = 12;
|
|
596 |
mask = 0xFFF;
|
|
597 |
}
|
|
598 |
}
|
0
|
599 |
}
|
12
|
600 |
out: ;
|
0
|
601 |
/* free stuff */
|
|
602 |
while (scratchBuffer) {
|
77
|
603 |
newBuffer = scratchBuffer;
|
|
604 |
scratchBuffer = scratchBuffer->prev;
|
|
605 |
free(newBuffer);
|
0
|
606 |
}
|
46
|
607 |
|
12
|
608 |
free(strings);
|
|
609 |
free(stringLen);
|
46
|
610 |
|
12
|
611 |
return 1;
|
0
|
612 |
}
|
|
613 |
|
|
614 |
/*
|
|
615 |
* delta decoding (TIFF predictor = 2)
|
|
616 |
*/
|
77
|
617 |
__decodeDelta__(bytes, width, height)
|
0
|
618 |
register unsigned char *bytes;
|
|
619 |
{
|
77
|
620 |
register w;
|
|
621 |
unsigned char r, g, b;
|
0
|
622 |
|
77
|
623 |
while (height--) {
|
|
624 |
r = g = b = 0;
|
|
625 |
for (w = width; w; w--) {
|
|
626 |
r += *bytes;
|
|
627 |
*bytes++ = r;
|
|
628 |
g += *bytes;
|
|
629 |
*bytes++ = g;
|
|
630 |
b += *bytes;
|
|
631 |
*bytes++ = b;
|
|
632 |
}
|
|
633 |
}
|
0
|
634 |
}
|
|
635 |
|
46
|
636 |
/*
|
|
637 |
* GIF decompression
|
|
638 |
*/
|
77
|
639 |
__decodeGIF__(from, to, inCount, initialCodeLen)
|
0
|
640 |
unsigned char *from;
|
|
641 |
unsigned char *to;
|
|
642 |
{
|
|
643 |
register unsigned code;
|
12
|
644 |
unsigned short *prefix;
|
|
645 |
unsigned short *suffix;
|
|
646 |
unsigned short *outCode;
|
0
|
647 |
int outCount;
|
|
648 |
unsigned maxCode, oldCode, fin, inCode, curCode;
|
|
649 |
register unsigned bits;
|
|
650 |
register int nBits, mask, shift;
|
|
651 |
int i;
|
|
652 |
int len;
|
|
653 |
int endCode, clearCode, freeCode;
|
|
654 |
int codeLen = initialCodeLen;
|
12
|
655 |
static int ranges[] = {0, 1, 2, 4, 8, 16, 32, 64,
|
77
|
656 |
128, 256, 512, 1024, 2048 };
|
0
|
657 |
|
12
|
658 |
prefix = (unsigned short *)malloc(sizeof(short) * 4096);
|
|
659 |
if (! prefix) return 0;
|
|
660 |
suffix = (unsigned short *)malloc(sizeof(short) * 4096);
|
|
661 |
if (! suffix) {
|
77
|
662 |
free(prefix);
|
|
663 |
return 0;
|
12
|
664 |
}
|
|
665 |
outCode = (unsigned short *)malloc(sizeof(short) * 4096);
|
|
666 |
if (! outCode) {
|
77
|
667 |
free(prefix);
|
|
668 |
free(suffix);
|
|
669 |
return 0;
|
12
|
670 |
}
|
0
|
671 |
clearCode = ranges[codeLen]; /* 256 */
|
|
672 |
endCode = clearCode + 1; /* 257 */
|
|
673 |
freeCode = clearCode + 2; /* 258 */
|
|
674 |
maxCode = clearCode << 1; /* 512 */
|
|
675 |
outCount = 0;
|
|
676 |
|
|
677 |
mask = maxCode - 1; /* 1FF */
|
|
678 |
nBits = 0;
|
|
679 |
bits = 0;
|
|
680 |
while (inCount) {
|
77
|
681 |
/* fetch code */
|
|
682 |
while (nBits < codeLen) {
|
|
683 |
bits = bits | (*from++ << nBits);
|
|
684 |
inCount--;
|
|
685 |
nBits += 8;
|
|
686 |
}
|
|
687 |
code = bits & mask;
|
|
688 |
bits >>= codeLen;
|
|
689 |
nBits -= codeLen;
|
|
690 |
if (code == endCode) break;
|
|
691 |
if (code == clearCode) {
|
|
692 |
if (! inCount)
|
|
693 |
break;
|
0
|
694 |
|
77
|
695 |
codeLen = initialCodeLen;
|
|
696 |
maxCode = clearCode<<1;
|
|
697 |
mask = maxCode - 1;
|
|
698 |
freeCode = clearCode + 2;
|
0
|
699 |
|
77
|
700 |
/* fetch code */
|
|
701 |
while (nBits < codeLen) {
|
|
702 |
bits = bits | (*from++ << nBits);
|
|
703 |
inCount--;
|
|
704 |
nBits += 8;
|
|
705 |
}
|
|
706 |
code = bits & mask;
|
|
707 |
bits >>= codeLen;
|
|
708 |
nBits -= codeLen;
|
|
709 |
if (code == endCode) break;
|
|
710 |
/* add to output */
|
|
711 |
*to++ = code;
|
|
712 |
oldCode = fin = curCode = code;
|
|
713 |
} else {
|
|
714 |
curCode = inCode = code;
|
|
715 |
if (curCode >= freeCode) {
|
|
716 |
curCode = oldCode;
|
|
717 |
outCode[outCount++] = fin;
|
|
718 |
}
|
0
|
719 |
|
77
|
720 |
while (curCode >= clearCode) {
|
|
721 |
if (outCount > 1024) {
|
|
722 |
goto out;
|
|
723 |
}
|
|
724 |
outCode[outCount++] = suffix[curCode];
|
|
725 |
curCode = prefix[curCode];
|
|
726 |
}
|
0
|
727 |
|
77
|
728 |
fin = curCode;
|
|
729 |
outCode[outCount++] = fin;
|
0
|
730 |
|
77
|
731 |
for (i = outCount - 1; i >= 0; i--)
|
|
732 |
*to++ = outCode[i];
|
|
733 |
outCount = 0;
|
0
|
734 |
|
77
|
735 |
prefix[freeCode] = oldCode;
|
|
736 |
suffix[freeCode] = fin;
|
|
737 |
oldCode = inCode;
|
0
|
738 |
|
77
|
739 |
freeCode++;
|
|
740 |
if (freeCode >= maxCode) {
|
|
741 |
if (codeLen < 12) {
|
|
742 |
codeLen++;
|
|
743 |
maxCode *= 2;
|
|
744 |
mask = (1 << codeLen) - 1;
|
|
745 |
}
|
|
746 |
}
|
|
747 |
}
|
0
|
748 |
}
|
12
|
749 |
out: ;
|
|
750 |
free(prefix);
|
|
751 |
free(suffix);
|
|
752 |
free(outCode);
|
46
|
753 |
|
12
|
754 |
return 1;
|
0
|
755 |
}
|
|
756 |
|
|
757 |
%}
|
77
|
758 |
! !
|
105
|
759 |
|
|
760 |
!ImageReader class methodsFor:'documentation'!
|
|
761 |
|
|
762 |
copyright
|
|
763 |
"
|
|
764 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
|
765 |
All Rights Reserved
|
|
766 |
|
|
767 |
This software is furnished under a license and may be used
|
|
768 |
only in accordance with the terms of that license and with the
|
|
769 |
inclusion of the above copyright notice. This software may not
|
|
770 |
be provided or otherwise made available to, or used by, any
|
|
771 |
other person. No title to or ownership of the software is
|
|
772 |
hereby transferred.
|
|
773 |
"
|
|
774 |
!
|
|
775 |
|
|
776 |
version
|
|
777 |
"
|
|
778 |
$Header: /cvs/stx/stx/libview/ImageReader.st,v 1.15 1995-02-18 15:57:27 claus Exp $
|
|
779 |
"
|
|
780 |
!
|
|
781 |
|
|
782 |
documentation
|
|
783 |
"
|
|
784 |
Abstract class to provide common functions for image-readers
|
|
785 |
(i.e. TIFFReader, GIFReader etc.)
|
|
786 |
|
|
787 |
ImageReaders are created temporary to read an image from a stream.
|
|
788 |
They read the stream and collect all relevant information internally.
|
|
789 |
Once done with reading, the image is asked for.
|
|
790 |
|
|
791 |
See implementation of fromStream: in concrete subclasses.
|
|
792 |
The public interfaces are:
|
|
793 |
<ConcreteReaderClass> fromFile:aFilename
|
|
794 |
or:
|
|
795 |
<ConcreteReaderClass> fromStream:aStream
|
|
796 |
|
|
797 |
However, usually this is done indirectly through
|
|
798 |
Image fromFile:aFileName
|
|
799 |
"
|
|
800 |
! !
|
|
801 |
|
|
802 |
!ImageReader class methodsFor:'i/o support'!
|
|
803 |
|
|
804 |
streamReadingFile:aFilename
|
|
805 |
"return a stream to read aFilename.
|
|
806 |
If the filename ends with '.Z' or '.gz', return a stream
|
|
807 |
to a pipe for the uncompressor. Otherwise, return a stream to read
|
|
808 |
the file directly."
|
|
809 |
|
|
810 |
|inStream name|
|
|
811 |
|
|
812 |
name := aFilename asString.
|
|
813 |
((name endsWith:'.Z') or:[name endsWith:'.gz']) ifTrue:[
|
|
814 |
inStream := PipeStream readingFrom:'gunzip < ' , name.
|
|
815 |
inStream isNil ifTrue:[
|
|
816 |
inStream := PipeStream readingFrom:'uncompress < ' , name.
|
|
817 |
]
|
|
818 |
] ifFalse:[
|
|
819 |
inStream := aFilename asFilename readStream.
|
|
820 |
].
|
|
821 |
inStream isNil ifTrue:[
|
|
822 |
'IMGREADER: open error on: ' errorPrint. aFilename errorPrintNL.
|
|
823 |
].
|
|
824 |
^ inStream
|
|
825 |
! !
|
|
826 |
|
|
827 |
!ImageReader class methodsFor:'fileIn / fileOut'!
|
|
828 |
|
|
829 |
fromStream:aStream
|
|
830 |
"read an image (in my format) from aStream"
|
|
831 |
|
|
832 |
|reader|
|
|
833 |
|
|
834 |
reader := self new fromStream:aStream.
|
|
835 |
reader notNil ifTrue:[
|
|
836 |
^ reader image
|
|
837 |
].
|
|
838 |
^ nil
|
|
839 |
!
|
|
840 |
|
|
841 |
fromFile:aFileName
|
|
842 |
"read an image (in my format) from aFileName.
|
|
843 |
Return the image or nil on error."
|
|
844 |
|
|
845 |
|result inStream|
|
|
846 |
|
|
847 |
inStream := self streamReadingFile:aFileName.
|
|
848 |
inStream isNil ifTrue:[
|
|
849 |
'IMGREADER: file open error' errorPrintNL.
|
|
850 |
^ nil
|
|
851 |
].
|
|
852 |
result := self fromStream:inStream.
|
|
853 |
inStream close.
|
|
854 |
^ result
|
|
855 |
|
|
856 |
"
|
|
857 |
XPMReader fromFile:'bitmaps/ljet.xpm'
|
|
858 |
XBMReader fromFile:'bitmaps/SBrowser.xbm'
|
|
859 |
"
|
|
860 |
!
|
|
861 |
|
|
862 |
save:anImage onFile:aFileName
|
|
863 |
"save the image in my format on aFileName"
|
|
864 |
|
|
865 |
^ (self basicNew) save:anImage onFile:aFileName
|
|
866 |
! !
|
|
867 |
|
|
868 |
!ImageReader class methodsFor:'testing'!
|
|
869 |
|
|
870 |
isValidImageFile:aFileName
|
|
871 |
"return true, if aFileName contains an image this
|
|
872 |
reader understands - should be redefined in subclasses"
|
|
873 |
|
|
874 |
^ false
|
|
875 |
! !
|
|
876 |
|
|
877 |
!ImageReader class methodsFor:'constants'!
|
|
878 |
|
|
879 |
reverseBits
|
|
880 |
"return a table filled with bit reverse information.
|
|
881 |
To convert from msbit-first to lsbit-first bytes, use
|
|
882 |
the value as index into the table, retrieving the reverse
|
|
883 |
value. Since indexing must start at 1, use (value + 1) as
|
|
884 |
index."
|
|
885 |
|
|
886 |
|val "{ Class: SmallInteger }" |
|
|
887 |
|
|
888 |
ReverseBits isNil ifTrue:[
|
|
889 |
ReverseBits := ByteArray new:256.
|
|
890 |
0 to:255 do:[:i |
|
|
891 |
val := 0.
|
|
892 |
(i bitTest:16r01) ifTrue:[val := val bitOr:16r80].
|
|
893 |
(i bitTest:16r02) ifTrue:[val := val bitOr:16r40].
|
|
894 |
(i bitTest:16r04) ifTrue:[val := val bitOr:16r20].
|
|
895 |
(i bitTest:16r08) ifTrue:[val := val bitOr:16r10].
|
|
896 |
(i bitTest:16r10) ifTrue:[val := val bitOr:16r08].
|
|
897 |
(i bitTest:16r20) ifTrue:[val := val bitOr:16r04].
|
|
898 |
(i bitTest:16r40) ifTrue:[val := val bitOr:16r02].
|
|
899 |
(i bitTest:16r80) ifTrue:[val := val bitOr:16r01].
|
|
900 |
ReverseBits at:(i + 1) put:val
|
|
901 |
]
|
|
902 |
].
|
|
903 |
^ ReverseBits
|
|
904 |
! !
|
|
905 |
|
|
906 |
!ImageReader class methodsFor:'cleanup'!
|
|
907 |
|
|
908 |
lowSpaceCleanup
|
|
909 |
"cleanup things we do not need"
|
|
910 |
|
|
911 |
ReverseBits := nil
|
|
912 |
! !
|
|
913 |
|
|
914 |
!ImageReader class methodsFor:'decompression support'!
|
|
915 |
|
|
916 |
decompressCCITT3From:srcBytes into:dstBytes startingAt:offset count:count
|
|
917 |
"decompress CCITT Group 3 compressed image data.
|
|
918 |
count bytes from srcBytes are decompressed into dstBytes.
|
|
919 |
Calls primitive c function for speed"
|
|
920 |
%{
|
|
921 |
if (__isByteArray(srcBytes)
|
|
922 |
&& __isByteArray(dstBytes)
|
|
923 |
&& __bothSmallInteger(offset, count)) {
|
|
924 |
if (__decodeCCITTgroup3__(_ByteArrayInstPtr(srcBytes)->ba_element,
|
|
925 |
_ByteArrayInstPtr(dstBytes)->ba_element
|
|
926 |
+ _intVal(offset) - 1,
|
|
927 |
_intVal(count))) {
|
|
928 |
RETURN ( self );
|
|
929 |
}
|
|
930 |
}
|
|
931 |
%}
|
|
932 |
.
|
|
933 |
self primitiveFailed
|
|
934 |
!
|
|
935 |
|
|
936 |
decompressLZWFrom:srcBytes count:count into:dstBytes startingAt:offset
|
|
937 |
"decompress LZW (tiff) compressed image data.
|
|
938 |
count bytes from srcBytes are decompressed into dstBytes.
|
|
939 |
Calls primitive c function for speed"
|
|
940 |
%{
|
|
941 |
if (__isByteArray(srcBytes)
|
|
942 |
&& __isByteArray(dstBytes)
|
|
943 |
&& __bothSmallInteger(offset, count)) {
|
|
944 |
if (__decodeLZW__(_ByteArrayInstPtr(srcBytes)->ba_element,
|
|
945 |
_ByteArrayInstPtr(dstBytes)->ba_element
|
|
946 |
+ _intVal(offset) - 1,
|
|
947 |
_intVal(count))) {
|
|
948 |
RETURN ( self );
|
|
949 |
}
|
|
950 |
}
|
|
951 |
%}
|
|
952 |
.
|
|
953 |
self primitiveFailed
|
|
954 |
!
|
|
955 |
|
|
956 |
decompressGIFFrom:srcBytes count:count into:dstBytes startingAt:offset codeLen:codeLen
|
|
957 |
"decompress GIF compressed image data.
|
|
958 |
count bytes from srcBytes are decompressed into dstBytes.
|
|
959 |
Calls primitive c function for speed"
|
|
960 |
%{
|
|
961 |
if (__isByteArray(srcBytes)
|
|
962 |
&& __isByteArray(dstBytes)
|
|
963 |
&& __bothSmallInteger(codeLen, offset)
|
|
964 |
&& __isSmallInteger(count)) {
|
|
965 |
if (__decodeGIF__(_ByteArrayInstPtr(srcBytes)->ba_element,
|
|
966 |
_ByteArrayInstPtr(dstBytes)->ba_element
|
|
967 |
+ _intVal(offset) - 1,
|
|
968 |
_intVal(count),
|
|
969 |
_intVal(codeLen))) {
|
|
970 |
RETURN ( self );
|
|
971 |
}
|
|
972 |
}
|
|
973 |
%}
|
|
974 |
.
|
|
975 |
self primitiveFailed
|
|
976 |
!
|
|
977 |
|
|
978 |
decodeDelta:step in:data width:width height:height
|
|
979 |
"perform NeXT special predictor delta decoding inplace in data.
|
|
980 |
Calls primitive c function for speed"
|
|
981 |
|
|
982 |
(step ~~ 3) ifTrue:[
|
|
983 |
^ self error:'only rgb pictures supported'
|
|
984 |
].
|
|
985 |
|
|
986 |
%{
|
|
987 |
if (__isByteArray(data)
|
|
988 |
&& __bothSmallInteger(width, height)) {
|
|
989 |
__decodeDelta__(_ByteArrayInstPtr(data)->ba_element,
|
|
990 |
_intVal(width), _intVal(height));
|
|
991 |
RETURN ( self );
|
|
992 |
}
|
|
993 |
%}
|
|
994 |
.
|
|
995 |
self primitiveFailed
|
|
996 |
! !
|
|
997 |
|
|
998 |
!ImageReader methodsFor:'accessing'!
|
|
999 |
|
|
1000 |
image
|
|
1001 |
"return the image represented by myself"
|
|
1002 |
|
|
1003 |
|image depth|
|
|
1004 |
|
|
1005 |
depth := self bitsPerPixel.
|
|
1006 |
image := (Image implementorForDepth:depth) new.
|
|
1007 |
image width:width.
|
|
1008 |
image height:height.
|
|
1009 |
image photometric:photometric.
|
|
1010 |
image samplesPerPixel:samplesPerPixel.
|
|
1011 |
image bitsPerSample:bitsPerSample.
|
|
1012 |
image colorMap:colorMap.
|
|
1013 |
image data:data.
|
|
1014 |
^ image
|
|
1015 |
!
|
|
1016 |
|
|
1017 |
height
|
|
1018 |
^ height
|
|
1019 |
!
|
|
1020 |
|
|
1021 |
photometric
|
|
1022 |
^ photometric
|
|
1023 |
!
|
|
1024 |
|
|
1025 |
width
|
|
1026 |
^ width
|
|
1027 |
!
|
|
1028 |
|
|
1029 |
samplesPerPixel
|
|
1030 |
^ samplesPerPixel
|
|
1031 |
!
|
|
1032 |
|
|
1033 |
data
|
|
1034 |
^ data
|
|
1035 |
!
|
|
1036 |
|
|
1037 |
colorMap
|
|
1038 |
^ colorMap
|
|
1039 |
!
|
|
1040 |
|
|
1041 |
bitsPerSample
|
|
1042 |
^ bitsPerSample
|
|
1043 |
!
|
|
1044 |
|
|
1045 |
bitsPerPixel
|
|
1046 |
"return the number of bits per pixel"
|
|
1047 |
|
|
1048 |
^ (bitsPerSample inject:0 into:[:sum :i | sum + i])
|
|
1049 |
! !
|
|
1050 |
|
|
1051 |
!ImageReader methodsFor:'fileIn / fileOut'!
|
|
1052 |
|
|
1053 |
save:image onFile:aFileName
|
|
1054 |
"save image in my format on aFile"
|
|
1055 |
|
|
1056 |
^ self subclassResponsibility
|
|
1057 |
!
|
|
1058 |
|
|
1059 |
fromStream:aStream
|
|
1060 |
"read imagedata in my format from aStream"
|
|
1061 |
|
|
1062 |
^ self subclassResponsibility
|
|
1063 |
! !
|
|
1064 |
|
|
1065 |
!ImageReader methodsFor:'i/o support'!
|
|
1066 |
|
|
1067 |
readLong
|
|
1068 |
"return the next 4-byte long, honoring the byte-order"
|
|
1069 |
|
|
1070 |
^ inStream nextLongMSB:(byteOrder ~~ #lsb)
|
|
1071 |
!
|
|
1072 |
|
|
1073 |
readShort
|
|
1074 |
"return the next 2-byte short, honoring the byte-order"
|
|
1075 |
|
|
1076 |
^ inStream nextUnsignedShortMSB:(byteOrder ~~ #lsb)
|
|
1077 |
!
|
|
1078 |
|
|
1079 |
readShortLong
|
|
1080 |
"return the next 2-byte short, honoring the byte-order.
|
|
1081 |
There are actually 4 bytes read, but only 2 looked at."
|
|
1082 |
|
|
1083 |
|bytes val|
|
|
1084 |
|
|
1085 |
bytes := ByteArray new:4.
|
|
1086 |
inStream nextBytes:4 into:bytes.
|
|
1087 |
(byteOrder == #lsb) ifTrue:[
|
|
1088 |
val := bytes at:2.
|
|
1089 |
val := val * 256 + (bytes at:1)
|
|
1090 |
] ifFalse:[
|
|
1091 |
val := bytes at:3.
|
|
1092 |
val := val * 256 + (bytes at:4)
|
|
1093 |
].
|
|
1094 |
^ val
|
|
1095 |
!
|
|
1096 |
|
|
1097 |
writeLong:anInteger
|
|
1098 |
"write a 4-byte long, honoring the byte-order."
|
|
1099 |
|
|
1100 |
outStream nextPutLong:anInteger MSB:(byteOrder ~~ #lsb)
|
|
1101 |
!
|
|
1102 |
|
|
1103 |
writeShort:anInteger
|
|
1104 |
"write a 2-byte short, honoring the byte-order."
|
|
1105 |
|
|
1106 |
outStream nextPutShort:anInteger MSB:(byteOrder ~~ #lsb)
|
|
1107 |
! !
|
|
1108 |
|