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