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