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