author | Claus Gittinger <cg@exept.de> |
Tue, 21 Oct 1997 20:27:06 +0200 | |
changeset 713 | 548898fdd1dc |
parent 712 | 6403dd3407eb |
child 714 | c89f5c12538c |
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 |
" |
|
12 |
||
713 | 13 |
'From Smalltalk/X, Version:3.2.1 on 21-oct-1997 at 5:07:17 pm' ! |
14 |
||
0 | 15 |
ImageReader subclass:#GIFReader |
708 | 16 |
instanceVariableNames:'redMap greenMap blueMap pass xpos ypos rowByteSize remainBitCount |
17 |
bufByte bufStream prefixTable suffixTable clearCode eoiCode |
|
18 |
freeCode codeSize maxCode interlace' |
|
19 |
classVariableNames:'ImageSeparator Extension Terminator' |
|
154 | 20 |
poolDictionaries:'' |
259 | 21 |
category:'Graphics-Images-Support' |
0 | 22 |
! |
23 |
||
339 | 24 |
!GIFReader class methodsFor:'documentation'! |
0 | 25 |
|
21 | 26 |
copyright |
27 |
" |
|
28 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
26 | 29 |
All Rights Reserved |
21 | 30 |
|
31 |
This software is furnished under a license and may be used |
|
32 |
only in accordance with the terms of that license and with the |
|
33 |
inclusion of the above copyright notice. This software may not |
|
34 |
be provided or otherwise made available to, or used by, any |
|
35 |
other person. No title to or ownership of the software is |
|
36 |
hereby transferred. |
|
37 |
" |
|
38 |
! |
|
39 |
||
0 | 40 |
documentation |
41 |
" |
|
42 |
this class provides methods for loading and saving GIF pictures. |
|
21 | 43 |
It has been tested with some different GIF87a pictures, I dont |
0 | 44 |
know, if it works with other GIF versions. |
45 |
GIF extension blocks are not handled. |
|
46 |
||
47 |
GIF file writing is not implemented (use TIFF). |
|
48 |
||
21 | 49 |
legal stuff extracted from GIF87a documentation: |
0 | 50 |
|
51 |
CompuServe Incorporated hereby grants a limited, non-exclusive, royalty-free |
|
52 |
license for the use of the Graphics Interchange Format(sm) in computer |
|
53 |
software; computer software utilizing GIF(sm) must acknowledge ownership of the |
|
54 |
Graphics Interchange Format and its Service Mark by CompuServe Incorporated, in |
|
55 |
User and Technical Documentation. |
|
56 |
||
57 |
The Graphics Interchange Format(c) is the Copyright property of |
|
58 |
CompuServe Incorporated. GIF(sm) is a Service Mark property of |
|
59 |
CompuServe Incorporated. |
|
194 | 60 |
|
61 |
[See also:] |
|
234 | 62 |
Image Form Icon |
194 | 63 |
BlitImageReader FaceReader JPEGReader PBMReader PCXReader |
210 | 64 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
209 | 65 |
XBMReader XPMReader XWDReader |
220 | 66 |
|
67 |
[author:] |
|
68 |
Claus Gittinger |
|
0 | 69 |
" |
70 |
! ! |
|
71 |
||
339 | 72 |
!GIFReader class methodsFor:'initialization'! |
26 | 73 |
|
74 |
initialize |
|
198 | 75 |
"install myself in the Image classes fileFormat table |
398 | 76 |
for the `.gif' extensions." |
198 | 77 |
|
708 | 78 |
ImageSeparator := $, asciiValue. |
79 |
Extension := $!! asciiValue. |
|
80 |
Terminator := $; asciiValue. |
|
81 |
||
647 | 82 |
MIMETypes defineImageType:'image/gif' suffix:'gif' reader:self. |
198 | 83 |
|
708 | 84 |
"Modified: 14.10.1997 / 18:47:27 / cg" |
26 | 85 |
! ! |
86 |
||
339 | 87 |
!GIFReader class methodsFor:'testing'! |
1 | 88 |
|
713 | 89 |
canRepresent:anImage |
90 |
"return true, if anImage can be represented in my file format. |
|
91 |
GIF supports depth 8 images only." |
|
92 |
||
93 |
^ anImage depth == 8 |
|
94 |
||
95 |
"Created: 17.10.1997 / 20:19:20 / cg" |
|
96 |
! |
|
97 |
||
1 | 98 |
isValidImageFile:aFileName |
99 |
"return true, if aFileName contains a GIF image" |
|
100 |
||
101 |
|id inStream| |
|
102 |
||
9 | 103 |
inStream := self streamReadingFile:aFileName. |
1 | 104 |
inStream isNil ifTrue:[^ false]. |
105 |
||
332 | 106 |
inStream text. |
107 |
||
1 | 108 |
id := String new:6. |
109 |
inStream nextBytes:6 into:id. |
|
110 |
inStream close. |
|
111 |
||
112 |
(id = 'GIF87a') ifFalse:[ |
|
174 | 113 |
(id startsWith:'GIF') ifFalse:[^ false]. |
1 | 114 |
|
174 | 115 |
id ~= 'GIF89a' ifTrue:[ |
355 | 116 |
'GIFReader [info]: not a GIF87a/GIF89a file - hope that works' infoPrintCR. |
174 | 117 |
] |
1 | 118 |
]. |
119 |
^ true |
|
174 | 120 |
|
355 | 121 |
"Modified: 10.1.1997 / 15:40:34 / cg" |
1 | 122 |
! ! |
123 |
||
708 | 124 |
!GIFReader methodsFor:'private - writing'! |
125 |
||
126 |
checkCodeSize |
|
127 |
(freeCode > maxCode and: [codeSize < 12]) |
|
128 |
ifTrue: |
|
129 |
[codeSize := codeSize + 1. |
|
130 |
maxCode := (1 bitShift: codeSize) - 1] |
|
131 |
||
132 |
"Created: 14.10.1997 / 18:42:01 / cg" |
|
133 |
! |
|
134 |
||
135 |
flushBits |
|
136 |
remainBitCount = 0 |
|
137 |
ifFalse: |
|
138 |
[self nextBytePut: bufByte. |
|
139 |
remainBitCount := 0]. |
|
140 |
self flushBuffer |
|
141 |
||
142 |
"Modified: 14.10.1997 / 18:58:06 / cg" |
|
143 |
! |
|
144 |
||
145 |
flushBuffer |
|
146 |
bufStream isEmpty ifTrue: [^ self]. |
|
147 |
outStream nextPut: bufStream size. |
|
148 |
outStream nextPutAll: bufStream contents. |
|
149 |
bufStream := WriteStream on: (ByteArray new: 256) |
|
150 |
||
151 |
"Modified: 14.10.1997 / 20:46:04 / cg" |
|
152 |
! |
|
153 |
||
154 |
flushCode |
|
155 |
self flushBits |
|
156 |
||
157 |
"Created: 14.10.1997 / 18:57:33 / cg" |
|
158 |
! |
|
159 |
||
710 | 160 |
nextBitsPut: anInteger |
161 |
| integer writeBitCount shiftCount | |
|
162 |
shiftCount _ 0. |
|
163 |
remainBitCount = 0 |
|
164 |
ifTrue: |
|
165 |
[writeBitCount _ 8. |
|
166 |
integer _ anInteger] |
|
167 |
ifFalse: |
|
168 |
[writeBitCount _ remainBitCount. |
|
169 |
integer _ bufByte + (anInteger bitShift: 8 - remainBitCount)]. |
|
170 |
[writeBitCount < codeSize] |
|
171 |
whileTrue: |
|
172 |
[self nextBytePut: ((integer bitShift: shiftCount) bitAnd: 255). |
|
173 |
shiftCount _ shiftCount - 8. |
|
174 |
writeBitCount _ writeBitCount + 8]. |
|
175 |
(remainBitCount _ writeBitCount - codeSize) = 0 |
|
176 |
ifTrue: [self nextBytePut: (integer bitShift: shiftCount)] |
|
177 |
ifFalse: [bufByte _ integer bitShift: shiftCount]. |
|
178 |
^anInteger |
|
708 | 179 |
|
710 | 180 |
"Modified: 15.10.1997 / 16:50:30 / cg" |
708 | 181 |
! |
182 |
||
710 | 183 |
nextBytePut: aByte |
184 |
bufStream nextPut: aByte. |
|
185 |
bufStream size >= 254 ifTrue: [self flushBuffer] |
|
708 | 186 |
|
710 | 187 |
"Modified: 15.10.1997 / 16:50:52 / cg" |
708 | 188 |
! |
189 |
||
710 | 190 |
readPixelFrom: bits |
191 |
| pixel | |
|
708 | 192 |
ypos >= height ifTrue: [^ nil]. |
710 | 193 |
pixel := bits at: ypos * rowByteSize + xpos + 1. |
708 | 194 |
self updatePixelPosition. |
710 | 195 |
^ pixel |
708 | 196 |
|
197 |
"Created: 14.10.1997 / 18:43:50 / cg" |
|
710 | 198 |
"Modified: 15.10.1997 / 16:46:43 / cg" |
708 | 199 |
! |
200 |
||
201 |
setParameters:bitsPerPixel |
|
202 |
clearCode := 1 bitShift:bitsPerPixel. |
|
203 |
eoiCode := clearCode + 1. |
|
204 |
freeCode := clearCode + 2. |
|
205 |
codeSize := bitsPerPixel + 1. |
|
206 |
maxCode := (1 bitShift: codeSize) - 1 |
|
207 |
||
208 |
"Modified: 14.10.1997 / 20:09:48 / cg" |
|
209 |
! |
|
210 |
||
211 |
updatePixelPosition |
|
212 |
(xpos _ xpos + 1) >= width ifFalse: [^ self]. |
|
213 |
xpos _ 0. |
|
214 |
interlace == true |
|
215 |
ifFalse: |
|
216 |
[ypos _ ypos + 1. |
|
217 |
^ self]. |
|
218 |
pass = 0 |
|
219 |
ifTrue: |
|
220 |
[(ypos _ ypos + 8) >= height |
|
221 |
ifTrue: |
|
222 |
[pass _ pass + 1. |
|
223 |
ypos _ 4]. |
|
224 |
^ self]. |
|
225 |
pass = 1 |
|
226 |
ifTrue: |
|
227 |
[(ypos _ ypos + 8) >= height |
|
228 |
ifTrue: |
|
229 |
[pass _ pass + 1. |
|
230 |
ypos _ 2]. |
|
231 |
^ self]. |
|
232 |
pass = 2 |
|
233 |
ifTrue: |
|
234 |
[(ypos _ ypos + 4) >= height |
|
235 |
ifTrue: |
|
236 |
[pass _ pass + 1. |
|
237 |
ypos _ 1]. |
|
238 |
^ self]. |
|
239 |
pass = 3 |
|
240 |
ifTrue: |
|
241 |
[ypos _ ypos + 2. |
|
242 |
^ self]. |
|
243 |
^ self error: 'can''t happen' |
|
244 |
||
245 |
"Modified: 14.10.1997 / 18:44:27 / cg" |
|
246 |
! |
|
247 |
||
710 | 248 |
writeCode: aCode |
249 |
self nextBitsPut: aCode |
|
708 | 250 |
|
251 |
"Created: 14.10.1997 / 18:38:35 / cg" |
|
710 | 252 |
"Modified: 15.10.1997 / 17:01:47 / cg" |
708 | 253 |
! |
254 |
||
255 |
writeCodeAndCheckCodeSize: t1 |
|
256 |
self writeCode: t1. |
|
257 |
self checkCodeSize |
|
258 |
||
259 |
"Created: 14.10.1997 / 18:38:24 / cg" |
|
260 |
"Modified: 14.10.1997 / 18:40:56 / cg" |
|
261 |
! ! |
|
262 |
||
0 | 263 |
!GIFReader methodsFor:'reading from file'! |
264 |
||
1 | 265 |
checkGreyscaleColormap |
239 | 266 |
"return true, if colormap is actually a greymap. |
267 |
Could be used to convert it into a greyScale image - which is not yet done." |
|
1 | 268 |
|
20 | 269 |
|sz "{ Class: SmallInteger }" |
270 |
redVal| |
|
271 |
||
272 |
sz := redMap size. |
|
273 |
||
274 |
1 to:sz do:[:i | |
|
239 | 275 |
redVal := redMap at:i. |
276 |
redVal ~~ (greenMap at:i) ifTrue:[^ false]. |
|
277 |
redVal ~~ (blueMap at:i) ifTrue:[^ false]. |
|
1 | 278 |
]. |
279 |
^ true |
|
239 | 280 |
|
281 |
"Modified: 2.5.1996 / 17:54:40 / cg" |
|
1 | 282 |
! |
283 |
||
43 | 284 |
fromStream:aStream |
192 | 285 |
"read a stream containing a GIF image. |
286 |
Leave image description in instance variables." |
|
43 | 287 |
|
708 | 288 |
|byte index flag count fileColorMap |
43 | 289 |
colorMapSize bitsPerPixel scrWidth scrHeight |
290 |
hasColorMap hasLocalColorMap interlaced id |
|
291 |
leftOffs topOffs codeLen |
|
292 |
compressedData compressedSize |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
293 |
tmp srcOffset dstOffset isGif89 atEnd |
64 | 294 |
h "{ Class: SmallInteger }"| |
43 | 295 |
|
296 |
inStream := aStream. |
|
297 |
aStream binary. |
|
298 |
||
299 |
"GIF-files are always lsb (intel-world)" |
|
300 |
byteOrder := #lsb. |
|
301 |
||
331
304f9c439efa
allow reading from ByteArray-readStreams
Claus Gittinger <cg@exept.de>
parents:
315
diff
changeset
|
302 |
id := ByteArray new:6. |
304f9c439efa
allow reading from ByteArray-readStreams
Claus Gittinger <cg@exept.de>
parents:
315
diff
changeset
|
303 |
aStream nextBytes:6 into:id startingAt:1. |
304f9c439efa
allow reading from ByteArray-readStreams
Claus Gittinger <cg@exept.de>
parents:
315
diff
changeset
|
304 |
id := id asString. |
43 | 305 |
|
306 |
"all I had for testing where GIF87a files; |
|
307 |
I hope later versions work too ..." |
|
308 |
||
154 | 309 |
isGif89 := false. |
43 | 310 |
(id ~= 'GIF87a') ifTrue:[ |
154 | 311 |
(id startsWith:'GIF') ifFalse:[ |
355 | 312 |
'GIFReader [info]: not a gif file' infoPrintCR. |
154 | 313 |
^ nil |
314 |
]. |
|
315 |
id ~= 'GIF89a' ifTrue:[ |
|
355 | 316 |
'GIFReader [info]: not a GIF87a/GIF89a file - hope that works' infoPrintCR. |
154 | 317 |
] |
43 | 318 |
]. |
319 |
||
320 |
"get screen dimensions (not used)" |
|
321 |
scrWidth := aStream nextShortMSB:false. |
|
322 |
scrHeight := aStream nextShortMSB:false. |
|
323 |
||
324 |
"get flag byte" |
|
325 |
flag := aStream nextByte. |
|
326 |
hasColorMap := (flag bitAnd:2r10000000) ~~ 0. |
|
327 |
"bitsPerRGB := ((flag bitAnd:2r01110000) bitShift:-4) + 1. " |
|
328 |
"colorMapSorted := ((flag bitAnd:2r00001000) ~~ 0. " |
|
329 |
bitsPerPixel := (flag bitAnd:2r00000111) + 1. |
|
330 |
colorMapSize := 1 bitShift:bitsPerPixel. |
|
331 |
||
332 |
"get background (not used)" |
|
333 |
aStream nextByte. |
|
334 |
||
335 |
"aspect ratio (not used)" |
|
336 |
aStream nextByte. |
|
337 |
||
338 |
"get colorMap" |
|
339 |
hasColorMap ifTrue:[ |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
340 |
self readColorMap:colorMapSize. |
708 | 341 |
fileColorMap := Colormap |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
342 |
redVector:redMap |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
343 |
greenVector:greenMap |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
344 |
blueVector:blueMap. |
43 | 345 |
]. |
708 | 346 |
colorMap := fileColorMap. |
43 | 347 |
|
348 |
photometric := #palette. |
|
349 |
samplesPerPixel := 1. |
|
350 |
bitsPerSample := #(8). |
|
351 |
||
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
352 |
atEnd := false. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
353 |
[atEnd] whileFalse:[ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
354 |
"gif89a extensions" |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
355 |
byte := aStream nextByte. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
356 |
|
708 | 357 |
byte == Extension ifTrue:[ |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
358 |
self readExtension:aStream. |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
359 |
] ifFalse:[ |
708 | 360 |
(byte == Terminator) ifTrue:[ |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
361 |
atEnd := true |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
362 |
] ifFalse:[ |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
363 |
"must be image separator" |
708 | 364 |
(byte ~~ ImageSeparator) ifTrue:[ |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
365 |
('GIFReader [info]: corrupted gif file (no IMAGESEP): ' , (byte printStringRadix:16)) infoPrintCR. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
366 |
^ nil |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
367 |
]. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
368 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
369 |
"get image data" |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
370 |
leftOffs := aStream nextShortMSB:false. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
371 |
topOffs := aStream nextShortMSB:false. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
372 |
width := aStream nextShortMSB:false. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
373 |
height := aStream nextShortMSB:false. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
374 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
375 |
dimensionCallBack notNil ifTrue:[ |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
376 |
dimensionCallBack value:self |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
377 |
]. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
378 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
379 |
"/ |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
380 |
"/ 'width ' print. width printNewline. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
381 |
"/ 'height ' print. height printNewline. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
382 |
"/ |
43 | 383 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
384 |
"another flag byte" |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
385 |
flag := aStream nextByte. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
386 |
interlaced := (flag bitAnd:2r01000000) ~~ 0. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
387 |
hasLocalColorMap := (flag bitAnd:2r10000000) ~~ 0. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
388 |
"localColorMapSorted := (flag bitAnd:2r00100000) ~~ 0. " |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
389 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
390 |
"if image has a local colormap, this one is used" |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
391 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
392 |
hasLocalColorMap ifTrue:[ |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
393 |
"local descr. overwrites" |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
394 |
bitsPerPixel := (flag bitAnd:2r00000111) + 1. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
395 |
colorMapSize := 1 bitShift:bitsPerPixel. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
396 |
"overwrite colormap" |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
397 |
self readColorMap:colorMapSize. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
398 |
colorMap := Colormap |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
399 |
redVector:redMap |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
400 |
greenVector:greenMap |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
401 |
blueVector:blueMap. |
708 | 402 |
] ifFalse:[ |
403 |
colorMap := fileColorMap |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
404 |
]. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
405 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
406 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
407 |
"get codelen for decompression" |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
408 |
codeLen := aStream nextByte. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
409 |
|
691 | 410 |
compressedData := ByteArray uninitializedNew:(aStream fileSize). |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
411 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
412 |
"get compressed data" |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
413 |
index := 1. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
414 |
count := aStream nextByte. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
415 |
[count notNil and:[count ~~ 0]] whileTrue:[ |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
416 |
aStream nextBytes:count into:compressedData startingAt:index blockSize:4096. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
417 |
index := index + count. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
418 |
count := aStream nextByte |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
419 |
]. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
420 |
compressedSize := index - 1. |
83 | 421 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
422 |
h := height. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
423 |
data := ByteArray new:((width + 1) * (h + 1)). |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
424 |
"/ 'GIFReader: decompressing ...' infoPrintCR. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
425 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
426 |
self class decompressGIFFrom:compressedData |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
427 |
count:compressedSize |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
428 |
into:data |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
429 |
startingAt:1 |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
430 |
codeLen:(codeLen + 1). |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
431 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
432 |
interlaced ifTrue:[ |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
433 |
"/ 'GIFREADER: deinterlacing ...' infoPrintCR. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
434 |
tmp := ByteArray new:(data size). |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
435 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
436 |
"phase 1: 0, 8, 16, 24, ..." |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
437 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
438 |
srcOffset := 1. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
439 |
0 to:(h - 1) by:8 do:[:dstRow | |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
440 |
dstOffset := dstRow * width + 1. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
441 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
442 |
with:data startingAt:srcOffset. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
443 |
srcOffset := srcOffset + width. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
444 |
]. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
445 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
446 |
"phase 2: 4, 12, 20, 28, ..." |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
447 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
448 |
4 to:(h - 1) by:8 do:[:dstRow | |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
449 |
dstOffset := dstRow * width + 1. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
450 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
451 |
with:data startingAt:srcOffset. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
452 |
srcOffset := srcOffset + width. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
453 |
]. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
454 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
455 |
"phase 3: 2, 6, 10, 14, ..." |
43 | 456 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
457 |
2 to:(h - 1) by:4 do:[:dstRow | |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
458 |
dstOffset := dstRow * width + 1. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
459 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
460 |
with:data startingAt:srcOffset. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
461 |
srcOffset := srcOffset + width. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
462 |
]. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
463 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
464 |
"phase 4: 1, 3, 5, 7, ..." |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
465 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
466 |
1 to:(h - 1) by:2 do:[:dstRow | |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
467 |
dstOffset := dstRow * width + 1. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
468 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1) |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
469 |
with:data startingAt:srcOffset. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
470 |
srcOffset := srcOffset + width. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
471 |
]. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
472 |
|
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
473 |
data := tmp. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
474 |
tmp := nil. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
475 |
]. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
476 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
477 |
imageSequence isNil ifTrue:[ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
478 |
imageSequence := OrderedCollection new. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
479 |
]. |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
480 |
maskPixel notNil ifTrue:[ |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
481 |
"/ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
482 |
"/ ok, there is a maskValue |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
483 |
"/ build a Depth1Image for it. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
484 |
"/ |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
485 |
self buildMaskFromColor:maskPixel |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
486 |
]. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
487 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
488 |
imageSequence add:(self image). |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
489 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
490 |
aStream atEnd ifTrue:[ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
491 |
atEnd := true. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
492 |
] |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
493 |
] |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
494 |
]. |
307
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
495 |
]. |
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
496 |
|
43 | 497 |
" |
498 |
GIFReader fromFile:'../fileIn/bitmaps/claus.gif |
|
499 |
GIFReader fromFile:'../fileIn/bitmaps/garfield.gif' |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
500 |
GIFReader new fromStream:('/home2/cg/.misc/circum.gif' asFilename readStream) |
43 | 501 |
" |
154 | 502 |
|
315
506e0ce38534
Moved #buildMask from GIFReader to ImageReader>>#buildMaskFromColor:
Stefan Vogel <sv@exept.de>
parents:
312
diff
changeset
|
503 |
"Modified: 5.7.1996 / 17:32:01 / stefan" |
708 | 504 |
"Modified: 14.10.1997 / 20:45:57 / cg" |
135 | 505 |
! |
506 |
||
507 |
makeGreyscale |
|
508 |
"not yet implemented/needed" |
|
509 |
! |
|
510 |
||
511 |
readColorMap:colorMapSize |
|
512 |
"get gif colormap consisting of colorMapSize entries" |
|
513 |
||
514 |
|sz "{ Class: SmallInteger }"| |
|
515 |
||
516 |
redMap := ByteArray uninitializedNew:colorMapSize. |
|
517 |
greenMap := ByteArray uninitializedNew:colorMapSize. |
|
518 |
blueMap := ByteArray uninitializedNew:colorMapSize. |
|
239 | 519 |
|
135 | 520 |
sz := colorMapSize. |
521 |
1 to:sz do:[:i | |
|
239 | 522 |
redMap at:i put:(inStream nextByte). |
523 |
greenMap at:i put:(inStream nextByte). |
|
524 |
blueMap at:i put:(inStream nextByte) |
|
307
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
525 |
]. |
239 | 526 |
|
307
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
527 |
"Modified: 21.6.1996 / 12:32:43 / cg" |
154 | 528 |
! |
529 |
||
530 |
readExtension:aStream |
|
239 | 531 |
"get gif89 extension - this is currently ignored" |
154 | 532 |
|
533 |
|type blockSize subBlockSize |
|
304 | 534 |
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
|
535 |
animationType animationTime animationMask |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
536 |
appID appAUTH |
625 | 537 |
iterationCount b ok| |
154 | 538 |
|
539 |
type := aStream nextByte. |
|
540 |
type == $R asciiValue ifTrue:[ |
|
239 | 541 |
"/ |
154 | 542 |
"/ Ratio extension |
239 | 543 |
"/ |
354
b4d2ce853c24
stefans suggested info/warning/error/fatal
Claus Gittinger <cg@exept.de>
parents:
339
diff
changeset
|
544 |
'GIFREADER [info]: ratio extension ignored' infoPrintCR. |
154 | 545 |
blockSize := aStream nextByte. |
546 |
(blockSize == 2) ifTrue:[ |
|
547 |
aspNum := aStream nextByte. |
|
548 |
aspDen := aStream nextByte |
|
549 |
] ifFalse:[ |
|
550 |
aStream skip:blockSize |
|
551 |
]. |
|
552 |
"/ eat subblocks |
|
553 |
||
554 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
|
555 |
aStream skip:subBlockSize |
|
239 | 556 |
]. |
557 |
^ self |
|
558 |
]. |
|
559 |
||
560 |
type == 16r01 ifTrue:[ |
|
561 |
"/ |
|
562 |
"/ plaintext extension |
|
563 |
"/ |
|
625 | 564 |
"/ 'GIFREADER [info]: plaintext extension ignored' infoPrintCR. |
239 | 565 |
subBlockSize := aStream nextByte. |
566 |
left := aStream nextShortMSB:false. |
|
567 |
top := aStream nextShortMSB:false. |
|
568 |
width := aStream nextShortMSB:false. |
|
569 |
height := aStream nextShortMSB:false. |
|
570 |
cWidth := aStream nextByte. |
|
571 |
cHeight := aStream nextByte. |
|
572 |
fg := aStream nextByte. |
|
573 |
bg := aStream nextByte. |
|
574 |
aStream skip:12. |
|
575 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
|
576 |
aStream skip:subBlockSize |
|
577 |
]. |
|
578 |
^ self |
|
579 |
]. |
|
580 |
||
581 |
type == 16rF9 ifTrue:[ |
|
582 |
"/ |
|
583 |
"/ graphic control extension |
|
584 |
"/ |
|
354
b4d2ce853c24
stefans suggested info/warning/error/fatal
Claus Gittinger <cg@exept.de>
parents:
339
diff
changeset
|
585 |
"/ 'GIFREADER [info]: graphic control extension ignored' infoPrintCR. |
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
586 |
|
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
587 |
[(subBlockSize := aStream nextByte) ~~ 0] whileTrue:[ |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
588 |
"/ type bitAnd:1 means: animationMask is transparent pixel |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
589 |
"/ to be implemented in Image ... |
305 | 590 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
591 |
animationType := aStream nextByte. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
592 |
animationTime := aStream nextShortMSB:false. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
593 |
animationMask := aStream nextByte. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
594 |
subBlockSize := subBlockSize - 4. |
307
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
595 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
596 |
(animationType bitTest: 1) ifTrue:[ |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
597 |
maskPixel := animationMask. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
598 |
"/ 'GIFREADER [info]: mask: ' infoPrint. (maskPixel printStringRadix:16) infoPrintCR. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
599 |
]. |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
600 |
"/ 'GIFREADER [info]: animationTime: ' infoPrint. (animationTime * (1/100)) infoPrintCR. |
307
9eaeaca798a5
create a mask-image, if present
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
601 |
|
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
602 |
subBlockSize ~~ 0 ifTrue:[ |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
603 |
aStream skip:subBlockSize |
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
604 |
]. |
239 | 605 |
]. |
606 |
^ self |
|
607 |
]. |
|
608 |
||
519 | 609 |
type == 16rFE ifTrue:[ |
610 |
"/ |
|
611 |
"/ comment extension |
|
612 |
"/ |
|
625 | 613 |
"/ 'GIFREADER [info]: comment extension ignored' infoPrintCR. |
519 | 614 |
[(blockSize := aStream nextByte) ~~ 0] whileTrue:[ |
615 |
aStream skip:blockSize |
|
616 |
]. |
|
617 |
^ self |
|
618 |
]. |
|
619 |
||
239 | 620 |
type == 16rFF ifTrue:[ |
621 |
"/ |
|
622 |
"/ application extension |
|
623 |
"/ |
|
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
624 |
subBlockSize := aStream nextByte. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
625 |
appID := (aStream nextBytes:8 ) asString. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
626 |
appAUTH := aStream nextBytes:3. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
627 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
628 |
subBlockSize := aStream nextByte. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
629 |
|
625 | 630 |
ok := false. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
631 |
appID = 'NETSCAPE' ifTrue:[ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
632 |
appAUTH asString = '2.0' ifTrue:[ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
633 |
subBlockSize == 3 ifTrue:[ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
634 |
b := aStream nextByte. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
635 |
iterationCount := aStream nextShortMSB:false. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
636 |
subBlockSize := aStream nextByte. |
625 | 637 |
ok := true. |
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
638 |
] |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
639 |
] |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
640 |
]. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
641 |
|
625 | 642 |
ok ifFalse:[ |
643 |
('GIFREADER [info]: application extension (' , appID , ') ignored') infoPrintCR. |
|
644 |
]. |
|
645 |
||
618
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
646 |
[subBlockSize > 0] whileTrue:[ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
647 |
aStream skip:subBlockSize. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
648 |
subBlockSize := aStream nextByte. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
649 |
]. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
650 |
^ self |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
651 |
]. |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
652 |
|
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
653 |
type == 16r2C ifTrue:[ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
654 |
"/ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
655 |
"/ image descriptor extension |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
656 |
"/ |
ab83e72fd105
support multiple images (animated gif)
Claus Gittinger <cg@exept.de>
parents:
563
diff
changeset
|
657 |
'GIFREADER [info]: image descriptor extension ignored' infoPrintCR. |
239 | 658 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
659 |
aStream skip:subBlockSize |
|
660 |
]. |
|
661 |
^ self |
|
662 |
]. |
|
663 |
||
664 |
"/ |
|
665 |
"/ unknown extension |
|
666 |
"/ |
|
354
b4d2ce853c24
stefans suggested info/warning/error/fatal
Claus Gittinger <cg@exept.de>
parents:
339
diff
changeset
|
667 |
'GIFREADER [info]: unknown extension ignored' infoPrintCR. |
239 | 668 |
[(subBlockSize := aStream nextByte) > 0] whileTrue:[ |
669 |
aStream skip:subBlockSize |
|
154 | 670 |
] |
671 |
||
670
6a7f628ee66c
oops - mask was only set for interlaced images (stupid typo)
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
672 |
"Modified: 24.7.1997 / 18:02:49 / cg" |
0 | 673 |
! ! |
43 | 674 |
|
708 | 675 |
!GIFReader methodsFor:'writing to file'! |
676 |
||
677 |
save:image onFile:aFileName |
|
678 |
"save image as GIF file on aFileName" |
|
679 |
||
680 |
outStream := FileStream newFileNamed:aFileName. |
|
681 |
outStream isNil ifTrue:[ |
|
682 |
^ Image fileCreationErrorSignal |
|
683 |
raiseWith:image |
|
684 |
errorString:('file creation error: ' , aFileName asString). |
|
685 |
]. |
|
686 |
outStream binary. |
|
687 |
||
688 |
image mask notNil ifTrue:[ |
|
689 |
Image informationLostQuerySignal |
|
690 |
raiseWith:image |
|
691 |
errorString:('GFF writer does not (yet) support an imageMask'). |
|
692 |
]. |
|
693 |
||
694 |
byteOrder := #lsb. |
|
695 |
width := image width. |
|
696 |
height := image height. |
|
697 |
photometric := image photometric. |
|
698 |
samplesPerPixel := image samplesPerPixel. |
|
699 |
bitsPerSample := image bitsPerSample. |
|
700 |
colorMap := image colorMap. |
|
701 |
data := image bits. |
|
702 |
||
703 |
self writeHeaderFor:image. |
|
704 |
self writeBitDataFor:image. |
|
705 |
outStream close. |
|
706 |
||
707 |
" |
|
708 |
|i| |
|
709 |
||
710 |
i := Image fromFile:'bitmaps/gifImages/garfield.gif'. |
|
711 |
GIFReader save:i onFile:'foo.gif'. |
|
712 |
(Image fromFile:'./foo.gif') inspect |
|
713 |
" |
|
714 |
||
715 |
"Created: 14.10.1997 / 17:40:12 / cg" |
|
716 |
"Modified: 14.10.1997 / 18:59:22 / cg" |
|
717 |
! |
|
718 |
||
710 | 719 |
writeBitDataFor: image |
720 |
"using modified Lempel-Ziv Welch algorithm." |
|
721 |
||
722 |
| bits bitsPerPixel |
|
723 |
maxBits maxMaxCode tSize initCodeSize ent tShift fCode pixel index disp nomatch | |
|
724 |
||
725 |
bitsPerPixel := image bitsPerPixel. |
|
726 |
bits := image bits. |
|
727 |
||
728 |
pass := 0. |
|
729 |
xpos := 0. |
|
730 |
ypos := 0. |
|
712 | 731 |
rowByteSize := image bytesPerRow. "/ width * 8 + 31 // 32 * 4. |
710 | 732 |
remainBitCount := 0. |
733 |
bufByte := 0. |
|
734 |
bufStream := WriteStream on: (ByteArray new: 256). |
|
708 | 735 |
|
710 | 736 |
maxBits := 12. |
737 |
maxMaxCode := 1 bitShift: maxBits. |
|
738 |
tSize := 5003. |
|
739 |
prefixTable := Array new: tSize. |
|
740 |
suffixTable := Array new: tSize. |
|
741 |
||
742 |
initCodeSize := bitsPerPixel <= 1 ifTrue: [2] ifFalse: [bitsPerPixel]. |
|
743 |
outStream nextPut: initCodeSize. |
|
744 |
self setParameters: initCodeSize. |
|
745 |
||
746 |
tShift := 0. |
|
747 |
fCode := tSize. |
|
748 |
[fCode < 65536] whileTrue: |
|
749 |
[tShift := tShift + 1. |
|
750 |
fCode := fCode * 2]. |
|
751 |
tShift := 8 - tShift. |
|
752 |
1 to: tSize do: [:i | suffixTable at: i put: -1]. |
|
708 | 753 |
|
710 | 754 |
self writeCodeAndCheckCodeSize: clearCode. |
755 |
ent := self readPixelFrom: bits. |
|
756 |
[(pixel := self readPixelFrom: bits) == nil] whileFalse: |
|
708 | 757 |
[ |
710 | 758 |
fCode := (pixel bitShift: maxBits) + ent. |
759 |
index := ((pixel bitShift: tShift) bitXor: ent) + 1. |
|
760 |
(suffixTable at: index) = fCode |
|
761 |
ifTrue: [ent := prefixTable at: index] |
|
762 |
ifFalse: |
|
763 |
[nomatch := true. |
|
764 |
(suffixTable at: index) >= 0 |
|
765 |
ifTrue: |
|
766 |
[disp := tSize - index + 1. |
|
767 |
index = 1 ifTrue: [disp := 1]. |
|
768 |
"probe" |
|
769 |
[(index := index - disp) < 1 ifTrue: [index := index + tSize]. |
|
770 |
(suffixTable at: index) = fCode |
|
771 |
ifTrue: |
|
772 |
[ent := prefixTable at: index. |
|
773 |
nomatch := false. |
|
774 |
"continue whileFalse:"]. |
|
775 |
nomatch and: [(suffixTable at: index) > 0]] |
|
776 |
whileTrue: ["probe"]]. |
|
777 |
"nomatch" |
|
778 |
nomatch ifTrue: |
|
779 |
[self writeCodeAndCheckCodeSize: ent. |
|
780 |
ent := pixel. |
|
781 |
freeCode < maxMaxCode |
|
782 |
ifTrue: |
|
783 |
[prefixTable at: index put: freeCode. |
|
784 |
suffixTable at: index put: fCode. |
|
785 |
freeCode := freeCode + 1] |
|
786 |
ifFalse: |
|
787 |
[self writeCodeAndCheckCodeSize: clearCode. |
|
788 |
1 to: tSize do: [:i | suffixTable at: i put: -1]. |
|
789 |
self setParameters: initCodeSize]]]]. |
|
790 |
prefixTable := suffixTable := nil. |
|
791 |
self writeCodeAndCheckCodeSize: ent. |
|
792 |
self writeCodeAndCheckCodeSize: eoiCode. |
|
793 |
self flushCode. |
|
708 | 794 |
|
710 | 795 |
outStream nextPut: 0. "zero-length packet" |
796 |
outStream nextPut: Terminator. |
|
797 |
||
712 | 798 |
"Modified: 15.10.1997 / 19:56:28 / cg" |
708 | 799 |
! |
800 |
||
801 |
writeHeaderFor:image |
|
802 |
"save image as GIF file on aFileName" |
|
803 |
||
804 |
|bitsPerPixel t1 n| |
|
805 |
||
806 |
bitsPerPixel := image bitsPerPixel. |
|
807 |
||
808 |
outStream nextPutAll: 'GIF87a' asByteArray. |
|
712 | 809 |
self writeShort:width. "/ screen size |
810 |
self writeShort:height. |
|
708 | 811 |
t1 := 128. |
712 | 812 |
t1 := t1 bitOr:(bitsPerPixel - 1 bitShift:5). |
813 |
t1 := t1 bitOr:(bitsPerPixel - 1). |
|
814 |
outStream nextPut:t1. "/ flag |
|
815 |
outStream nextPut:0. "/ background (not used) |
|
816 |
outStream nextPut:0. "/ aspect ratio |
|
708 | 817 |
|
713 | 818 |
0 to:(1 bitShift:bitsPerPixel)-1 do:[:pixel | |
819 |
|clr red green blue| |
|
712 | 820 |
|
713 | 821 |
clr := image colorFromValue:pixel. |
822 |
clr isNil ifTrue:[ |
|
823 |
"/ unused colorMap slot |
|
824 |
red := green := blue := 0. |
|
825 |
] ifFalse:[ |
|
826 |
red := (clr redByte). |
|
827 |
green := (clr greenByte). |
|
828 |
blue := (clr blueByte). |
|
829 |
]. |
|
830 |
outStream |
|
831 |
nextPut:red; nextPut:green; nextPut:blue. |
|
832 |
]. |
|
833 |
"/ n := 0. |
|
834 |
"/ image colorMap notNil ifTrue:[ |
|
835 |
"/ image colorMap do:[:clr | |
|
836 |
"/ |red green blue| |
|
837 |
"/ |
|
838 |
"/ clr isNil ifTrue:[ |
|
839 |
"/ "/ unused colorMap slot |
|
840 |
"/ red := green := blue := 0. |
|
841 |
"/ ] ifFalse:[ |
|
842 |
"/ red := (clr redByte). |
|
843 |
"/ green := (clr greenByte). |
|
844 |
"/ blue := (clr blueByte). |
|
845 |
"/ ]. |
|
846 |
"/ outStream |
|
847 |
"/ nextPut:red; nextPut:green; nextPut:blue. |
|
848 |
"/ n := n + 1. |
|
849 |
"/ ] |
|
850 |
"/ ]. |
|
851 |
"/ n+1 to:(1 bitShift:bitsPerPixel) do:[:i | |
|
852 |
"/ outStream nextPut:0; nextPut:0; nextPut:0 |
|
853 |
"/ ]. |
|
708 | 854 |
outStream nextPut:ImageSeparator. |
855 |
self writeShort:0. "/ |
|
856 |
self writeShort:0. "/ |
|
857 |
self writeShort:width. "/ image size |
|
858 |
self writeShort:height. |
|
859 |
||
860 |
interlace == true ifTrue:[ |
|
861 |
t1 := 64 |
|
862 |
] ifFalse:[ |
|
863 |
t1 := 0 |
|
864 |
]. |
|
865 |
outStream nextPut:t1 "/ another flag |
|
866 |
||
867 |
"Created: 14.10.1997 / 17:41:28 / cg" |
|
713 | 868 |
"Modified: 21.10.1997 / 04:52:18 / cg" |
708 | 869 |
! ! |
870 |
||
339 | 871 |
!GIFReader class methodsFor:'documentation'! |
135 | 872 |
|
873 |
version |
|
713 | 874 |
^ '$Header: /cvs/stx/stx/libview2/GIFReader.st,v 1.60 1997-10-21 18:26:40 cg Exp $' |
135 | 875 |
! ! |
43 | 876 |
GIFReader initialize! |