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