author | Stefan Vogel <sv@exept.de> |
Thu, 16 May 2019 19:32:28 +0200 | |
changeset 4266 | 12c13fe33766 |
parent 4105 | c1a6b872831b |
child 4341 | f1e9c4428cd3 |
permissions | -rw-r--r-- |
4105 | 1 |
"{ Encoding: utf8 }" |
2 |
||
0 | 3 |
" |
6 | 4 |
COPYRIGHT (c) 1991 by Claus Gittinger |
26 | 5 |
All Rights Reserved |
0 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
1393
abed635fb3ce
convert non-depth8 images when trying to save
Claus Gittinger <cg@exept.de>
parents:
1313
diff
changeset
|
14 |
"{ Package: 'stx:libview2' }" |
abed635fb3ce
convert non-depth8 images when trying to save
Claus Gittinger <cg@exept.de>
parents:
1313
diff
changeset
|
15 |
|
3468 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
0 | 18 |
ImageReader subclass:#GIFReader |
1739 | 19 |
instanceVariableNames:'pass xpos ypos rowByteSize remainBitCount bufByte bufStream |
20 |
prefixTable suffixTable clearCode eoiCode freeCode codeSize |
|
21 |
maxCode interlace frameDelay iterationCount leftOffs topOffs' |
|
708 | 22 |
classVariableNames:'ImageSeparator Extension Terminator' |
154 | 23 |
poolDictionaries:'' |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1739
diff
changeset
|
24 |
category:'Graphics-Images-Readers' |
0 | 25 |
! |
26 |
||
339 | 27 |
!GIFReader class methodsFor:'documentation'! |
0 | 28 |
|
21 | 29 |
copyright |
30 |
" |
|
31 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
26 | 32 |
All Rights Reserved |
21 | 33 |
|
34 |
This software is furnished under a license and may be used |
|
35 |
only in accordance with the terms of that license and with the |
|
36 |
inclusion of the above copyright notice. This software may not |
|
37 |
be provided or otherwise made available to, or used by, any |
|
38 |
other person. No title to or ownership of the software is |
|
39 |
hereby transferred. |
|
40 |
" |
|
41 |
! |
|
42 |
||
0 | 43 |
documentation |
44 |
" |
|
45 |
this class provides methods for loading and saving GIF pictures. |
|
3775 | 46 |
It has been tested with some different GIF87a pictures, I don't |
0 | 47 |
know, if it works with other GIF versions. |
48 |
GIF extension blocks are not handled. |
|
49 |
||
21 | 50 |
legal stuff extracted from GIF87a documentation: |
0 | 51 |
|
52 |
CompuServe Incorporated hereby grants a limited, non-exclusive, royalty-free |
|
53 |
license for the use of the Graphics Interchange Format(sm) in computer |
|
54 |
software; computer software utilizing GIF(sm) must acknowledge ownership of the |
|
55 |
Graphics Interchange Format and its Service Mark by CompuServe Incorporated, in |
|
3392 | 56 |
User and Technical Documentation. |
0 | 57 |
|
58 |
The Graphics Interchange Format(c) is the Copyright property of |
|
59 |
CompuServe Incorporated. GIF(sm) is a Service Mark property of |
|
60 |
CompuServe Incorporated. |
|
194 | 61 |
|
3120 | 62 |
Notice: |
3775 | 63 |
there has been some annoyance regarding a patent on the compression algorithm |
3902 | 64 |
used in gif. There is no warranty from exept, regarding any legal problems, when using GIF. |
65 |
We therefore highly recommend to use newer (and especially: royalty-free) formats, such as PNG. |
|
66 |
||
67 |
This patent is now obsolete: |
|
68 |
The United States LZW patent expired on 20 June 2003. |
|
69 |
The counterpart patents in the United Kingdom, France, Germany and Italy |
|
70 |
expired on 18 June 2004, the Japanese patents expired on 20 June 2004, |
|
71 |
and the Canadian patent expired on 7 July 2004. |
|
72 |
Consequently, the GIF format may now be used freely. |
|
3120 | 73 |
|
194 | 74 |
[See also:] |
3775 | 75 |
Image Form Icon |
4105 | 76 |
BlitImageReader FaceReader JPEGReader MacOSXIconReader |
77 |
PBMReader PCXReader PNGReader |
|
3775 | 78 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
79 |
XBMReader XPMReader XWDReader |
|
220 | 80 |
|
81 |
[author:] |
|
3775 | 82 |
Claus Gittinger |
0 | 83 |
" |
3283 | 84 |
! |
85 |
||
86 |
examples |
|
87 |
" |
|
88 |
saving an animated gif sequence: |
|
89 |
||
90 |
a view showing rainfall: |
|
3392 | 91 |
[exBegin] |
3284 | 92 |
|BG CLR N1 H W v WIND drops gen newDrops draw remove move buffer| |
3283 | 93 |
|
94 |
BG := Color black. |
|
95 |
CLR := Color blue lightened. |
|
96 |
H := 100. |
|
97 |
W := 100. |
|
98 |
N1 := 10. |
|
99 |
WIND := 0. |
|
100 |
drops := OrderedCollection new. |
|
101 |
||
102 |
gen := [:n | ((1 to:n) collect:[:i | Random nextIntegerBetween:1 and:W] as:Set) collect:[:x | x@0]]. |
|
103 |
newDrops := [drops addAll:(gen value:N1)]. |
|
104 |
draw := [buffer fill:BG; paint:CLR. drops do:[:d | buffer displayPoint:d]]. |
|
105 |
remove := [drops := drops reject:[:d | d y > H]]. |
|
106 |
move := [:wind | drops := drops collect:[:d| (d x + wind)\\W @ (d y + 1)]]. |
|
107 |
v := View new openAndWait. |
|
108 |
buffer := Form extent:(v extent) depth:24 onDevice:v device. |
|
109 |
||
110 |
[ |
|
3392 | 111 |
[v shown] whileTrue:[ |
112 |
draw value. |
|
113 |
v displayForm:buffer. |
|
114 |
move value:WIND. |
|
115 |
remove value. |
|
116 |
newDrops value. |
|
117 |
WIND := (WIND+(Random nextBetween:-1 and:1)) clampBetween:-5 and:5. |
|
118 |
Delay waitForSeconds:0.1. |
|
119 |
] |
|
3283 | 120 |
] fork. |
3392 | 121 |
[exEnd] |
3283 | 122 |
|
123 |
saving those images: |
|
3392 | 124 |
[exBegin] |
3283 | 125 |
|seq img BG CLR N1 H W v drops gen gen1 buffer| |
126 |
||
127 |
BG := Color black. |
|
128 |
CLR := Color blue lightened. |
|
129 |
H := 100. |
|
130 |
W := 100. |
|
131 |
N1 := 10. |
|
132 |
WIND := 0. |
|
133 |
drops := OrderedCollection new. |
|
134 |
||
135 |
gen := [:n | ((1 to:n) collect:[:i | Random nextIntegerBetween:1 and:W] as:Set) collect:[:x | x@0]]. |
|
136 |
newDrops := [drops addAll:(gen value:N1)]. |
|
137 |
draw := [buffer fill:BG; paint:CLR. drops do:[:d | buffer displayPoint:d]]. |
|
138 |
remove := [drops := drops reject:[:d | d y > H]]. |
|
139 |
move := [:wind | drops := drops collect:[:d| (d x + wind)\\W @ (d y + 1)]]. |
|
140 |
buffer := Form extent:W@H depth:8 onDevice:Display. |
|
141 |
||
142 |
seq := OrderedCollection new. |
|
143 |
500 timesRepeat:[ |
|
3392 | 144 |
move value:WIND. |
145 |
remove value. |
|
146 |
newDrops value. |
|
147 |
draw value. |
|
148 |
seq add:(ImageFrame new delay:100; image:(Depth8Image fromForm:buffer)). |
|
149 |
WIND := (WIND+(Random nextBetween:-1 and:1)) clampBetween:-5 and:5. |
|
3283 | 150 |
]. |
151 |
||
152 |
img := seq first image. |
|
153 |
img imageSequence:(seq copyFrom:2). |
|
154 |
img imageSequence do:[:each | each image colorMap:img colorMap]. |
|
155 |
GIFReader save:img onFile:'/tmp/img.gif' |
|
3392 | 156 |
[exEnd] |
3283 | 157 |
" |
0 | 158 |
! ! |
159 |
||
339 | 160 |
!GIFReader class methodsFor:'initialization'! |
26 | 161 |
|
162 |
initialize |
|
198 | 163 |
"install myself in the Image classes fileFormat table |
398 | 164 |
for the `.gif' extensions." |
3468 | 165 |
|
166 |
ImageSeparator isNil ifTrue:[ |
|
167 |
ImageSeparator := $, codePoint. |
|
168 |
Extension := $!! codePoint. |
|
169 |
Terminator := $; codePoint. |
|
198 | 170 |
|
3468 | 171 |
MIMETypes defineImageType:'image/gif' suffix:'gif' reader:self. |
172 |
]. |
|
198 | 173 |
|
708 | 174 |
"Modified: 14.10.1997 / 18:47:27 / cg" |
26 | 175 |
! ! |
176 |
||
339 | 177 |
!GIFReader class methodsFor:'testing'! |
1 | 178 |
|
713 | 179 |
canRepresent:anImage |
180 |
"return true, if anImage can be represented in my file format. |
|
181 |
GIF supports depth 8 images only." |
|
182 |
||
1521 | 183 |
anImage depth == 8 ifTrue:[^ true]. |
3573 | 184 |
('GIFReader [info]: image depth is not 8.') infoPrintCR. |
2030 | 185 |
^ false. |
713 | 186 |
|
187 |
"Created: 17.10.1997 / 20:19:20 / cg" |
|
188 |
! |
|
189 |
||
1 | 190 |
isValidImageFile:aFileName |
191 |
"return true, if aFileName contains a GIF image" |
|
192 |
||
193 |
|id inStream| |
|
194 |
||
9 | 195 |
inStream := self streamReadingFile:aFileName. |
1 | 196 |
inStream isNil ifTrue:[^ false]. |
197 |
||
332 | 198 |
inStream text. |
199 |
||
1 | 200 |
id := String new:6. |
201 |
inStream nextBytes:6 into:id. |
|
202 |
inStream close. |
|
203 |
||
204 |
(id = 'GIF87a') ifFalse:[ |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
205 |
(id startsWith:'GIF') ifFalse:[^ false]. |
1 | 206 |
|
3392 | 207 |
id ~= 'GIF89a' ifTrue:[ |
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
208 |
'GIFReader [info]: not a GIF87a/GIF89a file - hope that works' infoPrintCR. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
209 |
] |
1 | 210 |
]. |
211 |
^ true |
|
174 | 212 |
|
355 | 213 |
"Modified: 10.1.1997 / 15:40:34 / cg" |
1 | 214 |
! ! |
215 |
||
2694 | 216 |
!GIFReader methodsFor:'image reading'! |
154 | 217 |
|
2596 | 218 |
fromStream:aStream |
219 |
"read a stream containing a GIF image (or an image sequence). |
|
220 |
Leave image description in instance variables." |
|
221 |
||
3392 | 222 |
|byte flag fileColorMap |
2596 | 223 |
colorMapSize bitsPerPixel scrWidth scrHeight |
3392 | 224 |
hasColorMap interlaced id |
225 |
isGif89 atEnd |
|
2596 | 226 |
img firstImage firstOffset firstFrameDelay frame imageCount| |
227 |
||
228 |
inStream := aStream. |
|
229 |
aStream binary. |
|
230 |
||
231 |
"GIF-files are always lsb (intel-world)" |
|
232 |
byteOrder := #lsb. |
|
233 |
||
234 |
id := ByteArray new:6. |
|
235 |
(aStream nextBytes:6 into:id startingAt:1) ~~ 6 ifTrue:[ |
|
3553 | 236 |
^ self fileFormatError:'not a gif file (short read)'. |
2596 | 237 |
]. |
238 |
id := id asString. |
|
239 |
||
240 |
"all I had for testing where GIF87a files; |
|
241 |
I hope later versions work too ..." |
|
242 |
||
243 |
isGif89 := false. |
|
244 |
(id ~= 'GIF87a') ifTrue:[ |
|
3553 | 245 |
(id startsWith:'GIF') ifFalse:[ |
246 |
^ self fileFormatError:('not a gif file (id=''' , id , ''')'). |
|
247 |
]. |
|
248 |
id ~= 'GIF89a' ifTrue:[ |
|
249 |
'GIFReader [info]: not a GIF87a/GIF89a file - hope that works' infoPrintCR. |
|
250 |
] |
|
2596 | 251 |
]. |
252 |
||
253 |
"get screen dimensions (not used)" |
|
3553 | 254 |
scrWidth := aStream nextInt16MSB:false. |
255 |
scrHeight := aStream nextInt16MSB:false. |
|
2596 | 256 |
|
257 |
"get flag byte" |
|
258 |
flag := aStream nextByte. |
|
259 |
hasColorMap := (flag bitAnd:2r10000000) ~~ 0. |
|
260 |
"bitsPerRGB := ((flag bitAnd:2r01110000) bitShift:-4) + 1. " |
|
261 |
"colorMapSorted := ((flag bitAnd:2r00001000) ~~ 0. " |
|
262 |
bitsPerPixel := (flag bitAnd:2r00000111) + 1. |
|
263 |
colorMapSize := 1 bitShift:bitsPerPixel. |
|
264 |
||
265 |
"get background (not used)" |
|
266 |
aStream nextByte. |
|
267 |
||
268 |
"aspect ratio (not used)" |
|
269 |
aStream nextByte. |
|
270 |
||
271 |
"get colorMap" |
|
272 |
hasColorMap ifTrue:[ |
|
3553 | 273 |
fileColorMap := self readColorMap:colorMapSize. |
2596 | 274 |
]. |
275 |
colorMap := fileColorMap. |
|
276 |
||
277 |
photometric := #palette. |
|
278 |
samplesPerPixel := 1. |
|
279 |
bitsPerSample := #(8). |
|
280 |
||
281 |
imageCount := 0. |
|
282 |
atEnd := false. |
|
283 |
[atEnd] whileFalse:[ |
|
3553 | 284 |
"gif89a extensions" |
2596 | 285 |
|
3553 | 286 |
byte := aStream nextByte. |
287 |
byte isNil ifTrue:[ |
|
288 |
"/ atEnd-Terminator missing |
|
289 |
atEnd := true |
|
290 |
] ifFalse:[ |
|
291 |
byte == Extension ifTrue:[ |
|
3901 | 292 |
"/ 'Ext' infoPrintCR. |
3553 | 293 |
self readExtension:aStream. |
294 |
] ifFalse:[ |
|
295 |
(byte == Terminator) ifTrue:[ |
|
296 |
atEnd := true |
|
297 |
] ifFalse:[ |
|
298 |
"must be image separator" |
|
299 |
(byte ~~ ImageSeparator) ifTrue:[ |
|
300 |
^ self fileFormatError:('corrupted gif file (no IMAGESEP): ' , (byte printStringRadix:16)). |
|
301 |
]. |
|
3901 | 302 |
"/ 'Img' infoPrintCR. |
2596 | 303 |
|
3553 | 304 |
fileColorMap notNil ifTrue:[ |
305 |
colorMap := fileColorMap. |
|
306 |
]. |
|
307 |
Object primitiveFailureSignal handle:[:ex | |
|
308 |
^ self fileFormatError:('corrupted gif file'). |
|
309 |
] do:[ |
|
310 |
self readImage:aStream. |
|
311 |
]. |
|
2596 | 312 |
|
3553 | 313 |
maskPixel notNil ifTrue:[ |
314 |
"/ |
|
315 |
"/ ok, there is a maskValue |
|
316 |
"/ build a Depth1Image for it. |
|
317 |
"/ |
|
318 |
self buildMaskFromColor:maskPixel |
|
319 |
]. |
|
2596 | 320 |
|
3553 | 321 |
imageCount == 0 ifTrue:[ |
322 |
img := self makeImage. |
|
323 |
"/ remember first image in case more come later. |
|
324 |
firstImage := img. |
|
325 |
firstFrameDelay := frameDelay. |
|
326 |
firstOffset := (leftOffs @ topOffs). |
|
327 |
] ifFalse:[ |
|
328 |
imageCount == 1 ifTrue:[ |
|
329 |
imageSequence := ImageSequence new. |
|
330 |
img imageSequence:imageSequence. |
|
2596 | 331 |
|
3553 | 332 |
"/ add frame for first image. |
333 |
frame := ImageFrame new image:firstImage. |
|
334 |
frame delay:firstFrameDelay. |
|
335 |
frame offset:firstOffset. |
|
336 |
imageSequence add:frame. |
|
337 |
]. |
|
338 |
img := self makeImage. |
|
339 |
img imageSequence:imageSequence. |
|
2596 | 340 |
|
3553 | 341 |
"/ add frame for this image. |
342 |
frame := ImageFrame new image:img. |
|
343 |
frame delay:frameDelay. |
|
344 |
frame offset:(leftOffs @ topOffs). |
|
345 |
imageSequence add:frame. |
|
346 |
]. |
|
2596 | 347 |
|
3553 | 348 |
imageCount := imageCount + 1. |
2596 | 349 |
|
3553 | 350 |
frameDelay := nil. |
2596 | 351 |
|
3553 | 352 |
aStream atEnd ifTrue:[ |
353 |
atEnd := true. |
|
354 |
] |
|
355 |
] |
|
356 |
]. |
|
357 |
]. |
|
2596 | 358 |
]. |
359 |
||
360 |
imageSequence notNil ifTrue:[ |
|
3553 | 361 |
iterationCount notNil ifTrue:[ |
362 |
iterationCount == 0 ifTrue:[ |
|
363 |
imageSequence loop:true. |
|
364 |
] ifFalse:[ |
|
365 |
imageSequence loop:false. |
|
366 |
imageSequence iterationCount:iterationCount. |
|
367 |
] |
|
368 |
] |
|
2596 | 369 |
]. |
370 |
||
371 |
" |
|
372 |
Image fromFile:'/home/cg/work/stx/goodies/bitmaps/gifImages/animated/vrml.gif' |
|
373 |
Image fromFile:'/home/cg/work/stx/goodies/bitmaps/gifImages/animated/arrow.gif' |
|
374 |
" |
|
375 |
||
3901 | 376 |
"Modified: / 05-07-1996 / 17:32:01 / stefan" |
377 |
"Modified: / 21-08-1998 / 22:20:00 / cg" |
|
378 |
"Modified (format): / 17-02-2017 / 12:29:10 / cg" |
|
2694 | 379 |
! ! |
380 |
||
381 |
!GIFReader methodsFor:'private-reading'! |
|
382 |
||
383 |
makeGreyscale |
|
384 |
"not yet implemented/needed" |
|
385 |
! |
|
386 |
||
387 |
readColorMap:colorMapSize |
|
388 |
"get a gif colormap consisting of colorMapSize entries" |
|
389 |
||
390 |
|rgbVector| |
|
391 |
||
392 |
rgbVector := inStream nextBytes:colorMapSize*3. |
|
3392 | 393 |
^ MappedPalette rgbBytesVector:rgbVector |
2596 | 394 |
! |
395 |
||
154 | 396 |
readExtension:aStream |
882 | 397 |
"get gif89 extension" |
154 | 398 |
|
399 |
|type blockSize subBlockSize |
|
304 | 400 |
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
|
401 |
animationType animationTime animationMask |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
402 |
appID appAUTH |
888 | 403 |
b ok| |
154 | 404 |
|
405 |
type := aStream nextByte. |
|
1952
5009754ae556
Use #codePoint instead of deprecated #asciiValue
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
406 |
type == $R codePoint ifTrue:[ |
3553 | 407 |
"/ |
408 |
"/ Ratio extension |
|
409 |
"/ |
|
410 |
"/ 'GIFREADER [info]: ratio extension ignored' infoPrintCR. |
|
411 |
blockSize := aStream nextByte. |
|
412 |
(blockSize == 2) ifTrue:[ |
|
413 |
aspNum := aStream nextByte. |
|
414 |
aspDen := aStream nextByte |
|
415 |
] ifFalse:[ |
|
416 |
aStream skip:blockSize |
|
417 |
]. |
|
2844 | 418 |
|
3553 | 419 |
"/ eat subblocks |
420 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
|
421 |
aStream skip:subBlockSize |
|
422 |
]. |
|
423 |
^ self |
|
239 | 424 |
]. |
425 |
||
426 |
type == 16r01 ifTrue:[ |
|
3553 | 427 |
"/ |
428 |
"/ plaintext extension |
|
429 |
"/ |
|
430 |
"/ 'GIFREADER [info]: plaintext extension ignored' infoPrintCR. |
|
431 |
subBlockSize := aStream nextByte. |
|
432 |
left := aStream nextInt16MSB:false. |
|
433 |
top := aStream nextInt16MSB:false. |
|
434 |
width := aStream nextInt16MSB:false. |
|
435 |
height := aStream nextInt16MSB:false. |
|
436 |
cWidth := aStream nextByte. |
|
437 |
cHeight := aStream nextByte. |
|
438 |
fg := aStream nextByte. |
|
439 |
bg := aStream nextByte. |
|
440 |
aStream skip:12. |
|
441 |
"/ eat subblocks |
|
442 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
|
443 |
aStream skip:subBlockSize |
|
444 |
]. |
|
445 |
^ self |
|
239 | 446 |
]. |
447 |
||
448 |
type == 16rF9 ifTrue:[ |
|
3553 | 449 |
"/ |
450 |
"/ graphic control extension |
|
451 |
"/ |
|
452 |
"/ 'GIFREADER [info]: graphic control extension' infoPrintCR. |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
453 |
|
3553 | 454 |
[(subBlockSize := aStream nextByte) ~~ 0 and:[subBlockSize notNil]] whileTrue:[ |
455 |
"/ type bitAnd:1 means: animationMask is transparent pixel |
|
456 |
"/ to be implemented in Image ... |
|
305 | 457 |
|
3553 | 458 |
animationType := aStream nextByte. |
459 |
animationTime := aStream nextInt16MSB:false. |
|
460 |
animationMask := aStream nextByte. |
|
2595
37dc71e1e59b
be tolerant to corrupted gif files (subblock size in extension)
Claus Gittinger <cg@exept.de>
parents:
2056
diff
changeset
|
461 |
|
3553 | 462 |
subBlockSize := subBlockSize - 4. |
307
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
463 |
|
3553 | 464 |
(animationType bitTest: 1) ifTrue:[ |
465 |
maskPixel := animationMask. |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
466 |
"/ 'GIFREADER [info]: mask: ' infoPrint. (maskPixel printStringRadix:16) infoPrintCR. |
3553 | 467 |
]. |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
468 |
"/ 'GIFREADER [info]: animationTime: ' infoPrint. (animationTime * (1/100)) infoPrintCR. |
1429 | 469 |
"/ 'GIFREADER [info]: animationType: ' infoPrint. (animationType) infoPrintCR. |
470 |
"/ 'GIFREADER [info]: animationMask: ' infoPrint. (animationMask) infoPrintCR. |
|
307
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
471 |
|
3553 | 472 |
frameDelay := (animationTime * (1/100)) * 1000. |
888 | 473 |
|
3553 | 474 |
subBlockSize ~~ 0 ifTrue:[ |
475 |
aStream skip:subBlockSize |
|
476 |
]. |
|
477 |
]. |
|
478 |
^ self |
|
239 | 479 |
]. |
480 |
||
519 | 481 |
type == 16rFE ifTrue:[ |
3553 | 482 |
"/ |
483 |
"/ comment extension |
|
484 |
"/ |
|
485 |
"/ 'GIFREADER [info]: comment extension' infoPrintCR. |
|
486 |
[(blockSize := aStream nextByte) ~~ 0] whileTrue:[ |
|
487 |
aStream skip:blockSize |
|
488 |
]. |
|
489 |
^ self |
|
519 | 490 |
]. |
491 |
||
239 | 492 |
type == 16rFF ifTrue:[ |
3553 | 493 |
"/ |
494 |
"/ application extension |
|
495 |
"/ |
|
496 |
"/ 'GIFREADER [info]: application extension' infoPrintCR. |
|
497 |
subBlockSize := aStream nextByte. |
|
498 |
appID := (aStream nextBytes:8 ) asString. |
|
499 |
appAUTH := aStream nextBytes:3. |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
500 |
|
3553 | 501 |
subBlockSize := aStream nextByte. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
502 |
|
3553 | 503 |
ok := false. |
504 |
appID = 'NETSCAPE' ifTrue:[ |
|
505 |
appAUTH asString = '2.0' ifTrue:[ |
|
506 |
subBlockSize == 3 ifTrue:[ |
|
507 |
b := aStream nextByte. |
|
508 |
iterationCount := aStream nextInt16MSB:false. |
|
509 |
subBlockSize := aStream nextByte. |
|
510 |
ok := true. |
|
888 | 511 |
"/ ('GIFREADER [info]: NETSCAPE application extension - iterationCount = ') infoPrint. |
512 |
"/ iterationCount infoPrintCR. |
|
3553 | 513 |
] |
514 |
] |
|
515 |
]. |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
516 |
|
3553 | 517 |
ok ifFalse:[ |
518 |
('GIFREADER [info]: application extension (' , appID , ') ignored') infoPrintCR. |
|
519 |
]. |
|
625 | 520 |
|
3553 | 521 |
[subBlockSize > 0] whileTrue:[ |
522 |
aStream skip:subBlockSize. |
|
523 |
subBlockSize := aStream nextByte. |
|
524 |
]. |
|
525 |
^ self |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
526 |
]. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
527 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
528 |
type == 16r2C ifTrue:[ |
3553 | 529 |
"/ |
530 |
"/ image descriptor extension |
|
531 |
"/ |
|
532 |
"/ 'GIFREADER [info]: image descriptor extension ignored' infoPrintCR. |
|
533 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
|
534 |
aStream skip:subBlockSize |
|
535 |
]. |
|
536 |
^ self |
|
239 | 537 |
]. |
538 |
||
539 |
"/ |
|
540 |
"/ unknown extension |
|
541 |
"/ |
|
354
b4d2ce853c24
stefans suggested info/warning/error/fatal
Claus Gittinger <cg@exept.de>
parents:
339
diff
changeset
|
542 |
'GIFREADER [info]: unknown extension ignored' infoPrintCR. |
239 | 543 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
3553 | 544 |
aStream skip:subBlockSize |
154 | 545 |
] |
546 |
||
2844 | 547 |
"Modified: / 02-06-2010 / 12:21:53 / cg" |
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
548 |
! |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
549 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
550 |
readImage:aStream |
882 | 551 |
"read a single image from aStream." |
552 |
||
1429 | 553 |
| flag interlaced hasLocalColorMap bitsPerPixel colorMapSize |
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
554 |
codeLen compressedData compressedSize index count h tmp srcOffset dstOffset |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
555 |
initialBuffSize| |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
556 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
557 |
"get image data" |
3553 | 558 |
leftOffs := aStream nextInt16MSB:false. |
559 |
topOffs := aStream nextInt16MSB:false. |
|
3901 | 560 |
"/ 'GIFReader: leftOffs ' infoPrint. leftOffs infoPrintCR. |
561 |
"/ 'GIFReader: topOffs ' infoPrint. topOffs infoPrintCR. |
|
3553 | 562 |
width := aStream nextInt16MSB:false. |
563 |
height := aStream nextInt16MSB:false. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
564 |
|
1846 | 565 |
self reportDimension. |
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
566 |
|
3901 | 567 |
"/ |
568 |
"/ 'width ' print. width printNewline. |
|
569 |
"/ 'height ' print. height printNewline. |
|
570 |
"/ |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
571 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
572 |
"another flag byte" |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
573 |
flag := aStream nextByte. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
574 |
interlaced := (flag bitAnd:2r01000000) ~~ 0. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
575 |
hasLocalColorMap := (flag bitAnd:2r10000000) ~~ 0. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
576 |
"localColorMapSorted := (flag bitAnd:2r00100000) ~~ 0. " |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
577 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
578 |
"if image has a local colormap, this one is used" |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
579 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
580 |
hasLocalColorMap ifTrue:[ |
3553 | 581 |
"local descr. overwrites" |
582 |
bitsPerPixel := (flag bitAnd:2r00000111) + 1. |
|
583 |
colorMapSize := 1 bitShift:bitsPerPixel. |
|
584 |
"overwrite colormap" |
|
585 |
colorMap := self readColorMap:colorMapSize. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
586 |
]. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
587 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
588 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
589 |
"get codelen for decompression" |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
590 |
codeLen := aStream nextByte. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
591 |
(aStream respondsTo:#fileSize) ifTrue:[ |
3553 | 592 |
initialBuffSize := aStream fileSize. |
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
593 |
] ifFalse:[ |
3553 | 594 |
initialBuffSize := 512. |
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
595 |
]. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
596 |
compressedData := ByteArray uninitializedNew:initialBuffSize. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
597 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
598 |
"get compressed data" |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
599 |
index := 1. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
600 |
count := aStream nextByte. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
601 |
[count notNil and:[count ~~ 0]] whileTrue:[ |
3553 | 602 |
(compressedData size < (index+count)) ifTrue:[ |
603 |
|t| |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
604 |
|
3784 | 605 |
t := ByteArray uninitializedNew:((index+count)*3//2). |
3553 | 606 |
t replaceBytesFrom:1 to:index-1 with:compressedData startingAt:1. |
607 |
compressedData := t. |
|
608 |
]. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
609 |
|
3553 | 610 |
aStream nextBytes:count into:compressedData startingAt:index blockSize:4096. |
611 |
index := index + count. |
|
612 |
count := aStream nextByte |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
613 |
]. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
614 |
compressedSize := index - 1. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
615 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
616 |
h := height. |
1037
90d38c8e3f5f
use initialized byteArrays, in case rows are padded
Claus Gittinger <cg@exept.de>
parents:
891
diff
changeset
|
617 |
data := ByteArray new:((width + 1) * (h + 1)). |
3901 | 618 |
"/ 'GIFReader: decompressing ...' infoPrintCR. |
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
619 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
620 |
self class decompressGIFFrom:compressedData |
3553 | 621 |
count:compressedSize |
622 |
into:data |
|
623 |
startingAt:1 |
|
624 |
codeLen:(codeLen + 1). |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
625 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
626 |
interlaced ifTrue:[ |
3901 | 627 |
"/ 'GIFREADER: deinterlacing ...' infoPrintCR. |
3553 | 628 |
tmp := ByteArray new:(data size). |
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
629 |
|
3553 | 630 |
"phase 1: 0, 8, 16, 24, ..." |
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
631 |
|
3553 | 632 |
srcOffset := 1. |
633 |
0 to:(h - 1) by:8 do:[:dstRow | |
|
634 |
dstOffset := dstRow * width + 1. |
|
635 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
|
636 |
with:data startingAt:srcOffset. |
|
637 |
srcOffset := srcOffset + width. |
|
638 |
]. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
639 |
|
3553 | 640 |
"phase 2: 4, 12, 20, 28, ..." |
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
641 |
|
3553 | 642 |
4 to:(h - 1) by:8 do:[:dstRow | |
643 |
dstOffset := dstRow * width + 1. |
|
644 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
|
645 |
with:data startingAt:srcOffset. |
|
646 |
srcOffset := srcOffset + width. |
|
647 |
]. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
648 |
|
3553 | 649 |
"phase 3: 2, 6, 10, 14, ..." |
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
650 |
|
3553 | 651 |
2 to:(h - 1) by:4 do:[:dstRow | |
652 |
dstOffset := dstRow * width + 1. |
|
653 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
|
654 |
with:data startingAt:srcOffset. |
|
655 |
srcOffset := srcOffset + width. |
|
656 |
]. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
657 |
|
3553 | 658 |
"phase 4: 1, 3, 5, 7, ..." |
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
659 |
|
3553 | 660 |
1 to:(h - 1) by:2 do:[:dstRow | |
661 |
dstOffset := dstRow * width + 1. |
|
662 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
|
663 |
with:data startingAt:srcOffset. |
|
664 |
srcOffset := srcOffset + width. |
|
665 |
]. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
666 |
|
3553 | 667 |
data := tmp. |
668 |
tmp := nil. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
669 |
]. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
670 |
|
3784 | 671 |
"Created: / 13-01-1998 / 10:44:05 / cg" |
672 |
"Modified: / 12-08-1998 / 13:55:32 / cg" |
|
3901 | 673 |
"Modified (format): / 17-02-2017 / 12:29:44 / cg" |
0 | 674 |
! ! |
43 | 675 |
|
1805 | 676 |
!GIFReader methodsFor:'private-writing'! |
677 |
||
678 |
assignTransparentPixelIn:image |
|
679 |
"find an usused pixelValue in the colorMap (or image)." |
|
680 |
||
3689 | 681 |
|cmap cmapSize imgDepth usedPixelValues unusedValues| |
1805 | 682 |
|
3689 | 683 |
cmap := image colorMap. |
684 |
cmapSize := cmap size. |
|
685 |
((cmapSize > 0) and:[cmapSize < 256]) ifTrue:[ |
|
686 |
maskPixel := cmapSize. |
|
687 |
^ self |
|
1805 | 688 |
]. |
3689 | 689 |
|
3009 | 690 |
image compressColorMap. |
3689 | 691 |
imgDepth := image depth. |
1805 | 692 |
usedPixelValues := image usedValues. |
3689 | 693 |
usedPixelValues size < (1 bitShift:imgDepth) ifFalse:[ |
694 |
Image informationLostQuerySignal |
|
695 |
raiseRequestWith:image |
|
696 |
errorString:('GIF writer cannot assign a transparent pixel - all pixelValues used by image'). |
|
2056
b7eea50081f2
fixed transparency-pixel bug (with full colormap)
Claus Gittinger <cg@exept.de>
parents:
2032
diff
changeset
|
697 |
]. |
b7eea50081f2
fixed transparency-pixel bug (with full colormap)
Claus Gittinger <cg@exept.de>
parents:
2032
diff
changeset
|
698 |
|
b7eea50081f2
fixed transparency-pixel bug (with full colormap)
Claus Gittinger <cg@exept.de>
parents:
2032
diff
changeset
|
699 |
"/ there must be an unused pixelValue |
3689 | 700 |
unusedValues := (0 to:(1 bitShift:imgDepth)-1) asSet. |
2056
b7eea50081f2
fixed transparency-pixel bug (with full colormap)
Claus Gittinger <cg@exept.de>
parents:
2032
diff
changeset
|
701 |
unusedValues removeAll:usedPixelValues. |
b7eea50081f2
fixed transparency-pixel bug (with full colormap)
Claus Gittinger <cg@exept.de>
parents:
2032
diff
changeset
|
702 |
|
b7eea50081f2
fixed transparency-pixel bug (with full colormap)
Claus Gittinger <cg@exept.de>
parents:
2032
diff
changeset
|
703 |
maskPixel := unusedValues first. |
3009 | 704 |
|
705 |
"Modified: / 17-07-2012 / 12:25:24 / anwild" |
|
1805 | 706 |
! |
707 |
||
708 |
checkCodeSize |
|
709 |
(freeCode > maxCode and: [codeSize < 12]) |
|
3392 | 710 |
ifTrue: |
1805 | 711 |
[codeSize := codeSize + 1. |
712 |
maxCode := (1 bitShift: codeSize) - 1] |
|
713 |
||
714 |
"Created: 14.10.1997 / 18:42:01 / cg" |
|
715 |
! |
|
716 |
||
717 |
flushBits |
|
718 |
remainBitCount = 0 |
|
3392 | 719 |
ifFalse: |
1805 | 720 |
[self nextBytePut: bufByte. |
721 |
remainBitCount := 0]. |
|
722 |
self flushBuffer |
|
723 |
||
724 |
"Modified: 14.10.1997 / 18:58:06 / cg" |
|
725 |
! |
|
726 |
||
727 |
flushBuffer |
|
728 |
bufStream isEmpty ifTrue: [^ self]. |
|
729 |
outStream nextPut: bufStream size. |
|
730 |
outStream nextPutAll: bufStream contents. |
|
731 |
bufStream := WriteStream on: (ByteArray new: 256) |
|
732 |
||
733 |
"Modified: 14.10.1997 / 20:46:04 / cg" |
|
734 |
! |
|
735 |
||
736 |
flushCode |
|
737 |
self flushBits |
|
738 |
||
739 |
"Created: 14.10.1997 / 18:57:33 / cg" |
|
740 |
! |
|
741 |
||
742 |
nextBitsPut: anInteger |
|
3392 | 743 |
| integer writeBitCount shiftCount | |
744 |
shiftCount := 0. |
|
745 |
remainBitCount = 0 |
|
746 |
ifTrue: |
|
747 |
[writeBitCount := 8. |
|
748 |
integer := anInteger] |
|
749 |
ifFalse: |
|
750 |
[writeBitCount := remainBitCount. |
|
751 |
integer := bufByte + (anInteger bitShift: 8 - remainBitCount)]. |
|
752 |
[writeBitCount < codeSize] |
|
753 |
whileTrue: |
|
754 |
[self nextBytePut: ((integer bitShift: shiftCount) bitAnd: 255). |
|
755 |
shiftCount := shiftCount - 8. |
|
756 |
writeBitCount := writeBitCount + 8]. |
|
757 |
(remainBitCount := writeBitCount - codeSize) = 0 |
|
758 |
ifTrue: [self nextBytePut: (integer bitShift: shiftCount)] |
|
759 |
ifFalse: [bufByte := integer bitShift: shiftCount]. |
|
760 |
^anInteger |
|
1805 | 761 |
|
762 |
"Modified: 15.10.1997 / 16:50:30 / cg" |
|
763 |
! |
|
764 |
||
765 |
nextBytePut: aByte |
|
766 |
bufStream nextPut: aByte. |
|
767 |
bufStream size >= 254 ifTrue: [self flushBuffer] |
|
768 |
||
769 |
"Modified: 15.10.1997 / 16:50:52 / cg" |
|
770 |
! |
|
771 |
||
3392 | 772 |
readPixelFrom: bits |
1805 | 773 |
| pixel | |
774 |
ypos >= height ifTrue: [^ nil]. |
|
3392 | 775 |
(maskPixel notNil |
1805 | 776 |
and:[(mask pixelAtX:xpos y:ypos) == 0]) ifTrue:[ |
777 |
pixel := maskPixel |
|
778 |
] ifFalse:[ |
|
779 |
pixel := bits at: ypos * rowByteSize + xpos + 1. |
|
780 |
]. |
|
781 |
self updatePixelPosition. |
|
782 |
^ pixel |
|
783 |
||
784 |
"Created: 14.10.1997 / 18:43:50 / cg" |
|
785 |
"Modified: 15.10.1997 / 16:46:43 / cg" |
|
786 |
! |
|
787 |
||
3392 | 788 |
setParameters:bitsPerPixel |
1805 | 789 |
clearCode := 1 bitShift:bitsPerPixel. |
790 |
eoiCode := clearCode + 1. |
|
791 |
freeCode := clearCode + 2. |
|
792 |
codeSize := bitsPerPixel + 1. |
|
793 |
maxCode := (1 bitShift: codeSize) - 1 |
|
794 |
||
795 |
"Modified: 14.10.1997 / 20:09:48 / cg" |
|
796 |
! |
|
797 |
||
798 |
updatePixelPosition |
|
799 |
(xpos := xpos + 1) >= width ifFalse: [^ self]. |
|
800 |
||
801 |
xpos := 0. |
|
802 |
interlace == true ifFalse:[ |
|
3392 | 803 |
ypos := ypos + 1. |
804 |
^ self |
|
1805 | 805 |
]. |
806 |
||
807 |
pass == 0 ifTrue:[ |
|
3392 | 808 |
(ypos := ypos + 8) >= height ifTrue:[ |
809 |
pass := pass + 1. |
|
810 |
ypos := 4 |
|
811 |
]. |
|
812 |
^ self |
|
1805 | 813 |
]. |
814 |
pass == 1 ifTrue:[ |
|
3392 | 815 |
(ypos := ypos + 8) >= height ifTrue:[ |
816 |
pass := pass + 1. |
|
817 |
ypos := 2 |
|
818 |
]. |
|
819 |
^ self |
|
1805 | 820 |
]. |
821 |
pass == 2 ifTrue:[ |
|
3392 | 822 |
(ypos := ypos + 4) >= height ifTrue:[ |
823 |
pass := pass + 1. |
|
824 |
ypos := 1 |
|
825 |
]. |
|
826 |
^ self |
|
1805 | 827 |
]. |
828 |
pass == 3 ifTrue:[ |
|
3392 | 829 |
ypos := ypos + 2. |
830 |
^ self |
|
1805 | 831 |
]. |
832 |
^ self error: 'can''t happen' |
|
833 |
||
834 |
"Modified: 14.10.1997 / 18:44:27 / cg" |
|
835 |
! |
|
836 |
||
2032 | 837 |
writeBitDataFor: image |
838 |
"using modified Lempel-Ziv Welch algorithm." |
|
839 |
||
840 |
| bits bitsPerPixel t1 |
|
3849 | 841 |
maxBits maxMaxCode tSize initCodeSize ent tShift fCode |
842 |
pixel |
|
843 |
index disp nomatch | |
|
2032 | 844 |
|
845 |
outStream nextPut:ImageSeparator. |
|
3282 | 846 |
self writeShort:0. "/ leftOffs |
847 |
self writeShort:0. "/ topOffs |
|
2032 | 848 |
self writeShort:width. "/ image size |
849 |
self writeShort:height. |
|
850 |
||
851 |
interlace == true ifTrue:[ |
|
3848 | 852 |
t1 := 64 |
2032 | 853 |
] ifFalse:[ |
3848 | 854 |
t1 := 0 "/ no local colormap |
2032 | 855 |
]. |
856 |
outStream nextPut:t1. "/ another flag |
|
857 |
||
858 |
bitsPerPixel := image bitsPerPixel. |
|
859 |
bits := image bits. |
|
860 |
||
861 |
pass := 0. |
|
862 |
xpos := 0. |
|
863 |
ypos := 0. |
|
864 |
rowByteSize := image bytesPerRow. "/ width * 8 + 31 // 32 * 4. |
|
865 |
remainBitCount := 0. |
|
866 |
bufByte := 0. |
|
867 |
bufStream := WriteStream on: (ByteArray new: 256). |
|
868 |
||
869 |
maxBits := 12. |
|
870 |
maxMaxCode := 1 bitShift: maxBits. |
|
871 |
tSize := 5003. |
|
872 |
prefixTable := Array new: tSize. |
|
3364 | 873 |
suffixTable := Array new: tSize withAll:-1. |
2032 | 874 |
|
875 |
initCodeSize := bitsPerPixel <= 1 ifTrue: [2] ifFalse: [bitsPerPixel]. |
|
876 |
outStream nextPut: initCodeSize. |
|
877 |
self setParameters: initCodeSize. |
|
878 |
||
879 |
tShift := 0. |
|
880 |
fCode := tSize. |
|
2845
62d4128bb923
comment/format in: #writeBitDataFor:
Claus Gittinger <cg@exept.de>
parents:
2844
diff
changeset
|
881 |
[fCode < 65536] whileTrue:[ |
3848 | 882 |
tShift := tShift + 1. |
883 |
fCode := fCode * 2 |
|
2845
62d4128bb923
comment/format in: #writeBitDataFor:
Claus Gittinger <cg@exept.de>
parents:
2844
diff
changeset
|
884 |
]. |
2032 | 885 |
tShift := 8 - tShift. |
3364 | 886 |
"/ 1 to: tSize do: [:i | suffixTable at: i put: -1]. - cg changed initialization above |
2032 | 887 |
|
888 |
self writeCodeAndCheckCodeSize: clearCode. |
|
889 |
ent := self readPixelFrom: bits. |
|
3848 | 890 |
[(pixel := self readPixelFrom: bits) notNil] whileTrue:[ |
891 |
fCode := (pixel bitShift: maxBits) + ent. |
|
892 |
index := ((pixel bitShift: tShift) bitXor: ent) + 1. |
|
893 |
(suffixTable at: index) = fCode |
|
894 |
ifTrue: [ent := prefixTable at: index] |
|
895 |
ifFalse:[ |
|
896 |
nomatch := true. |
|
897 |
(suffixTable at: index) >= 0 |
|
898 |
ifTrue:[ |
|
899 |
disp := tSize - index + 1. |
|
900 |
index = 1 ifTrue: [disp := 1]. |
|
901 |
"probe" |
|
902 |
[(index := index - disp) < 1 ifTrue: [index := index + tSize]. |
|
903 |
(suffixTable at: index) = fCode |
|
904 |
ifTrue:[ |
|
905 |
ent := prefixTable at: index. |
|
906 |
nomatch := false. |
|
907 |
"continue whileFalse:"]. |
|
908 |
nomatch and: [(suffixTable at: index) > 0]] |
|
909 |
whileTrue: ["probe"]]. |
|
910 |
"nomatch" |
|
911 |
nomatch ifTrue:[ |
|
912 |
self writeCodeAndCheckCodeSize: ent. |
|
913 |
ent := pixel. |
|
914 |
freeCode < maxMaxCode |
|
915 |
ifTrue:[ |
|
916 |
prefixTable at: index put: freeCode. |
|
917 |
suffixTable at: index put: fCode. |
|
918 |
freeCode := freeCode + 1] |
|
919 |
ifFalse:[ |
|
920 |
self writeCodeAndCheckCodeSize: clearCode. |
|
921 |
suffixTable from:1 to:tSize put:-1. |
|
922 |
self setParameters: initCodeSize]]]]. |
|
2032 | 923 |
prefixTable := suffixTable := nil. |
924 |
self writeCodeAndCheckCodeSize: ent. |
|
925 |
self writeCodeAndCheckCodeSize: eoiCode. |
|
926 |
self flushCode. |
|
927 |
outStream nextPut: 0. "zero-length packet" |
|
928 |
||
2845
62d4128bb923
comment/format in: #writeBitDataFor:
Claus Gittinger <cg@exept.de>
parents:
2844
diff
changeset
|
929 |
"Modified: / 02-06-2010 / 12:24:20 / cg" |
2032 | 930 |
! |
931 |
||
3392 | 932 |
writeCode: aCode |
1805 | 933 |
self nextBitsPut: aCode |
934 |
||
935 |
"Created: 14.10.1997 / 18:38:35 / cg" |
|
936 |
"Modified: 15.10.1997 / 17:01:47 / cg" |
|
937 |
! |
|
938 |
||
3392 | 939 |
writeCodeAndCheckCodeSize: t1 |
1805 | 940 |
self writeCode: t1. |
941 |
self checkCodeSize |
|
942 |
||
943 |
"Created: 14.10.1997 / 18:38:24 / cg" |
|
944 |
"Modified: 14.10.1997 / 18:40:56 / cg" |
|
2032 | 945 |
! |
946 |
||
947 |
writeHeaderFor:image |
|
948 |
"write the gif header" |
|
949 |
||
950 |
|bitsPerPixel t1| |
|
951 |
||
952 |
bitsPerPixel := image bitsPerPixel. |
|
953 |
||
954 |
outStream nextPutAll: 'GIF89a' asByteArray. |
|
955 |
self writeShort:width. "/ screen size |
|
3392 | 956 |
self writeShort:height. |
2032 | 957 |
t1 := 128. |
958 |
t1 := t1 bitOr:(bitsPerPixel - 1 bitShift:5). |
|
959 |
t1 := t1 bitOr:(bitsPerPixel - 1). |
|
960 |
outStream nextPut:t1. "/ flag |
|
961 |
outStream nextPut:0. "/ background (not used) |
|
962 |
outStream nextPut:0. "/ aspect ratio |
|
963 |
||
964 |
0 to:(1 bitShift:bitsPerPixel)-1 do:[:pixel | |
|
3392 | 965 |
|clr red green blue| |
2032 | 966 |
|
3392 | 967 |
clr := image colorFromValue:pixel. |
968 |
clr isNil ifTrue:[ |
|
969 |
"/ unused colorMap slot |
|
970 |
red := green := blue := 0. |
|
971 |
] ifFalse:[ |
|
972 |
red := (clr redByte). |
|
973 |
green := (clr greenByte). |
|
974 |
blue := (clr blueByte). |
|
975 |
]. |
|
976 |
outStream |
|
977 |
nextPut:red; nextPut:green; nextPut:blue. |
|
978 |
]. |
|
2032 | 979 |
|
980 |
"Created: / 14.10.1997 / 17:41:28 / cg" |
|
981 |
"Modified: / 31.10.1997 / 16:12:13 / cg" |
|
982 |
! |
|
983 |
||
984 |
writeMaskExtensionHeaderFor:image |
|
985 |
"write an extension header for the transparent pixel" |
|
986 |
||
987 |
outStream nextPut:Extension. |
|
988 |
outStream nextPut:16rF9. "/ graphic control extension |
|
989 |
outStream nextPut:4. "/ subBlockSize |
|
990 |
||
991 |
outStream nextPut:1. "/ animationType |
|
3658 | 992 |
outStream nextPutInt16LSB:1. "/ animationTime |
2032 | 993 |
outStream nextPut:maskPixel. "/ animationMask |
994 |
||
995 |
outStream nextPut:0. |
|
1805 | 996 |
! ! |
997 |
||
708 | 998 |
!GIFReader methodsFor:'writing to file'! |
999 |
||
1000 |
save:image onFile:aFileName |
|
1001 |
"save image as GIF file on aFileName" |
|
1002 |
||
1813
aa74c7bd22ad
image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents:
1811
diff
changeset
|
1003 |
|convertedImage| |
1206
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1004 |
|
718
41ade132da98
raise an error, if depth is not supported.
Claus Gittinger <cg@exept.de>
parents:
714
diff
changeset
|
1005 |
image depth ~~ 8 ifTrue:[ |
3392 | 1006 |
Error handle:[:ex | |
1007 |
^ Image cannotRepresentImageSignal |
|
1008 |
raiseWith:image |
|
1009 |
errorString:('GIF failed to convert image to depth8: ', ex description). |
|
1010 |
"/ ex return. |
|
1011 |
] do:[ |
|
1012 |
convertedImage := Image newForDepth:8. |
|
1013 |
convertedImage fromImage:image photometric:#palette. |
|
1014 |
]. |
|
1015 |
convertedImage notNil ifTrue:[ |
|
1016 |
^ self save:convertedImage onFile:aFileName |
|
1017 |
]. |
|
1018 |
^ Image cannotRepresentImageSignal |
|
1019 |
raiseWith:image |
|
1020 |
errorString:('GIF (currently) only supports depth8 images (cannot convert)'). |
|
718
41ade132da98
raise an error, if depth is not supported.
Claus Gittinger <cg@exept.de>
parents:
714
diff
changeset
|
1021 |
]. |
41ade132da98
raise an error, if depth is not supported.
Claus Gittinger <cg@exept.de>
parents:
714
diff
changeset
|
1022 |
|
1813
aa74c7bd22ad
image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents:
1811
diff
changeset
|
1023 |
super save:image onFile:aFileName. |
1206
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1024 |
|
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1025 |
" |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1026 |
|i| |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1027 |
|
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1028 |
i := Image fromFile:'bitmaps/gifImages/garfield.gif'. |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1029 |
GIFReader save:i onFile:'foo.gif'. |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1030 |
(Image fromFile:'./foo.gif') inspect |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1031 |
" |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1032 |
|
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1033 |
"Created: / 14.10.1997 / 17:40:12 / cg" |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1034 |
"Modified: / 27.10.1997 / 22:42:31 / cg" |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1035 |
! |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1036 |
|
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1037 |
save:image onStream:aStream |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1038 |
"save image in GIF-file-format onto aStream" |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1039 |
|
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1040 |
image depth ~~ 8 ifTrue:[ |
3553 | 1041 |
^ Image cannotRepresentImageSignal |
1042 |
raiseWith:image |
|
1043 |
errorString:('GIF (currently) only supports depth8 images'). |
|
1206
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1044 |
]. |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1045 |
|
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1046 |
outStream := aStream. |
708 | 1047 |
outStream binary. |
1048 |
||
714 | 1049 |
mask := image mask. |
1050 |
mask notNil ifTrue:[ |
|
3553 | 1051 |
self assignTransparentPixelIn:image |
708 | 1052 |
]. |
1053 |
||
1054 |
byteOrder := #lsb. |
|
1055 |
width := image width. |
|
1056 |
height := image height. |
|
1057 |
photometric := image photometric. |
|
1058 |
samplesPerPixel := image samplesPerPixel. |
|
1059 |
bitsPerSample := image bitsPerSample. |
|
1060 |
colorMap := image colorMap. |
|
1061 |
data := image bits. |
|
1062 |
||
1063 |
self writeHeaderFor:image. |
|
714 | 1064 |
maskPixel notNil ifTrue:[ |
3553 | 1065 |
self writeMaskExtensionHeaderFor:image. |
714 | 1066 |
]. |
1067 |
||
708 | 1068 |
self writeBitDataFor:image. |
714 | 1069 |
|
3282 | 1070 |
image imageSequence notEmptyOrNil ifTrue:[ |
3553 | 1071 |
image imageSequence do:[:eachFrame | |
1072 |
outStream nextPut:Extension. |
|
1073 |
outStream nextPut:16rF9. "/ graphic control extension |
|
1074 |
outStream nextPut:4. "/ sub block size |
|
1075 |
outStream nextPut:0. "/ animation type 0 |
|
3658 | 1076 |
outStream nextPutInt16LSB:(eachFrame delay / 10) rounded asInteger. |
3553 | 1077 |
outStream nextPut:0. "/ animation mask |
1078 |
outStream nextPut:0. "/ subblock size |
|
3282 | 1079 |
|
3553 | 1080 |
self writeBitDataFor:eachFrame image. |
3392 | 1081 |
|
3553 | 1082 |
]. |
3282 | 1083 |
]. |
1084 |
||
714 | 1085 |
outStream nextPut: Terminator. |
708 | 1086 |
|
1087 |
" |
|
1088 |
|i| |
|
1089 |
||
1090 |
i := Image fromFile:'bitmaps/gifImages/garfield.gif'. |
|
1091 |
GIFReader save:i onFile:'foo.gif'. |
|
1092 |
(Image fromFile:'./foo.gif') inspect |
|
1093 |
" |
|
1094 |
||
718
41ade132da98
raise an error, if depth is not supported.
Claus Gittinger <cg@exept.de>
parents:
714
diff
changeset
|
1095 |
"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
|
1096 |
"Modified: / 27.10.1997 / 22:42:31 / cg" |
708 | 1097 |
! ! |
1098 |
||
339 | 1099 |
!GIFReader class methodsFor:'documentation'! |
135 | 1100 |
|
1101 |
version |
|
3553 | 1102 |
^ '$Header$' |
2844 | 1103 |
! |
1104 |
||
1105 |
version_CVS |
|
3553 | 1106 |
^ '$Header$' |
135 | 1107 |
! ! |
1711
b3ba13167ee7
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1521
diff
changeset
|
1108 |
|
3119 | 1109 |
|
43 | 1110 |
GIFReader initialize! |