0
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1991-93 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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
|
|
14 |
instanceVariableNames:'width height data byteOrder inStream outStream
|
|
15 |
photometric samplesPerPixel bitsPerSample
|
|
16 |
colorMap'
|
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'Graphics-Support'
|
|
20 |
!
|
|
21 |
|
|
22 |
ImageReader comment:'
|
|
23 |
|
|
24 |
COPYRIGHT (c) 1991-93 by Claus Gittinger
|
|
25 |
All Rights Reserved
|
|
26 |
|
|
27 |
common functions for image-readers (i.e. TIFFReader, GIFReader etc.)
|
|
28 |
|
|
29 |
%W% %E%
|
|
30 |
written Summer 91 by claus
|
|
31 |
'!
|
|
32 |
|
|
33 |
!ImageReader class methodsFor:'testing'!
|
|
34 |
|
|
35 |
isValidImageFile:aFileName
|
|
36 |
"return true, if aFileName contains an image this
|
|
37 |
reader understands - should be redefined in subclasses"
|
|
38 |
|
|
39 |
^ false
|
|
40 |
! !
|
|
41 |
|
|
42 |
!ImageReader class methodsFor:'fileIn / fileOut'!
|
|
43 |
|
|
44 |
fromFile:aFileName
|
|
45 |
|reader image|
|
|
46 |
|
|
47 |
reader := self new.
|
|
48 |
(reader fromFile:aFileName) notNil ifTrue:[
|
|
49 |
image := Image new.
|
|
50 |
image width:(reader width).
|
|
51 |
image height:(reader height).
|
|
52 |
image photometric:(reader photometric).
|
|
53 |
image samplesPerPixel:(reader samplesPerPixel).
|
|
54 |
image bitsPerSample:(reader bitsPerSample).
|
|
55 |
image colorMap:(reader colorMap).
|
|
56 |
image data:(reader data).
|
|
57 |
^ image
|
|
58 |
].
|
|
59 |
^ nil
|
|
60 |
!
|
|
61 |
|
|
62 |
save:anImage onFile:aFileName
|
|
63 |
^ (self basicNew) save:anImage onFile:aFileName
|
|
64 |
! !
|
|
65 |
|
|
66 |
!ImageReader methodsFor:'accessing'!
|
|
67 |
|
|
68 |
width
|
|
69 |
^ width
|
|
70 |
!
|
|
71 |
|
|
72 |
height
|
|
73 |
^ height
|
|
74 |
!
|
|
75 |
|
|
76 |
data
|
|
77 |
^ data
|
|
78 |
!
|
|
79 |
|
|
80 |
photometric
|
|
81 |
^ photometric
|
|
82 |
!
|
|
83 |
|
|
84 |
colorMap
|
|
85 |
^ colorMap
|
|
86 |
!
|
|
87 |
|
|
88 |
samplesPerPixel
|
|
89 |
^ samplesPerPixel
|
|
90 |
!
|
|
91 |
|
|
92 |
bitsPerSample
|
|
93 |
^ bitsPerSample
|
|
94 |
! !
|
|
95 |
|
|
96 |
!ImageReader methodsFor:'fileIn / fileOut'!
|
|
97 |
|
|
98 |
fromFile:aFileName
|
|
99 |
^ self subclassResponsibility
|
|
100 |
!
|
|
101 |
|
|
102 |
save:image onFile:aFileName
|
|
103 |
^ self subclassResponsibility
|
|
104 |
! !
|
|
105 |
|
|
106 |
!ImageReader methodsFor:'i/o support'!
|
|
107 |
|
|
108 |
readLong
|
|
109 |
|bytes val|
|
|
110 |
|
|
111 |
bytes := ByteArray new:4.
|
|
112 |
inStream nextBytes:4 into:bytes.
|
|
113 |
(byteOrder == #lsb) ifTrue:[
|
|
114 |
val := bytes at:4.
|
|
115 |
val := val * 256 + (bytes at:3).
|
|
116 |
val := val * 256 + (bytes at:2).
|
|
117 |
val := val * 256 + (bytes at:1)
|
|
118 |
] ifFalse:[
|
|
119 |
val := bytes at:1.
|
|
120 |
val := val * 256 + (bytes at:2).
|
|
121 |
val := val * 256 + (bytes at:3).
|
|
122 |
val := val * 256 + (bytes at:4)
|
|
123 |
].
|
|
124 |
^ val
|
|
125 |
!
|
|
126 |
|
|
127 |
readShort
|
|
128 |
|bytes val|
|
|
129 |
|
|
130 |
bytes := ByteArray new:2.
|
|
131 |
inStream nextBytes:2 into:bytes.
|
|
132 |
(byteOrder == #lsb) ifTrue:[
|
|
133 |
val := bytes at:2.
|
|
134 |
val := val * 256 + (bytes at:1)
|
|
135 |
] ifFalse:[
|
|
136 |
val := bytes at:1.
|
|
137 |
val := val * 256 + (bytes at:2)
|
|
138 |
].
|
|
139 |
^ val
|
|
140 |
!
|
|
141 |
|
|
142 |
readShortLong
|
|
143 |
|bytes val|
|
|
144 |
|
|
145 |
bytes := ByteArray new:4.
|
|
146 |
inStream nextBytes:4 into:bytes.
|
|
147 |
(byteOrder == #lsb) ifTrue:[
|
|
148 |
val := bytes at:2.
|
|
149 |
val := val * 256 + (bytes at:1)
|
|
150 |
] ifFalse:[
|
|
151 |
val := bytes at:3.
|
|
152 |
val := val * 256 + (bytes at:4)
|
|
153 |
].
|
|
154 |
^ val
|
|
155 |
!
|
|
156 |
|
|
157 |
writeLong:anInteger
|
|
158 |
|bytes i|
|
|
159 |
|
|
160 |
i := anInteger.
|
|
161 |
bytes := ByteArray new:4.
|
|
162 |
(byteOrder == #lsb) ifTrue:[
|
|
163 |
bytes at:1 put:(i bitAnd:16rFF).
|
|
164 |
i := i // 256.
|
|
165 |
bytes at:2 put:(i bitAnd:16rFF).
|
|
166 |
i := i // 256.
|
|
167 |
bytes at:3 put:(i bitAnd:16rFF).
|
|
168 |
i := i // 256.
|
|
169 |
bytes at:4 put:(i bitAnd:16rFF).
|
|
170 |
] ifFalse:[
|
|
171 |
bytes at:4 put:(i bitAnd:16rFF).
|
|
172 |
i := i // 256.
|
|
173 |
bytes at:3 put:(i bitAnd:16rFF).
|
|
174 |
i := i // 256.
|
|
175 |
bytes at:2 put:(i bitAnd:16rFF).
|
|
176 |
i := i // 256.
|
|
177 |
bytes at:1 put:(i bitAnd:16rFF).
|
|
178 |
].
|
|
179 |
outStream nextPutBytes:4 from:bytes
|
|
180 |
!
|
|
181 |
|
|
182 |
writeShort:anInteger
|
|
183 |
|bytes i|
|
|
184 |
|
|
185 |
i := anInteger.
|
|
186 |
bytes := ByteArray new:2.
|
|
187 |
(byteOrder == #lsb) ifTrue:[
|
|
188 |
bytes at:1 put:(i bitAnd:16rFF).
|
|
189 |
i := i // 256.
|
|
190 |
bytes at:2 put:(i bitAnd:16rFF).
|
|
191 |
] ifFalse:[
|
|
192 |
bytes at:2 put:(i bitAnd:16rFF).
|
|
193 |
i := i // 256.
|
|
194 |
bytes at:1 put:(i bitAnd:16rFF).
|
|
195 |
].
|
|
196 |
outStream nextPutBytes:2 from:bytes
|
|
197 |
! !
|
|
198 |
|
|
199 |
!ImageReader class methodsFor:'decompression support'!
|
|
200 |
|
|
201 |
decompressCCITT3From:srcBytes into:dstBytes startingAt:offset count:count
|
|
202 |
"same as above but using primitive for speed"
|
|
203 |
%{
|
|
204 |
if ((_Class(srcBytes) == ByteArray)
|
|
205 |
&& (_Class(dstBytes) == ByteArray)
|
|
206 |
&& _isSmallInteger(offset)
|
|
207 |
&& _isSmallInteger(count)) {
|
|
208 |
decodeCCITTgroup3(_ByteArrayInstPtr(srcBytes)->ba_element,
|
|
209 |
_ByteArrayInstPtr(dstBytes)->ba_element
|
|
210 |
+ _intVal(offset) - 1,
|
|
211 |
_intVal(count));
|
|
212 |
RETURN ( self );
|
|
213 |
}
|
|
214 |
%}
|
|
215 |
.
|
|
216 |
self primitiveFailed
|
|
217 |
!
|
|
218 |
|
|
219 |
decompressLZWFrom:srcBytes count:count into:dstBytes startingAt:offset
|
|
220 |
%{
|
|
221 |
if ((_Class(srcBytes) == ByteArray)
|
|
222 |
&& (_Class(dstBytes) == ByteArray)
|
|
223 |
&& _isSmallInteger(offset)
|
|
224 |
&& _isSmallInteger(count)) {
|
|
225 |
decodeLZW(_ByteArrayInstPtr(srcBytes)->ba_element,
|
|
226 |
_ByteArrayInstPtr(dstBytes)->ba_element
|
|
227 |
+ _intVal(offset) - 1,
|
|
228 |
_intVal(count));
|
|
229 |
RETURN ( self );
|
|
230 |
}
|
|
231 |
%}
|
|
232 |
.
|
|
233 |
self primitiveFailed
|
|
234 |
!
|
|
235 |
|
|
236 |
decodeDelta:step in:data width:width height:height
|
|
237 |
(step ~~ 3) ifTrue:[
|
|
238 |
^ self error:'only rgb pictures supported'
|
|
239 |
].
|
|
240 |
|
|
241 |
%{
|
|
242 |
if ((_Class(data) == ByteArray)
|
|
243 |
&& _isSmallInteger(width)
|
|
244 |
&& _isSmallInteger(height)) {
|
|
245 |
decodeDelta(_ByteArrayInstPtr(data)->ba_element,
|
|
246 |
_intVal(width), _intVal(height));
|
|
247 |
RETURN ( self );
|
|
248 |
}
|
|
249 |
%}
|
|
250 |
.
|
|
251 |
self primitiveFailed
|
|
252 |
!
|
|
253 |
|
|
254 |
decompressGIFFrom:srcBytes count:count into:dstBytes startingAt:offset codeLen:codeLen
|
|
255 |
%{
|
|
256 |
if ((_Class(srcBytes) == ByteArray)
|
|
257 |
&& (_Class(dstBytes) == ByteArray)
|
|
258 |
&& _isSmallInteger(codeLen)
|
|
259 |
&& _isSmallInteger(offset)
|
|
260 |
&& _isSmallInteger(count)) {
|
|
261 |
decodeGIF(_ByteArrayInstPtr(srcBytes)->ba_element,
|
|
262 |
_ByteArrayInstPtr(dstBytes)->ba_element
|
|
263 |
+ _intVal(offset) - 1,
|
|
264 |
_intVal(count),
|
|
265 |
_intVal(codeLen));
|
|
266 |
RETURN ( self );
|
|
267 |
}
|
|
268 |
%}
|
|
269 |
.
|
|
270 |
self primitiveFailed
|
|
271 |
! !
|
|
272 |
|
|
273 |
%{
|
|
274 |
|
|
275 |
/*
|
|
276 |
* ccitt decompression
|
|
277 |
*/
|
|
278 |
static short *whiteCountTable;
|
|
279 |
static char *whiteShiftTable;
|
|
280 |
static short *blackCountTable;
|
|
281 |
static char *blackShiftTable;
|
|
282 |
|
|
283 |
struct ccitt_def {
|
|
284 |
unsigned short bits;
|
|
285 |
short nBits;
|
|
286 |
};
|
|
287 |
|
|
288 |
static struct ccitt_def
|
|
289 |
whiteDef[] = {
|
|
290 |
{ 0x3500, 8 }, /* 0 */
|
|
291 |
{ 0x1c00, 6 },
|
|
292 |
{ 0x7000, 4 },
|
|
293 |
{ 0x8000, 4 },
|
|
294 |
{ 0xb000, 4 },
|
|
295 |
{ 0xc000, 4 },
|
|
296 |
{ 0xe000, 4 },
|
|
297 |
{ 0xf000, 4 },
|
|
298 |
{ 0x9800, 5 },
|
|
299 |
{ 0xA000, 5 },
|
|
300 |
{ 0x3800, 5 }, /* 10 */
|
|
301 |
{ 0x4000, 5 },
|
|
302 |
{ 0x2000, 6 },
|
|
303 |
{ 0x0c00, 6 },
|
|
304 |
{ 0xd000, 6 },
|
|
305 |
{ 0xd400, 6 },
|
|
306 |
{ 0xa800, 6 },
|
|
307 |
{ 0xac00, 6 },
|
|
308 |
{ 0x4e00, 7 },
|
|
309 |
{ 0x1800, 7 },
|
|
310 |
{ 0x1000, 7 }, /* 20 */
|
|
311 |
{ 0x2e00, 7 },
|
|
312 |
{ 0x0600, 7 },
|
|
313 |
{ 0x0800, 7 },
|
|
314 |
{ 0x5000, 7 },
|
|
315 |
{ 0x5600, 7 },
|
|
316 |
{ 0x2600, 7 },
|
|
317 |
{ 0x4800, 7 },
|
|
318 |
{ 0x3000, 7 },
|
|
319 |
{ 0x0200, 8 },
|
|
320 |
{ 0x0300, 8 }, /* 30 */
|
|
321 |
{ 0x1a00, 8 },
|
|
322 |
{ 0x1b00, 8 },
|
|
323 |
{ 0x1200, 8 },
|
|
324 |
{ 0x1300, 8 },
|
|
325 |
{ 0x1400, 8 },
|
|
326 |
{ 0x1500, 8 },
|
|
327 |
{ 0x1600, 8 },
|
|
328 |
{ 0x1700, 8 },
|
|
329 |
{ 0x2800, 8 },
|
|
330 |
{ 0x2900, 8 }, /* 40 */
|
|
331 |
{ 0x2a00, 8 },
|
|
332 |
{ 0x2b00, 8 },
|
|
333 |
{ 0x2c00, 8 },
|
|
334 |
{ 0x2d00, 8 },
|
|
335 |
{ 0x0400, 8 },
|
|
336 |
{ 0x0500, 8 },
|
|
337 |
{ 0x0a00, 8 },
|
|
338 |
{ 0x0b00, 8 },
|
|
339 |
{ 0x5200, 8 },
|
|
340 |
{ 0x5300, 8 }, /* 50 */
|
|
341 |
{ 0x5400, 8 },
|
|
342 |
{ 0x5500, 8 },
|
|
343 |
{ 0x2400, 8 },
|
|
344 |
{ 0x2500, 8 },
|
|
345 |
{ 0x5800, 8 },
|
|
346 |
{ 0x5900, 8 },
|
|
347 |
{ 0x5a00, 8 },
|
|
348 |
{ 0x5b00, 8 },
|
|
349 |
{ 0x4a00, 8 },
|
|
350 |
{ 0x4b00, 8 }, /* 60 */
|
|
351 |
{ 0x3200, 8 },
|
|
352 |
{ 0x3300, 8 },
|
|
353 |
{ 0x3400, 8 },
|
|
354 |
/* ---------------- */
|
|
355 |
{ 0xd800, 5 }, /* 64 */
|
|
356 |
{ 0x9000, 5 }, /* 128 */
|
|
357 |
{ 0x5c00, 6 }, /* 192 */
|
|
358 |
{ 0x6e00, 7 }, /* 256 */
|
|
359 |
{ 0x3600, 8 }, /* 320 */
|
|
360 |
{ 0x3700, 8 },
|
|
361 |
{ 0x6400, 8 },
|
|
362 |
{ 0x6500, 8 },
|
|
363 |
{ 0x6800, 8 },
|
|
364 |
{ 0x6700, 8 }, /* 640 */
|
|
365 |
{ 0x6600, 9 }, /* 704 */
|
|
366 |
{ 0x6680, 9 },
|
|
367 |
{ 0x6900, 9 },
|
|
368 |
{ 0x6980, 9 },
|
|
369 |
{ 0x6a00, 9 },
|
|
370 |
{ 0x6a80, 9 },
|
|
371 |
{ 0x6b00, 9 },
|
|
372 |
{ 0x6b80, 9 },
|
|
373 |
{ 0x6c00, 9 },
|
|
374 |
{ 0x6c80, 9 },
|
|
375 |
{ 0x6d00, 9 },
|
|
376 |
{ 0x6d80, 9 },
|
|
377 |
{ 0x4c00, 9 },
|
|
378 |
{ 0x4c80, 9 },
|
|
379 |
{ 0x4d00, 9 }, /* 1600 */
|
|
380 |
{ 0x6000, 6 }, /* 1664 */
|
|
381 |
{ 0x4d80, 9 }, /* 1728 */
|
|
382 |
/* -------------------------------- */
|
|
383 |
{ 0x0100, 11 }, /* 1792 */
|
|
384 |
{ 0x0180, 11 },
|
|
385 |
{ 0x01a0, 11 }, /* 1920 */
|
|
386 |
{ 0x0120, 12 }, /* 1984 */
|
|
387 |
{ 0x0130, 12 },
|
|
388 |
{ 0x0140, 12 },
|
|
389 |
{ 0x0150, 12 },
|
|
390 |
{ 0x0160, 12 },
|
|
391 |
{ 0x0170, 12 },
|
|
392 |
{ 0x01c0, 12 },
|
|
393 |
{ 0x01d0, 12 },
|
|
394 |
{ 0x01e0, 12 },
|
|
395 |
{ 0x01f0, 12 }, /* 2560 */
|
|
396 |
/* -------------------------------- */
|
|
397 |
{ 0x0010, 12 }, /* EOL */
|
|
398 |
};
|
|
399 |
|
|
400 |
static struct ccitt_def
|
|
401 |
blackDef[] = {
|
|
402 |
{ 0x0dc0, 10 }, /* 0 */
|
|
403 |
{ 0x4000, 3 },
|
|
404 |
{ 0xc000, 2 },
|
|
405 |
{ 0x8000, 2 },
|
|
406 |
{ 0x6000, 3 },
|
|
407 |
{ 0x3000, 4 },
|
|
408 |
{ 0x2000, 4 },
|
|
409 |
{ 0x1800, 5 },
|
|
410 |
{ 0x1400, 6 },
|
|
411 |
{ 0x1000, 6 },
|
|
412 |
{ 0x0800, 7 }, /* 10 */
|
|
413 |
{ 0x0a00, 7 },
|
|
414 |
{ 0x0e00, 7 },
|
|
415 |
{ 0x0400, 8 },
|
|
416 |
{ 0x0700, 8 },
|
|
417 |
{ 0x0c00, 9 },
|
|
418 |
{ 0x05c0, 10 },
|
|
419 |
{ 0x0600, 10 },
|
|
420 |
{ 0x0200, 10 },
|
|
421 |
{ 0x0ce0, 11 },
|
|
422 |
{ 0x0d00, 11 }, /* 20 */
|
|
423 |
{ 0x0d80, 11 },
|
|
424 |
{ 0x06e0, 11 },
|
|
425 |
{ 0x0500, 11 },
|
|
426 |
{ 0x02e0, 11 },
|
|
427 |
{ 0x0300, 11 },
|
|
428 |
{ 0x0ca0, 12 },
|
|
429 |
{ 0x0cb0, 12 },
|
|
430 |
{ 0x0cc0, 12 },
|
|
431 |
{ 0x0cd0, 12 },
|
|
432 |
{ 0x0680, 12 }, /* 30 */
|
|
433 |
{ 0x0690, 12 },
|
|
434 |
{ 0x06a0, 12 },
|
|
435 |
{ 0x06b0, 12 },
|
|
436 |
{ 0x0d20, 12 },
|
|
437 |
{ 0x0d30, 12 },
|
|
438 |
{ 0x0d40, 12 },
|
|
439 |
{ 0x0d50, 12 },
|
|
440 |
{ 0x0d60, 12 },
|
|
441 |
{ 0x0d70, 12 },
|
|
442 |
{ 0x06c0, 12 }, /* 40 */
|
|
443 |
{ 0x06d0, 12 },
|
|
444 |
{ 0x0da0, 12 },
|
|
445 |
{ 0x0db0, 12 },
|
|
446 |
{ 0x0540, 12 },
|
|
447 |
{ 0x0550, 12 },
|
|
448 |
{ 0x0560, 12 },
|
|
449 |
{ 0x0570, 12 },
|
|
450 |
{ 0x0640, 12 },
|
|
451 |
{ 0x0650, 12 },
|
|
452 |
{ 0x0520, 12 }, /* 50 */
|
|
453 |
{ 0x0530, 12 },
|
|
454 |
{ 0x0240, 12 },
|
|
455 |
{ 0x0370, 12 },
|
|
456 |
{ 0x0380, 12 },
|
|
457 |
{ 0x0270, 12 },
|
|
458 |
{ 0x0280, 12 },
|
|
459 |
{ 0x0580, 12 },
|
|
460 |
{ 0x0590, 12 },
|
|
461 |
{ 0x02b0, 12 },
|
|
462 |
{ 0x02c0, 12 }, /* 60 */
|
|
463 |
{ 0x05a0, 12 },
|
|
464 |
{ 0x0660, 12 },
|
|
465 |
{ 0x0670, 12 },
|
|
466 |
/* ---------------- */
|
|
467 |
{ 0x03c0, 10 }, /* 64 */
|
|
468 |
{ 0x0c80, 12 }, /* 128 */
|
|
469 |
{ 0x0c90, 12 }, /* 192 */
|
|
470 |
{ 0x05b0, 12 }, /* 256 */
|
|
471 |
{ 0x0330, 12 }, /* 320 */
|
|
472 |
{ 0x0340, 12 },
|
|
473 |
{ 0x0350, 12 }, /* 448 */
|
|
474 |
{ 0x0360, 13 }, /* 512 */
|
|
475 |
{ 0x0368, 13 },
|
|
476 |
{ 0x0250, 13 }, /* 640 */
|
|
477 |
{ 0x0258, 13 }, /* 704 */
|
|
478 |
{ 0x0260, 13 },
|
|
479 |
{ 0x0268, 13 },
|
|
480 |
{ 0x0390, 13 },
|
|
481 |
{ 0x0398, 13 },
|
|
482 |
{ 0x03a0, 13 },
|
|
483 |
{ 0x03a8, 13 },
|
|
484 |
{ 0x03b0, 13 },
|
|
485 |
{ 0x03b8, 13 },
|
|
486 |
{ 0x0290, 13 },
|
|
487 |
{ 0x0298, 13 },
|
|
488 |
{ 0x02a0, 13 },
|
|
489 |
{ 0x02a8, 13 },
|
|
490 |
{ 0x02d0, 13 },
|
|
491 |
{ 0x02d8, 13 }, /* 1600 */
|
|
492 |
{ 0x0320, 13 }, /* 1664 */
|
|
493 |
{ 0x0328, 13 }, /* 1728 */
|
|
494 |
/* -------------------------------- */
|
|
495 |
};
|
|
496 |
|
|
497 |
static
|
|
498 |
initCCITTTables() {
|
|
499 |
register cnt, bits, value;
|
|
500 |
int nBits, index;
|
|
501 |
|
|
502 |
if (whiteCountTable != (short *)0) return;
|
|
503 |
|
|
504 |
whiteCountTable = (short *) malloc(sizeof(short) * 8192);
|
|
505 |
whiteShiftTable = (char *) malloc(sizeof(char) * 8192);
|
|
506 |
blackCountTable = (short *) malloc(sizeof(short) * 8192);
|
|
507 |
blackShiftTable = (char *) malloc(sizeof(char) * 8192);
|
|
508 |
|
|
509 |
for (index = 0; index < 8192; index++) {
|
|
510 |
whiteCountTable[index] = -1;
|
|
511 |
blackCountTable[index] = -1;
|
|
512 |
}
|
|
513 |
|
|
514 |
for (value = 0; value <= 63; value++) {
|
|
515 |
nBits = whiteDef[value].nBits;
|
|
516 |
bits = whiteDef[value].bits >> 3;
|
|
517 |
for (cnt = 1 << (13 - nBits); cnt; cnt--, bits++) {
|
|
518 |
whiteCountTable[bits] = value;
|
|
519 |
whiteShiftTable[bits] = nBits;
|
|
520 |
}
|
|
521 |
nBits = blackDef[value].nBits;
|
|
522 |
bits = blackDef[value].bits >> 3;
|
|
523 |
for (cnt = 1 << (13 - nBits); cnt; cnt--, bits++) {
|
|
524 |
blackCountTable[bits] = value;
|
|
525 |
blackShiftTable[bits] = nBits;
|
|
526 |
}
|
|
527 |
}
|
|
528 |
index = value;
|
|
529 |
|
|
530 |
for (; value <= 1728; value += 64) {
|
|
531 |
nBits = whiteDef[index].nBits;
|
|
532 |
bits = whiteDef[index].bits >> 3;
|
|
533 |
for (cnt = 1 << (13 - nBits); cnt; cnt--, bits++) {
|
|
534 |
whiteCountTable[bits] = value;
|
|
535 |
whiteShiftTable[bits] = nBits;
|
|
536 |
}
|
|
537 |
nBits = blackDef[index].nBits;
|
|
538 |
bits = blackDef[index].bits >> 3;
|
|
539 |
for (cnt = 1 << (13 - nBits); cnt; cnt--, bits++) {
|
|
540 |
blackCountTable[bits] = value;
|
|
541 |
blackShiftTable[bits] = nBits;
|
|
542 |
}
|
|
543 |
index++;
|
|
544 |
}
|
|
545 |
|
|
546 |
for (; value <= 2560; value += 64) {
|
|
547 |
nBits = whiteDef[index].nBits;
|
|
548 |
bits = whiteDef[index].bits >> 3;
|
|
549 |
for (cnt = 1 << (13 - nBits); cnt; cnt--, bits++) {
|
|
550 |
whiteCountTable[bits] = value;
|
|
551 |
whiteShiftTable[bits] = nBits;
|
|
552 |
blackCountTable[bits] = value;
|
|
553 |
blackShiftTable[bits] = nBits;
|
|
554 |
}
|
|
555 |
index++;
|
|
556 |
}
|
|
557 |
}
|
|
558 |
|
|
559 |
static short
|
|
560 |
leftBits[] = {
|
|
561 |
0, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFF
|
|
562 |
};
|
|
563 |
|
|
564 |
decodeCCITTgroup3(from, to, len)
|
|
565 |
unsigned char *from;
|
|
566 |
register unsigned char *to;
|
|
567 |
{
|
|
568 |
register cnt;
|
|
569 |
register short *countPtr;
|
|
570 |
register char *shiftPtr;
|
|
571 |
unsigned bits, bits13;
|
|
572 |
int shift, outCount, nBitsLess13;
|
|
573 |
int outBitOffset;
|
|
574 |
int nLeft, t;
|
|
575 |
|
|
576 |
if (! whiteCountTable) initCCITTTables();
|
|
577 |
|
|
578 |
countPtr = whiteCountTable;
|
|
579 |
shiftPtr = whiteShiftTable;
|
|
580 |
outCount = 0;
|
|
581 |
outBitOffset = 0;
|
|
582 |
bits = *from++;
|
|
583 |
bits = (bits << 8) | *from++;
|
|
584 |
nBitsLess13 = 3;
|
|
585 |
for (;;) {
|
|
586 |
bits13 = (bits >> nBitsLess13) & 0x1FFF;
|
|
587 |
cnt = countPtr[bits13];
|
|
588 |
if (cnt < 0) return;
|
|
589 |
shift = shiftPtr[bits13];
|
|
590 |
outCount += cnt;
|
|
591 |
if (countPtr == blackCountTable) {
|
|
592 |
/* toggle if it was a terminating code */
|
|
593 |
if (cnt < 64) {
|
|
594 |
countPtr = whiteCountTable;
|
|
595 |
shiftPtr = whiteShiftTable;
|
|
596 |
}
|
|
597 |
|
|
598 |
/* draw cnt black bits */
|
|
599 |
if (cnt) {
|
|
600 |
if (outBitOffset) {
|
|
601 |
nLeft = 8 - outBitOffset;
|
|
602 |
if (cnt < nLeft) nLeft = cnt;
|
|
603 |
t = leftBits[nLeft] >> outBitOffset;
|
|
604 |
*to |= t;
|
|
605 |
cnt -= nLeft;
|
|
606 |
outBitOffset += nLeft;
|
|
607 |
if (outBitOffset >= 8) {
|
|
608 |
to++;
|
|
609 |
outBitOffset -= 8;
|
|
610 |
}
|
|
611 |
}
|
|
612 |
if (cnt > 256) {
|
|
613 |
while ((int)to & 3) {
|
|
614 |
*to++ = 0xFF;
|
|
615 |
cnt -= 8;
|
|
616 |
}
|
|
617 |
while (cnt >= 32) {
|
|
618 |
(*(long *)to) = 0xFFFFFFFF;
|
|
619 |
to += 4;
|
|
620 |
cnt -= 32;
|
|
621 |
}
|
|
622 |
}
|
|
623 |
while (cnt >= 8) {
|
|
624 |
*to++ = 0xFF;
|
|
625 |
cnt -= 8;
|
|
626 |
}
|
|
627 |
*to |= leftBits[cnt];
|
|
628 |
outBitOffset += cnt;
|
|
629 |
}
|
|
630 |
} else {
|
|
631 |
/* toggle if it was a terminating code */
|
|
632 |
if (cnt < 64) {
|
|
633 |
countPtr = blackCountTable;
|
|
634 |
shiftPtr = blackShiftTable;
|
|
635 |
}
|
|
636 |
|
|
637 |
/* skip cnt bits */
|
|
638 |
to += cnt >> 3;
|
|
639 |
outBitOffset += cnt & 7;
|
|
640 |
if (outBitOffset >= 8) {
|
|
641 |
to++;
|
|
642 |
outBitOffset -= 8;
|
|
643 |
}
|
|
644 |
}
|
|
645 |
if (outCount >= len) return;
|
|
646 |
|
|
647 |
nBitsLess13 -= shift;
|
|
648 |
while (nBitsLess13 < 0) {
|
|
649 |
bits = (bits << 8) | *from++;
|
|
650 |
nBitsLess13 += 8;
|
|
651 |
}
|
|
652 |
}
|
|
653 |
}
|
|
654 |
|
|
655 |
/*
|
|
656 |
* LZW decompression
|
|
657 |
*/
|
|
658 |
struct buffer {
|
|
659 |
struct buffer *prev;
|
|
660 |
unsigned char chars[8192 - 4];
|
|
661 |
};
|
|
662 |
|
|
663 |
decodeLZW(from, to, inCount)
|
|
664 |
unsigned char *from;
|
|
665 |
unsigned char *to;
|
|
666 |
{
|
|
667 |
register unsigned code;
|
|
668 |
unsigned char *strings[4096];
|
|
669 |
short stringLen[4096];
|
|
670 |
struct buffer *scratchBuffer;
|
|
671 |
struct buffer *newBuffer;
|
|
672 |
unsigned char *scratchPtr;
|
|
673 |
int nScratch;
|
|
674 |
unsigned nextCode, oldCode;
|
|
675 |
register unsigned bits;
|
|
676 |
int nBits, mask, shift;
|
|
677 |
int i;
|
|
678 |
int len;
|
|
679 |
int codeLen = 9;
|
|
680 |
|
|
681 |
scratchBuffer = (struct buffer *)malloc(sizeof(struct buffer));
|
|
682 |
if (! scratchBuffer) return;
|
|
683 |
scratchBuffer->prev = (struct buffer *)0;
|
|
684 |
scratchPtr = scratchBuffer->chars;
|
|
685 |
nScratch = sizeof(scratchBuffer->chars);
|
|
686 |
|
|
687 |
for (i = 0; i < 256; i++) {
|
|
688 |
*scratchPtr = i;
|
|
689 |
strings[i] = scratchPtr++;
|
|
690 |
stringLen[i] = 1;
|
|
691 |
}
|
|
692 |
|
|
693 |
nextCode = 258;
|
|
694 |
nScratch -= 256;
|
|
695 |
mask = 0x1FF;
|
|
696 |
nBits = 0;
|
|
697 |
bits = 0;
|
|
698 |
while (inCount) {
|
|
699 |
/* fetch code */
|
|
700 |
while (nBits < codeLen) {
|
|
701 |
bits = (bits<<8) | *from++;
|
|
702 |
inCount--;
|
|
703 |
nBits += 8;
|
|
704 |
}
|
|
705 |
shift = nBits - codeLen;
|
|
706 |
code = (bits >> shift) & mask;
|
|
707 |
bits &= ~(mask << shift);
|
|
708 |
nBits -= codeLen;
|
|
709 |
if (code == 257) break;
|
|
710 |
if (code == 256) {
|
|
711 |
if (! inCount)
|
|
712 |
break;
|
|
713 |
|
|
714 |
/* free stuff */
|
|
715 |
while (scratchBuffer->prev) {
|
|
716 |
newBuffer = scratchBuffer;
|
|
717 |
scratchBuffer = scratchBuffer->prev;
|
|
718 |
free(newBuffer);
|
|
719 |
}
|
|
720 |
/* reset everything */
|
|
721 |
scratchPtr = scratchBuffer->chars + 256;
|
|
722 |
nScratch = sizeof(scratchBuffer->chars) - 256;
|
|
723 |
codeLen = 9;
|
|
724 |
nextCode = 258;
|
|
725 |
mask = 0x1FF;
|
|
726 |
/* fetch code */
|
|
727 |
while (nBits < codeLen) {
|
|
728 |
bits = (bits<<8) | *from++;
|
|
729 |
inCount--;
|
|
730 |
nBits += 8;
|
|
731 |
}
|
|
732 |
shift = nBits - codeLen;
|
|
733 |
code = (bits >> shift) & mask;
|
|
734 |
bits &= ~(mask << shift);
|
|
735 |
nBits -= codeLen;
|
|
736 |
if (code == 257) break;
|
|
737 |
/* add to output */
|
|
738 |
*to++ = code;
|
|
739 |
oldCode = code;
|
|
740 |
} else {
|
|
741 |
if (code < nextCode) {
|
|
742 |
/* writeString(string[code]) */
|
|
743 |
len = stringLen[code];
|
|
744 |
bcopy(strings[code], to, len);
|
|
745 |
to += len;
|
|
746 |
|
|
747 |
/* add( string[oldcode] + first(string[code]) ) */
|
|
748 |
len = stringLen[oldCode] + 1;
|
|
749 |
if (nScratch < len) {
|
|
750 |
newBuffer = (struct buffer *)malloc(sizeof(struct buffer));
|
|
751 |
newBuffer->prev = scratchBuffer;
|
|
752 |
scratchBuffer = newBuffer;
|
|
753 |
scratchPtr = scratchBuffer->chars;
|
|
754 |
nScratch = sizeof(scratchBuffer->chars);
|
|
755 |
}
|
|
756 |
stringLen[nextCode] = len;
|
|
757 |
strings[nextCode] = scratchPtr;
|
|
758 |
bcopy(strings[oldCode], scratchPtr, len-1);
|
|
759 |
scratchPtr += len-1;
|
|
760 |
*scratchPtr++ = strings[code][0];
|
|
761 |
nScratch -= len;
|
|
762 |
} else {
|
|
763 |
/* writeString(string[oldCode] + first(string[oldCode]) ) */
|
|
764 |
len = stringLen[oldCode];
|
|
765 |
bcopy(strings[oldCode], to, len);
|
|
766 |
to += len;
|
|
767 |
*to++ = strings[oldCode][0];
|
|
768 |
|
|
769 |
/* add( string[oldcode] + first(string[oldCode]) ) */
|
|
770 |
len++;
|
|
771 |
if (nScratch < len) {
|
|
772 |
newBuffer = (struct buffer *)malloc(sizeof(struct buffer));
|
|
773 |
newBuffer->prev = scratchBuffer;
|
|
774 |
scratchBuffer = newBuffer;
|
|
775 |
scratchPtr = scratchBuffer->chars;
|
|
776 |
nScratch = sizeof(scratchBuffer->chars);
|
|
777 |
}
|
|
778 |
stringLen[nextCode] = len;
|
|
779 |
strings[nextCode] = scratchPtr;
|
|
780 |
bcopy(strings[oldCode], scratchPtr, len-1);
|
|
781 |
scratchPtr += len-1;
|
|
782 |
*scratchPtr++ = strings[oldCode][0];
|
|
783 |
nScratch -= len;
|
|
784 |
}
|
|
785 |
oldCode = code;
|
|
786 |
nextCode++;
|
|
787 |
if (nextCode >= 511)
|
|
788 |
if (nextCode == 511) {
|
|
789 |
codeLen = 10;
|
|
790 |
mask = 0x3FF;
|
|
791 |
} else if (nextCode >= 1023)
|
|
792 |
if (nextCode == 1023) {
|
|
793 |
codeLen = 11;
|
|
794 |
mask = 0x7FF;
|
|
795 |
} else
|
|
796 |
if (nextCode == 2047) {
|
|
797 |
codeLen = 12;
|
|
798 |
mask = 0xFFF;
|
|
799 |
}
|
|
800 |
}
|
|
801 |
}
|
|
802 |
/* free stuff */
|
|
803 |
while (scratchBuffer) {
|
|
804 |
newBuffer = scratchBuffer;
|
|
805 |
scratchBuffer = scratchBuffer->prev;
|
|
806 |
free(newBuffer);
|
|
807 |
}
|
|
808 |
}
|
|
809 |
|
|
810 |
/*
|
|
811 |
* delta decoding (TIFF predictor = 2)
|
|
812 |
*/
|
|
813 |
decodeDelta(bytes, width, height)
|
|
814 |
register unsigned char *bytes;
|
|
815 |
{
|
|
816 |
register w;
|
|
817 |
unsigned char r, g, b;
|
|
818 |
|
|
819 |
while (height--) {
|
|
820 |
r = g = b = 0;
|
|
821 |
for (w = width; w; w--) {
|
|
822 |
r += *bytes;
|
|
823 |
*bytes++ = r;
|
|
824 |
g += *bytes;
|
|
825 |
*bytes++ = g;
|
|
826 |
b += *bytes;
|
|
827 |
*bytes++ = b;
|
|
828 |
}
|
|
829 |
}
|
|
830 |
}
|
|
831 |
|
|
832 |
decodeGIF(from, to, inCount, initialCodeLen)
|
|
833 |
unsigned char *from;
|
|
834 |
unsigned char *to;
|
|
835 |
{
|
|
836 |
register unsigned code;
|
|
837 |
unsigned short prefix[4096];
|
|
838 |
unsigned short suffix[4096];
|
|
839 |
unsigned short outCode[4096];
|
|
840 |
int outCount;
|
|
841 |
unsigned maxCode, oldCode, fin, inCode, curCode;
|
|
842 |
register unsigned bits;
|
|
843 |
register int nBits, mask, shift;
|
|
844 |
int i;
|
|
845 |
int len;
|
|
846 |
int endCode, clearCode, freeCode;
|
|
847 |
int codeLen = initialCodeLen;
|
|
848 |
static int ranges[] = {0, 1, 2, 4,
|
|
849 |
8, 16, 32, 64,
|
|
850 |
128, 256, 512, 1024,
|
|
851 |
2048 };
|
|
852 |
|
|
853 |
clearCode = ranges[codeLen]; /* 256 */
|
|
854 |
endCode = clearCode + 1; /* 257 */
|
|
855 |
freeCode = clearCode + 2; /* 258 */
|
|
856 |
maxCode = clearCode << 1; /* 512 */
|
|
857 |
outCount = 0;
|
|
858 |
|
|
859 |
mask = maxCode - 1; /* 1FF */
|
|
860 |
nBits = 0;
|
|
861 |
bits = 0;
|
|
862 |
while (inCount) {
|
|
863 |
/* fetch code */
|
|
864 |
while (nBits < codeLen) {
|
|
865 |
bits = bits | (*from++ << nBits);
|
|
866 |
inCount--;
|
|
867 |
nBits += 8;
|
|
868 |
}
|
|
869 |
code = bits & mask;
|
|
870 |
bits >>= codeLen;
|
|
871 |
nBits -= codeLen;
|
|
872 |
if (code == endCode) break;
|
|
873 |
if (code == clearCode) {
|
|
874 |
if (! inCount)
|
|
875 |
break;
|
|
876 |
|
|
877 |
codeLen = initialCodeLen;
|
|
878 |
maxCode = clearCode<<1;
|
|
879 |
mask = maxCode - 1;
|
|
880 |
freeCode = clearCode + 2;
|
|
881 |
|
|
882 |
/* fetch code */
|
|
883 |
while (nBits < codeLen) {
|
|
884 |
bits = bits | (*from++ << nBits);
|
|
885 |
inCount--;
|
|
886 |
nBits += 8;
|
|
887 |
}
|
|
888 |
code = bits & mask;
|
|
889 |
bits >>= codeLen;
|
|
890 |
nBits -= codeLen;
|
|
891 |
if (code == endCode) break;
|
|
892 |
/* add to output */
|
|
893 |
*to++ = code;
|
|
894 |
oldCode = fin = curCode = code;
|
|
895 |
} else {
|
|
896 |
curCode = inCode = code;
|
|
897 |
if (curCode >= freeCode) {
|
|
898 |
curCode = oldCode;
|
|
899 |
outCode[outCount++] = fin;
|
|
900 |
}
|
|
901 |
|
|
902 |
while (curCode >= clearCode) {
|
|
903 |
if (outCount > 1024) {
|
|
904 |
return;
|
|
905 |
}
|
|
906 |
outCode[outCount++] = suffix[curCode];
|
|
907 |
curCode = prefix[curCode];
|
|
908 |
}
|
|
909 |
|
|
910 |
fin = curCode;
|
|
911 |
outCode[outCount++] = fin;
|
|
912 |
|
|
913 |
for (i = outCount - 1; i >= 0; i--)
|
|
914 |
*to++ = outCode[i];
|
|
915 |
outCount = 0;
|
|
916 |
|
|
917 |
prefix[freeCode] = oldCode;
|
|
918 |
suffix[freeCode] = fin;
|
|
919 |
oldCode = inCode;
|
|
920 |
|
|
921 |
freeCode++;
|
|
922 |
if (freeCode >= maxCode) {
|
|
923 |
if (codeLen < 12) {
|
|
924 |
codeLen++;
|
|
925 |
maxCode *= 2;
|
|
926 |
mask = (1 << codeLen) - 1;
|
|
927 |
}
|
|
928 |
}
|
|
929 |
}
|
|
930 |
}
|
|
931 |
}
|
|
932 |
|
|
933 |
%}
|