author | Claus Gittinger <cg@exept.de> |
Tue, 10 Sep 2019 09:38:49 +0200 | |
changeset 4348 | 6113e0a7959c |
parent 4341 | f1e9c4428cd3 |
child 4374 | 9ee6defe2909 |
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 |
||
4348 | 190 |
hasValidImageHeader:someHeaderBytes |
191 |
"return true, if someHeaderBytes |
|
192 |
(which does not need to be the whole data, but large enough |
|
193 |
to detect the header) contains a valid GIF image header" |
|
194 |
||
195 |
|id| |
|
196 |
||
197 |
id := (someHeaderBytes copyFrom:1 to:6) asString. |
|
198 |
(id = 'GIF87a') ifFalse:[ |
|
199 |
id = 'GIF89a' ifFalse:[ |
|
200 |
(id startsWith:'GIF') ifFalse:[^ false]. |
|
201 |
'GIFReader [info]: not a GIF87a/GIF89a file - hope that works' infoPrintCR. |
|
202 |
] |
|
203 |
]. |
|
204 |
^ true |
|
205 |
||
206 |
"Modified: 10.1.1997 / 15:40:34 / cg" |
|
207 |
! |
|
208 |
||
1 | 209 |
isValidImageFile:aFileName |
210 |
"return true, if aFileName contains a GIF image" |
|
211 |
||
4348 | 212 |
|someBytes inStream| |
1 | 213 |
|
9 | 214 |
inStream := self streamReadingFile:aFileName. |
1 | 215 |
inStream isNil ifTrue:[^ false]. |
4348 | 216 |
someBytes := inStream nextBytes:6. |
1 | 217 |
inStream close. |
4348 | 218 |
^ self hasValidImageHeader:someBytes |
174 | 219 |
|
355 | 220 |
"Modified: 10.1.1997 / 15:40:34 / cg" |
1 | 221 |
! ! |
222 |
||
2694 | 223 |
!GIFReader methodsFor:'image reading'! |
154 | 224 |
|
2596 | 225 |
fromStream:aStream |
226 |
"read a stream containing a GIF image (or an image sequence). |
|
227 |
Leave image description in instance variables." |
|
228 |
||
3392 | 229 |
|byte flag fileColorMap |
2596 | 230 |
colorMapSize bitsPerPixel scrWidth scrHeight |
3392 | 231 |
hasColorMap interlaced id |
232 |
isGif89 atEnd |
|
2596 | 233 |
img firstImage firstOffset firstFrameDelay frame imageCount| |
234 |
||
235 |
inStream := aStream. |
|
236 |
aStream binary. |
|
237 |
||
238 |
"GIF-files are always lsb (intel-world)" |
|
239 |
byteOrder := #lsb. |
|
240 |
||
241 |
id := ByteArray new:6. |
|
242 |
(aStream nextBytes:6 into:id startingAt:1) ~~ 6 ifTrue:[ |
|
3553 | 243 |
^ self fileFormatError:'not a gif file (short read)'. |
2596 | 244 |
]. |
245 |
id := id asString. |
|
246 |
||
247 |
"all I had for testing where GIF87a files; |
|
248 |
I hope later versions work too ..." |
|
249 |
||
250 |
isGif89 := false. |
|
251 |
(id ~= 'GIF87a') ifTrue:[ |
|
3553 | 252 |
(id startsWith:'GIF') ifFalse:[ |
253 |
^ self fileFormatError:('not a gif file (id=''' , id , ''')'). |
|
254 |
]. |
|
255 |
id ~= 'GIF89a' ifTrue:[ |
|
256 |
'GIFReader [info]: not a GIF87a/GIF89a file - hope that works' infoPrintCR. |
|
257 |
] |
|
2596 | 258 |
]. |
259 |
||
260 |
"get screen dimensions (not used)" |
|
3553 | 261 |
scrWidth := aStream nextInt16MSB:false. |
262 |
scrHeight := aStream nextInt16MSB:false. |
|
2596 | 263 |
|
264 |
"get flag byte" |
|
265 |
flag := aStream nextByte. |
|
266 |
hasColorMap := (flag bitAnd:2r10000000) ~~ 0. |
|
267 |
"bitsPerRGB := ((flag bitAnd:2r01110000) bitShift:-4) + 1. " |
|
268 |
"colorMapSorted := ((flag bitAnd:2r00001000) ~~ 0. " |
|
269 |
bitsPerPixel := (flag bitAnd:2r00000111) + 1. |
|
270 |
colorMapSize := 1 bitShift:bitsPerPixel. |
|
271 |
||
272 |
"get background (not used)" |
|
273 |
aStream nextByte. |
|
274 |
||
275 |
"aspect ratio (not used)" |
|
276 |
aStream nextByte. |
|
277 |
||
278 |
"get colorMap" |
|
279 |
hasColorMap ifTrue:[ |
|
3553 | 280 |
fileColorMap := self readColorMap:colorMapSize. |
2596 | 281 |
]. |
282 |
colorMap := fileColorMap. |
|
283 |
||
284 |
photometric := #palette. |
|
285 |
samplesPerPixel := 1. |
|
286 |
bitsPerSample := #(8). |
|
287 |
||
288 |
imageCount := 0. |
|
289 |
atEnd := false. |
|
290 |
[atEnd] whileFalse:[ |
|
3553 | 291 |
"gif89a extensions" |
2596 | 292 |
|
3553 | 293 |
byte := aStream nextByte. |
294 |
byte isNil ifTrue:[ |
|
295 |
"/ atEnd-Terminator missing |
|
296 |
atEnd := true |
|
297 |
] ifFalse:[ |
|
298 |
byte == Extension ifTrue:[ |
|
3901 | 299 |
"/ 'Ext' infoPrintCR. |
3553 | 300 |
self readExtension:aStream. |
301 |
] ifFalse:[ |
|
302 |
(byte == Terminator) ifTrue:[ |
|
303 |
atEnd := true |
|
304 |
] ifFalse:[ |
|
305 |
"must be image separator" |
|
306 |
(byte ~~ ImageSeparator) ifTrue:[ |
|
307 |
^ self fileFormatError:('corrupted gif file (no IMAGESEP): ' , (byte printStringRadix:16)). |
|
308 |
]. |
|
3901 | 309 |
"/ 'Img' infoPrintCR. |
2596 | 310 |
|
3553 | 311 |
fileColorMap notNil ifTrue:[ |
312 |
colorMap := fileColorMap. |
|
313 |
]. |
|
314 |
Object primitiveFailureSignal handle:[:ex | |
|
315 |
^ self fileFormatError:('corrupted gif file'). |
|
316 |
] do:[ |
|
317 |
self readImage:aStream. |
|
318 |
]. |
|
2596 | 319 |
|
3553 | 320 |
maskPixel notNil ifTrue:[ |
321 |
"/ |
|
322 |
"/ ok, there is a maskValue |
|
323 |
"/ build a Depth1Image for it. |
|
324 |
"/ |
|
325 |
self buildMaskFromColor:maskPixel |
|
326 |
]. |
|
2596 | 327 |
|
3553 | 328 |
imageCount == 0 ifTrue:[ |
329 |
img := self makeImage. |
|
330 |
"/ remember first image in case more come later. |
|
331 |
firstImage := img. |
|
332 |
firstFrameDelay := frameDelay. |
|
333 |
firstOffset := (leftOffs @ topOffs). |
|
334 |
] ifFalse:[ |
|
335 |
imageCount == 1 ifTrue:[ |
|
336 |
imageSequence := ImageSequence new. |
|
337 |
img imageSequence:imageSequence. |
|
2596 | 338 |
|
3553 | 339 |
"/ add frame for first image. |
340 |
frame := ImageFrame new image:firstImage. |
|
341 |
frame delay:firstFrameDelay. |
|
342 |
frame offset:firstOffset. |
|
343 |
imageSequence add:frame. |
|
344 |
]. |
|
345 |
img := self makeImage. |
|
346 |
img imageSequence:imageSequence. |
|
2596 | 347 |
|
3553 | 348 |
"/ add frame for this image. |
349 |
frame := ImageFrame new image:img. |
|
350 |
frame delay:frameDelay. |
|
351 |
frame offset:(leftOffs @ topOffs). |
|
352 |
imageSequence add:frame. |
|
353 |
]. |
|
2596 | 354 |
|
3553 | 355 |
imageCount := imageCount + 1. |
2596 | 356 |
|
3553 | 357 |
frameDelay := nil. |
2596 | 358 |
|
3553 | 359 |
aStream atEnd ifTrue:[ |
360 |
atEnd := true. |
|
361 |
] |
|
362 |
] |
|
363 |
]. |
|
364 |
]. |
|
2596 | 365 |
]. |
366 |
||
367 |
imageSequence notNil ifTrue:[ |
|
3553 | 368 |
iterationCount notNil ifTrue:[ |
369 |
iterationCount == 0 ifTrue:[ |
|
370 |
imageSequence loop:true. |
|
371 |
] ifFalse:[ |
|
372 |
imageSequence loop:false. |
|
373 |
imageSequence iterationCount:iterationCount. |
|
374 |
] |
|
375 |
] |
|
2596 | 376 |
]. |
377 |
||
378 |
" |
|
379 |
Image fromFile:'/home/cg/work/stx/goodies/bitmaps/gifImages/animated/vrml.gif' |
|
380 |
Image fromFile:'/home/cg/work/stx/goodies/bitmaps/gifImages/animated/arrow.gif' |
|
381 |
" |
|
382 |
||
3901 | 383 |
"Modified: / 05-07-1996 / 17:32:01 / stefan" |
384 |
"Modified: / 21-08-1998 / 22:20:00 / cg" |
|
385 |
"Modified (format): / 17-02-2017 / 12:29:10 / cg" |
|
2694 | 386 |
! ! |
387 |
||
388 |
!GIFReader methodsFor:'private-reading'! |
|
389 |
||
390 |
makeGreyscale |
|
391 |
"not yet implemented/needed" |
|
392 |
! |
|
393 |
||
394 |
readColorMap:colorMapSize |
|
395 |
"get a gif colormap consisting of colorMapSize entries" |
|
396 |
||
397 |
|rgbVector| |
|
398 |
||
399 |
rgbVector := inStream nextBytes:colorMapSize*3. |
|
3392 | 400 |
^ MappedPalette rgbBytesVector:rgbVector |
2596 | 401 |
! |
402 |
||
154 | 403 |
readExtension:aStream |
882 | 404 |
"get gif89 extension" |
154 | 405 |
|
406 |
|type blockSize subBlockSize |
|
304 | 407 |
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
|
408 |
animationType animationTime animationMask |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
409 |
appID appAUTH |
888 | 410 |
b ok| |
154 | 411 |
|
412 |
type := aStream nextByte. |
|
1952
5009754ae556
Use #codePoint instead of deprecated #asciiValue
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
413 |
type == $R codePoint ifTrue:[ |
3553 | 414 |
"/ |
415 |
"/ Ratio extension |
|
416 |
"/ |
|
417 |
"/ 'GIFREADER [info]: ratio extension ignored' infoPrintCR. |
|
418 |
blockSize := aStream nextByte. |
|
419 |
(blockSize == 2) ifTrue:[ |
|
420 |
aspNum := aStream nextByte. |
|
421 |
aspDen := aStream nextByte |
|
422 |
] ifFalse:[ |
|
423 |
aStream skip:blockSize |
|
424 |
]. |
|
2844 | 425 |
|
3553 | 426 |
"/ eat subblocks |
427 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
|
428 |
aStream skip:subBlockSize |
|
429 |
]. |
|
430 |
^ self |
|
239 | 431 |
]. |
432 |
||
433 |
type == 16r01 ifTrue:[ |
|
3553 | 434 |
"/ |
435 |
"/ plaintext extension |
|
436 |
"/ |
|
437 |
"/ 'GIFREADER [info]: plaintext extension ignored' infoPrintCR. |
|
438 |
subBlockSize := aStream nextByte. |
|
439 |
left := aStream nextInt16MSB:false. |
|
440 |
top := aStream nextInt16MSB:false. |
|
441 |
width := aStream nextInt16MSB:false. |
|
442 |
height := aStream nextInt16MSB:false. |
|
443 |
cWidth := aStream nextByte. |
|
444 |
cHeight := aStream nextByte. |
|
445 |
fg := aStream nextByte. |
|
446 |
bg := aStream nextByte. |
|
447 |
aStream skip:12. |
|
448 |
"/ eat subblocks |
|
449 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
|
450 |
aStream skip:subBlockSize |
|
451 |
]. |
|
452 |
^ self |
|
239 | 453 |
]. |
454 |
||
455 |
type == 16rF9 ifTrue:[ |
|
3553 | 456 |
"/ |
457 |
"/ graphic control extension |
|
458 |
"/ |
|
459 |
"/ '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
|
460 |
|
3553 | 461 |
[(subBlockSize := aStream nextByte) ~~ 0 and:[subBlockSize notNil]] whileTrue:[ |
462 |
"/ type bitAnd:1 means: animationMask is transparent pixel |
|
463 |
"/ to be implemented in Image ... |
|
305 | 464 |
|
3553 | 465 |
animationType := aStream nextByte. |
466 |
animationTime := aStream nextInt16MSB:false. |
|
467 |
animationMask := aStream nextByte. |
|
2595
37dc71e1e59b
be tolerant to corrupted gif files (subblock size in extension)
Claus Gittinger <cg@exept.de>
parents:
2056
diff
changeset
|
468 |
|
3553 | 469 |
subBlockSize := subBlockSize - 4. |
307
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
470 |
|
3553 | 471 |
(animationType bitTest: 1) ifTrue:[ |
472 |
maskPixel := animationMask. |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
473 |
"/ 'GIFREADER [info]: mask: ' infoPrint. (maskPixel printStringRadix:16) infoPrintCR. |
3553 | 474 |
]. |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
475 |
"/ 'GIFREADER [info]: animationTime: ' infoPrint. (animationTime * (1/100)) infoPrintCR. |
1429 | 476 |
"/ 'GIFREADER [info]: animationType: ' infoPrint. (animationType) infoPrintCR. |
477 |
"/ 'GIFREADER [info]: animationMask: ' infoPrint. (animationMask) infoPrintCR. |
|
307
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
478 |
|
3553 | 479 |
frameDelay := (animationTime * (1/100)) * 1000. |
888 | 480 |
|
3553 | 481 |
subBlockSize ~~ 0 ifTrue:[ |
482 |
aStream skip:subBlockSize |
|
483 |
]. |
|
484 |
]. |
|
485 |
^ self |
|
239 | 486 |
]. |
487 |
||
519 | 488 |
type == 16rFE ifTrue:[ |
3553 | 489 |
"/ |
490 |
"/ comment extension |
|
491 |
"/ |
|
492 |
"/ 'GIFREADER [info]: comment extension' infoPrintCR. |
|
493 |
[(blockSize := aStream nextByte) ~~ 0] whileTrue:[ |
|
494 |
aStream skip:blockSize |
|
495 |
]. |
|
496 |
^ self |
|
519 | 497 |
]. |
498 |
||
239 | 499 |
type == 16rFF ifTrue:[ |
3553 | 500 |
"/ |
501 |
"/ application extension |
|
502 |
"/ |
|
503 |
"/ 'GIFREADER [info]: application extension' infoPrintCR. |
|
504 |
subBlockSize := aStream nextByte. |
|
505 |
appID := (aStream nextBytes:8 ) asString. |
|
506 |
appAUTH := aStream nextBytes:3. |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
507 |
|
3553 | 508 |
subBlockSize := aStream nextByte. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
509 |
|
3553 | 510 |
ok := false. |
511 |
appID = 'NETSCAPE' ifTrue:[ |
|
512 |
appAUTH asString = '2.0' ifTrue:[ |
|
513 |
subBlockSize == 3 ifTrue:[ |
|
514 |
b := aStream nextByte. |
|
515 |
iterationCount := aStream nextInt16MSB:false. |
|
516 |
subBlockSize := aStream nextByte. |
|
517 |
ok := true. |
|
888 | 518 |
"/ ('GIFREADER [info]: NETSCAPE application extension - iterationCount = ') infoPrint. |
519 |
"/ iterationCount infoPrintCR. |
|
3553 | 520 |
] |
521 |
] |
|
522 |
]. |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
523 |
|
3553 | 524 |
ok ifFalse:[ |
525 |
('GIFREADER [info]: application extension (' , appID , ') ignored') infoPrintCR. |
|
526 |
]. |
|
625 | 527 |
|
3553 | 528 |
[subBlockSize > 0] whileTrue:[ |
529 |
aStream skip:subBlockSize. |
|
530 |
subBlockSize := aStream nextByte. |
|
531 |
]. |
|
532 |
^ self |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
533 |
]. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
534 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
535 |
type == 16r2C ifTrue:[ |
3553 | 536 |
"/ |
537 |
"/ image descriptor extension |
|
538 |
"/ |
|
539 |
"/ 'GIFREADER [info]: image descriptor extension ignored' infoPrintCR. |
|
540 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
|
541 |
aStream skip:subBlockSize |
|
542 |
]. |
|
543 |
^ self |
|
239 | 544 |
]. |
545 |
||
546 |
"/ |
|
547 |
"/ unknown extension |
|
548 |
"/ |
|
354
b4d2ce853c24
stefans suggested info/warning/error/fatal
Claus Gittinger <cg@exept.de>
parents:
339
diff
changeset
|
549 |
'GIFREADER [info]: unknown extension ignored' infoPrintCR. |
239 | 550 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
3553 | 551 |
aStream skip:subBlockSize |
154 | 552 |
] |
553 |
||
2844 | 554 |
"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
|
555 |
! |
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 |
readImage:aStream |
882 | 558 |
"read a single image from aStream." |
559 |
||
1429 | 560 |
| 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
|
561 |
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
|
562 |
initialBuffSize| |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
563 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
564 |
"get image data" |
3553 | 565 |
leftOffs := aStream nextInt16MSB:false. |
566 |
topOffs := aStream nextInt16MSB:false. |
|
3901 | 567 |
"/ 'GIFReader: leftOffs ' infoPrint. leftOffs infoPrintCR. |
568 |
"/ 'GIFReader: topOffs ' infoPrint. topOffs infoPrintCR. |
|
3553 | 569 |
width := aStream nextInt16MSB:false. |
570 |
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
|
571 |
|
1846 | 572 |
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
|
573 |
|
3901 | 574 |
"/ |
575 |
"/ 'width ' print. width printNewline. |
|
576 |
"/ 'height ' print. height printNewline. |
|
577 |
"/ |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
578 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
579 |
"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
|
580 |
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
|
581 |
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
|
582 |
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
|
583 |
"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
|
584 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
585 |
"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
|
586 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
587 |
hasLocalColorMap ifTrue:[ |
3553 | 588 |
"local descr. overwrites" |
589 |
bitsPerPixel := (flag bitAnd:2r00000111) + 1. |
|
590 |
colorMapSize := 1 bitShift:bitsPerPixel. |
|
591 |
"overwrite colormap" |
|
592 |
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
|
593 |
]. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
594 |
|
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 |
"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
|
597 |
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
|
598 |
(aStream respondsTo:#fileSize) ifTrue:[ |
3553 | 599 |
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
|
600 |
] ifFalse:[ |
3553 | 601 |
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
|
602 |
]. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
603 |
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
|
604 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
605 |
"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
|
606 |
index := 1. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
607 |
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
|
608 |
[count notNil and:[count ~~ 0]] whileTrue:[ |
3553 | 609 |
(compressedData size < (index+count)) ifTrue:[ |
610 |
|t| |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
611 |
|
3784 | 612 |
t := ByteArray uninitializedNew:((index+count)*3//2). |
3553 | 613 |
t replaceBytesFrom:1 to:index-1 with:compressedData startingAt:1. |
614 |
compressedData := t. |
|
615 |
]. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
616 |
|
3553 | 617 |
aStream nextBytes:count into:compressedData startingAt:index blockSize:4096. |
618 |
index := index + count. |
|
619 |
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
|
620 |
]. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
621 |
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
|
622 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
623 |
h := height. |
1037
90d38c8e3f5f
use initialized byteArrays, in case rows are padded
Claus Gittinger <cg@exept.de>
parents:
891
diff
changeset
|
624 |
data := ByteArray new:((width + 1) * (h + 1)). |
3901 | 625 |
"/ '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
|
626 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
627 |
self class decompressGIFFrom:compressedData |
3553 | 628 |
count:compressedSize |
629 |
into:data |
|
630 |
startingAt:1 |
|
631 |
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
|
632 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
633 |
interlaced ifTrue:[ |
3901 | 634 |
"/ 'GIFREADER: deinterlacing ...' infoPrintCR. |
3553 | 635 |
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
|
636 |
|
3553 | 637 |
"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
|
638 |
|
3553 | 639 |
srcOffset := 1. |
640 |
0 to:(h - 1) by:8 do:[:dstRow | |
|
641 |
dstOffset := dstRow * width + 1. |
|
642 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
|
643 |
with:data startingAt:srcOffset. |
|
644 |
srcOffset := srcOffset + width. |
|
645 |
]. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
646 |
|
3553 | 647 |
"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
|
648 |
|
3553 | 649 |
4 to:(h - 1) by:8 do:[:dstRow | |
650 |
dstOffset := dstRow * width + 1. |
|
651 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
|
652 |
with:data startingAt:srcOffset. |
|
653 |
srcOffset := srcOffset + width. |
|
654 |
]. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
655 |
|
3553 | 656 |
"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
|
657 |
|
3553 | 658 |
2 to:(h - 1) by:4 do:[:dstRow | |
659 |
dstOffset := dstRow * width + 1. |
|
660 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
|
661 |
with:data startingAt:srcOffset. |
|
662 |
srcOffset := srcOffset + width. |
|
663 |
]. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
664 |
|
3553 | 665 |
"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
|
666 |
|
3553 | 667 |
1 to:(h - 1) by:2 do:[:dstRow | |
668 |
dstOffset := dstRow * width + 1. |
|
669 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
|
670 |
with:data startingAt:srcOffset. |
|
671 |
srcOffset := srcOffset + width. |
|
672 |
]. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
673 |
|
3553 | 674 |
data := tmp. |
675 |
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
|
676 |
]. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
677 |
|
3784 | 678 |
"Created: / 13-01-1998 / 10:44:05 / cg" |
679 |
"Modified: / 12-08-1998 / 13:55:32 / cg" |
|
3901 | 680 |
"Modified (format): / 17-02-2017 / 12:29:44 / cg" |
0 | 681 |
! ! |
43 | 682 |
|
1805 | 683 |
!GIFReader methodsFor:'private-writing'! |
684 |
||
685 |
assignTransparentPixelIn:image |
|
686 |
"find an usused pixelValue in the colorMap (or image)." |
|
687 |
||
3689 | 688 |
|cmap cmapSize imgDepth usedPixelValues unusedValues| |
1805 | 689 |
|
3689 | 690 |
cmap := image colorMap. |
691 |
cmapSize := cmap size. |
|
692 |
((cmapSize > 0) and:[cmapSize < 256]) ifTrue:[ |
|
693 |
maskPixel := cmapSize. |
|
694 |
^ self |
|
1805 | 695 |
]. |
3689 | 696 |
|
3009 | 697 |
image compressColorMap. |
3689 | 698 |
imgDepth := image depth. |
1805 | 699 |
usedPixelValues := image usedValues. |
3689 | 700 |
usedPixelValues size < (1 bitShift:imgDepth) ifFalse:[ |
701 |
Image informationLostQuerySignal |
|
702 |
raiseRequestWith:image |
|
703 |
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
|
704 |
]. |
b7eea50081f2
fixed transparency-pixel bug (with full colormap)
Claus Gittinger <cg@exept.de>
parents:
2032
diff
changeset
|
705 |
|
b7eea50081f2
fixed transparency-pixel bug (with full colormap)
Claus Gittinger <cg@exept.de>
parents:
2032
diff
changeset
|
706 |
"/ there must be an unused pixelValue |
3689 | 707 |
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
|
708 |
unusedValues removeAll:usedPixelValues. |
b7eea50081f2
fixed transparency-pixel bug (with full colormap)
Claus Gittinger <cg@exept.de>
parents:
2032
diff
changeset
|
709 |
|
b7eea50081f2
fixed transparency-pixel bug (with full colormap)
Claus Gittinger <cg@exept.de>
parents:
2032
diff
changeset
|
710 |
maskPixel := unusedValues first. |
3009 | 711 |
|
712 |
"Modified: / 17-07-2012 / 12:25:24 / anwild" |
|
1805 | 713 |
! |
714 |
||
715 |
checkCodeSize |
|
716 |
(freeCode > maxCode and: [codeSize < 12]) |
|
3392 | 717 |
ifTrue: |
1805 | 718 |
[codeSize := codeSize + 1. |
719 |
maxCode := (1 bitShift: codeSize) - 1] |
|
720 |
||
721 |
"Created: 14.10.1997 / 18:42:01 / cg" |
|
722 |
! |
|
723 |
||
724 |
flushBits |
|
4341 | 725 |
remainBitCount == 0 |
726 |
ifFalse: |
|
727 |
[self nextBytePut: bufByte. |
|
728 |
remainBitCount := 0]. |
|
729 |
self flushBuffer |
|
1805 | 730 |
|
731 |
"Modified: 14.10.1997 / 18:58:06 / cg" |
|
732 |
! |
|
733 |
||
734 |
flushBuffer |
|
735 |
bufStream isEmpty ifTrue: [^ self]. |
|
736 |
outStream nextPut: bufStream size. |
|
737 |
outStream nextPutAll: bufStream contents. |
|
738 |
bufStream := WriteStream on: (ByteArray new: 256) |
|
739 |
||
740 |
"Modified: 14.10.1997 / 20:46:04 / cg" |
|
741 |
! |
|
742 |
||
743 |
flushCode |
|
744 |
self flushBits |
|
745 |
||
746 |
"Created: 14.10.1997 / 18:57:33 / cg" |
|
747 |
! |
|
748 |
||
749 |
nextBitsPut: anInteger |
|
4341 | 750 |
| integer writeBitCount shiftCount | |
751 |
shiftCount := 0. |
|
752 |
remainBitCount == 0 |
|
753 |
ifTrue: |
|
754 |
[writeBitCount := 8. |
|
755 |
integer := anInteger] |
|
756 |
ifFalse: |
|
757 |
[writeBitCount := remainBitCount. |
|
758 |
integer := bufByte + (anInteger bitShift: 8 - remainBitCount)]. |
|
759 |
[writeBitCount < codeSize] |
|
760 |
whileTrue: |
|
761 |
[self nextBytePut: ((integer bitShift: shiftCount) bitAnd: 255). |
|
762 |
shiftCount := shiftCount - 8. |
|
763 |
writeBitCount := writeBitCount + 8]. |
|
764 |
(remainBitCount := writeBitCount - codeSize) = 0 |
|
765 |
ifTrue: [self nextBytePut: (integer bitShift: shiftCount)] |
|
766 |
ifFalse: [bufByte := integer bitShift: shiftCount]. |
|
767 |
^anInteger |
|
1805 | 768 |
|
769 |
"Modified: 15.10.1997 / 16:50:30 / cg" |
|
770 |
! |
|
771 |
||
772 |
nextBytePut: aByte |
|
773 |
bufStream nextPut: aByte. |
|
774 |
bufStream size >= 254 ifTrue: [self flushBuffer] |
|
775 |
||
776 |
"Modified: 15.10.1997 / 16:50:52 / cg" |
|
777 |
! |
|
778 |
||
3392 | 779 |
readPixelFrom: bits |
1805 | 780 |
| pixel | |
781 |
ypos >= height ifTrue: [^ nil]. |
|
3392 | 782 |
(maskPixel notNil |
1805 | 783 |
and:[(mask pixelAtX:xpos y:ypos) == 0]) ifTrue:[ |
784 |
pixel := maskPixel |
|
785 |
] ifFalse:[ |
|
786 |
pixel := bits at: ypos * rowByteSize + xpos + 1. |
|
787 |
]. |
|
788 |
self updatePixelPosition. |
|
789 |
^ pixel |
|
790 |
||
791 |
"Created: 14.10.1997 / 18:43:50 / cg" |
|
792 |
"Modified: 15.10.1997 / 16:46:43 / cg" |
|
793 |
! |
|
794 |
||
3392 | 795 |
setParameters:bitsPerPixel |
1805 | 796 |
clearCode := 1 bitShift:bitsPerPixel. |
797 |
eoiCode := clearCode + 1. |
|
798 |
freeCode := clearCode + 2. |
|
799 |
codeSize := bitsPerPixel + 1. |
|
800 |
maxCode := (1 bitShift: codeSize) - 1 |
|
801 |
||
802 |
"Modified: 14.10.1997 / 20:09:48 / cg" |
|
803 |
! |
|
804 |
||
805 |
updatePixelPosition |
|
806 |
(xpos := xpos + 1) >= width ifFalse: [^ self]. |
|
807 |
||
808 |
xpos := 0. |
|
809 |
interlace == true ifFalse:[ |
|
3392 | 810 |
ypos := ypos + 1. |
811 |
^ self |
|
1805 | 812 |
]. |
813 |
||
814 |
pass == 0 ifTrue:[ |
|
3392 | 815 |
(ypos := ypos + 8) >= height ifTrue:[ |
816 |
pass := pass + 1. |
|
817 |
ypos := 4 |
|
818 |
]. |
|
819 |
^ self |
|
1805 | 820 |
]. |
821 |
pass == 1 ifTrue:[ |
|
3392 | 822 |
(ypos := ypos + 8) >= height ifTrue:[ |
823 |
pass := pass + 1. |
|
824 |
ypos := 2 |
|
825 |
]. |
|
826 |
^ self |
|
1805 | 827 |
]. |
828 |
pass == 2 ifTrue:[ |
|
3392 | 829 |
(ypos := ypos + 4) >= height ifTrue:[ |
830 |
pass := pass + 1. |
|
831 |
ypos := 1 |
|
832 |
]. |
|
833 |
^ self |
|
1805 | 834 |
]. |
835 |
pass == 3 ifTrue:[ |
|
3392 | 836 |
ypos := ypos + 2. |
837 |
^ self |
|
1805 | 838 |
]. |
839 |
^ self error: 'can''t happen' |
|
840 |
||
841 |
"Modified: 14.10.1997 / 18:44:27 / cg" |
|
842 |
! |
|
843 |
||
2032 | 844 |
writeBitDataFor: image |
845 |
"using modified Lempel-Ziv Welch algorithm." |
|
846 |
||
847 |
| bits bitsPerPixel t1 |
|
3849 | 848 |
maxBits maxMaxCode tSize initCodeSize ent tShift fCode |
849 |
pixel |
|
850 |
index disp nomatch | |
|
2032 | 851 |
|
852 |
outStream nextPut:ImageSeparator. |
|
3282 | 853 |
self writeShort:0. "/ leftOffs |
854 |
self writeShort:0. "/ topOffs |
|
2032 | 855 |
self writeShort:width. "/ image size |
856 |
self writeShort:height. |
|
857 |
||
858 |
interlace == true ifTrue:[ |
|
3848 | 859 |
t1 := 64 |
2032 | 860 |
] ifFalse:[ |
3848 | 861 |
t1 := 0 "/ no local colormap |
2032 | 862 |
]. |
863 |
outStream nextPut:t1. "/ another flag |
|
864 |
||
865 |
bitsPerPixel := image bitsPerPixel. |
|
866 |
bits := image bits. |
|
867 |
||
868 |
pass := 0. |
|
869 |
xpos := 0. |
|
870 |
ypos := 0. |
|
871 |
rowByteSize := image bytesPerRow. "/ width * 8 + 31 // 32 * 4. |
|
872 |
remainBitCount := 0. |
|
873 |
bufByte := 0. |
|
874 |
bufStream := WriteStream on: (ByteArray new: 256). |
|
875 |
||
876 |
maxBits := 12. |
|
877 |
maxMaxCode := 1 bitShift: maxBits. |
|
878 |
tSize := 5003. |
|
879 |
prefixTable := Array new: tSize. |
|
3364 | 880 |
suffixTable := Array new: tSize withAll:-1. |
2032 | 881 |
|
882 |
initCodeSize := bitsPerPixel <= 1 ifTrue: [2] ifFalse: [bitsPerPixel]. |
|
883 |
outStream nextPut: initCodeSize. |
|
884 |
self setParameters: initCodeSize. |
|
885 |
||
886 |
tShift := 0. |
|
887 |
fCode := tSize. |
|
2845
62d4128bb923
comment/format in: #writeBitDataFor:
Claus Gittinger <cg@exept.de>
parents:
2844
diff
changeset
|
888 |
[fCode < 65536] whileTrue:[ |
3848 | 889 |
tShift := tShift + 1. |
890 |
fCode := fCode * 2 |
|
2845
62d4128bb923
comment/format in: #writeBitDataFor:
Claus Gittinger <cg@exept.de>
parents:
2844
diff
changeset
|
891 |
]. |
2032 | 892 |
tShift := 8 - tShift. |
3364 | 893 |
"/ 1 to: tSize do: [:i | suffixTable at: i put: -1]. - cg changed initialization above |
2032 | 894 |
|
895 |
self writeCodeAndCheckCodeSize: clearCode. |
|
896 |
ent := self readPixelFrom: bits. |
|
3848 | 897 |
[(pixel := self readPixelFrom: bits) notNil] whileTrue:[ |
898 |
fCode := (pixel bitShift: maxBits) + ent. |
|
899 |
index := ((pixel bitShift: tShift) bitXor: ent) + 1. |
|
900 |
(suffixTable at: index) = fCode |
|
901 |
ifTrue: [ent := prefixTable at: index] |
|
902 |
ifFalse:[ |
|
903 |
nomatch := true. |
|
904 |
(suffixTable at: index) >= 0 |
|
905 |
ifTrue:[ |
|
906 |
disp := tSize - index + 1. |
|
907 |
index = 1 ifTrue: [disp := 1]. |
|
908 |
"probe" |
|
909 |
[(index := index - disp) < 1 ifTrue: [index := index + tSize]. |
|
910 |
(suffixTable at: index) = fCode |
|
911 |
ifTrue:[ |
|
912 |
ent := prefixTable at: index. |
|
913 |
nomatch := false. |
|
914 |
"continue whileFalse:"]. |
|
915 |
nomatch and: [(suffixTable at: index) > 0]] |
|
916 |
whileTrue: ["probe"]]. |
|
917 |
"nomatch" |
|
918 |
nomatch ifTrue:[ |
|
919 |
self writeCodeAndCheckCodeSize: ent. |
|
920 |
ent := pixel. |
|
921 |
freeCode < maxMaxCode |
|
922 |
ifTrue:[ |
|
923 |
prefixTable at: index put: freeCode. |
|
924 |
suffixTable at: index put: fCode. |
|
925 |
freeCode := freeCode + 1] |
|
926 |
ifFalse:[ |
|
927 |
self writeCodeAndCheckCodeSize: clearCode. |
|
928 |
suffixTable from:1 to:tSize put:-1. |
|
929 |
self setParameters: initCodeSize]]]]. |
|
2032 | 930 |
prefixTable := suffixTable := nil. |
931 |
self writeCodeAndCheckCodeSize: ent. |
|
932 |
self writeCodeAndCheckCodeSize: eoiCode. |
|
933 |
self flushCode. |
|
934 |
outStream nextPut: 0. "zero-length packet" |
|
935 |
||
2845
62d4128bb923
comment/format in: #writeBitDataFor:
Claus Gittinger <cg@exept.de>
parents:
2844
diff
changeset
|
936 |
"Modified: / 02-06-2010 / 12:24:20 / cg" |
2032 | 937 |
! |
938 |
||
3392 | 939 |
writeCode: aCode |
1805 | 940 |
self nextBitsPut: aCode |
941 |
||
942 |
"Created: 14.10.1997 / 18:38:35 / cg" |
|
943 |
"Modified: 15.10.1997 / 17:01:47 / cg" |
|
944 |
! |
|
945 |
||
3392 | 946 |
writeCodeAndCheckCodeSize: t1 |
1805 | 947 |
self writeCode: t1. |
948 |
self checkCodeSize |
|
949 |
||
950 |
"Created: 14.10.1997 / 18:38:24 / cg" |
|
951 |
"Modified: 14.10.1997 / 18:40:56 / cg" |
|
2032 | 952 |
! |
953 |
||
954 |
writeHeaderFor:image |
|
955 |
"write the gif header" |
|
956 |
||
957 |
|bitsPerPixel t1| |
|
958 |
||
959 |
bitsPerPixel := image bitsPerPixel. |
|
960 |
||
961 |
outStream nextPutAll: 'GIF89a' asByteArray. |
|
962 |
self writeShort:width. "/ screen size |
|
3392 | 963 |
self writeShort:height. |
2032 | 964 |
t1 := 128. |
965 |
t1 := t1 bitOr:(bitsPerPixel - 1 bitShift:5). |
|
966 |
t1 := t1 bitOr:(bitsPerPixel - 1). |
|
967 |
outStream nextPut:t1. "/ flag |
|
968 |
outStream nextPut:0. "/ background (not used) |
|
969 |
outStream nextPut:0. "/ aspect ratio |
|
970 |
||
971 |
0 to:(1 bitShift:bitsPerPixel)-1 do:[:pixel | |
|
3392 | 972 |
|clr red green blue| |
2032 | 973 |
|
3392 | 974 |
clr := image colorFromValue:pixel. |
975 |
clr isNil ifTrue:[ |
|
976 |
"/ unused colorMap slot |
|
977 |
red := green := blue := 0. |
|
978 |
] ifFalse:[ |
|
979 |
red := (clr redByte). |
|
980 |
green := (clr greenByte). |
|
981 |
blue := (clr blueByte). |
|
982 |
]. |
|
983 |
outStream |
|
984 |
nextPut:red; nextPut:green; nextPut:blue. |
|
985 |
]. |
|
2032 | 986 |
|
987 |
"Created: / 14.10.1997 / 17:41:28 / cg" |
|
988 |
"Modified: / 31.10.1997 / 16:12:13 / cg" |
|
989 |
! |
|
990 |
||
991 |
writeMaskExtensionHeaderFor:image |
|
992 |
"write an extension header for the transparent pixel" |
|
993 |
||
994 |
outStream nextPut:Extension. |
|
995 |
outStream nextPut:16rF9. "/ graphic control extension |
|
996 |
outStream nextPut:4. "/ subBlockSize |
|
997 |
||
998 |
outStream nextPut:1. "/ animationType |
|
3658 | 999 |
outStream nextPutInt16LSB:1. "/ animationTime |
2032 | 1000 |
outStream nextPut:maskPixel. "/ animationMask |
1001 |
||
1002 |
outStream nextPut:0. |
|
1805 | 1003 |
! ! |
1004 |
||
708 | 1005 |
!GIFReader methodsFor:'writing to file'! |
1006 |
||
1007 |
save:image onFile:aFileName |
|
1008 |
"save image as GIF file on aFileName" |
|
1009 |
||
1813
aa74c7bd22ad
image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents:
1811
diff
changeset
|
1010 |
|convertedImage| |
1206
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1011 |
|
718
41ade132da98
raise an error, if depth is not supported.
Claus Gittinger <cg@exept.de>
parents:
714
diff
changeset
|
1012 |
image depth ~~ 8 ifTrue:[ |
3392 | 1013 |
Error handle:[:ex | |
1014 |
^ Image cannotRepresentImageSignal |
|
1015 |
raiseWith:image |
|
1016 |
errorString:('GIF failed to convert image to depth8: ', ex description). |
|
1017 |
"/ ex return. |
|
1018 |
] do:[ |
|
1019 |
convertedImage := Image newForDepth:8. |
|
1020 |
convertedImage fromImage:image photometric:#palette. |
|
1021 |
]. |
|
1022 |
convertedImage notNil ifTrue:[ |
|
1023 |
^ self save:convertedImage onFile:aFileName |
|
1024 |
]. |
|
1025 |
^ Image cannotRepresentImageSignal |
|
1026 |
raiseWith:image |
|
1027 |
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
|
1028 |
]. |
41ade132da98
raise an error, if depth is not supported.
Claus Gittinger <cg@exept.de>
parents:
714
diff
changeset
|
1029 |
|
1813
aa74c7bd22ad
image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents:
1811
diff
changeset
|
1030 |
super save:image onFile:aFileName. |
1206
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 |
|i| |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1034 |
|
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1035 |
i := Image fromFile:'bitmaps/gifImages/garfield.gif'. |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1036 |
GIFReader save:i onFile:'foo.gif'. |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1037 |
(Image fromFile:'./foo.gif') inspect |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1038 |
" |
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 |
"Created: / 14.10.1997 / 17:40:12 / cg" |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1041 |
"Modified: / 27.10.1997 / 22:42:31 / cg" |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1042 |
! |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1043 |
|
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1044 |
save:image onStream:aStream |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1045 |
"save image in GIF-file-format onto aStream" |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1046 |
|
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1047 |
image depth ~~ 8 ifTrue:[ |
3553 | 1048 |
^ Image cannotRepresentImageSignal |
1049 |
raiseWith:image |
|
1050 |
errorString:('GIF (currently) only supports depth8 images'). |
|
1206
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1051 |
]. |
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1052 |
|
fe2354feee01
separated image writer for stream writing
Claus Gittinger <cg@exept.de>
parents:
1052
diff
changeset
|
1053 |
outStream := aStream. |
708 | 1054 |
outStream binary. |
1055 |
||
714 | 1056 |
mask := image mask. |
1057 |
mask notNil ifTrue:[ |
|
3553 | 1058 |
self assignTransparentPixelIn:image |
708 | 1059 |
]. |
1060 |
||
1061 |
byteOrder := #lsb. |
|
1062 |
width := image width. |
|
1063 |
height := image height. |
|
1064 |
photometric := image photometric. |
|
1065 |
samplesPerPixel := image samplesPerPixel. |
|
1066 |
bitsPerSample := image bitsPerSample. |
|
1067 |
colorMap := image colorMap. |
|
1068 |
data := image bits. |
|
1069 |
||
1070 |
self writeHeaderFor:image. |
|
714 | 1071 |
maskPixel notNil ifTrue:[ |
3553 | 1072 |
self writeMaskExtensionHeaderFor:image. |
714 | 1073 |
]. |
1074 |
||
708 | 1075 |
self writeBitDataFor:image. |
714 | 1076 |
|
3282 | 1077 |
image imageSequence notEmptyOrNil ifTrue:[ |
3553 | 1078 |
image imageSequence do:[:eachFrame | |
1079 |
outStream nextPut:Extension. |
|
1080 |
outStream nextPut:16rF9. "/ graphic control extension |
|
1081 |
outStream nextPut:4. "/ sub block size |
|
1082 |
outStream nextPut:0. "/ animation type 0 |
|
3658 | 1083 |
outStream nextPutInt16LSB:(eachFrame delay / 10) rounded asInteger. |
3553 | 1084 |
outStream nextPut:0. "/ animation mask |
1085 |
outStream nextPut:0. "/ subblock size |
|
3282 | 1086 |
|
3553 | 1087 |
self writeBitDataFor:eachFrame image. |
3392 | 1088 |
|
3553 | 1089 |
]. |
3282 | 1090 |
]. |
1091 |
||
714 | 1092 |
outStream nextPut: Terminator. |
708 | 1093 |
|
1094 |
" |
|
1095 |
|i| |
|
1096 |
||
1097 |
i := Image fromFile:'bitmaps/gifImages/garfield.gif'. |
|
1098 |
GIFReader save:i onFile:'foo.gif'. |
|
1099 |
(Image fromFile:'./foo.gif') inspect |
|
1100 |
" |
|
1101 |
||
718
41ade132da98
raise an error, if depth is not supported.
Claus Gittinger <cg@exept.de>
parents:
714
diff
changeset
|
1102 |
"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
|
1103 |
"Modified: / 27.10.1997 / 22:42:31 / cg" |
708 | 1104 |
! ! |
1105 |
||
339 | 1106 |
!GIFReader class methodsFor:'documentation'! |
135 | 1107 |
|
1108 |
version |
|
3553 | 1109 |
^ '$Header$' |
2844 | 1110 |
! |
1111 |
||
1112 |
version_CVS |
|
3553 | 1113 |
^ '$Header$' |
135 | 1114 |
! ! |
1711
b3ba13167ee7
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1521
diff
changeset
|
1115 |
|
3119 | 1116 |
|
43 | 1117 |
GIFReader initialize! |