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