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 |
|
|
50 |
header := ByteArray new:16r50.
|
|
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 |
|
|
58 |
rawMap := ByteArray new:(16*3).
|
|
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 |
|
|
77 |
bytesPerRow := width * inDepth / 8.
|
|
78 |
data4 := ByteArray new:(height * bytesPerRow).
|
|
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 |
|
|
90 |
tmp := ByteArray new:(height * bytesPerRow).
|
|
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
|
|
125 |
data4 mask tmp bytesPerRow nColors|
|
|
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 |
|
|
138 |
header := ByteArray new:8r110.
|
|
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 |
|
|
157 |
rawMap := ByteArray new:(nColors*3).
|
|
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 |
|
|
174 |
mask := ByteArray new:(width * height / 8).
|
|
175 |
inStream nextBytes:(width * height / 8) into:mask.
|
|
176 |
|
|
177 |
"what is this"
|
|
178 |
|
|
179 |
inStream nextBytes:(width * height / 8) into:mask.
|
|
180 |
|
|
181 |
"read the data bits"
|
|
182 |
|
|
183 |
bytesPerRow := width * inDepth / 8.
|
|
184 |
data4 := ByteArray new:(height * bytesPerRow).
|
|
185 |
inStream nextBytes:(height * bytesPerRow) into:data4.
|
|
186 |
|
|
187 |
"stupid: last row first"
|
|
188 |
|
|
189 |
tmp := ByteArray new:(height * bytesPerRow).
|
|
190 |
srcIndex := 1.
|
|
191 |
dstIndex := (height - 1) * bytesPerRow + 1.
|
|
192 |
1 to:height do:[:row |
|
|
193 |
tmp replaceFrom:dstIndex to:(dstIndex + bytesPerRow - 1)
|
|
194 |
with:data4 startingAt:srcIndex.
|
|
195 |
srcIndex := srcIndex + bytesPerRow.
|
|
196 |
dstIndex := dstIndex - bytesPerRow.
|
|
197 |
].
|
|
198 |
data4 := tmp.
|
|
199 |
|
|
200 |
"expand into bytes"
|
|
201 |
|
|
202 |
data := ByteArray new:(width * height).
|
|
203 |
data4 expandPixels:inDepth width:width height:height
|
|
204 |
into:data mapping:nil.
|
|
205 |
|
|
206 |
photometric := #palette.
|
|
207 |
samplesPerPixel := 1.
|
|
208 |
bitsPerSample := #(8).
|
|
209 |
colorMap := Array with:rMap with:gMap with:bMap.
|
|
210 |
inStream close.
|
|
211 |
|
|
212 |
"
|
|
213 |
|i f|
|
|
214 |
i := Image fromFile:'/LocalLibrary/Images/OS2/dos3.ico'.
|
|
215 |
f := i asFormOn:Display.
|
|
216 |
v drawOpaqueForm:(f magnifyBy:2@2) x:5 y:5
|
|
217 |
"
|
|
218 |
!
|
|
219 |
|
|
220 |
fromFile: aFilename
|
|
221 |
| fileSize header |
|
|
222 |
|
|
223 |
inStream := FileStream readonlyFileNamed:aFilename.
|
|
224 |
inStream isNil ifTrue:[
|
|
225 |
'open error' printNewline.
|
|
226 |
^ nil
|
|
227 |
].
|
|
228 |
|
|
229 |
inStream binary.
|
|
230 |
fileSize := inStream size.
|
|
231 |
|
|
232 |
fileSize < 16 ifTrue:[
|
|
233 |
inStream close.
|
|
234 |
self error:'short file'.
|
|
235 |
^ nil
|
|
236 |
].
|
|
237 |
|
|
238 |
header := ByteArray new:16.
|
|
239 |
inStream nextBytes:16 into:header.
|
|
240 |
(header startsWith:#(66 65)) ifTrue:[ "BA"
|
|
241 |
inStream close.
|
|
242 |
^ self fromOS2File:aFilename
|
|
243 |
].
|
|
244 |
(header startsWith:#(73 67)) ifTrue:[ "IC"
|
|
245 |
inStream close.
|
|
246 |
^ self fromOS2File:aFilename
|
|
247 |
].
|
|
248 |
(header startsWith:#(0 0 1 0 1 0)) ifTrue:[
|
|
249 |
inStream close.
|
|
250 |
^ self fromWindowsFile:aFilename
|
|
251 |
].
|
|
252 |
self error:'format not supported'.
|
|
253 |
inStream close.
|
|
254 |
^ nil
|
|
255 |
|
|
256 |
"Image fromFile:'/LocalLibrary/Images/OS2_icons/dos.ico'"
|
|
257 |
! !
|