0
|
1 |
"
|
6
|
2 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
0
|
3 |
All Rights Reserved
|
|
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 |
|
|
13 |
ImageReader subclass:#GIFReader
|
|
14 |
instanceVariableNames:'redMap greenMap blueMap'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Graphics-Support'
|
|
18 |
!
|
|
19 |
|
|
20 |
GIFReader comment:'
|
|
21 |
|
6
|
22 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
0
|
23 |
All Rights Reserved
|
|
24 |
|
6
|
25 |
$Header: /cvs/stx/stx/libview2/GIFReader.st,v 1.4 1993-10-13 02:43:29 claus Exp $
|
0
|
26 |
|
|
27 |
written Dec 91 by claus
|
|
28 |
'!
|
|
29 |
|
|
30 |
!GIFReader class methodsFor:'documentation'!
|
|
31 |
|
|
32 |
documentation
|
|
33 |
"
|
|
34 |
this class provides methods for loading and saving GIF pictures.
|
|
35 |
It has been tested with some different GIF89a pictures, I dont
|
|
36 |
know, if it works with other GIF versions.
|
|
37 |
GIF extension blocks are not handled.
|
|
38 |
|
|
39 |
GIF file writing is not implemented (use TIFF).
|
|
40 |
|
|
41 |
legal stuff extracted from GIF89a documentation:
|
|
42 |
|
|
43 |
CompuServe Incorporated hereby grants a limited, non-exclusive, royalty-free
|
|
44 |
license for the use of the Graphics Interchange Format(sm) in computer
|
|
45 |
software; computer software utilizing GIF(sm) must acknowledge ownership of the
|
|
46 |
Graphics Interchange Format and its Service Mark by CompuServe Incorporated, in
|
|
47 |
User and Technical Documentation.
|
|
48 |
|
|
49 |
The Graphics Interchange Format(c) is the Copyright property of
|
|
50 |
CompuServe Incorporated. GIF(sm) is a Service Mark property of
|
|
51 |
CompuServe Incorporated.
|
|
52 |
"
|
|
53 |
! !
|
|
54 |
|
1
|
55 |
!GIFReader class methodsFor:'testing'!
|
|
56 |
|
|
57 |
isValidImageFile:aFileName
|
|
58 |
"return true, if aFileName contains a GIF image"
|
|
59 |
|
|
60 |
|id inStream|
|
|
61 |
|
|
62 |
inStream := FileStream readonlyFileNamed:aFileName.
|
|
63 |
inStream isNil ifTrue:[^ false].
|
|
64 |
|
|
65 |
id := String new:6.
|
|
66 |
inStream nextBytes:6 into:id.
|
|
67 |
inStream close.
|
|
68 |
|
|
69 |
(id = 'GIF87a') ifFalse:[
|
|
70 |
(id startsWith:'GIF') ifFalse:[^ false].
|
|
71 |
|
|
72 |
'not GIF87a - untested' printNewline.
|
|
73 |
].
|
|
74 |
^ true
|
|
75 |
! !
|
|
76 |
|
0
|
77 |
!GIFReader methodsFor:'reading from file'!
|
|
78 |
|
|
79 |
fromFile:aFileName
|
|
80 |
"read a GIF file"
|
|
81 |
|
1
|
82 |
|byte index flag count
|
0
|
83 |
colorMapSize bitsPerPixel scrWidth scrHeight
|
|
84 |
hasColorMap hasLocalColorMap interlaced id
|
|
85 |
leftOffs topOffs codeLen
|
|
86 |
compressedData compressedSize
|
|
87 |
tmp srcOffset dstOffset|
|
|
88 |
|
|
89 |
inStream := FileStream readonlyFileNamed:aFileName.
|
|
90 |
inStream isNil ifTrue:[
|
|
91 |
'open error' printNewline.
|
|
92 |
^ nil
|
|
93 |
].
|
|
94 |
|
1
|
95 |
inStream binary.
|
|
96 |
|
|
97 |
"GIF-files are always lsb (intel-world)"
|
0
|
98 |
byteOrder := #lsb.
|
|
99 |
|
|
100 |
id := String new:6.
|
|
101 |
inStream nextBytes:6 into:id.
|
|
102 |
|
|
103 |
"all I had for testing where GIF87a files;
|
|
104 |
I hope later versions work too ..."
|
|
105 |
|
|
106 |
(id ~= 'GIF87a') ifTrue:[
|
|
107 |
(id startsWith:'GIF') ifFalse:[
|
|
108 |
'not a gif file' printNewline.
|
|
109 |
inStream close.
|
|
110 |
^ nil
|
|
111 |
].
|
|
112 |
'not a gif87a file - hope that works' printNewline.
|
|
113 |
].
|
|
114 |
|
|
115 |
"get screen dimensions (not used)"
|
|
116 |
|
1
|
117 |
scrWidth := inStream nextShortMSB:false.
|
|
118 |
scrHeight := inStream nextShortMSB:false.
|
0
|
119 |
|
|
120 |
"get flag byte"
|
1
|
121 |
flag := inStream nextByte.
|
|
122 |
hasColorMap := (flag bitAnd:2r10000000) ~~ 0.
|
|
123 |
"bitsPerRGB := ((flag bitAnd:2r01110000) bitShift:-4) + 1. "
|
|
124 |
"colorMapSorted := ((flag bitAnd:2r00001000) ~~ 0. "
|
|
125 |
bitsPerPixel := (flag bitAnd:2r00000111) + 1.
|
0
|
126 |
colorMapSize := 1 bitShift:bitsPerPixel.
|
|
127 |
|
|
128 |
"get background (not used)"
|
1
|
129 |
inStream nextByte.
|
0
|
130 |
|
1
|
131 |
"aspect ratio (not used)"
|
|
132 |
inStream nextByte.
|
0
|
133 |
|
|
134 |
"get colorMap"
|
|
135 |
hasColorMap ifTrue:[
|
|
136 |
self readColorMap:colorMapSize
|
|
137 |
].
|
|
138 |
|
|
139 |
"image separator"
|
|
140 |
byte := inStream nextByte.
|
|
141 |
(byte ~~ 16r2C) ifTrue:[
|
|
142 |
'corrupted gif file (no imgSep)' printNewline.
|
|
143 |
^ nil
|
|
144 |
].
|
|
145 |
|
|
146 |
"get image data"
|
1
|
147 |
leftOffs := inStream nextShortMSB:false.
|
|
148 |
topOffs := inStream nextShortMSB:false.
|
|
149 |
width := inStream nextShortMSB:false.
|
|
150 |
height := inStream nextShortMSB:false.
|
0
|
151 |
|
|
152 |
'width ' print. width printNewline.
|
|
153 |
'height ' print. height printNewline.
|
|
154 |
|
1
|
155 |
"another flag byte"
|
|
156 |
flag := inStream nextByte.
|
|
157 |
interlaced := (flag bitAnd:2r01000000) ~~ 0.
|
|
158 |
hasLocalColorMap := (flag bitAnd:2r10000000) ~~ 0.
|
|
159 |
"localColorMapSorted := (flag bitAnd:2r00100000) ~~ 0. "
|
|
160 |
|
|
161 |
"if image has a local colormap, this one is used"
|
0
|
162 |
|
|
163 |
hasLocalColorMap ifTrue:[
|
|
164 |
"local descr. overwrites"
|
1
|
165 |
bitsPerPixel := (flag bitAnd:2r00000111) + 1.
|
0
|
166 |
colorMapSize := 1 bitShift:bitsPerPixel.
|
|
167 |
" 'local colormap' printNewline. "
|
|
168 |
"overwrite colormap"
|
|
169 |
self readColorMap:colorMapSize
|
|
170 |
].
|
|
171 |
|
1
|
172 |
"get codelen for decompression"
|
0
|
173 |
codeLen := inStream nextByte.
|
|
174 |
|
|
175 |
compressedData := ByteArray uninitializedNew:(inStream size).
|
|
176 |
|
1
|
177 |
"get compressed data"
|
0
|
178 |
index := 1.
|
1
|
179 |
count := inStream nextByte.
|
|
180 |
[count notNil and:[count ~~ 0]] whileTrue:[
|
|
181 |
inStream nextBytes:count into:compressedData startingAt:index.
|
|
182 |
index := index + count.
|
|
183 |
count := inStream nextByte
|
0
|
184 |
].
|
|
185 |
compressedSize := index - 1.
|
|
186 |
inStream close.
|
|
187 |
|
|
188 |
data := ByteArray uninitializedNew:((width + 1) * (height + 1)).
|
|
189 |
Transcript showCr:'decompressing'.
|
|
190 |
|
|
191 |
self class decompressGIFFrom:compressedData
|
|
192 |
count:compressedSize
|
|
193 |
into:data
|
|
194 |
startingAt:1
|
|
195 |
codeLen:(codeLen + 1).
|
|
196 |
|
|
197 |
interlaced ifTrue:[
|
1
|
198 |
Transcript showCr:'deinterlacing'.
|
0
|
199 |
tmp := ByteArray uninitializedNew:(data size).
|
|
200 |
|
|
201 |
"phase 1: 0, 8, 16, 24, ..."
|
|
202 |
|
|
203 |
srcOffset := 1.
|
|
204 |
0 to:(height - 1) by:8 do:[:dstRow |
|
|
205 |
dstOffset := dstRow * width + 1.
|
|
206 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1)
|
|
207 |
with:data startingAt:srcOffset.
|
|
208 |
srcOffset := srcOffset + width.
|
|
209 |
].
|
|
210 |
|
|
211 |
"phase 2: 4, 12, 20, 28, ..."
|
|
212 |
|
|
213 |
4 to:(height - 1) by:8 do:[:dstRow |
|
|
214 |
dstOffset := dstRow * width + 1.
|
|
215 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1)
|
|
216 |
with:data startingAt:srcOffset.
|
|
217 |
srcOffset := srcOffset + width.
|
|
218 |
].
|
|
219 |
|
|
220 |
"phase 3: 2, 6, 10, 14, ..."
|
|
221 |
|
|
222 |
2 to:(height - 1) by:4 do:[:dstRow |
|
|
223 |
dstOffset := dstRow * width + 1.
|
|
224 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1)
|
|
225 |
with:data startingAt:srcOffset.
|
|
226 |
srcOffset := srcOffset + width.
|
|
227 |
].
|
|
228 |
|
|
229 |
"phase 4: 1, 3, 5, 7, ..."
|
|
230 |
|
|
231 |
1 to:(height - 1) by:2 do:[:dstRow |
|
|
232 |
dstOffset := dstRow * width + 1.
|
|
233 |
tmp replaceFrom:dstOffset to:(dstOffset + width - 1)
|
|
234 |
with:data startingAt:srcOffset.
|
|
235 |
srcOffset := srcOffset + width.
|
|
236 |
].
|
|
237 |
|
|
238 |
data := tmp.
|
|
239 |
tmp := nil
|
|
240 |
].
|
|
241 |
|
|
242 |
photometric := #palette.
|
|
243 |
samplesPerPixel := 1.
|
|
244 |
bitsPerSample := #(8).
|
1
|
245 |
|
|
246 |
"check if only grey values are used,
|
|
247 |
could make it a greyscale image if so (currently not done)"
|
|
248 |
|
|
249 |
self checkGreyscaleColormap ifTrue:[
|
|
250 |
self makeGreyscale
|
|
251 |
].
|
|
252 |
|
0
|
253 |
colorMap := Array with:redMap with:greenMap with:blueMap.
|
|
254 |
|
|
255 |
"GIFReader fromFile:'../fileIn/bitmaps/claus.gif"
|
|
256 |
"GIFReader fromFile:'../fileIn/bitmaps/garfield.gif'"
|
|
257 |
!
|
|
258 |
|
|
259 |
readColorMap:colorMapSize
|
1
|
260 |
"get gif colormap consisting of colorMapSize entries"
|
|
261 |
|
|
262 |
redMap := Array new:colorMapSize.
|
|
263 |
greenMap := Array new:colorMapSize.
|
|
264 |
blueMap := Array new:colorMapSize.
|
|
265 |
1 to:colorMapSize do:[:i |
|
|
266 |
redMap at:i put:(inStream nextByte).
|
|
267 |
greenMap at:i put:(inStream nextByte).
|
|
268 |
blueMap at:i put:(inStream nextByte)
|
|
269 |
]
|
|
270 |
!
|
|
271 |
|
|
272 |
checkGreyscaleColormap
|
|
273 |
"return true, if colormap is really a greymap"
|
|
274 |
|
|
275 |
1 to:redMap size do:[:i |
|
|
276 |
(redMap at:i) ~~ (greenMap at:i) ifTrue:[^ false].
|
|
277 |
(redMap at:i) ~~ (blueMap at:i) ifTrue:[^ false].
|
|
278 |
].
|
|
279 |
^ true
|
|
280 |
!
|
|
281 |
|
|
282 |
makeGreyscale
|
|
283 |
"not yet implemented/needed"
|
0
|
284 |
! !
|