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