author | Claus Gittinger <cg@exept.de> |
Thu, 18 Dec 1997 11:47:43 +0100 | |
changeset 749 | 49c372aaf613 |
parent 732 | db51a760126a |
child 755 | f1135dda53b1 |
permissions | -rw-r--r-- |
0 | 1 |
" |
6 | 2 |
COPYRIGHT (c) 1991 by Claus Gittinger |
26 | 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 |
ImageReader subclass:#GIFReader |
|
708 | 14 |
instanceVariableNames:'redMap greenMap blueMap pass xpos ypos rowByteSize remainBitCount |
15 |
bufByte bufStream prefixTable suffixTable clearCode eoiCode |
|
16 |
freeCode codeSize maxCode interlace' |
|
17 |
classVariableNames:'ImageSeparator Extension Terminator' |
|
154 | 18 |
poolDictionaries:'' |
259 | 19 |
category:'Graphics-Images-Support' |
0 | 20 |
! |
21 |
||
339 | 22 |
!GIFReader class methodsFor:'documentation'! |
0 | 23 |
|
21 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
26 | 27 |
All Rights Reserved |
21 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
0 | 38 |
documentation |
39 |
" |
|
40 |
this class provides methods for loading and saving GIF pictures. |
|
21 | 41 |
It has been tested with some different GIF87a pictures, I dont |
0 | 42 |
know, if it works with other GIF versions. |
43 |
GIF extension blocks are not handled. |
|
44 |
||
45 |
GIF file writing is not implemented (use TIFF). |
|
46 |
||
21 | 47 |
legal stuff extracted from GIF87a documentation: |
0 | 48 |
|
49 |
CompuServe Incorporated hereby grants a limited, non-exclusive, royalty-free |
|
50 |
license for the use of the Graphics Interchange Format(sm) in computer |
|
51 |
software; computer software utilizing GIF(sm) must acknowledge ownership of the |
|
52 |
Graphics Interchange Format and its Service Mark by CompuServe Incorporated, in |
|
53 |
User and Technical Documentation. |
|
54 |
||
55 |
The Graphics Interchange Format(c) is the Copyright property of |
|
56 |
CompuServe Incorporated. GIF(sm) is a Service Mark property of |
|
57 |
CompuServe Incorporated. |
|
194 | 58 |
|
59 |
[See also:] |
|
234 | 60 |
Image Form Icon |
194 | 61 |
BlitImageReader FaceReader JPEGReader PBMReader PCXReader |
210 | 62 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
209 | 63 |
XBMReader XPMReader XWDReader |
220 | 64 |
|
65 |
[author:] |
|
66 |
Claus Gittinger |
|
0 | 67 |
" |
68 |
! ! |
|
69 |
||
339 | 70 |
!GIFReader class methodsFor:'initialization'! |
26 | 71 |
|
72 |
initialize |
|
198 | 73 |
"install myself in the Image classes fileFormat table |
398 | 74 |
for the `.gif' extensions." |
198 | 75 |
|
708 | 76 |
ImageSeparator := $, asciiValue. |
77 |
Extension := $!! asciiValue. |
|
78 |
Terminator := $; asciiValue. |
|
79 |
||
647 | 80 |
MIMETypes defineImageType:'image/gif' suffix:'gif' reader:self. |
198 | 81 |
|
708 | 82 |
"Modified: 14.10.1997 / 18:47:27 / cg" |
26 | 83 |
! ! |
84 |
||
339 | 85 |
!GIFReader class methodsFor:'testing'! |
1 | 86 |
|
713 | 87 |
canRepresent:anImage |
88 |
"return true, if anImage can be represented in my file format. |
|
89 |
GIF supports depth 8 images only." |
|
90 |
||
91 |
^ anImage depth == 8 |
|
92 |
||
93 |
"Created: 17.10.1997 / 20:19:20 / cg" |
|
94 |
! |
|
95 |
||
1 | 96 |
isValidImageFile:aFileName |
97 |
"return true, if aFileName contains a GIF image" |
|
98 |
||
99 |
|id inStream| |
|
100 |
||
9 | 101 |
inStream := self streamReadingFile:aFileName. |
1 | 102 |
inStream isNil ifTrue:[^ false]. |
103 |
||
332 | 104 |
inStream text. |
105 |
||
1 | 106 |
id := String new:6. |
107 |
inStream nextBytes:6 into:id. |
|
108 |
inStream close. |
|
109 |
||
110 |
(id = 'GIF87a') ifFalse:[ |
|
174 | 111 |
(id startsWith:'GIF') ifFalse:[^ false]. |
1 | 112 |
|
174 | 113 |
id ~= 'GIF89a' ifTrue:[ |
355 | 114 |
'GIFReader [info]: not a GIF87a/GIF89a file - hope that works' infoPrintCR. |
174 | 115 |
] |
1 | 116 |
]. |
117 |
^ true |
|
174 | 118 |
|
355 | 119 |
"Modified: 10.1.1997 / 15:40:34 / cg" |
1 | 120 |
! ! |
121 |
||
708 | 122 |
!GIFReader methodsFor:'private - writing'! |
123 |
||
714 | 124 |
assignTransparentPixelIn:image |
125 |
"find an usused pixelValue in the colorMap (or image)." |
|
126 |
||
127 |
|cmap usedPixelValues| |
|
128 |
||
129 |
(cmap := image colorMap) size > 0 ifTrue:[ |
|
130 |
cmap size < 256 ifTrue:[ |
|
131 |
maskPixel := cmap size. |
|
132 |
^ self |
|
133 |
] |
|
134 |
]. |
|
135 |
||
136 |
usedPixelValues := image usedValues. |
|
137 |
usedPixelValues size < (1 bitShift:image depth) ifTrue:[ |
|
138 |
"/ there must be an unused pixelValue |
|
139 |
maskPixel := ((0 to:(1 bitShift:image depth)-1) asSet removeAll:(usedPixelValues)) first. |
|
140 |
] ifFalse:[ |
|
141 |
Image informationLostQuerySignal |
|
142 |
raiseWith:image |
|
143 |
errorString:('GIF writer cannot assign a transparent pixel - all pixelValues used by image'). |
|
144 |
] |
|
145 |
! |
|
146 |
||
708 | 147 |
checkCodeSize |
148 |
(freeCode > maxCode and: [codeSize < 12]) |
|
149 |
ifTrue: |
|
150 |
[codeSize := codeSize + 1. |
|
151 |
maxCode := (1 bitShift: codeSize) - 1] |
|
152 |
||
153 |
"Created: 14.10.1997 / 18:42:01 / cg" |
|
154 |
! |
|
155 |
||
156 |
flushBits |
|
157 |
remainBitCount = 0 |
|
158 |
ifFalse: |
|
159 |
[self nextBytePut: bufByte. |
|
160 |
remainBitCount := 0]. |
|
161 |
self flushBuffer |
|
162 |
||
163 |
"Modified: 14.10.1997 / 18:58:06 / cg" |
|
164 |
! |
|
165 |
||
166 |
flushBuffer |
|
167 |
bufStream isEmpty ifTrue: [^ self]. |
|
168 |
outStream nextPut: bufStream size. |
|
169 |
outStream nextPutAll: bufStream contents. |
|
170 |
bufStream := WriteStream on: (ByteArray new: 256) |
|
171 |
||
172 |
"Modified: 14.10.1997 / 20:46:04 / cg" |
|
173 |
! |
|
174 |
||
175 |
flushCode |
|
176 |
self flushBits |
|
177 |
||
178 |
"Created: 14.10.1997 / 18:57:33 / cg" |
|
179 |
! |
|
180 |
||
710 | 181 |
nextBitsPut: anInteger |
182 |
| integer writeBitCount shiftCount | |
|
183 |
shiftCount _ 0. |
|
184 |
remainBitCount = 0 |
|
185 |
ifTrue: |
|
186 |
[writeBitCount _ 8. |
|
187 |
integer _ anInteger] |
|
188 |
ifFalse: |
|
189 |
[writeBitCount _ remainBitCount. |
|
190 |
integer _ bufByte + (anInteger bitShift: 8 - remainBitCount)]. |
|
191 |
[writeBitCount < codeSize] |
|
192 |
whileTrue: |
|
193 |
[self nextBytePut: ((integer bitShift: shiftCount) bitAnd: 255). |
|
194 |
shiftCount _ shiftCount - 8. |
|
195 |
writeBitCount _ writeBitCount + 8]. |
|
196 |
(remainBitCount _ writeBitCount - codeSize) = 0 |
|
197 |
ifTrue: [self nextBytePut: (integer bitShift: shiftCount)] |
|
198 |
ifFalse: [bufByte _ integer bitShift: shiftCount]. |
|
199 |
^anInteger |
|
708 | 200 |
|
710 | 201 |
"Modified: 15.10.1997 / 16:50:30 / cg" |
708 | 202 |
! |
203 |
||
710 | 204 |
nextBytePut: aByte |
205 |
bufStream nextPut: aByte. |
|
206 |
bufStream size >= 254 ifTrue: [self flushBuffer] |
|
708 | 207 |
|
710 | 208 |
"Modified: 15.10.1997 / 16:50:52 / cg" |
708 | 209 |
! |
210 |
||
710 | 211 |
readPixelFrom: bits |
212 |
| pixel | |
|
708 | 213 |
ypos >= height ifTrue: [^ nil]. |
714 | 214 |
(maskPixel notNil |
215 |
and:[(mask pixelAtX:xpos y:ypos) == 0]) ifTrue:[ |
|
216 |
pixel := maskPixel |
|
217 |
] ifFalse:[ |
|
218 |
pixel := bits at: ypos * rowByteSize + xpos + 1. |
|
219 |
]. |
|
708 | 220 |
self updatePixelPosition. |
710 | 221 |
^ pixel |
708 | 222 |
|
223 |
"Created: 14.10.1997 / 18:43:50 / cg" |
|
710 | 224 |
"Modified: 15.10.1997 / 16:46:43 / cg" |
708 | 225 |
! |
226 |
||
227 |
setParameters:bitsPerPixel |
|
228 |
clearCode := 1 bitShift:bitsPerPixel. |
|
229 |
eoiCode := clearCode + 1. |
|
230 |
freeCode := clearCode + 2. |
|
231 |
codeSize := bitsPerPixel + 1. |
|
232 |
maxCode := (1 bitShift: codeSize) - 1 |
|
233 |
||
234 |
"Modified: 14.10.1997 / 20:09:48 / cg" |
|
235 |
! |
|
236 |
||
237 |
updatePixelPosition |
|
238 |
(xpos _ xpos + 1) >= width ifFalse: [^ self]. |
|
239 |
xpos _ 0. |
|
240 |
interlace == true |
|
241 |
ifFalse: |
|
242 |
[ypos _ ypos + 1. |
|
243 |
^ self]. |
|
244 |
pass = 0 |
|
245 |
ifTrue: |
|
246 |
[(ypos _ ypos + 8) >= height |
|
247 |
ifTrue: |
|
248 |
[pass _ pass + 1. |
|
249 |
ypos _ 4]. |
|
250 |
^ self]. |
|
251 |
pass = 1 |
|
252 |
ifTrue: |
|
253 |
[(ypos _ ypos + 8) >= height |
|
254 |
ifTrue: |
|
255 |
[pass _ pass + 1. |
|
256 |
ypos _ 2]. |
|
257 |
^ self]. |
|
258 |
pass = 2 |
|
259 |
ifTrue: |
|
260 |
[(ypos _ ypos + 4) >= height |
|
261 |
ifTrue: |
|
262 |
[pass _ pass + 1. |
|
263 |
ypos _ 1]. |
|
264 |
^ self]. |
|
265 |
pass = 3 |
|
266 |
ifTrue: |
|
267 |
[ypos _ ypos + 2. |
|
268 |
^ self]. |
|
269 |
^ self error: 'can''t happen' |
|
270 |
||
271 |
"Modified: 14.10.1997 / 18:44:27 / cg" |
|
272 |
! |
|
273 |
||
710 | 274 |
writeCode: aCode |
275 |
self nextBitsPut: aCode |
|
708 | 276 |
|
277 |
"Created: 14.10.1997 / 18:38:35 / cg" |
|
710 | 278 |
"Modified: 15.10.1997 / 17:01:47 / cg" |
708 | 279 |
! |
280 |
||
281 |
writeCodeAndCheckCodeSize: t1 |
|
282 |
self writeCode: t1. |
|
283 |
self checkCodeSize |
|
284 |
||
285 |
"Created: 14.10.1997 / 18:38:24 / cg" |
|
286 |
"Modified: 14.10.1997 / 18:40:56 / cg" |
|
287 |
! ! |
|
288 |
||
0 | 289 |
!GIFReader methodsFor:'reading from file'! |
290 |
||
1 | 291 |
checkGreyscaleColormap |
239 | 292 |
"return true, if colormap is actually a greymap. |
293 |
Could be used to convert it into a greyScale image - which is not yet done." |
|
1 | 294 |
|
20 | 295 |
|sz "{ Class: SmallInteger }" |
296 |
redVal| |
|
297 |
||
298 |
sz := redMap size. |
|
299 |
||
300 |
1 to:sz do:[:i | |
|
239 | 301 |
redVal := redMap at:i. |
302 |
redVal ~~ (greenMap at:i) ifTrue:[^ false]. |
|
303 |
redVal ~~ (blueMap at:i) ifTrue:[^ false]. |
|
1 | 304 |
]. |
305 |
^ true |
|
239 | 306 |
|
307 |
"Modified: 2.5.1996 / 17:54:40 / cg" |
|
1 | 308 |
! |
309 |
||
43 | 310 |
fromStream:aStream |
192 | 311 |
"read a stream containing a GIF image. |
312 |
Leave image description in instance variables." |
|
43 | 313 |
|
708 | 314 |
|byte index flag count fileColorMap |
43 | 315 |
colorMapSize bitsPerPixel scrWidth scrHeight |
316 |
hasColorMap hasLocalColorMap interlaced id |
|
317 |
leftOffs topOffs codeLen |
|
318 |
compressedData compressedSize |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
319 |
tmp srcOffset dstOffset isGif89 atEnd |
64 | 320 |
h "{ Class: SmallInteger }"| |
43 | 321 |
|
322 |
inStream := aStream. |
|
323 |
aStream binary. |
|
324 |
||
325 |
"GIF-files are always lsb (intel-world)" |
|
326 |
byteOrder := #lsb. |
|
327 |
||
331
304f9c439efa
allow reading from ByteArray-readStreams
Claus Gittinger <cg@exept.de>
parents:
315
diff
changeset
|
328 |
id := ByteArray new:6. |
304f9c439efa
allow reading from ByteArray-readStreams
Claus Gittinger <cg@exept.de>
parents:
315
diff
changeset
|
329 |
aStream nextBytes:6 into:id startingAt:1. |
304f9c439efa
allow reading from ByteArray-readStreams
Claus Gittinger <cg@exept.de>
parents:
315
diff
changeset
|
330 |
id := id asString. |
43 | 331 |
|
332 |
"all I had for testing where GIF87a files; |
|
333 |
I hope later versions work too ..." |
|
334 |
||
154 | 335 |
isGif89 := false. |
43 | 336 |
(id ~= 'GIF87a') ifTrue:[ |
154 | 337 |
(id startsWith:'GIF') ifFalse:[ |
355 | 338 |
'GIFReader [info]: not a gif file' infoPrintCR. |
154 | 339 |
^ nil |
340 |
]. |
|
341 |
id ~= 'GIF89a' ifTrue:[ |
|
355 | 342 |
'GIFReader [info]: not a GIF87a/GIF89a file - hope that works' infoPrintCR. |
154 | 343 |
] |
43 | 344 |
]. |
345 |
||
346 |
"get screen dimensions (not used)" |
|
347 |
scrWidth := aStream nextShortMSB:false. |
|
348 |
scrHeight := aStream nextShortMSB:false. |
|
349 |
||
350 |
"get flag byte" |
|
351 |
flag := aStream nextByte. |
|
352 |
hasColorMap := (flag bitAnd:2r10000000) ~~ 0. |
|
353 |
"bitsPerRGB := ((flag bitAnd:2r01110000) bitShift:-4) + 1. " |
|
354 |
"colorMapSorted := ((flag bitAnd:2r00001000) ~~ 0. " |
|
355 |
bitsPerPixel := (flag bitAnd:2r00000111) + 1. |
|
356 |
colorMapSize := 1 bitShift:bitsPerPixel. |
|
357 |
||
358 |
"get background (not used)" |
|
359 |
aStream nextByte. |
|
360 |
||
361 |
"aspect ratio (not used)" |
|
362 |
aStream nextByte. |
|
363 |
||
364 |
"get colorMap" |
|
365 |
hasColorMap ifTrue:[ |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
366 |
self readColorMap:colorMapSize. |
708 | 367 |
fileColorMap := Colormap |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
368 |
redVector:redMap |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
369 |
greenVector:greenMap |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
370 |
blueVector:blueMap. |
43 | 371 |
]. |
708 | 372 |
colorMap := fileColorMap. |
43 | 373 |
|
374 |
photometric := #palette. |
|
375 |
samplesPerPixel := 1. |
|
376 |
bitsPerSample := #(8). |
|
377 |
||
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
378 |
atEnd := false. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
379 |
[atEnd] whileFalse:[ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
380 |
"gif89a extensions" |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
381 |
byte := aStream nextByte. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
382 |
|
708 | 383 |
byte == Extension ifTrue:[ |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
384 |
self readExtension:aStream. |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
385 |
] ifFalse:[ |
708 | 386 |
(byte == Terminator) ifTrue:[ |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
387 |
atEnd := true |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
388 |
] ifFalse:[ |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
389 |
"must be image separator" |
708 | 390 |
(byte ~~ ImageSeparator) ifTrue:[ |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
391 |
('GIFReader [info]: corrupted gif file (no IMAGESEP): ' , (byte printStringRadix:16)) infoPrintCR. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
392 |
^ nil |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
393 |
]. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
394 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
395 |
"get image data" |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
396 |
leftOffs := aStream nextShortMSB:false. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
397 |
topOffs := aStream nextShortMSB:false. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
398 |
width := aStream nextShortMSB:false. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
399 |
height := aStream nextShortMSB:false. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
400 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
401 |
dimensionCallBack notNil ifTrue:[ |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
402 |
dimensionCallBack value:self |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
403 |
]. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
404 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
405 |
"/ |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
406 |
"/ 'width ' print. width printNewline. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
407 |
"/ 'height ' print. height printNewline. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
408 |
"/ |
43 | 409 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
410 |
"another flag byte" |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
411 |
flag := aStream nextByte. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
412 |
interlaced := (flag bitAnd:2r01000000) ~~ 0. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
413 |
hasLocalColorMap := (flag bitAnd:2r10000000) ~~ 0. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
414 |
"localColorMapSorted := (flag bitAnd:2r00100000) ~~ 0. " |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
415 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
416 |
"if image has a local colormap, this one is used" |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
417 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
418 |
hasLocalColorMap ifTrue:[ |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
419 |
"local descr. overwrites" |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
420 |
bitsPerPixel := (flag bitAnd:2r00000111) + 1. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
421 |
colorMapSize := 1 bitShift:bitsPerPixel. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
422 |
"overwrite colormap" |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
423 |
self readColorMap:colorMapSize. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
424 |
colorMap := Colormap |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
425 |
redVector:redMap |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
426 |
greenVector:greenMap |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
427 |
blueVector:blueMap. |
708 | 428 |
] ifFalse:[ |
429 |
colorMap := fileColorMap |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
430 |
]. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
431 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
432 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
433 |
"get codelen for decompression" |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
434 |
codeLen := aStream nextByte. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
435 |
|
691 | 436 |
compressedData := ByteArray uninitializedNew:(aStream fileSize). |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
437 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
438 |
"get compressed data" |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
439 |
index := 1. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
440 |
count := aStream nextByte. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
441 |
[count notNil and:[count ~~ 0]] whileTrue:[ |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
442 |
aStream nextBytes:count into:compressedData startingAt:index blockSize:4096. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
443 |
index := index + count. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
444 |
count := aStream nextByte |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
445 |
]. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
446 |
compressedSize := index - 1. |
83 | 447 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
448 |
h := height. |
749 | 449 |
data := ByteArray uninitializedNew:((width + 1) * (h + 1)). |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
450 |
"/ 'GIFReader: decompressing ...' infoPrintCR. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
451 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
452 |
self class decompressGIFFrom:compressedData |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
453 |
count:compressedSize |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
454 |
into:data |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
455 |
startingAt:1 |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
456 |
codeLen:(codeLen + 1). |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
457 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
458 |
interlaced ifTrue:[ |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
459 |
"/ 'GIFREADER: deinterlacing ...' infoPrintCR. |
749 | 460 |
tmp := ByteArray uninitializedNew:(data size). |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
461 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
462 |
"phase 1: 0, 8, 16, 24, ..." |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
463 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
464 |
srcOffset := 1. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
465 |
0 to:(h - 1) by:8 do:[:dstRow | |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
466 |
dstOffset := dstRow * width + 1. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
467 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
468 |
with:data startingAt:srcOffset. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
469 |
srcOffset := srcOffset + width. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
470 |
]. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
471 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
472 |
"phase 2: 4, 12, 20, 28, ..." |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
473 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
474 |
4 to:(h - 1) by:8 do:[:dstRow | |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
475 |
dstOffset := dstRow * width + 1. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
476 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
477 |
with:data startingAt:srcOffset. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
478 |
srcOffset := srcOffset + width. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
479 |
]. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
480 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
481 |
"phase 3: 2, 6, 10, 14, ..." |
43 | 482 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
483 |
2 to:(h - 1) by:4 do:[:dstRow | |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
484 |
dstOffset := dstRow * width + 1. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
485 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
486 |
with:data startingAt:srcOffset. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
487 |
srcOffset := srcOffset + width. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
488 |
]. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
489 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
490 |
"phase 4: 1, 3, 5, 7, ..." |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
491 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
492 |
1 to:(h - 1) by:2 do:[:dstRow | |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
493 |
dstOffset := dstRow * width + 1. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
494 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
495 |
with:data startingAt:srcOffset. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
496 |
srcOffset := srcOffset + width. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
497 |
]. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
498 |
|
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
499 |
data := tmp. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
500 |
tmp := nil. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
501 |
]. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
502 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
503 |
imageSequence isNil ifTrue:[ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
504 |
imageSequence := OrderedCollection new. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
505 |
]. |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
506 |
maskPixel notNil ifTrue:[ |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
507 |
"/ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
508 |
"/ ok, there is a maskValue |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
509 |
"/ build a Depth1Image for it. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
510 |
"/ |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
511 |
self buildMaskFromColor:maskPixel |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
512 |
]. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
513 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
514 |
imageSequence add:(self image). |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
515 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
516 |
aStream atEnd ifTrue:[ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
517 |
atEnd := true. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
518 |
] |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
519 |
] |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
520 |
]. |
307
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
521 |
]. |
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
522 |
|
43 | 523 |
" |
524 |
GIFReader fromFile:'../fileIn/bitmaps/claus.gif |
|
525 |
GIFReader fromFile:'../fileIn/bitmaps/garfield.gif' |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
526 |
GIFReader new fromStream:('/home2/cg/.misc/circum.gif' asFilename readStream) |
43 | 527 |
" |
154 | 528 |
|
749 | 529 |
"Modified: / 5.7.1996 / 17:32:01 / stefan" |
530 |
"Modified: / 18.12.1997 / 10:23:38 / cg" |
|
135 | 531 |
! |
532 |
||
533 |
makeGreyscale |
|
534 |
"not yet implemented/needed" |
|
535 |
! |
|
536 |
||
537 |
readColorMap:colorMapSize |
|
538 |
"get gif colormap consisting of colorMapSize entries" |
|
539 |
||
540 |
|sz "{ Class: SmallInteger }"| |
|
541 |
||
542 |
redMap := ByteArray uninitializedNew:colorMapSize. |
|
543 |
greenMap := ByteArray uninitializedNew:colorMapSize. |
|
544 |
blueMap := ByteArray uninitializedNew:colorMapSize. |
|
239 | 545 |
|
135 | 546 |
sz := colorMapSize. |
547 |
1 to:sz do:[:i | |
|
239 | 548 |
redMap at:i put:(inStream nextByte). |
549 |
greenMap at:i put:(inStream nextByte). |
|
550 |
blueMap at:i put:(inStream nextByte) |
|
307
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
551 |
]. |
239 | 552 |
|
307
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
553 |
"Modified: 21.6.1996 / 12:32:43 / cg" |
154 | 554 |
! |
555 |
||
556 |
readExtension:aStream |
|
239 | 557 |
"get gif89 extension - this is currently ignored" |
154 | 558 |
|
559 |
|type blockSize subBlockSize |
|
304 | 560 |
aspNum aspDen left top width height cWidth cHeight fg bg |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
561 |
animationType animationTime animationMask |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
562 |
appID appAUTH |
625 | 563 |
iterationCount b ok| |
154 | 564 |
|
565 |
type := aStream nextByte. |
|
566 |
type == $R asciiValue ifTrue:[ |
|
239 | 567 |
"/ |
154 | 568 |
"/ Ratio extension |
239 | 569 |
"/ |
354
b4d2ce853c24
stefans suggested info/warning/error/fatal
Claus Gittinger <cg@exept.de>
parents:
339
diff
changeset
|
570 |
'GIFREADER [info]: ratio extension ignored' infoPrintCR. |
154 | 571 |
blockSize := aStream nextByte. |
572 |
(blockSize == 2) ifTrue:[ |
|
573 |
aspNum := aStream nextByte. |
|
574 |
aspDen := aStream nextByte |
|
575 |
] ifFalse:[ |
|
576 |
aStream skip:blockSize |
|
577 |
]. |
|
578 |
"/ eat subblocks |
|
579 |
||
580 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
|
581 |
aStream skip:subBlockSize |
|
239 | 582 |
]. |
583 |
^ self |
|
584 |
]. |
|
585 |
||
586 |
type == 16r01 ifTrue:[ |
|
587 |
"/ |
|
588 |
"/ plaintext extension |
|
589 |
"/ |
|
625 | 590 |
"/ 'GIFREADER [info]: plaintext extension ignored' infoPrintCR. |
239 | 591 |
subBlockSize := aStream nextByte. |
592 |
left := aStream nextShortMSB:false. |
|
593 |
top := aStream nextShortMSB:false. |
|
594 |
width := aStream nextShortMSB:false. |
|
595 |
height := aStream nextShortMSB:false. |
|
596 |
cWidth := aStream nextByte. |
|
597 |
cHeight := aStream nextByte. |
|
598 |
fg := aStream nextByte. |
|
599 |
bg := aStream nextByte. |
|
600 |
aStream skip:12. |
|
601 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
|
602 |
aStream skip:subBlockSize |
|
603 |
]. |
|
604 |
^ self |
|
605 |
]. |
|
606 |
||
607 |
type == 16rF9 ifTrue:[ |
|
608 |
"/ |
|
609 |
"/ graphic control extension |
|
610 |
"/ |
|
354
b4d2ce853c24
stefans suggested info/warning/error/fatal
Claus Gittinger <cg@exept.de>
parents:
339
diff
changeset
|
611 |
"/ 'GIFREADER [info]: graphic control extension ignored' infoPrintCR. |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
612 |
|
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
613 |
[(subBlockSize := aStream nextByte) ~~ 0] whileTrue:[ |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
614 |
"/ type bitAnd:1 means: animationMask is transparent pixel |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
615 |
"/ to be implemented in Image ... |
305 | 616 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
617 |
animationType := aStream nextByte. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
618 |
animationTime := aStream nextShortMSB:false. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
619 |
animationMask := aStream nextByte. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
620 |
subBlockSize := subBlockSize - 4. |
307
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
621 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
622 |
(animationType bitTest: 1) ifTrue:[ |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
623 |
maskPixel := animationMask. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
624 |
"/ 'GIFREADER [info]: mask: ' infoPrint. (maskPixel printStringRadix:16) infoPrintCR. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
625 |
]. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
626 |
"/ 'GIFREADER [info]: animationTime: ' infoPrint. (animationTime * (1/100)) infoPrintCR. |
307
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
627 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
628 |
subBlockSize ~~ 0 ifTrue:[ |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
629 |
aStream skip:subBlockSize |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
630 |
]. |
239 | 631 |
]. |
632 |
^ self |
|
633 |
]. |
|
634 |
||
519 | 635 |
type == 16rFE ifTrue:[ |
636 |
"/ |
|
637 |
"/ comment extension |
|
638 |
"/ |
|
625 | 639 |
"/ 'GIFREADER [info]: comment extension ignored' infoPrintCR. |
519 | 640 |
[(blockSize := aStream nextByte) ~~ 0] whileTrue:[ |
641 |
aStream skip:blockSize |
|
642 |
]. |
|
643 |
^ self |
|
644 |
]. |
|
645 |
||
239 | 646 |
type == 16rFF ifTrue:[ |
647 |
"/ |
|
648 |
"/ application extension |
|
649 |
"/ |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
650 |
subBlockSize := aStream nextByte. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
651 |
appID := (aStream nextBytes:8 ) asString. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
652 |
appAUTH := aStream nextBytes:3. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
653 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
654 |
subBlockSize := aStream nextByte. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
655 |
|
625 | 656 |
ok := false. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
657 |
appID = 'NETSCAPE' ifTrue:[ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
658 |
appAUTH asString = '2.0' ifTrue:[ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
659 |
subBlockSize == 3 ifTrue:[ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
660 |
b := aStream nextByte. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
661 |
iterationCount := aStream nextShortMSB:false. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
662 |
subBlockSize := aStream nextByte. |
625 | 663 |
ok := true. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
664 |
] |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
665 |
] |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
666 |
]. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
667 |
|
625 | 668 |
ok ifFalse:[ |
669 |
('GIFREADER [info]: application extension (' , appID , ') ignored') infoPrintCR. |
|
670 |
]. |
|
671 |
||
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
672 |
[subBlockSize > 0] whileTrue:[ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
673 |
aStream skip:subBlockSize. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
674 |
subBlockSize := aStream nextByte. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
675 |
]. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
676 |
^ self |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
677 |
]. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
678 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
679 |
type == 16r2C ifTrue:[ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
680 |
"/ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
681 |
"/ image descriptor extension |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
682 |
"/ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
683 |
'GIFREADER [info]: image descriptor extension ignored' infoPrintCR. |
239 | 684 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
685 |
aStream skip:subBlockSize |
|
686 |
]. |
|
687 |
^ self |
|
688 |
]. |
|
689 |
||
690 |
"/ |
|
691 |
"/ unknown extension |
|
692 |
"/ |
|
354
b4d2ce853c24
stefans suggested info/warning/error/fatal
Claus Gittinger <cg@exept.de>
parents:
339
diff
changeset
|
693 |
'GIFREADER [info]: unknown extension ignored' infoPrintCR. |
239 | 694 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
695 |
aStream skip:subBlockSize |
|
154 | 696 |
] |
697 |
||
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
698 |
"Modified: 24.7.1997 / 18:02:49 / cg" |
0 | 699 |
! ! |
43 | 700 |
|
708 | 701 |
!GIFReader methodsFor:'writing to file'! |
702 |
||
703 |
save:image onFile:aFileName |
|
704 |
"save image as GIF file on aFileName" |
|
705 |
||
718
41ade132da98
raise an error, if depth is not supported.
Claus Gittinger <cg@exept.de>
parents:
714
diff
changeset
|
706 |
image depth ~~ 8 ifTrue:[ |
41ade132da98
raise an error, if depth is not supported.
Claus Gittinger <cg@exept.de>
parents:
714
diff
changeset
|
707 |
^ Image cannotRepresentImageSignal |
41ade132da98
raise an error, if depth is not supported.
Claus Gittinger <cg@exept.de>
parents:
714
diff
changeset
|
708 |
raiseWith:image |
41ade132da98
raise an error, if depth is not supported.
Claus Gittinger <cg@exept.de>
parents:
714
diff
changeset
|
709 |
errorString:('GIF (currently) only supports depth8 images'). |
41ade132da98
raise an error, if depth is not supported.
Claus Gittinger <cg@exept.de>
parents:
714
diff
changeset
|
710 |
]. |
41ade132da98
raise an error, if depth is not supported.
Claus Gittinger <cg@exept.de>
parents:
714
diff
changeset
|
711 |
|
708 | 712 |
outStream := FileStream newFileNamed:aFileName. |
713 |
outStream isNil ifTrue:[ |
|
714 |
^ Image fileCreationErrorSignal |
|
715 |
raiseWith:image |
|
716 |
errorString:('file creation error: ' , aFileName asString). |
|
717 |
]. |
|
718 |
outStream binary. |
|
719 |
||
714 | 720 |
mask := image mask. |
721 |
mask notNil ifTrue:[ |
|
722 |
self assignTransparentPixelIn:image |
|
708 | 723 |
]. |
724 |
||
725 |
byteOrder := #lsb. |
|
726 |
width := image width. |
|
727 |
height := image height. |
|
728 |
photometric := image photometric. |
|
729 |
samplesPerPixel := image samplesPerPixel. |
|
730 |
bitsPerSample := image bitsPerSample. |
|
731 |
colorMap := image colorMap. |
|
732 |
data := image bits. |
|
733 |
||
734 |
self writeHeaderFor:image. |
|
714 | 735 |
maskPixel notNil ifTrue:[ |
736 |
self writeMaskExtensionHeaderFor:image. |
|
737 |
]. |
|
738 |
||
708 | 739 |
self writeBitDataFor:image. |
714 | 740 |
|
741 |
outStream nextPut: Terminator. |
|
708 | 742 |
outStream close. |
743 |
||
744 |
" |
|
745 |
|i| |
|
746 |
||
747 |
i := Image fromFile:'bitmaps/gifImages/garfield.gif'. |
|
748 |
GIFReader save:i onFile:'foo.gif'. |
|
749 |
(Image fromFile:'./foo.gif') inspect |
|
750 |
" |
|
751 |
||
718
41ade132da98
raise an error, if depth is not supported.
Claus Gittinger <cg@exept.de>
parents:
714
diff
changeset
|
752 |
"Created: / 14.10.1997 / 17:40:12 / cg" |
41ade132da98
raise an error, if depth is not supported.
Claus Gittinger <cg@exept.de>
parents:
714
diff
changeset
|
753 |
"Modified: / 27.10.1997 / 22:42:31 / cg" |
708 | 754 |
! |
755 |
||
710 | 756 |
writeBitDataFor: image |
757 |
"using modified Lempel-Ziv Welch algorithm." |
|
758 |
||
714 | 759 |
| bits bitsPerPixel t1 |
710 | 760 |
maxBits maxMaxCode tSize initCodeSize ent tShift fCode pixel index disp nomatch | |
761 |
||
714 | 762 |
outStream nextPut:ImageSeparator. |
763 |
self writeShort:0. "/ |
|
764 |
self writeShort:0. "/ |
|
765 |
self writeShort:width. "/ image size |
|
766 |
self writeShort:height. |
|
767 |
||
768 |
interlace == true ifTrue:[ |
|
769 |
t1 := 64 |
|
770 |
] ifFalse:[ |
|
771 |
t1 := 0 |
|
772 |
]. |
|
773 |
outStream nextPut:t1. "/ another flag |
|
774 |
||
710 | 775 |
bitsPerPixel := image bitsPerPixel. |
776 |
bits := image bits. |
|
777 |
||
778 |
pass := 0. |
|
779 |
xpos := 0. |
|
780 |
ypos := 0. |
|
712 | 781 |
rowByteSize := image bytesPerRow. "/ width * 8 + 31 // 32 * 4. |
710 | 782 |
remainBitCount := 0. |
783 |
bufByte := 0. |
|
784 |
bufStream := WriteStream on: (ByteArray new: 256). |
|
708 | 785 |
|
710 | 786 |
maxBits := 12. |
787 |
maxMaxCode := 1 bitShift: maxBits. |
|
788 |
tSize := 5003. |
|
789 |
prefixTable := Array new: tSize. |
|
790 |
suffixTable := Array new: tSize. |
|
791 |
||
792 |
initCodeSize := bitsPerPixel <= 1 ifTrue: [2] ifFalse: [bitsPerPixel]. |
|
793 |
outStream nextPut: initCodeSize. |
|
794 |
self setParameters: initCodeSize. |
|
795 |
||
796 |
tShift := 0. |
|
797 |
fCode := tSize. |
|
798 |
[fCode < 65536] whileTrue: |
|
799 |
[tShift := tShift + 1. |
|
800 |
fCode := fCode * 2]. |
|
801 |
tShift := 8 - tShift. |
|
802 |
1 to: tSize do: [:i | suffixTable at: i put: -1]. |
|
708 | 803 |
|
710 | 804 |
self writeCodeAndCheckCodeSize: clearCode. |
805 |
ent := self readPixelFrom: bits. |
|
806 |
[(pixel := self readPixelFrom: bits) == nil] whileFalse: |
|
708 | 807 |
[ |
710 | 808 |
fCode := (pixel bitShift: maxBits) + ent. |
809 |
index := ((pixel bitShift: tShift) bitXor: ent) + 1. |
|
810 |
(suffixTable at: index) = fCode |
|
811 |
ifTrue: [ent := prefixTable at: index] |
|
812 |
ifFalse: |
|
813 |
[nomatch := true. |
|
814 |
(suffixTable at: index) >= 0 |
|
815 |
ifTrue: |
|
816 |
[disp := tSize - index + 1. |
|
817 |
index = 1 ifTrue: [disp := 1]. |
|
818 |
"probe" |
|
819 |
[(index := index - disp) < 1 ifTrue: [index := index + tSize]. |
|
820 |
(suffixTable at: index) = fCode |
|
821 |
ifTrue: |
|
822 |
[ent := prefixTable at: index. |
|
823 |
nomatch := false. |
|
824 |
"continue whileFalse:"]. |
|
825 |
nomatch and: [(suffixTable at: index) > 0]] |
|
826 |
whileTrue: ["probe"]]. |
|
827 |
"nomatch" |
|
828 |
nomatch ifTrue: |
|
829 |
[self writeCodeAndCheckCodeSize: ent. |
|
830 |
ent := pixel. |
|
831 |
freeCode < maxMaxCode |
|
832 |
ifTrue: |
|
833 |
[prefixTable at: index put: freeCode. |
|
834 |
suffixTable at: index put: fCode. |
|
835 |
freeCode := freeCode + 1] |
|
836 |
ifFalse: |
|
837 |
[self writeCodeAndCheckCodeSize: clearCode. |
|
838 |
1 to: tSize do: [:i | suffixTable at: i put: -1]. |
|
839 |
self setParameters: initCodeSize]]]]. |
|
840 |
prefixTable := suffixTable := nil. |
|
841 |
self writeCodeAndCheckCodeSize: ent. |
|
842 |
self writeCodeAndCheckCodeSize: eoiCode. |
|
843 |
self flushCode. |
|
844 |
outStream nextPut: 0. "zero-length packet" |
|
845 |
||
712 | 846 |
"Modified: 15.10.1997 / 19:56:28 / cg" |
708 | 847 |
! |
848 |
||
849 |
writeHeaderFor:image |
|
714 | 850 |
"write the gif header" |
708 | 851 |
|
852 |
|bitsPerPixel t1 n| |
|
853 |
||
854 |
bitsPerPixel := image bitsPerPixel. |
|
855 |
||
732 | 856 |
outStream nextPutAll: 'GIF89a' asByteArray. |
712 | 857 |
self writeShort:width. "/ screen size |
858 |
self writeShort:height. |
|
708 | 859 |
t1 := 128. |
712 | 860 |
t1 := t1 bitOr:(bitsPerPixel - 1 bitShift:5). |
861 |
t1 := t1 bitOr:(bitsPerPixel - 1). |
|
862 |
outStream nextPut:t1. "/ flag |
|
863 |
outStream nextPut:0. "/ background (not used) |
|
864 |
outStream nextPut:0. "/ aspect ratio |
|
708 | 865 |
|
713 | 866 |
0 to:(1 bitShift:bitsPerPixel)-1 do:[:pixel | |
867 |
|clr red green blue| |
|
712 | 868 |
|
713 | 869 |
clr := image colorFromValue:pixel. |
870 |
clr isNil ifTrue:[ |
|
871 |
"/ unused colorMap slot |
|
872 |
red := green := blue := 0. |
|
873 |
] ifFalse:[ |
|
874 |
red := (clr redByte). |
|
875 |
green := (clr greenByte). |
|
876 |
blue := (clr blueByte). |
|
877 |
]. |
|
878 |
outStream |
|
879 |
nextPut:red; nextPut:green; nextPut:blue. |
|
880 |
]. |
|
881 |
"/ n := 0. |
|
882 |
"/ image colorMap notNil ifTrue:[ |
|
883 |
"/ image colorMap do:[:clr | |
|
884 |
"/ |red green blue| |
|
885 |
"/ |
|
886 |
"/ clr isNil ifTrue:[ |
|
887 |
"/ "/ unused colorMap slot |
|
888 |
"/ red := green := blue := 0. |
|
889 |
"/ ] ifFalse:[ |
|
890 |
"/ red := (clr redByte). |
|
891 |
"/ green := (clr greenByte). |
|
892 |
"/ blue := (clr blueByte). |
|
893 |
"/ ]. |
|
894 |
"/ outStream |
|
895 |
"/ nextPut:red; nextPut:green; nextPut:blue. |
|
896 |
"/ n := n + 1. |
|
897 |
"/ ] |
|
898 |
"/ ]. |
|
899 |
"/ n+1 to:(1 bitShift:bitsPerPixel) do:[:i | |
|
900 |
"/ outStream nextPut:0; nextPut:0; nextPut:0 |
|
901 |
"/ ]. |
|
708 | 902 |
|
732 | 903 |
"Created: / 14.10.1997 / 17:41:28 / cg" |
904 |
"Modified: / 31.10.1997 / 16:12:13 / cg" |
|
714 | 905 |
! |
906 |
||
907 |
writeMaskExtensionHeaderFor:image |
|
908 |
"write an extension header for the transparent pixel" |
|
909 |
||
910 |
outStream nextPut:Extension. |
|
911 |
outStream nextPut:16rF9. "/ graphic control extension |
|
912 |
outStream nextPut:4. "/ subBlockSize |
|
913 |
||
914 |
outStream nextPut:1. "/ animationType |
|
915 |
outStream nextPutShort:1 MSB:false. "/ animationTime |
|
916 |
outStream nextPut:maskPixel. "/ animationMask |
|
917 |
||
918 |
outStream nextPut:0. |
|
708 | 919 |
! ! |
920 |
||
339 | 921 |
!GIFReader class methodsFor:'documentation'! |
135 | 922 |
|
923 |
version |
|
749 | 924 |
^ '$Header: /cvs/stx/stx/libview2/GIFReader.st,v 1.64 1997-12-18 10:47:43 cg Exp $' |
135 | 925 |
! ! |
43 | 926 |
GIFReader initialize! |