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