0
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
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:#WindowsIconReader
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Graphics-Support'
|
|
18 |
!
|
|
19 |
|
|
20 |
WindowsIconReader comment:'
|
|
21 |
|
|
22 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
this class provides methods for loading and saving Windows and OS2
|
|
26 |
icon files..
|
|
27 |
|
|
28 |
%W% %E%
|
|
29 |
written Jun 93 by claus
|
|
30 |
'!
|
|
31 |
|
|
32 |
!WindowsIconReader methodsFor:'reading from file'!
|
|
33 |
|
|
34 |
fromWindowsFile: aFilename
|
|
35 |
| fileSize header inDepth
|
|
36 |
rawMap rMap gMap bMap srcIndex dstIndex
|
|
37 |
data4 mask tmp bytesPerRow|
|
|
38 |
|
|
39 |
inStream := FileStream readonlyFileNamed:aFilename.
|
|
40 |
inStream isNil ifTrue:[
|
|
41 |
'open error' printNewline.
|
|
42 |
^ nil
|
|
43 |
].
|
|
44 |
|
|
45 |
inStream binary.
|
|
46 |
fileSize := inStream size.
|
|
47 |
|
|
48 |
"read the header"
|
|
49 |
|
3
|
50 |
header := ByteArray uninitializedNew:16r50.
|
0
|
51 |
inStream nextBytes:16r50 into:header.
|
|
52 |
width := header at:7.
|
|
53 |
height := header at:8.
|
|
54 |
inDepth := header at:16r25.
|
|
55 |
|
|
56 |
"read the colormap"
|
|
57 |
|
3
|
58 |
rawMap := ByteArray uninitializedNew:(16*3).
|
0
|
59 |
inStream nextBytes:(16*3) into:rawMap.
|
|
60 |
rMap := Array new:16.
|
|
61 |
gMap := Array new:16.
|
|
62 |
bMap := Array new:16.
|
|
63 |
srcIndex := 1.
|
|
64 |
1 to:16 do:[:i |
|
|
65 |
rMap at:i put:(rawMap at:srcIndex).
|
|
66 |
srcIndex := srcIndex + 1.
|
|
67 |
gMap at:i put:(rawMap at:srcIndex).
|
|
68 |
srcIndex := srcIndex + 1.
|
|
69 |
bMap at:i put:(rawMap at:srcIndex).
|
|
70 |
srcIndex := srcIndex + 1.
|
|
71 |
].
|
|
72 |
|
|
73 |
inStream position:16r7F.
|
|
74 |
|
|
75 |
"read the data bits"
|
|
76 |
|
3
|
77 |
bytesPerRow := width * inDepth + 7 // 8.
|
|
78 |
data4 := ByteArray uninitializedNew:(height * bytesPerRow).
|
0
|
79 |
inStream nextBytes:(height * bytesPerRow) into:data4.
|
|
80 |
|
|
81 |
"read mask"
|
|
82 |
|
|
83 |
"
|
|
84 |
mask := ByteArray new:(width * height / 8).
|
|
85 |
inStream nextBytes:(width * height / 8) into:mask.
|
|
86 |
"
|
|
87 |
|
|
88 |
"stupid: last row first"
|
|
89 |
|
3
|
90 |
tmp := ByteArray uninitializedNew:(height * bytesPerRow).
|
0
|
91 |
srcIndex := 1.
|
|
92 |
dstIndex := (height - 1) * bytesPerRow + 1.
|
|
93 |
1 to:height do:[:row |
|
|
94 |
tmp replaceFrom:dstIndex to:(dstIndex + bytesPerRow - 1)
|
|
95 |
with:data4 startingAt:srcIndex.
|
|
96 |
srcIndex := srcIndex + bytesPerRow.
|
|
97 |
dstIndex := dstIndex - bytesPerRow.
|
|
98 |
].
|
|
99 |
data4 := tmp.
|
|
100 |
|
|
101 |
"expand into bytes"
|
|
102 |
|
|
103 |
data := ByteArray new:(width * height).
|
|
104 |
data4 expandPixels:inDepth width:width height:height
|
|
105 |
into:data mapping:nil.
|
|
106 |
|
|
107 |
photometric := #palette.
|
|
108 |
samplesPerPixel := 1.
|
|
109 |
bitsPerSample := #(8).
|
|
110 |
colorMap := Array with:rMap with:gMap with:bMap.
|
|
111 |
inStream close.
|
|
112 |
|
|
113 |
"
|
|
114 |
|i f|
|
|
115 |
i := Image fromFile:'/LocalLibrary/Images/WIN_icons/ibm.ico'.
|
|
116 |
f := i asFormOn:Display.
|
|
117 |
v drawOpaqueForm:(f ) x:5 y:5.
|
|
118 |
v drawOpaqueForm:(f magnifyBy:2@2) x:45 y:5
|
|
119 |
"
|
|
120 |
!
|
|
121 |
|
|
122 |
fromOS2File: aFilename
|
|
123 |
| fileSize header inDepth
|
|
124 |
rawMap rMap gMap bMap srcIndex dstIndex
|
3
|
125 |
data4 mask tmp bytesPerRow nColors nByte|
|
0
|
126 |
|
|
127 |
inStream := FileStream readonlyFileNamed:aFilename.
|
|
128 |
inStream isNil ifTrue:[
|
|
129 |
'open error' printNewline.
|
|
130 |
^ nil
|
|
131 |
].
|
|
132 |
|
|
133 |
inStream binary.
|
|
134 |
fileSize := inStream size.
|
|
135 |
|
|
136 |
"read the header"
|
|
137 |
|
3
|
138 |
header := ByteArray uninitializedNew:8r110.
|
0
|
139 |
inStream nextBytes:16 into:header.
|
|
140 |
(header startsWith:#(73 67)) ifTrue:[
|
|
141 |
"IC format"
|
|
142 |
inStream nextBytes:10 into:header startingAt:17.
|
|
143 |
width := header at:7.
|
|
144 |
height := header at:9.
|
|
145 |
inDepth := 2 "header at:11". "where is it"
|
|
146 |
] ifFalse:[
|
|
147 |
inStream nextBytes:(8r110-16) into:header startingAt:17.
|
|
148 |
width := header at:8r101.
|
|
149 |
height := header at:8r103.
|
|
150 |
inDepth := header at:8r107.
|
|
151 |
].
|
|
152 |
|
|
153 |
"read the colormap"
|
|
154 |
|
|
155 |
nColors := 1 bitShift:inDepth.
|
|
156 |
|
3
|
157 |
rawMap := ByteArray uninitializedNew:(nColors*3).
|
0
|
158 |
inStream nextBytes:(nColors*3) into:rawMap.
|
|
159 |
rMap := Array new:nColors.
|
|
160 |
gMap := Array new:nColors.
|
|
161 |
bMap := Array new:nColors.
|
|
162 |
srcIndex := 1.
|
|
163 |
1 to:nColors do:[:i |
|
|
164 |
bMap at:i put:(rawMap at:srcIndex).
|
|
165 |
srcIndex := srcIndex + 1.
|
|
166 |
gMap at:i put:(rawMap at:srcIndex).
|
|
167 |
srcIndex := srcIndex + 1.
|
|
168 |
rMap at:i put:(rawMap at:srcIndex).
|
|
169 |
srcIndex := srcIndex + 1.
|
|
170 |
].
|
|
171 |
|
|
172 |
"read mask"
|
|
173 |
|
3
|
174 |
nByte := width * height + 7 // 8.
|
|
175 |
mask := ByteArray uninitializedNew:nByte.
|
|
176 |
inStream nextBytes:nByte into:mask.
|
0
|
177 |
|
|
178 |
"what is this"
|
|
179 |
|
3
|
180 |
inStream nextBytes:nByte into:mask.
|
0
|
181 |
|
|
182 |
"read the data bits"
|
|
183 |
|
3
|
184 |
bytesPerRow := width * inDepth + 7 // 8.
|
|
185 |
data4 := ByteArray uninitializedNew:(height * bytesPerRow).
|
0
|
186 |
inStream nextBytes:(height * bytesPerRow) into:data4.
|
|
187 |
|
|
188 |
"stupid: last row first"
|
|
189 |
|
|
190 |
tmp := ByteArray new:(height * bytesPerRow).
|
|
191 |
srcIndex := 1.
|
|
192 |
dstIndex := (height - 1) * bytesPerRow + 1.
|
|
193 |
1 to:height do:[:row |
|
|
194 |
tmp replaceFrom:dstIndex to:(dstIndex + bytesPerRow - 1)
|
|
195 |
with:data4 startingAt:srcIndex.
|
|
196 |
srcIndex := srcIndex + bytesPerRow.
|
|
197 |
dstIndex := dstIndex - bytesPerRow.
|
|
198 |
].
|
|
199 |
data4 := tmp.
|
|
200 |
|
|
201 |
"expand into bytes"
|
|
202 |
|
|
203 |
data := ByteArray new:(width * height).
|
|
204 |
data4 expandPixels:inDepth width:width height:height
|
|
205 |
into:data mapping:nil.
|
|
206 |
|
|
207 |
photometric := #palette.
|
|
208 |
samplesPerPixel := 1.
|
|
209 |
bitsPerSample := #(8).
|
|
210 |
colorMap := Array with:rMap with:gMap with:bMap.
|
|
211 |
inStream close.
|
|
212 |
|
|
213 |
"
|
|
214 |
|i f|
|
|
215 |
i := Image fromFile:'/LocalLibrary/Images/OS2/dos3.ico'.
|
|
216 |
f := i asFormOn:Display.
|
|
217 |
v drawOpaqueForm:(f magnifyBy:2@2) x:5 y:5
|
|
218 |
"
|
|
219 |
!
|
|
220 |
|
3
|
221 |
fromWindowsBMPFile: aFilename
|
|
222 |
| fileSize header inDepth
|
|
223 |
rawMap rMap gMap bMap srcIndex dstIndex
|
|
224 |
data4 mask tmp bytesPerRow|
|
|
225 |
|
|
226 |
inStream := FileStream readonlyFileNamed:aFilename.
|
|
227 |
inStream isNil ifTrue:[
|
|
228 |
'open error' printNewline.
|
|
229 |
^ nil
|
|
230 |
].
|
|
231 |
|
|
232 |
inStream binary.
|
|
233 |
fileSize := inStream size.
|
|
234 |
|
|
235 |
"read the header"
|
|
236 |
|
|
237 |
header := ByteArray uninitializedNew:16r50.
|
|
238 |
inStream nextBytes:16r50 into:header.
|
|
239 |
((header at:15) == 40) ifTrue:[
|
|
240 |
width := (header at:19) + ((header at:20) * 256).
|
|
241 |
height := (header at:23) + ((header at:24) * 256).
|
|
242 |
inDepth := header at:29.
|
|
243 |
].
|
|
244 |
((header at:15) == 12) ifTrue:[
|
|
245 |
width := (header at:19) + ((header at:20) * 256).
|
|
246 |
height := (header at:21) + ((header at:22) * 256).
|
|
247 |
inDepth := header at:25.
|
|
248 |
].
|
|
249 |
|
|
250 |
width isNil ifTrue:[
|
|
251 |
^ nil
|
|
252 |
].
|
|
253 |
|
|
254 |
"read the colormap"
|
|
255 |
|
|
256 |
rawMap := ByteArray uninitializedNew:(16*3).
|
|
257 |
inStream nextBytes:(16*3) into:rawMap.
|
|
258 |
rMap := Array new:16.
|
|
259 |
gMap := Array new:16.
|
|
260 |
bMap := Array new:16.
|
|
261 |
srcIndex := 1.
|
|
262 |
1 to:16 do:[:i |
|
|
263 |
rMap at:i put:(rawMap at:srcIndex).
|
|
264 |
srcIndex := srcIndex + 1.
|
|
265 |
gMap at:i put:(rawMap at:srcIndex).
|
|
266 |
srcIndex := srcIndex + 1.
|
|
267 |
bMap at:i put:(rawMap at:srcIndex).
|
|
268 |
srcIndex := srcIndex + 1.
|
|
269 |
].
|
|
270 |
|
|
271 |
inStream position:16r78.
|
|
272 |
|
|
273 |
"read the data bits"
|
|
274 |
|
|
275 |
bytesPerRow := width * inDepth + 7 // 8.
|
|
276 |
data4 := ByteArray uninitializedNew:(height * bytesPerRow).
|
|
277 |
inStream nextBytes:(height * bytesPerRow) into:data4.
|
|
278 |
|
|
279 |
"read mask"
|
|
280 |
|
|
281 |
"
|
|
282 |
mask := ByteArray new:(width * height / 8).
|
|
283 |
inStream nextBytes:(width * height / 8) into:mask.
|
|
284 |
"
|
|
285 |
|
|
286 |
"stupid: last row first"
|
|
287 |
|
|
288 |
tmp := ByteArray uninitializedNew:(height * bytesPerRow).
|
|
289 |
srcIndex := 1.
|
|
290 |
dstIndex := (height - 1) * bytesPerRow + 1.
|
|
291 |
1 to:height do:[:row |
|
|
292 |
tmp replaceFrom:dstIndex to:(dstIndex + bytesPerRow - 1)
|
|
293 |
with:data4 startingAt:srcIndex.
|
|
294 |
srcIndex := srcIndex + bytesPerRow.
|
|
295 |
dstIndex := dstIndex - bytesPerRow.
|
|
296 |
].
|
|
297 |
data4 := tmp.
|
|
298 |
|
|
299 |
"expand into bytes"
|
|
300 |
|
|
301 |
data := ByteArray new:(width * height).
|
|
302 |
data4 expandPixels:inDepth width:width height:height
|
|
303 |
into:data mapping:nil.
|
|
304 |
|
|
305 |
photometric := #palette.
|
|
306 |
samplesPerPixel := 1.
|
|
307 |
bitsPerSample := #(8).
|
|
308 |
colorMap := Array with:rMap with:gMap with:bMap.
|
|
309 |
inStream close.
|
|
310 |
|
|
311 |
!
|
|
312 |
|
0
|
313 |
fromFile: aFilename
|
|
314 |
| fileSize header |
|
|
315 |
|
|
316 |
inStream := FileStream readonlyFileNamed:aFilename.
|
|
317 |
inStream isNil ifTrue:[
|
|
318 |
'open error' printNewline.
|
|
319 |
^ nil
|
|
320 |
].
|
|
321 |
|
|
322 |
inStream binary.
|
|
323 |
fileSize := inStream size.
|
|
324 |
|
|
325 |
fileSize < 16 ifTrue:[
|
|
326 |
inStream close.
|
|
327 |
self error:'short file'.
|
|
328 |
^ nil
|
|
329 |
].
|
|
330 |
|
3
|
331 |
header := ByteArray uninitializedNew:16.
|
0
|
332 |
inStream nextBytes:16 into:header.
|
3
|
333 |
(header startsWith:#(66 77)) ifTrue:[ "BM"
|
|
334 |
inStream close.
|
|
335 |
^ self fromWindowsBMPFile:aFilename
|
|
336 |
].
|
0
|
337 |
(header startsWith:#(66 65)) ifTrue:[ "BA"
|
|
338 |
inStream close.
|
|
339 |
^ self fromOS2File:aFilename
|
|
340 |
].
|
|
341 |
(header startsWith:#(73 67)) ifTrue:[ "IC"
|
|
342 |
inStream close.
|
|
343 |
^ self fromOS2File:aFilename
|
|
344 |
].
|
|
345 |
(header startsWith:#(0 0 1 0 1 0)) ifTrue:[
|
|
346 |
inStream close.
|
|
347 |
^ self fromWindowsFile:aFilename
|
|
348 |
].
|
|
349 |
self error:'format not supported'.
|
|
350 |
inStream close.
|
|
351 |
^ nil
|
|
352 |
|
|
353 |
"Image fromFile:'/LocalLibrary/Images/OS2_icons/dos.ico'"
|
|
354 |
! !
|