author | Claus Gittinger <cg@exept.de> |
Thu, 27 Feb 2020 16:08:38 +0100 | |
changeset 4457 | 8c328436fba9 |
parent 4450 | c07db2570587 |
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 |
4105 | 74 |
BlitImageReader FaceReader JPEGReader MacOSXIconReader |
75 |
PBMReader PCXReader PNGReader |
|
3775 | 76 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
77 |
XBMReader XPMReader XWDReader |
|
220 | 78 |
|
79 |
[author:] |
|
3775 | 80 |
Claus Gittinger |
0 | 81 |
" |
3283 | 82 |
! |
83 |
||
84 |
examples |
|
85 |
" |
|
86 |
saving an animated gif sequence: |
|
87 |
||
88 |
a view showing rainfall: |
|
3392 | 89 |
[exBegin] |
3284 | 90 |
|BG CLR N1 H W v WIND drops gen newDrops draw remove move buffer| |
3283 | 91 |
|
92 |
BG := Color black. |
|
93 |
CLR := Color blue lightened. |
|
94 |
H := 100. |
|
95 |
W := 100. |
|
96 |
N1 := 10. |
|
97 |
WIND := 0. |
|
98 |
drops := OrderedCollection new. |
|
99 |
||
100 |
gen := [:n | ((1 to:n) collect:[:i | Random nextIntegerBetween:1 and:W] as:Set) collect:[:x | x@0]]. |
|
101 |
newDrops := [drops addAll:(gen value:N1)]. |
|
102 |
draw := [buffer fill:BG; paint:CLR. drops do:[:d | buffer displayPoint:d]]. |
|
103 |
remove := [drops := drops reject:[:d | d y > H]]. |
|
104 |
move := [:wind | drops := drops collect:[:d| (d x + wind)\\W @ (d y + 1)]]. |
|
105 |
v := View new openAndWait. |
|
106 |
buffer := Form extent:(v extent) depth:24 onDevice:v device. |
|
107 |
||
108 |
[ |
|
3392 | 109 |
[v shown] whileTrue:[ |
110 |
draw value. |
|
111 |
v displayForm:buffer. |
|
112 |
move value:WIND. |
|
113 |
remove value. |
|
114 |
newDrops value. |
|
115 |
WIND := (WIND+(Random nextBetween:-1 and:1)) clampBetween:-5 and:5. |
|
116 |
Delay waitForSeconds:0.1. |
|
117 |
] |
|
3283 | 118 |
] fork. |
3392 | 119 |
[exEnd] |
3283 | 120 |
|
121 |
saving those images: |
|
3392 | 122 |
[exBegin] |
3283 | 123 |
|seq img BG CLR N1 H W v drops gen gen1 buffer| |
124 |
||
125 |
BG := Color black. |
|
126 |
CLR := Color blue lightened. |
|
127 |
H := 100. |
|
128 |
W := 100. |
|
129 |
N1 := 10. |
|
130 |
WIND := 0. |
|
131 |
drops := OrderedCollection new. |
|
132 |
||
133 |
gen := [:n | ((1 to:n) collect:[:i | Random nextIntegerBetween:1 and:W] as:Set) collect:[:x | x@0]]. |
|
134 |
newDrops := [drops addAll:(gen value:N1)]. |
|
135 |
draw := [buffer fill:BG; paint:CLR. drops do:[:d | buffer displayPoint:d]]. |
|
136 |
remove := [drops := drops reject:[:d | d y > H]]. |
|
137 |
move := [:wind | drops := drops collect:[:d| (d x + wind)\\W @ (d y + 1)]]. |
|
138 |
buffer := Form extent:W@H depth:8 onDevice:Display. |
|
139 |
||
140 |
seq := OrderedCollection new. |
|
141 |
500 timesRepeat:[ |
|
3392 | 142 |
move value:WIND. |
143 |
remove value. |
|
144 |
newDrops value. |
|
145 |
draw value. |
|
146 |
seq add:(ImageFrame new delay:100; image:(Depth8Image fromForm:buffer)). |
|
147 |
WIND := (WIND+(Random nextBetween:-1 and:1)) clampBetween:-5 and:5. |
|
3283 | 148 |
]. |
149 |
||
150 |
img := seq first image. |
|
151 |
img imageSequence:(seq copyFrom:2). |
|
152 |
img imageSequence do:[:each | each image colorMap:img colorMap]. |
|
153 |
GIFReader save:img onFile:'/tmp/img.gif' |
|
3392 | 154 |
[exEnd] |
3283 | 155 |
" |
0 | 156 |
! ! |
157 |
||
339 | 158 |
!GIFReader class methodsFor:'initialization'! |
26 | 159 |
|
160 |
initialize |
|
198 | 161 |
"install myself in the Image classes fileFormat table |
398 | 162 |
for the `.gif' extensions." |
3468 | 163 |
|
164 |
ImageSeparator isNil ifTrue:[ |
|
165 |
ImageSeparator := $, codePoint. |
|
166 |
Extension := $!! codePoint. |
|
167 |
Terminator := $; codePoint. |
|
198 | 168 |
|
3468 | 169 |
MIMETypes defineImageType:'image/gif' suffix:'gif' reader:self. |
170 |
]. |
|
198 | 171 |
|
708 | 172 |
"Modified: 14.10.1997 / 18:47:27 / cg" |
26 | 173 |
! ! |
174 |
||
339 | 175 |
!GIFReader class methodsFor:'testing'! |
1 | 176 |
|
713 | 177 |
canRepresent:anImage |
178 |
"return true, if anImage can be represented in my file format. |
|
179 |
GIF supports depth 8 images only." |
|
180 |
||
1521 | 181 |
anImage depth == 8 ifTrue:[^ true]. |
3573 | 182 |
('GIFReader [info]: image depth is not 8.') infoPrintCR. |
2030 | 183 |
^ false. |
713 | 184 |
|
185 |
"Created: 17.10.1997 / 20:19:20 / cg" |
|
186 |
! |
|
187 |
||
4348 | 188 |
hasValidImageHeader:someHeaderBytes |
189 |
"return true, if someHeaderBytes |
|
190 |
(which does not need to be the whole data, but large enough |
|
191 |
to detect the header) contains a valid GIF image header" |
|
192 |
||
193 |
|id| |
|
194 |
||
195 |
id := (someHeaderBytes copyFrom:1 to:6) asString. |
|
196 |
(id = 'GIF87a') ifFalse:[ |
|
197 |
id = 'GIF89a' ifFalse:[ |
|
198 |
(id startsWith:'GIF') ifFalse:[^ false]. |
|
199 |
'GIFReader [info]: not a GIF87a/GIF89a file - hope that works' infoPrintCR. |
|
200 |
] |
|
201 |
]. |
|
202 |
^ true |
|
203 |
||
204 |
"Modified: 10.1.1997 / 15:40:34 / cg" |
|
205 |
! |
|
206 |
||
1 | 207 |
isValidImageFile:aFileName |
208 |
"return true, if aFileName contains a GIF image" |
|
209 |
||
4348 | 210 |
|someBytes inStream| |
1 | 211 |
|
9 | 212 |
inStream := self streamReadingFile:aFileName. |
1 | 213 |
inStream isNil ifTrue:[^ false]. |
4348 | 214 |
someBytes := inStream nextBytes:6. |
1 | 215 |
inStream close. |
4374 | 216 |
^ (someBytes size == 6) and:[self hasValidImageHeader:someBytes] |
217 |
||
218 |
" |
|
219 |
GIFReader isValidImageFile:'../../goodies/bitmaps/gifImages/a.gif' |
|
220 |
GIFReader isValidImageFile:'../../goodies/bitmaps/gifImages/garfield.gif' |
|
221 |
GIFReader isValidImageFile:'fooBar' |
|
222 |
GIFReader isValidImageFile:'../../goodies/bitmaps/xpmBitmaps/device_images/ljet.xpm' |
|
223 |
" |
|
174 | 224 |
|
355 | 225 |
"Modified: 10.1.1997 / 15:40:34 / cg" |
1 | 226 |
! ! |
227 |
||
2694 | 228 |
!GIFReader methodsFor:'image reading'! |
154 | 229 |
|
2596 | 230 |
fromStream:aStream |
231 |
"read a stream containing a GIF image (or an image sequence). |
|
232 |
Leave image description in instance variables." |
|
233 |
||
3392 | 234 |
|byte flag fileColorMap |
2596 | 235 |
colorMapSize bitsPerPixel scrWidth scrHeight |
3392 | 236 |
hasColorMap interlaced id |
237 |
isGif89 atEnd |
|
2596 | 238 |
img firstImage firstOffset firstFrameDelay frame imageCount| |
239 |
||
240 |
inStream := aStream. |
|
241 |
aStream binary. |
|
242 |
||
243 |
"GIF-files are always lsb (intel-world)" |
|
244 |
byteOrder := #lsb. |
|
245 |
||
246 |
id := ByteArray new:6. |
|
247 |
(aStream nextBytes:6 into:id startingAt:1) ~~ 6 ifTrue:[ |
|
3553 | 248 |
^ self fileFormatError:'not a gif file (short read)'. |
2596 | 249 |
]. |
250 |
id := id asString. |
|
251 |
||
252 |
"all I had for testing where GIF87a files; |
|
253 |
I hope later versions work too ..." |
|
254 |
||
255 |
isGif89 := false. |
|
256 |
(id ~= 'GIF87a') ifTrue:[ |
|
3553 | 257 |
(id startsWith:'GIF') ifFalse:[ |
258 |
^ self fileFormatError:('not a gif file (id=''' , id , ''')'). |
|
259 |
]. |
|
260 |
id ~= 'GIF89a' ifTrue:[ |
|
261 |
'GIFReader [info]: not a GIF87a/GIF89a file - hope that works' infoPrintCR. |
|
262 |
] |
|
2596 | 263 |
]. |
264 |
||
265 |
"get screen dimensions (not used)" |
|
3553 | 266 |
scrWidth := aStream nextInt16MSB:false. |
267 |
scrHeight := aStream nextInt16MSB:false. |
|
2596 | 268 |
|
269 |
"get flag byte" |
|
270 |
flag := aStream nextByte. |
|
271 |
hasColorMap := (flag bitAnd:2r10000000) ~~ 0. |
|
272 |
"bitsPerRGB := ((flag bitAnd:2r01110000) bitShift:-4) + 1. " |
|
273 |
"colorMapSorted := ((flag bitAnd:2r00001000) ~~ 0. " |
|
274 |
bitsPerPixel := (flag bitAnd:2r00000111) + 1. |
|
275 |
colorMapSize := 1 bitShift:bitsPerPixel. |
|
276 |
||
277 |
"get background (not used)" |
|
278 |
aStream nextByte. |
|
279 |
||
280 |
"aspect ratio (not used)" |
|
281 |
aStream nextByte. |
|
282 |
||
283 |
"get colorMap" |
|
284 |
hasColorMap ifTrue:[ |
|
3553 | 285 |
fileColorMap := self readColorMap:colorMapSize. |
2596 | 286 |
]. |
287 |
colorMap := fileColorMap. |
|
288 |
||
289 |
photometric := #palette. |
|
290 |
samplesPerPixel := 1. |
|
4450 | 291 |
bitsPerSample := #[8]. |
2596 | 292 |
|
293 |
imageCount := 0. |
|
294 |
atEnd := false. |
|
295 |
[atEnd] whileFalse:[ |
|
3553 | 296 |
"gif89a extensions" |
2596 | 297 |
|
3553 | 298 |
byte := aStream nextByte. |
299 |
byte isNil ifTrue:[ |
|
300 |
"/ atEnd-Terminator missing |
|
301 |
atEnd := true |
|
302 |
] ifFalse:[ |
|
303 |
byte == Extension ifTrue:[ |
|
3901 | 304 |
"/ 'Ext' infoPrintCR. |
3553 | 305 |
self readExtension:aStream. |
306 |
] ifFalse:[ |
|
307 |
(byte == Terminator) ifTrue:[ |
|
308 |
atEnd := true |
|
309 |
] ifFalse:[ |
|
310 |
"must be image separator" |
|
311 |
(byte ~~ ImageSeparator) ifTrue:[ |
|
312 |
^ self fileFormatError:('corrupted gif file (no IMAGESEP): ' , (byte printStringRadix:16)). |
|
313 |
]. |
|
3901 | 314 |
"/ 'Img' infoPrintCR. |
2596 | 315 |
|
3553 | 316 |
fileColorMap notNil ifTrue:[ |
317 |
colorMap := fileColorMap. |
|
318 |
]. |
|
319 |
Object primitiveFailureSignal handle:[:ex | |
|
320 |
^ self fileFormatError:('corrupted gif file'). |
|
321 |
] do:[ |
|
322 |
self readImage:aStream. |
|
323 |
]. |
|
2596 | 324 |
|
3553 | 325 |
maskPixel notNil ifTrue:[ |
326 |
"/ |
|
327 |
"/ ok, there is a maskValue |
|
328 |
"/ build a Depth1Image for it. |
|
329 |
"/ |
|
330 |
self buildMaskFromColor:maskPixel |
|
331 |
]. |
|
2596 | 332 |
|
3553 | 333 |
imageCount == 0 ifTrue:[ |
334 |
img := self makeImage. |
|
335 |
"/ remember first image in case more come later. |
|
336 |
firstImage := img. |
|
337 |
firstFrameDelay := frameDelay. |
|
338 |
firstOffset := (leftOffs @ topOffs). |
|
339 |
] ifFalse:[ |
|
340 |
imageCount == 1 ifTrue:[ |
|
341 |
imageSequence := ImageSequence new. |
|
342 |
img imageSequence:imageSequence. |
|
2596 | 343 |
|
3553 | 344 |
"/ add frame for first image. |
345 |
frame := ImageFrame new image:firstImage. |
|
346 |
frame delay:firstFrameDelay. |
|
347 |
frame offset:firstOffset. |
|
348 |
imageSequence add:frame. |
|
349 |
]. |
|
350 |
img := self makeImage. |
|
351 |
img imageSequence:imageSequence. |
|
2596 | 352 |
|
3553 | 353 |
"/ add frame for this image. |
354 |
frame := ImageFrame new image:img. |
|
355 |
frame delay:frameDelay. |
|
356 |
frame offset:(leftOffs @ topOffs). |
|
357 |
imageSequence add:frame. |
|
358 |
]. |
|
2596 | 359 |
|
3553 | 360 |
imageCount := imageCount + 1. |
2596 | 361 |
|
3553 | 362 |
frameDelay := nil. |
2596 | 363 |
|
3553 | 364 |
aStream atEnd ifTrue:[ |
365 |
atEnd := true. |
|
366 |
] |
|
367 |
] |
|
368 |
]. |
|
369 |
]. |
|
2596 | 370 |
]. |
371 |
||
372 |
imageSequence notNil ifTrue:[ |
|
3553 | 373 |
iterationCount notNil ifTrue:[ |
374 |
iterationCount == 0 ifTrue:[ |
|
375 |
imageSequence loop:true. |
|
376 |
] ifFalse:[ |
|
377 |
imageSequence loop:false. |
|
378 |
imageSequence iterationCount:iterationCount. |
|
379 |
] |
|
380 |
] |
|
2596 | 381 |
]. |
382 |
||
383 |
" |
|
384 |
Image fromFile:'/home/cg/work/stx/goodies/bitmaps/gifImages/animated/vrml.gif' |
|
385 |
Image fromFile:'/home/cg/work/stx/goodies/bitmaps/gifImages/animated/arrow.gif' |
|
386 |
" |
|
387 |
||
3901 | 388 |
"Modified: / 05-07-1996 / 17:32:01 / stefan" |
389 |
"Modified: / 21-08-1998 / 22:20:00 / cg" |
|
390 |
"Modified (format): / 17-02-2017 / 12:29:10 / cg" |
|
2694 | 391 |
! ! |
392 |
||
393 |
!GIFReader methodsFor:'private-reading'! |
|
394 |
||
395 |
makeGreyscale |
|
396 |
"not yet implemented/needed" |
|
397 |
! |
|
398 |
||
399 |
readColorMap:colorMapSize |
|
400 |
"get a gif colormap consisting of colorMapSize entries" |
|
401 |
||
402 |
|rgbVector| |
|
403 |
||
404 |
rgbVector := inStream nextBytes:colorMapSize*3. |
|
3392 | 405 |
^ MappedPalette rgbBytesVector:rgbVector |
2596 | 406 |
! |
407 |
||
154 | 408 |
readExtension:aStream |
882 | 409 |
"get gif89 extension" |
154 | 410 |
|
411 |
|type blockSize subBlockSize |
|
304 | 412 |
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
|
413 |
animationType animationTime animationMask |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
414 |
appID appAUTH |
888 | 415 |
b ok| |
154 | 416 |
|
417 |
type := aStream nextByte. |
|
1952
5009754ae556
Use #codePoint instead of deprecated #asciiValue
Stefan Vogel <sv@exept.de>
parents:
1846
diff
changeset
|
418 |
type == $R codePoint ifTrue:[ |
3553 | 419 |
"/ |
420 |
"/ Ratio extension |
|
421 |
"/ |
|
422 |
"/ 'GIFREADER [info]: ratio extension ignored' infoPrintCR. |
|
423 |
blockSize := aStream nextByte. |
|
424 |
(blockSize == 2) ifTrue:[ |
|
425 |
aspNum := aStream nextByte. |
|
426 |
aspDen := aStream nextByte |
|
427 |
] ifFalse:[ |
|
428 |
aStream skip:blockSize |
|
429 |
]. |
|
2844 | 430 |
|
3553 | 431 |
"/ eat subblocks |
432 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
|
433 |
aStream skip:subBlockSize |
|
434 |
]. |
|
435 |
^ self |
|
239 | 436 |
]. |
437 |
||
438 |
type == 16r01 ifTrue:[ |
|
3553 | 439 |
"/ |
440 |
"/ plaintext extension |
|
441 |
"/ |
|
442 |
"/ 'GIFREADER [info]: plaintext extension ignored' infoPrintCR. |
|
443 |
subBlockSize := aStream nextByte. |
|
444 |
left := aStream nextInt16MSB:false. |
|
445 |
top := aStream nextInt16MSB:false. |
|
446 |
width := aStream nextInt16MSB:false. |
|
447 |
height := aStream nextInt16MSB:false. |
|
448 |
cWidth := aStream nextByte. |
|
449 |
cHeight := aStream nextByte. |
|
450 |
fg := aStream nextByte. |
|
451 |
bg := aStream nextByte. |
|
452 |
aStream skip:12. |
|
453 |
"/ eat subblocks |
|
454 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
|
455 |
aStream skip:subBlockSize |
|
456 |
]. |
|
457 |
^ self |
|
239 | 458 |
]. |
459 |
||
460 |
type == 16rF9 ifTrue:[ |
|
3553 | 461 |
"/ |
462 |
"/ graphic control extension |
|
463 |
"/ |
|
464 |
"/ '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
|
465 |
|
3553 | 466 |
[(subBlockSize := aStream nextByte) ~~ 0 and:[subBlockSize notNil]] whileTrue:[ |
467 |
"/ type bitAnd:1 means: animationMask is transparent pixel |
|
468 |
"/ to be implemented in Image ... |
|
305 | 469 |
|
3553 | 470 |
animationType := aStream nextByte. |
471 |
animationTime := aStream nextInt16MSB:false. |
|
472 |
animationMask := aStream nextByte. |
|
2595
37dc71e1e59b
be tolerant to corrupted gif files (subblock size in extension)
Claus Gittinger <cg@exept.de>
parents:
2056
diff
changeset
|
473 |
|
3553 | 474 |
subBlockSize := subBlockSize - 4. |
307
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
475 |
|
3553 | 476 |
(animationType bitTest: 1) ifTrue:[ |
477 |
maskPixel := animationMask. |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
478 |
"/ 'GIFREADER [info]: mask: ' infoPrint. (maskPixel printStringRadix:16) infoPrintCR. |
3553 | 479 |
]. |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
480 |
"/ 'GIFREADER [info]: animationTime: ' infoPrint. (animationTime * (1/100)) infoPrintCR. |
1429 | 481 |
"/ 'GIFREADER [info]: animationType: ' infoPrint. (animationType) infoPrintCR. |
482 |
"/ 'GIFREADER [info]: animationMask: ' infoPrint. (animationMask) infoPrintCR. |
|
307
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
483 |
|
3553 | 484 |
frameDelay := (animationTime * (1/100)) * 1000. |
888 | 485 |
|
3553 | 486 |
subBlockSize ~~ 0 ifTrue:[ |
487 |
aStream skip:subBlockSize |
|
488 |
]. |
|
489 |
]. |
|
490 |
^ self |
|
239 | 491 |
]. |
492 |
||
519 | 493 |
type == 16rFE ifTrue:[ |
3553 | 494 |
"/ |
495 |
"/ comment extension |
|
496 |
"/ |
|
497 |
"/ 'GIFREADER [info]: comment extension' infoPrintCR. |
|
498 |
[(blockSize := aStream nextByte) ~~ 0] whileTrue:[ |
|
499 |
aStream skip:blockSize |
|
500 |
]. |
|
501 |
^ self |
|
519 | 502 |
]. |
503 |
||
239 | 504 |
type == 16rFF ifTrue:[ |
3553 | 505 |
"/ |
506 |
"/ application extension |
|
507 |
"/ |
|
508 |
"/ 'GIFREADER [info]: application extension' infoPrintCR. |
|
509 |
subBlockSize := aStream nextByte. |
|
510 |
appID := (aStream nextBytes:8 ) asString. |
|
511 |
appAUTH := aStream nextBytes:3. |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
512 |
|
3553 | 513 |
subBlockSize := aStream nextByte. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
514 |
|
3553 | 515 |
ok := false. |
516 |
appID = 'NETSCAPE' ifTrue:[ |
|
517 |
appAUTH asString = '2.0' ifTrue:[ |
|
518 |
subBlockSize == 3 ifTrue:[ |
|
519 |
b := aStream nextByte. |
|
520 |
iterationCount := aStream nextInt16MSB:false. |
|
521 |
subBlockSize := aStream nextByte. |
|
522 |
ok := true. |
|
888 | 523 |
"/ ('GIFREADER [info]: NETSCAPE application extension - iterationCount = ') infoPrint. |
524 |
"/ iterationCount infoPrintCR. |
|
3553 | 525 |
] |
526 |
] |
|
527 |
]. |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
528 |
|
3553 | 529 |
ok ifFalse:[ |
530 |
('GIFREADER [info]: application extension (' , appID , ') ignored') infoPrintCR. |
|
531 |
]. |
|
625 | 532 |
|
3553 | 533 |
[subBlockSize > 0] whileTrue:[ |
534 |
aStream skip:subBlockSize. |
|
535 |
subBlockSize := aStream nextByte. |
|
536 |
]. |
|
537 |
^ self |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
538 |
]. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
539 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
540 |
type == 16r2C ifTrue:[ |
3553 | 541 |
"/ |
542 |
"/ image descriptor extension |
|
543 |
"/ |
|
544 |
"/ 'GIFREADER [info]: image descriptor extension ignored' infoPrintCR. |
|
545 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
|
546 |
aStream skip:subBlockSize |
|
547 |
]. |
|
548 |
^ self |
|
239 | 549 |
]. |
550 |
||
551 |
"/ |
|
552 |
"/ unknown extension |
|
553 |
"/ |
|
354
b4d2ce853c24
stefans suggested info/warning/error/fatal
Claus Gittinger <cg@exept.de>
parents:
339
diff
changeset
|
554 |
'GIFREADER [info]: unknown extension ignored' infoPrintCR. |
239 | 555 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
3553 | 556 |
aStream skip:subBlockSize |
154 | 557 |
] |
558 |
||
2844 | 559 |
"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
|
560 |
! |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
561 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
562 |
readImage:aStream |
882 | 563 |
"read a single image from aStream." |
564 |
||
1429 | 565 |
| 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
|
566 |
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
|
567 |
initialBuffSize| |
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 |
"get image data" |
3553 | 570 |
leftOffs := aStream nextInt16MSB:false. |
571 |
topOffs := aStream nextInt16MSB:false. |
|
3901 | 572 |
"/ 'GIFReader: leftOffs ' infoPrint. leftOffs infoPrintCR. |
573 |
"/ 'GIFReader: topOffs ' infoPrint. topOffs infoPrintCR. |
|
3553 | 574 |
width := aStream nextInt16MSB:false. |
575 |
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
|
576 |
|
1846 | 577 |
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
|
578 |
|
3901 | 579 |
"/ |
580 |
"/ 'width ' print. width printNewline. |
|
581 |
"/ 'height ' print. height printNewline. |
|
582 |
"/ |
|
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 |
"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
|
585 |
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
|
586 |
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
|
587 |
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
|
588 |
"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
|
589 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
590 |
"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
|
591 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
592 |
hasLocalColorMap ifTrue:[ |
3553 | 593 |
"local descr. overwrites" |
594 |
bitsPerPixel := (flag bitAnd:2r00000111) + 1. |
|
595 |
colorMapSize := 1 bitShift:bitsPerPixel. |
|
596 |
"overwrite colormap" |
|
597 |
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
|
598 |
]. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
599 |
|
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 |
"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
|
602 |
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
|
603 |
(aStream respondsTo:#fileSize) ifTrue:[ |
3553 | 604 |
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
|
605 |
] ifFalse:[ |
3553 | 606 |
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
|
607 |
]. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
608 |
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
|
609 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
610 |
"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
|
611 |
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 |
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
|
613 |
[count notNil and:[count ~~ 0]] whileTrue:[ |
3553 | 614 |
(compressedData size < (index+count)) ifTrue:[ |
615 |
|t| |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
616 |
|
3784 | 617 |
t := ByteArray uninitializedNew:((index+count)*3//2). |
3553 | 618 |
t replaceBytesFrom:1 to:index-1 with:compressedData startingAt:1. |
619 |
compressedData := t. |
|
620 |
]. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
621 |
|
3553 | 622 |
aStream nextBytes:count into:compressedData startingAt:index blockSize:4096. |
623 |
index := index + count. |
|
624 |
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
|
625 |
]. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
626 |
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
|
627 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
628 |
h := height. |
1037
90d38c8e3f5f
use initialized byteArrays, in case rows are padded
Claus Gittinger <cg@exept.de>
parents:
891
diff
changeset
|
629 |
data := ByteArray new:((width + 1) * (h + 1)). |
3901 | 630 |
"/ '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
|
631 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
632 |
self class decompressGIFFrom:compressedData |
3553 | 633 |
count:compressedSize |
634 |
into:data |
|
635 |
startingAt:1 |
|
636 |
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
|
637 |
|
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
638 |
interlaced ifTrue:[ |
4450 | 639 |
"/ 'GIFREADER: deinterlacing ...' infoPrintCR. |
3553 | 640 |
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
|
641 |
|
3553 | 642 |
"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
|
643 |
|
3553 | 644 |
srcOffset := 1. |
645 |
0 to:(h - 1) by:8 do:[:dstRow | |
|
646 |
dstOffset := dstRow * width + 1. |
|
647 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
|
648 |
with:data startingAt:srcOffset. |
|
649 |
srcOffset := srcOffset + width. |
|
650 |
]. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
651 |
|
3553 | 652 |
"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
|
653 |
|
3553 | 654 |
4 to:(h - 1) by:8 do:[:dstRow | |
655 |
dstOffset := dstRow * width + 1. |
|
656 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
|
657 |
with:data startingAt:srcOffset. |
|
658 |
srcOffset := srcOffset + width. |
|
659 |
]. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
660 |
|
3553 | 661 |
"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
|
662 |
|
3553 | 663 |
2 to:(h - 1) by:4 do:[:dstRow | |
664 |
dstOffset := dstRow * width + 1. |
|
665 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
|
666 |
with:data startingAt:srcOffset. |
|
667 |
srcOffset := srcOffset + width. |
|
668 |
]. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
669 |
|
3553 | 670 |
"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
|
671 |
|
3553 | 672 |
1 to:(h - 1) by:2 do:[:dstRow | |
673 |
dstOffset := dstRow * width + 1. |
|
674 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
|
675 |
with:data startingAt:srcOffset. |
|
676 |
srcOffset := srcOffset + width. |
|
677 |
]. |
|
757
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
678 |
|
3553 | 679 |
data := tmp. |
680 |
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
|
681 |
]. |
094c6c7c4ce6
extracted raw image reading (i.e. after header & extensions have been read)
Claus Gittinger <cg@exept.de>
parents:
755
diff
changeset
|
682 |
|
3784 | 683 |
"Created: / 13-01-1998 / 10:44:05 / cg" |
684 |
"Modified: / 12-08-1998 / 13:55:32 / cg" |
|
3901 | 685 |
"Modified (format): / 17-02-2017 / 12:29:44 / cg" |
0 | 686 |
! ! |
43 | 687 |
|
1805 | 688 |
!GIFReader methodsFor:'private-writing'! |
689 |
||
690 |
assignTransparentPixelIn:image |
|
691 |
"find an usused pixelValue in the colorMap (or image)." |
|
692 |
||
3689 | 693 |
|cmap cmapSize imgDepth usedPixelValues unusedValues| |
1805 | 694 |
|
3689 | 695 |
cmap := image colorMap. |
696 |
cmapSize := cmap size. |
|
697 |
((cmapSize > 0) and:[cmapSize < 256]) ifTrue:[ |
|
698 |
maskPixel := cmapSize. |
|
699 |
^ self |
|
1805 | 700 |
]. |
3689 | 701 |
|
3009 | 702 |
image compressColorMap. |
3689 | 703 |
imgDepth := image depth. |
1805 | 704 |
usedPixelValues := image usedValues. |
3689 | 705 |
usedPixelValues size < (1 bitShift:imgDepth) ifFalse:[ |
706 |
Image informationLostQuerySignal |
|
707 |
raiseRequestWith:image |
|
708 |
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
|
709 |
]. |
b7eea50081f2
fixed transparency-pixel bug (with full colormap)
Claus Gittinger <cg@exept.de>
parents:
2032
diff
changeset
|
710 |
|
b7eea50081f2
fixed transparency-pixel bug (with full colormap)
Claus Gittinger <cg@exept.de>
parents:
2032
diff
changeset
|
711 |
"/ there must be an unused pixelValue |
3689 | 712 |
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
|
713 |
unusedValues removeAll:usedPixelValues. |
b7eea50081f2
fixed transparency-pixel bug (with full colormap)
Claus Gittinger <cg@exept.de>
parents:
2032
diff
changeset
|
714 |
|
b7eea50081f2
fixed transparency-pixel bug (with full colormap)
Claus Gittinger <cg@exept.de>
parents:
2032
diff
changeset
|
715 |
maskPixel := unusedValues first. |
3009 | 716 |
|
717 |
"Modified: / 17-07-2012 / 12:25:24 / anwild" |
|
1805 | 718 |
! |
719 |
||
720 |
checkCodeSize |
|
721 |
(freeCode > maxCode and: [codeSize < 12]) |
|
3392 | 722 |
ifTrue: |
1805 | 723 |
[codeSize := codeSize + 1. |
724 |
maxCode := (1 bitShift: codeSize) - 1] |
|
725 |
||
726 |
"Created: 14.10.1997 / 18:42:01 / cg" |
|
727 |
! |
|
728 |
||
729 |
flushBits |
|
4341 | 730 |
remainBitCount == 0 |
731 |
ifFalse: |
|
732 |
[self nextBytePut: bufByte. |
|
733 |
remainBitCount := 0]. |
|
734 |
self flushBuffer |
|
1805 | 735 |
|
736 |
"Modified: 14.10.1997 / 18:58:06 / cg" |
|
737 |
! |
|
738 |
||
739 |
flushBuffer |
|
740 |
bufStream isEmpty ifTrue: [^ self]. |
|
741 |
outStream nextPut: bufStream size. |
|
742 |
outStream nextPutAll: bufStream contents. |
|
743 |
bufStream := WriteStream on: (ByteArray new: 256) |
|
744 |
||
745 |
"Modified: 14.10.1997 / 20:46:04 / cg" |
|
746 |
! |
|
747 |
||
748 |
flushCode |
|
749 |
self flushBits |
|
750 |
||
751 |
"Created: 14.10.1997 / 18:57:33 / cg" |
|
752 |
! |
|
753 |
||
754 |
nextBitsPut: anInteger |
|
4341 | 755 |
| integer writeBitCount shiftCount | |
756 |
shiftCount := 0. |
|
757 |
remainBitCount == 0 |
|
758 |
ifTrue: |
|
759 |
[writeBitCount := 8. |
|
760 |
integer := anInteger] |
|
761 |
ifFalse: |
|
762 |
[writeBitCount := remainBitCount. |
|
763 |
integer := bufByte + (anInteger bitShift: 8 - remainBitCount)]. |
|
764 |
[writeBitCount < codeSize] |
|
765 |
whileTrue: |
|
766 |
[self nextBytePut: ((integer bitShift: shiftCount) bitAnd: 255). |
|
767 |
shiftCount := shiftCount - 8. |
|
768 |
writeBitCount := writeBitCount + 8]. |
|
769 |
(remainBitCount := writeBitCount - codeSize) = 0 |
|
770 |
ifTrue: [self nextBytePut: (integer bitShift: shiftCount)] |
|
771 |
ifFalse: [bufByte := integer bitShift: shiftCount]. |
|
772 |
^anInteger |
|
1805 | 773 |
|
774 |
"Modified: 15.10.1997 / 16:50:30 / cg" |
|
775 |
! |
|
776 |
||
777 |
nextBytePut: aByte |
|
778 |
bufStream nextPut: aByte. |
|
779 |
bufStream size >= 254 ifTrue: [self flushBuffer] |
|
780 |
||
781 |
"Modified: 15.10.1997 / 16:50:52 / cg" |
|
782 |
! |
|
783 |
||
3392 | 784 |
readPixelFrom: bits |
1805 | 785 |
| pixel | |
786 |
ypos >= height ifTrue: [^ nil]. |
|
3392 | 787 |
(maskPixel notNil |
1805 | 788 |
and:[(mask pixelAtX:xpos y:ypos) == 0]) ifTrue:[ |
789 |
pixel := maskPixel |
|
790 |
] ifFalse:[ |
|
791 |
pixel := bits at: ypos * rowByteSize + xpos + 1. |
|
792 |
]. |
|
793 |
self updatePixelPosition. |
|
794 |
^ pixel |
|
795 |
||
796 |
"Created: 14.10.1997 / 18:43:50 / cg" |
|
797 |
"Modified: 15.10.1997 / 16:46:43 / cg" |
|
798 |
! |
|
799 |
||
3392 | 800 |
setParameters:bitsPerPixel |
1805 | 801 |
clearCode := 1 bitShift:bitsPerPixel. |
802 |
eoiCode := clearCode + 1. |
|
803 |
freeCode := clearCode + 2. |
|
804 |
codeSize := bitsPerPixel + 1. |
|
805 |
maxCode := (1 bitShift: codeSize) - 1 |
|
806 |
||
807 |
"Modified: 14.10.1997 / 20:09:48 / cg" |
|
808 |
! |
|
809 |
||
810 |
updatePixelPosition |
|
811 |
(xpos := xpos + 1) >= width ifFalse: [^ self]. |
|
812 |
||
813 |
xpos := 0. |
|
814 |
interlace == true ifFalse:[ |
|
3392 | 815 |
ypos := ypos + 1. |
816 |
^ self |
|
1805 | 817 |
]. |
818 |
||
819 |
pass == 0 ifTrue:[ |
|
3392 | 820 |
(ypos := ypos + 8) >= height ifTrue:[ |
821 |
pass := pass + 1. |
|
822 |
ypos := 4 |
|
823 |
]. |
|
824 |
^ self |
|
1805 | 825 |
]. |
826 |
pass == 1 ifTrue:[ |
|
3392 | 827 |
(ypos := ypos + 8) >= height ifTrue:[ |
828 |
pass := pass + 1. |
|
829 |
ypos := 2 |
|
830 |
]. |
|
831 |
^ self |
|
1805 | 832 |
]. |
833 |
pass == 2 ifTrue:[ |
|
3392 | 834 |
(ypos := ypos + 4) >= height ifTrue:[ |
835 |
pass := pass + 1. |
|
836 |
ypos := 1 |
|
837 |
]. |
|
838 |
^ self |
|
1805 | 839 |
]. |
840 |
pass == 3 ifTrue:[ |
|
3392 | 841 |
ypos := ypos + 2. |
842 |
^ self |
|
1805 | 843 |
]. |
844 |
^ self error: 'can''t happen' |
|
845 |
||
846 |
"Modified: 14.10.1997 / 18:44:27 / cg" |
|
847 |
! |
|
848 |
||
2032 | 849 |
writeBitDataFor: image |
850 |
"using modified Lempel-Ziv Welch algorithm." |
|
851 |
||
852 |
| bits bitsPerPixel t1 |
|
3849 | 853 |
maxBits maxMaxCode tSize initCodeSize ent tShift fCode |
854 |
pixel |
|
855 |
index disp nomatch | |
|
2032 | 856 |
|
857 |
outStream nextPut:ImageSeparator. |
|
3282 | 858 |
self writeShort:0. "/ leftOffs |
859 |
self writeShort:0. "/ topOffs |
|
2032 | 860 |
self writeShort:width. "/ image size |
861 |
self writeShort:height. |
|
862 |
||
863 |
interlace == true ifTrue:[ |
|
3848 | 864 |
t1 := 64 |
2032 | 865 |
] ifFalse:[ |
3848 | 866 |
t1 := 0 "/ no local colormap |
2032 | 867 |
]. |
868 |
outStream nextPut:t1. "/ another flag |
|
869 |
||
870 |
bitsPerPixel := image bitsPerPixel. |
|
871 |
bits := image bits. |
|
872 |
||
873 |
pass := 0. |
|
874 |
xpos := 0. |
|
875 |
ypos := 0. |
|
876 |
rowByteSize := image bytesPerRow. "/ width * 8 + 31 // 32 * 4. |
|
877 |
remainBitCount := 0. |
|
878 |
bufByte := 0. |
|
879 |
bufStream := WriteStream on: (ByteArray new: 256). |
|
880 |
||
881 |
maxBits := 12. |
|
882 |
maxMaxCode := 1 bitShift: maxBits. |
|
883 |
tSize := 5003. |
|
884 |
prefixTable := Array new: tSize. |
|
3364 | 885 |
suffixTable := Array new: tSize withAll:-1. |
2032 | 886 |
|
887 |
initCodeSize := bitsPerPixel <= 1 ifTrue: [2] ifFalse: [bitsPerPixel]. |
|
888 |
outStream nextPut: initCodeSize. |
|
889 |
self setParameters: initCodeSize. |
|
890 |
||
891 |
tShift := 0. |
|
892 |
fCode := tSize. |
|
2845
62d4128bb923
comment/format in: #writeBitDataFor:
Claus Gittinger <cg@exept.de>
parents:
2844
diff
changeset
|
893 |
[fCode < 65536] whileTrue:[ |
3848 | 894 |
tShift := tShift + 1. |
895 |
fCode := fCode * 2 |
|
2845
62d4128bb923
comment/format in: #writeBitDataFor:
Claus Gittinger <cg@exept.de>
parents:
2844
diff
changeset
|
896 |
]. |
2032 | 897 |
tShift := 8 - tShift. |
3364 | 898 |
"/ 1 to: tSize do: [:i | suffixTable at: i put: -1]. - cg changed initialization above |
2032 | 899 |
|
900 |
self writeCodeAndCheckCodeSize: clearCode. |
|
901 |
ent := self readPixelFrom: bits. |
|
3848 | 902 |
[(pixel := self readPixelFrom: bits) notNil] whileTrue:[ |
903 |
fCode := (pixel bitShift: maxBits) + ent. |
|
904 |
index := ((pixel bitShift: tShift) bitXor: ent) + 1. |
|
905 |
(suffixTable at: index) = fCode |
|
906 |
ifTrue: [ent := prefixTable at: index] |
|
907 |
ifFalse:[ |
|
908 |
nomatch := true. |
|
909 |
(suffixTable at: index) >= 0 |
|
910 |
ifTrue:[ |
|
911 |
disp := tSize - index + 1. |
|
912 |
index = 1 ifTrue: [disp := 1]. |
|
913 |
"probe" |
|
914 |
[(index := index - disp) < 1 ifTrue: [index := index + tSize]. |
|
915 |
(suffixTable at: index) = fCode |
|
916 |
ifTrue:[ |
|
917 |
ent := prefixTable at: index. |
|
918 |
nomatch := false. |
|
919 |
"continue whileFalse:"]. |
|
920 |
nomatch and: [(suffixTable at: index) > 0]] |
|
921 |
whileTrue: ["probe"]]. |
|
922 |
"nomatch" |
|
923 |
nomatch ifTrue:[ |
|
924 |
self writeCodeAndCheckCodeSize: ent. |
|
925 |
ent := pixel. |
|
926 |
freeCode < maxMaxCode |
|
927 |
ifTrue:[ |
|
928 |
prefixTable at: index put: freeCode. |
|
929 |
suffixTable at: index put: fCode. |
|
930 |
freeCode := freeCode + 1] |
|
931 |
ifFalse:[ |
|
932 |
self writeCodeAndCheckCodeSize: clearCode. |
|
933 |
suffixTable from:1 to:tSize put:-1. |
|
934 |
self setParameters: initCodeSize]]]]. |
|
2032 | 935 |
prefixTable := suffixTable := nil. |
936 |
self writeCodeAndCheckCodeSize: ent. |
|
937 |
self writeCodeAndCheckCodeSize: eoiCode. |
|
938 |
self flushCode. |
|
939 |
outStream nextPut: 0. "zero-length packet" |
|
940 |
||
2845
62d4128bb923
comment/format in: #writeBitDataFor:
Claus Gittinger <cg@exept.de>
parents:
2844
diff
changeset
|
941 |
"Modified: / 02-06-2010 / 12:24:20 / cg" |
2032 | 942 |
! |
943 |
||
3392 | 944 |
writeCode: aCode |
1805 | 945 |
self nextBitsPut: aCode |
946 |
||
947 |
"Created: 14.10.1997 / 18:38:35 / cg" |
|
948 |
"Modified: 15.10.1997 / 17:01:47 / cg" |
|
949 |
! |
|
950 |
||
3392 | 951 |
writeCodeAndCheckCodeSize: t1 |
1805 | 952 |
self writeCode: t1. |
953 |
self checkCodeSize |
|
954 |
||
955 |
"Created: 14.10.1997 / 18:38:24 / cg" |
|
956 |
"Modified: 14.10.1997 / 18:40:56 / cg" |
|
2032 | 957 |
! |
958 |
||
959 |
writeHeaderFor:image |
|
960 |
"write the gif header" |
|
961 |
||
962 |
|bitsPerPixel t1| |
|
963 |
||
964 |
bitsPerPixel := image bitsPerPixel. |
|
965 |
||
966 |
outStream nextPutAll: 'GIF89a' asByteArray. |
|
4450 | 967 |
|
968 |
"/ local screen descriptor |
|
969 |
outStream nextPutInt16:width MSB:(byteOrder ~~ #lsb). "/ screen size |
|
970 |
outStream nextPutInt16:height MSB:(byteOrder ~~ #lsb). |
|
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 | |
|
4450 | 979 |
|clr red green blue| |
2032 | 980 |
|
4450 | 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. |
|
3392 | 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! |