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