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
|
21 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
22 |
All Rights Reserved
|
|
23 |
'!
|
0
|
24 |
|
21
|
25 |
!WindowsIconReader class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
0
|
30 |
All Rights Reserved
|
|
31 |
|
21
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
0
|
40 |
|
21
|
41 |
version
|
|
42 |
"
|
|
43 |
$Header: /cvs/stx/stx/libview2/WindowsIconReader.st,v 1.6 1994-06-03 00:54:08 claus Exp $
|
|
44 |
"
|
|
45 |
!
|
|
46 |
|
|
47 |
documentation
|
|
48 |
"
|
|
49 |
this class provides methods for loading Windows and OS2 icon files..
|
|
50 |
"
|
|
51 |
! !
|
0
|
52 |
|
|
53 |
!WindowsIconReader methodsFor:'reading from file'!
|
|
54 |
|
21
|
55 |
fromOS2File: aFilename
|
|
56 |
inStream := self class streamReadingFile:aFilename.
|
|
57 |
inStream isNil ifTrue:[^ nil].
|
|
58 |
inStream binary.
|
|
59 |
^ self fromOS2Stream.
|
|
60 |
!
|
0
|
61 |
|
21
|
62 |
fromWindowsBMPFile: aFilename
|
12
|
63 |
inStream := self class streamReadingFile:aFilename.
|
|
64 |
inStream isNil ifTrue:[^ nil].
|
21
|
65 |
inStream binary.
|
|
66 |
^ self fromWindowsBMPStream.
|
|
67 |
!
|
|
68 |
|
|
69 |
fromWindowsICOFile: aFilename
|
|
70 |
inStream := self class streamReadingFile:aFilename.
|
|
71 |
inStream isNil ifTrue:[^ nil].
|
|
72 |
^ self fromWindowsICOStream.
|
|
73 |
|
|
74 |
"
|
|
75 |
Image fromFile:'/phys/clam2//LocalLibrary/Images/WIN_icons/ibm.ico'.
|
|
76 |
"
|
|
77 |
!
|
|
78 |
|
|
79 |
fromWindowsICOStream
|
|
80 |
| header inDepth
|
|
81 |
rawMap rMap gMap bMap srcIndex dstIndex
|
|
82 |
data4 mask tmp bytesPerRow nColor|
|
0
|
83 |
|
|
84 |
inStream binary.
|
|
85 |
|
|
86 |
"read the header"
|
|
87 |
|
21
|
88 |
header := ByteArray uninitializedNew:(6 + 16 + 40).
|
|
89 |
inStream nextBytes:(6 + 16 + 40) into:header.
|
|
90 |
width := header at:(6+1).
|
|
91 |
height := header at:(7+1).
|
|
92 |
nColor := header at:(8+1).
|
|
93 |
"10, 11, 12, 13, 14 ? (reserve)"
|
|
94 |
"15, 16, 17, 18 pixel array size"
|
|
95 |
"19, 20, 21, 22 offset "
|
|
96 |
"23, ... , 62 ?"
|
|
97 |
|
0
|
98 |
inDepth := header at:16r25.
|
|
99 |
|
|
100 |
"read the colormap"
|
|
101 |
|
21
|
102 |
rawMap := ByteArray uninitializedNew:(16*4).
|
|
103 |
inStream nextBytes:(16*4) into:rawMap.
|
0
|
104 |
rMap := Array new:16.
|
|
105 |
gMap := Array new:16.
|
|
106 |
bMap := Array new:16.
|
|
107 |
srcIndex := 1.
|
|
108 |
1 to:16 do:[:i |
|
21
|
109 |
bMap at:i put:(rawMap at:srcIndex).
|
0
|
110 |
srcIndex := srcIndex + 1.
|
|
111 |
gMap at:i put:(rawMap at:srcIndex).
|
|
112 |
srcIndex := srcIndex + 1.
|
21
|
113 |
rMap at:i put:(rawMap at:srcIndex).
|
|
114 |
srcIndex := srcIndex + 1.
|
0
|
115 |
srcIndex := srcIndex + 1.
|
|
116 |
].
|
|
117 |
|
|
118 |
"read the data bits"
|
|
119 |
|
3
|
120 |
bytesPerRow := width * inDepth + 7 // 8.
|
|
121 |
data4 := ByteArray uninitializedNew:(height * bytesPerRow).
|
0
|
122 |
inStream nextBytes:(height * bytesPerRow) into:data4.
|
|
123 |
|
|
124 |
"read mask"
|
|
125 |
|
|
126 |
"
|
|
127 |
mask := ByteArray new:(width * height / 8).
|
|
128 |
inStream nextBytes:(width * height / 8) into:mask.
|
|
129 |
"
|
|
130 |
|
|
131 |
"stupid: last row first"
|
|
132 |
|
3
|
133 |
tmp := ByteArray uninitializedNew:(height * bytesPerRow).
|
0
|
134 |
srcIndex := 1.
|
|
135 |
dstIndex := (height - 1) * bytesPerRow + 1.
|
|
136 |
1 to:height do:[:row |
|
|
137 |
tmp replaceFrom:dstIndex to:(dstIndex + bytesPerRow - 1)
|
|
138 |
with:data4 startingAt:srcIndex.
|
|
139 |
srcIndex := srcIndex + bytesPerRow.
|
|
140 |
dstIndex := dstIndex - bytesPerRow.
|
|
141 |
].
|
|
142 |
data4 := tmp.
|
|
143 |
|
|
144 |
"expand into bytes"
|
|
145 |
|
|
146 |
data := ByteArray new:(width * height).
|
|
147 |
data4 expandPixels:inDepth width:width height:height
|
|
148 |
into:data mapping:nil.
|
|
149 |
|
|
150 |
photometric := #palette.
|
|
151 |
samplesPerPixel := 1.
|
|
152 |
bitsPerSample := #(8).
|
|
153 |
colorMap := Array with:rMap with:gMap with:bMap.
|
|
154 |
inStream close.
|
|
155 |
|
|
156 |
"
|
21
|
157 |
WindowsIconReader new fromWindowsICOFile:'/phys/clam2//LocalLibrary/Images/WIN_icons/ibm.ico'.
|
0
|
158 |
"
|
|
159 |
!
|
|
160 |
|
21
|
161 |
fromWindowsBMPStream
|
|
162 |
| fileSize header inDepth inPlanes compression
|
|
163 |
imgSize resH resV numColor numImportantColor
|
|
164 |
dataStart
|
0
|
165 |
rawMap rMap gMap bMap srcIndex dstIndex
|
21
|
166 |
data4 mask tmp bytesPerRow fourBytesPerColorInfo|
|
|
167 |
|
|
168 |
fileSize := inStream size.
|
|
169 |
"read the header"
|
|
170 |
|
|
171 |
header := ByteArray uninitializedNew:16r54.
|
|
172 |
inStream nextBytes:18 into:header.
|
|
173 |
|
|
174 |
((header at:(16r0E + 1)) == 40) ifTrue:[ "header-size"
|
|
175 |
"
|
|
176 |
its an Windows3.x BMP file
|
|
177 |
or OS/2 vsn 2 BMP file
|
|
178 |
"
|
|
179 |
'BMP: Win3.x or OS/2 vsn 2 format' errorPrintNL.
|
|
180 |
|
|
181 |
inStream nextBytes:(40-4) into:header startingAt:19.
|
|
182 |
|
|
183 |
width := header wordAt:(16r12 + 1). "(header at:19) + ((header at:20) * 256). "
|
|
184 |
height := header wordAt:(16r16 + 1). "(header at:23) + ((header at:24) * 256). "
|
|
185 |
inPlanes := header wordAt:(16r1A + 1).
|
|
186 |
inDepth := header wordAt:(16r1C + 1).
|
|
187 |
compression := header wordAt:(16r1E + 1).
|
|
188 |
imgSize := header doubleWordAt:(16r22 + 1).
|
|
189 |
resH := header doubleWordAt:(16r26 + 1).
|
|
190 |
resV := header doubleWordAt:(16r2A + 1).
|
|
191 |
numColor := header doubleWordAt:(16r2E + 1).
|
|
192 |
numImportantColor := header doubleWordAt:(16r32 + 1).
|
|
193 |
|
|
194 |
numColor == 0 ifTrue:[
|
|
195 |
"
|
|
196 |
some bmp-writers seem to leave this as zero (which is wrong)
|
|
197 |
"
|
|
198 |
numColor := 1 bitShift:inDepth.
|
|
199 |
'BMP: missing nColor in header - assume ' errorPrint. numColor errorPrintNL
|
|
200 |
].
|
|
201 |
rawMap := ByteArray uninitializedNew:(numColor * 4).
|
|
202 |
inStream nextBytes:(numColor * 4) into:rawMap.
|
|
203 |
fourBytesPerColorInfo := true.
|
|
204 |
dataStart := header wordAt:(16r0A + 1)
|
|
205 |
] ifFalse:[
|
|
206 |
((header at:(16r0E + 1)) == 12) ifTrue:[ "core-info header size"
|
|
207 |
"
|
|
208 |
its an OS/2 (vsn1.2) BMP file
|
|
209 |
"
|
|
210 |
'BMP: OS/2 vsn 1.2 format' errorPrintNL.
|
|
211 |
inStream nextBytes:(12-4) into:header startingAt:19.
|
|
212 |
|
|
213 |
width := header wordAt:(16r12 + 1). "(header at:19) + ((header at:20) * 256). "
|
|
214 |
height := header wordAt:(16r14 + 1). "(header at:21) + ((header at:22) * 256). "
|
|
215 |
inPlanes := header wordAt:(16r16 + 1).
|
|
216 |
inDepth := header wordAt:(16r18 + 1).
|
|
217 |
numColor := 1 bitShift:inDepth.
|
|
218 |
rawMap := ByteArray uninitializedNew:(numColor * 3).
|
|
219 |
inStream nextBytes:(numColor * 3) into:rawMap.
|
|
220 |
fourBytesPerColorInfo := false.
|
|
221 |
compression := 0.
|
|
222 |
dataStart := header wordAt:(16r0A + 1)
|
|
223 |
] ifFalse:[
|
|
224 |
'BMP: unknown format' errorPrintNL.
|
|
225 |
inStream close.
|
|
226 |
^ nil
|
|
227 |
].
|
|
228 |
].
|
|
229 |
|
|
230 |
"read the colormap"
|
|
231 |
|
|
232 |
rMap := Array new:numColor.
|
|
233 |
gMap := Array new:numColor.
|
|
234 |
bMap := Array new:numColor.
|
|
235 |
srcIndex := 1.
|
|
236 |
1 to:numColor do:[:i |
|
|
237 |
bMap at:i put:(rawMap at:srcIndex).
|
|
238 |
srcIndex := srcIndex + 1.
|
|
239 |
gMap at:i put:(rawMap at:srcIndex).
|
|
240 |
srcIndex := srcIndex + 1.
|
|
241 |
rMap at:i put:(rawMap at:srcIndex).
|
|
242 |
srcIndex := srcIndex + 1.
|
|
243 |
fourBytesPerColorInfo ifTrue:[
|
|
244 |
srcIndex := srcIndex + 1.
|
|
245 |
]
|
|
246 |
].
|
|
247 |
|
|
248 |
"
|
|
249 |
currently only normal (non-rle) bitmaps
|
|
250 |
supported
|
|
251 |
"
|
|
252 |
compression ~~ 0 ifTrue:[
|
|
253 |
'BMP compression type ' errorPrint. compression errorPrint.
|
|
254 |
'not supported' errorPrintNL.
|
|
255 |
inStream close.
|
|
256 |
^ nil
|
|
257 |
].
|
|
258 |
inPlanes ~~ 1 ifTrue:[
|
|
259 |
'BMP only 1 plane images supported' errorPrintNL.
|
|
260 |
inStream close.
|
|
261 |
^ nil
|
|
262 |
].
|
|
263 |
|
|
264 |
"read the data bits"
|
|
265 |
|
|
266 |
bytesPerRow := width * inDepth + 7 // 8.
|
|
267 |
data4 := ByteArray uninitializedNew:(height * bytesPerRow).
|
|
268 |
|
|
269 |
inStream position:(dataStart + 1).
|
|
270 |
inStream nextBytes:(height * bytesPerRow) into:data4.
|
|
271 |
|
|
272 |
"read mask"
|
|
273 |
|
|
274 |
"
|
|
275 |
mask := ByteArray new:(width * height / 8).
|
|
276 |
inStream nextBytes:(width * height / 8) into:mask.
|
|
277 |
"
|
|
278 |
|
|
279 |
"stupid: last row first"
|
|
280 |
|
|
281 |
tmp := ByteArray uninitializedNew:(height * bytesPerRow).
|
|
282 |
srcIndex := 1.
|
|
283 |
dstIndex := (height - 1) * bytesPerRow + 1.
|
|
284 |
1 to:height do:[:row |
|
|
285 |
tmp replaceFrom:dstIndex to:(dstIndex + bytesPerRow - 1)
|
|
286 |
with:data4 startingAt:srcIndex.
|
|
287 |
srcIndex := srcIndex + bytesPerRow.
|
|
288 |
dstIndex := dstIndex - bytesPerRow.
|
|
289 |
].
|
|
290 |
data4 := tmp.
|
|
291 |
|
|
292 |
"expand into bytes"
|
|
293 |
|
|
294 |
data := ByteArray new:(width * height).
|
|
295 |
data4 expandPixels:inDepth width:width height:height
|
|
296 |
into:data mapping:nil.
|
|
297 |
|
|
298 |
photometric := #palette.
|
|
299 |
samplesPerPixel := 1.
|
|
300 |
bitsPerSample := #(8).
|
|
301 |
colorMap := Array with:rMap with:gMap with:bMap.
|
|
302 |
inStream close.
|
|
303 |
!
|
|
304 |
|
|
305 |
fromFile: aFilename
|
|
306 |
| fileSize header |
|
0
|
307 |
|
12
|
308 |
inStream := self class streamReadingFile:aFilename.
|
|
309 |
inStream isNil ifTrue:[^ nil].
|
0
|
310 |
|
|
311 |
inStream binary.
|
|
312 |
fileSize := inStream size.
|
|
313 |
|
21
|
314 |
fileSize < 16 ifTrue:[
|
|
315 |
inStream close.
|
|
316 |
self error:'WINREADER: short file'.
|
|
317 |
^ nil
|
|
318 |
].
|
|
319 |
|
|
320 |
header := ByteArray uninitializedNew:4.
|
|
321 |
inStream nextBytes:4 into:header.
|
|
322 |
|
|
323 |
(header startsWith:#(66 77)) ifTrue:[ "BM"
|
|
324 |
inStream position:1.
|
|
325 |
'WINREADER: Win3.x or OS/2 vsn 2 BM format' errorPrintNL.
|
|
326 |
^ self fromWindowsBMPStream
|
|
327 |
].
|
|
328 |
(header startsWith:#(66 65)) ifTrue:[ "BA"
|
|
329 |
inStream position:1.
|
|
330 |
'WINREADER: OS/2 vsn 2 BA format' errorPrintNL.
|
|
331 |
^ self fromOS2Stream
|
|
332 |
].
|
|
333 |
(header startsWith:#(73 67)) ifTrue:[ "IC"
|
|
334 |
inStream position:1.
|
|
335 |
'WINREADER: OS/2 IC format' errorPrintNL.
|
|
336 |
^ self fromOS2Stream
|
|
337 |
].
|
|
338 |
(header startsWith:#(80 84)) ifTrue:[ "PT"
|
|
339 |
inStream position:1.
|
|
340 |
'WINREADER: OS/2 PT format' errorPrintNL.
|
|
341 |
^ self fromOS2Stream
|
|
342 |
].
|
|
343 |
(header startsWith:#(0 0 1 0)) ifTrue:[
|
|
344 |
inStream position:1.
|
|
345 |
'WINREADER: Win3.x ICO format' errorPrintNL.
|
|
346 |
^ self fromWindowsICOStream
|
|
347 |
].
|
|
348 |
self error:'WINREADER: format not supported'.
|
|
349 |
inStream close.
|
|
350 |
^ nil
|
|
351 |
|
|
352 |
"
|
|
353 |
Image fromFile:'/phys/clam//LocalLibrary/Images/OS2_icons/dos.ico'
|
|
354 |
"
|
|
355 |
!
|
|
356 |
|
|
357 |
fromOS2Stream
|
|
358 |
| header inDepth
|
|
359 |
rawMap rMap gMap bMap srcIndex dstIndex
|
|
360 |
data4 mask tmp bytesPerRow nColors nByte|
|
|
361 |
|
|
362 |
inStream binary.
|
|
363 |
|
0
|
364 |
"read the header"
|
|
365 |
|
3
|
366 |
header := ByteArray uninitializedNew:8r110.
|
0
|
367 |
inStream nextBytes:16 into:header.
|
21
|
368 |
|
0
|
369 |
(header startsWith:#(73 67)) ifTrue:[
|
|
370 |
"IC format"
|
|
371 |
inStream nextBytes:10 into:header startingAt:17.
|
|
372 |
width := header at:7.
|
|
373 |
height := header at:9.
|
|
374 |
inDepth := 2 "header at:11". "where is it"
|
|
375 |
] ifFalse:[
|
|
376 |
inStream nextBytes:(8r110-16) into:header startingAt:17.
|
|
377 |
width := header at:8r101.
|
|
378 |
height := header at:8r103.
|
|
379 |
inDepth := header at:8r107.
|
|
380 |
].
|
|
381 |
|
|
382 |
"read the colormap"
|
|
383 |
|
|
384 |
nColors := 1 bitShift:inDepth.
|
|
385 |
|
3
|
386 |
rawMap := ByteArray uninitializedNew:(nColors*3).
|
0
|
387 |
inStream nextBytes:(nColors*3) into:rawMap.
|
|
388 |
rMap := Array new:nColors.
|
|
389 |
gMap := Array new:nColors.
|
|
390 |
bMap := Array new:nColors.
|
|
391 |
srcIndex := 1.
|
|
392 |
1 to:nColors do:[:i |
|
|
393 |
bMap at:i put:(rawMap at:srcIndex).
|
|
394 |
srcIndex := srcIndex + 1.
|
|
395 |
gMap at:i put:(rawMap at:srcIndex).
|
|
396 |
srcIndex := srcIndex + 1.
|
|
397 |
rMap at:i put:(rawMap at:srcIndex).
|
|
398 |
srcIndex := srcIndex + 1.
|
|
399 |
].
|
|
400 |
|
|
401 |
"read mask"
|
|
402 |
|
3
|
403 |
nByte := width * height + 7 // 8.
|
|
404 |
mask := ByteArray uninitializedNew:nByte.
|
|
405 |
inStream nextBytes:nByte into:mask.
|
0
|
406 |
|
|
407 |
"what is this"
|
|
408 |
|
3
|
409 |
inStream nextBytes:nByte into:mask.
|
0
|
410 |
|
|
411 |
"read the data bits"
|
|
412 |
|
3
|
413 |
bytesPerRow := width * inDepth + 7 // 8.
|
|
414 |
data4 := ByteArray uninitializedNew:(height * bytesPerRow).
|
0
|
415 |
inStream nextBytes:(height * bytesPerRow) into:data4.
|
|
416 |
|
|
417 |
"stupid: last row first"
|
|
418 |
|
|
419 |
tmp := ByteArray new:(height * bytesPerRow).
|
|
420 |
srcIndex := 1.
|
|
421 |
dstIndex := (height - 1) * bytesPerRow + 1.
|
|
422 |
1 to:height do:[:row |
|
|
423 |
tmp replaceFrom:dstIndex to:(dstIndex + bytesPerRow - 1)
|
|
424 |
with:data4 startingAt:srcIndex.
|
|
425 |
srcIndex := srcIndex + bytesPerRow.
|
|
426 |
dstIndex := dstIndex - bytesPerRow.
|
|
427 |
].
|
|
428 |
data4 := tmp.
|
|
429 |
|
|
430 |
"expand into bytes"
|
|
431 |
|
|
432 |
data := ByteArray new:(width * height).
|
|
433 |
data4 expandPixels:inDepth width:width height:height
|
|
434 |
into:data mapping:nil.
|
|
435 |
|
|
436 |
photometric := #palette.
|
|
437 |
samplesPerPixel := 1.
|
|
438 |
bitsPerSample := #(8).
|
|
439 |
colorMap := Array with:rMap with:gMap with:bMap.
|
|
440 |
inStream close.
|
|
441 |
|
|
442 |
"
|
|
443 |
|i f|
|
|
444 |
i := Image fromFile:'/LocalLibrary/Images/OS2/dos3.ico'.
|
|
445 |
f := i asFormOn:Display.
|
18
|
446 |
v displayOpaqueForm:(f magnifyBy:2@2) x:5 y:5
|
0
|
447 |
"
|
|
448 |
! !
|