author | Claus Gittinger <cg@exept.de> |
Tue, 02 Dec 2008 22:02:23 +0100 | |
changeset 2571 | ae60c0d0452d |
parent 2333 | a93f1f5392bd |
child 2578 | 7d49dc8ecb63 |
permissions | -rw-r--r-- |
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 |
" |
|
1385 | 12 |
"{ Package: 'stx:libview2' }" |
13 |
||
0 | 14 |
ImageReader subclass:#WindowsIconReader |
2571 | 15 |
instanceVariableNames:'compression inDepth' |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
16 |
classVariableNames:'' |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
17 |
poolDictionaries:'' |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
18 |
category:'Graphics-Images-Readers' |
0 | 19 |
! |
20 |
||
21 | 21 |
!WindowsIconReader class methodsFor:'documentation'! |
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
28 | 26 |
All Rights Reserved |
0 | 27 |
|
21 | 28 |
This software is furnished under a license and may be used |
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
0 | 36 |
|
21 | 37 |
documentation |
38 |
" |
|
32 | 39 |
this class provides methods for loading Windows and OS2 icon files. |
713 | 40 |
Image writing is only supported for BMP format with depth 1,4,8 and 24 bit images. |
211 | 41 |
|
42 |
The reader tries to figure out which version of BMP/ICO is used. |
|
43 |
It seems to be able to load most formats, but who knows ... |
|
44 |
||
1385 | 45 |
The class name *IconReader is a bad, historic choice - it ws originally |
46 |
written to read icons only, but evolved over time and it should now be also |
|
47 |
capapble of reading bmp and cursor files. |
|
48 |
||
211 | 49 |
[See also:] |
234 | 50 |
Image Form Icon |
211 | 51 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader |
52 |
ST80FormReader SunRasterReader TargaReader TIFFReader |
|
53 |
XBMReader XPMReader XWDReader |
|
21 | 54 |
" |
1385 | 55 |
! |
56 |
||
57 |
fileFormatDescription |
|
58 |
" |
|
59 |
Information from http://www.daubnet.com/formats/BMP.html - no Warranty. |
|
60 |
||
61 |
Name Size Description |
|
62 |
||
63 |
Header 14 bytes Windows Structure: BITMAPFILEHEADER |
|
64 |
Signature 2 bytes 'BM' |
|
65 |
FileSize 4 bytes File size in bytes |
|
66 |
reserved 4 bytes unused (=0) |
|
67 |
DataOffset 4 bytes File offset to Raster Data |
|
68 |
||
69 |
InfoHeader 40 bytes Windows Structure: BITMAPINFOHEADER |
|
70 |
||
71 |
Size 4 bytes Size of InfoHeader =40 |
|
72 |
Width 4 bytes Bitmap Width |
|
73 |
Height 4 bytes Bitmap Height |
|
74 |
Planes 2 bytes Number of Planes (=1) |
|
75 |
BitCount 2 bytes Bits per Pixel |
|
76 |
1 = monochrome palette. NumColors = 1 |
|
77 |
4 = 4bit palletized. NumColors = 16 |
|
78 |
8 = 8bit palletized. NumColors = 256 |
|
79 |
16 = 16bit RGB. NumColors = 65536 (?) |
|
80 |
24 = 24bit RGB. NumColors = 16M |
|
81 |
Compression 4 bytes Type of Compression |
|
82 |
0 = BI_RGB no compression |
|
83 |
1 = BI_RLE8 8bit RLE encoding |
|
84 |
2 = BI_RLE4 4bit RLE encoding |
|
85 |
ImageSize 4 bytes (compressed) Size of Image |
|
86 |
It is valid to set this =0 if Compression = 0 |
|
87 |
XpixelsPerM 4 bytes horizontal resolution: Pixels/meter |
|
88 |
YpixelsPerM 4 bytes vertical resolution: Pixels/meter |
|
89 |
ColorsUsed 4 bytes Number of actually used colors |
|
90 |
ColorsImportant |
|
91 |
4 bytes Number of important colors |
|
92 |
0 = all |
|
93 |
ColorTable 4 * NumColors bytes |
|
94 |
present only if Info.BitsPerPixel <= 8 |
|
95 |
colors should be ordered by importance |
|
96 |
||
97 |
Red 1 byte Red intensity |
|
98 |
Green 1 byte Green intensity |
|
99 |
Blue 1 byte Blue intensity |
|
100 |
reserved 1 byte unused (=0) |
|
101 |
repeated NumColors times |
|
102 |
||
103 |
Raster Data Info.ImageSize bytes The pixel data |
|
104 |
||
105 |
||
106 |
Raster Data encoding: |
|
107 |
Depending on the image's BitCount and on the Compression flag there are 6 different encoding schemes. |
|
108 |
All of them share the following: |
|
109 |
||
110 |
Pixels are stored bottom-up, left-to-right. Pixel lines are padded with zeros to end on a 32bit (4byte) boundary. For |
|
111 |
uncompressed formats every line will have the same number of bytes. Color indices are zero based, meaning a pixel |
|
112 |
color of 0 represents the first color table entry, a pixel color of 255 (if there are that many) represents the 256th entry. |
|
113 |
For images with more than 256 colors there is no color table. |
|
114 |
||
115 |
Raster Data encoding for 1bit / black & white images: |
|
116 |
BitCount = 1 Compression = 0 |
|
117 |
Every byte holds 8 pixels, its highest order bit representing the leftmost pixel of those. There are 2 color table entries. |
|
118 |
Some readers will ignore them though, and assume that 0 is black and 1 is white. If you are storing black and white |
|
119 |
pictures you should stick to this, with any other 2 colors this is not an issue. Remember padding with zeros up to a |
|
120 |
32bit boundary (This can be up to 31 zeros/pixels!!) |
|
121 |
||
122 |
Raster Data encoding for 4bit / 16 color images: |
|
123 |
BitCount = 4 Compression = 0 |
|
124 |
Every byte holds 2 pixels, its high order 4 bits representing the left of those. There are 16 color table entries. These |
|
125 |
colors do not have to be the 16 MS-Windows standard colors. Padding each line with zeros up to a 32bit boundary |
|
126 |
will result in up to 28 zeros = 7 'wasted pixels'. |
|
127 |
||
128 |
Raster Data encoding for 8bit / 256 color images: |
|
129 |
BitCount = 8 Compression = 0 |
|
130 |
Every byte holds 1 pixel. There are 256 color table entries. Padding each line with zeros up to a 32bit boundary will |
|
131 |
result in up to 3 bytes of zeros = 3 'wasted pixels'. |
|
132 |
||
133 |
Raster Data encoding for 16bit / hicolor images: |
|
134 |
BitCount = 16 Compression = 0 |
|
135 |
Every 2bytes / 16bit holds 1 pixel. |
|
136 |
<information missing: the 16 bit was introduced together with Video For Windows? Is it a memory-only-format?> |
|
137 |
The pixels are no color table pointers. There are no color table entries. Padding each line with zeros up to a 16bit |
|
138 |
boundary will result in up to 2 zero bytes. |
|
139 |
||
140 |
Raster Data encoding for 24bit / truecolor images: |
|
141 |
BitCount = 24 Compression = 0 |
|
142 |
Every 4bytes / 32bit holds 1 pixel. The first holds its red, the second its green, and the third its blue intensity. The |
|
143 |
fourth byte is reserved and should be zero. There are no color table entries. The pixels are no color table pointers. No |
|
144 |
zero padding necessary. |
|
145 |
||
146 |
||
147 |
Raster Data compression for 4bit / 16 color images: |
|
148 |
BitCount = 4 Compression = 2 |
|
149 |
The pixel data is stored in 2bytes / 16bit chunks. The first of these specifies the number of consecutive pixels with the |
|
150 |
same pair of color. The second byte defines two color indices. The resulting pixel pattern will be interleaved |
|
151 |
high-order 4bits and low order 4 bits (ABABA...). If the first byte is zero, the second defines an escape code. The |
|
152 |
End-of-Bitmap is zero padded to end on a 32bit boundary. Due to the 16bit-ness of this structure this will always be |
|
153 |
either two zero bytes or none. |
|
154 |
||
155 |
n (byte 1) c (Byte 2) Description |
|
156 |
>0 any n pixels are to be drawn. The 1st, 3rd, 5th, ... pixels' color is in c's high-order 4 bits, the even |
|
157 |
pixels' color is in c's low-order 4 bits. If both color indices are the same, it results in just n |
|
158 |
pixels of color c |
|
159 |
0 0 End-of-line |
|
160 |
0 1 End-of-Bitmap |
|
161 |
0 2 Delta. The following 2 bytes define an unsigned offset in x and y direction (y being up) The |
|
162 |
skipped pixels should get a color zero. |
|
163 |
0 >=3 The following c bytes will be read as single pixel colors just as in uncompressed files. up to 12 |
|
164 |
bits of zeros follow, to put the file/memory pointer on a 16bit boundary again. |
|
165 |
||
166 |
||
167 |
Example for 4bit RLE |
|
168 |
Compressed Data Expanded data |
|
169 |
03 04 0 4 0 |
|
170 |
05 06 0 6 0 6 0 |
|
171 |
00 06 45 56 67 00 4 5 5 6 6 7 |
|
172 |
04 78 7 8 7 8 |
|
173 |
00 02 05 01 Move 5 right and 1 up. (Windows docs say down, which is wrong) |
|
174 |
00 00 End-of-line |
|
175 |
09 1E 1 E 1 E 1 E 1 E 1 |
|
176 |
00 01 EndofBitmap |
|
177 |
00 00 Zero padding for 32bit boundary |
|
178 |
||
179 |
||
180 |
Raster Data compression for 8bit / 256 color images: |
|
181 |
||
182 |
BitCount = 8 Compression = 1 |
|
183 |
The pixel data is stored in 2bytes / 16bit chunks. The first of these specifies the number of consecutive pixels with the |
|
184 |
same color. The second byte defines their color index. If the first byte is zero, the second defines an escape code. The |
|
185 |
End-of-Bitmap is zero padded to end on a 32bit boundary. Due to the 16bit-ness of this structure this will always be |
|
186 |
either two zero bytes or none. |
|
187 |
||
188 |
n (byte 1) c (Byte 2) Description |
|
189 |
>0 any n pixels of color number c |
|
190 |
0 0 End-of-line |
|
191 |
0 1 End Of Bitmap |
|
192 |
0 2 Delta. The following 2 bytes define an unsigned offset in x and y direction (y being up) The |
|
193 |
skipped pixels should get a color zero. |
|
194 |
0 >=3 The following c bytes will be read as single pixel colors just as in uncompressed files. A zero |
|
195 |
follows, if c is odd, putting the file/memory pointer on a 16bit boundary again. |
|
196 |
||
197 |
||
198 |
Example for 8bit RLE |
|
199 |
Compressed Data Expanded data |
|
200 |
03 04 04 04 04 |
|
201 |
05 06 06 06 06 06 06 |
|
202 |
00 03 45 56 67 00 45 56 67 |
|
203 |
02 78 78 78 |
|
204 |
00 02 05 01 Move 5 right and 1 up. (Windows docs say down, which is wrong) |
|
205 |
00 00 End-of-line |
|
206 |
09 1E 1E 1E 1E 1E 1E 1E 1E 1E 1E |
|
207 |
00 01 End-of-bitmap |
|
208 |
00 00 Zero padding for 32bit boundary |
|
209 |
||
210 |
||
211 |
||
212 |
||
213 |
Portability: |
|
214 |
||
215 |
Although BMPs were invented by Microsoft for its Windows platform, a lot of programs on other platforms are capable |
|
216 |
of reading and writing them. Notice the Intel order in 2byte and 4-byte integer values (Least significant byte first). The |
|
217 |
16bit BMPs have been introduced to Windows after the others, still puzzling many applications. |
|
218 |
||
219 |
||
220 |
Trademarks, Patents and Royalties |
|
221 |
To my knowledge: None. |
|
222 |
" |
|
223 |
||
21 | 224 |
! ! |
0 | 225 |
|
28 | 226 |
!WindowsIconReader class methodsFor:'initialization'! |
227 |
||
228 |
initialize |
|
211 | 229 |
"tell Image-class, that a new fileReader is present |
398 | 230 |
for the '.bmp' and '.ico' extensions." |
211 | 231 |
|
647 | 232 |
MIMETypes defineImageType:'image/x-MS-bitmap' suffix:'bmp' reader:self. |
233 |
MIMETypes defineImageType:nil suffix:'ico' reader:self. |
|
211 | 234 |
|
398 | 235 |
"Modified: 1.2.1997 / 15:03:59 / cg" |
28 | 236 |
! ! |
237 |
||
2571 | 238 |
!WindowsIconReader class methodsFor:'image support'! |
239 |
||
240 |
loadBMPWidth:w height:h bytesPerPixel:bpp compression:c from:aStream into:data |
|
241 |
|buff idx fileBytesPerRow imgBytesPerRow align| |
|
242 |
||
243 |
align := 4. |
|
244 |
c == 0 ifTrue:[ |
|
245 |
imgBytesPerRow := w * bpp. |
|
246 |
fileBytesPerRow := imgBytesPerRow. |
|
247 |
(fileBytesPerRow bitAnd:(align-1)) ~~ 0 ifTrue:[ |
|
248 |
fileBytesPerRow := (fileBytesPerRow bitAnd:((align-1) bitInvert)) + align. |
|
249 |
]. |
|
250 |
"/ |
|
251 |
"/ stupid - last row comes first |
|
252 |
"/ |
|
253 |
idx := imgBytesPerRow * (h - 1) + 1. |
|
254 |
buff := ByteArray uninitializedNew:fileBytesPerRow. |
|
255 |
||
256 |
1 to:h do:[:row | |
|
257 |
(aStream nextBytes:fileBytesPerRow into:buff) ~~ fileBytesPerRow ifTrue:[ |
|
258 |
^ false |
|
259 |
]. |
|
260 |
data replaceFrom:idx to:idx+imgBytesPerRow-1 with:buff. |
|
261 |
idx := idx - imgBytesPerRow. |
|
262 |
]. |
|
263 |
^ true |
|
264 |
]. |
|
265 |
^ false. |
|
266 |
! ! |
|
267 |
||
102 | 268 |
!WindowsIconReader class methodsFor:'testing'! |
269 |
||
713 | 270 |
canRepresent:anImage |
271 |
"return true, if anImage can be represented in my file format. |
|
272 |
BMP supports depth 1,4,8 and 24." |
|
273 |
||
274 |
^ (#(1 4 8 24) includes:anImage depth) |
|
275 |
||
276 |
"Created: 17.10.1997 / 20:18:23 / cg" |
|
277 |
! |
|
278 |
||
102 | 279 |
isValidImageFile:aFileName |
280 |
"return true, if aFileName contains a valid windows bitmap-file image" |
|
281 |
||
282 |
|inStream header ok| |
|
283 |
||
284 |
inStream := self streamReadingFile:aFileName. |
|
285 |
inStream isNil ifTrue:[^ false]. |
|
286 |
||
287 |
inStream binary. |
|
104 | 288 |
ok := false. |
692
e6af4c70e066
size is going to be eliminated from the stream
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
289 |
inStream fileSize > 16 ifTrue:[ |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
290 |
header := ByteArray uninitializedNew:4. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
291 |
inStream nextBytes:4 into:header. |
102 | 292 |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
293 |
(header startsWith:#(66 77)) ifTrue:[ "BM" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
294 |
ok := true. |
104 | 295 |
"/ 'WINREADER: Win3.x or OS/2 vsn 2 BM format' infoPrintNL. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
296 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
297 |
(header startsWith:#(66 65)) ifTrue:[ "BA" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
298 |
ok := true. |
104 | 299 |
"/ 'WINREADER: OS/2 vsn 2 BA format' infoPrintNL. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
300 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
301 |
(header startsWith:#(73 67)) ifTrue:[ "IC" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
302 |
ok := true. |
104 | 303 |
"/ 'WINREADER: OS/2 IC format' infoPrintNL. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
304 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
305 |
(header startsWith:#(80 84)) ifTrue:[ "PT" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
306 |
ok := true. |
104 | 307 |
"/ 'WINREADER: OS/2 PT format' infoPrintNL. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
308 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
309 |
(header startsWith:#(0 0 1 0)) ifTrue:[ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
310 |
ok := true. |
104 | 311 |
"/ 'WINREADER: Win3.x ICO format' infoPrintNL. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
312 |
]. |
102 | 313 |
]. |
104 | 314 |
inStream close. |
315 |
^ ok |
|
102 | 316 |
|
317 |
" |
|
318 |
WindowsIconReader isValidImageFile:'/phys/clam2/LocalLibrary/Images/OS2_icons/dos.ico' |
|
319 |
" |
|
320 |
||
321 |
"Created: 17.9.1995 / 17:14:20 / claus" |
|
322 |
! ! |
|
323 |
||
103 | 324 |
!WindowsIconReader methodsFor:'private'! |
325 |
||
2571 | 326 |
loadBMP1From:aStream into:aByteArray |
327 |
"load bmp-1 bit per pixel imagedata." |
|
328 |
||
329 |
compression == 0 ifFalse:[ |
|
330 |
^ false |
|
331 |
]. |
|
332 |
||
333 |
^ self loadUncompressedFrom:aStream into:aByteArray |
|
334 |
! |
|
335 |
||
336 |
loadBMP2From:aStream into:aByteArray |
|
337 |
"load bmp-2 bit per pixel imagedata." |
|
338 |
||
339 |
compression == 0 ifFalse:[ |
|
340 |
^ false |
|
341 |
]. |
|
342 |
||
343 |
^ self loadUncompressedFrom:aStream into:aByteArray |
|
344 |
! |
|
345 |
||
346 |
loadBMP4From:aStream into:aByteArray |
|
347 |
"load bmp-4 bit per pixel imagedata." |
|
211 | 348 |
|
2571 | 349 |
compression == 0 ifTrue:[ |
350 |
^ self loadUncompressedFrom:aStream into:aByteArray |
|
351 |
]. |
|
352 |
compression == 2 ifTrue:[ |
|
353 |
^ self loadRLECompressedBMP4From:aStream into:aByteArray |
|
354 |
]. |
|
355 |
||
356 |
^ false |
|
357 |
! |
|
160
ee4d64b12c94
handle depth 24 rgb BMP files
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
358 |
|
2571 | 359 |
loadBMP4to8Width:width height:height from:aStream into:aByteArray |
360 |
"load bmp-4 bit per pixel imagedata. A helper for BMP image reader. |
|
361 |
Primitive c function for speed" |
|
362 |
||
363 |
|f bytes offset| |
|
364 |
||
365 |
aStream isExternalStream ifTrue:[ |
|
366 |
f := aStream filePointer. |
|
367 |
f isNil ifTrue:[^ false]. |
|
368 |
] ifFalse:[ |
|
369 |
aStream isInternalByteStream ifTrue:[ |
|
370 |
bytes := aStream collection. |
|
371 |
bytes isNil ifTrue:[^ false]. |
|
372 |
offset := aStream position. |
|
373 |
] ifFalse:[ |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
374 |
^ false |
2571 | 375 |
] |
160
ee4d64b12c94
handle depth 24 rgb BMP files
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
376 |
]. |
2571 | 377 |
%{ |
378 |
#define GETC(fp, cp) (fp ? getc(fp) : *cp++) |
|
379 |
#define FERROR(fp) (fp && ferror(fp)) |
|
380 |
||
381 |
FILE *__fp = NULL; |
|
382 |
char *__cp = NULL; |
|
383 |
||
384 |
if (f != nil) { |
|
385 |
__fp = __FILEVal(f); |
|
386 |
} else { |
|
387 |
__cp = __stringVal(bytes); |
|
388 |
__cp += __intVal(offset); |
|
389 |
} |
|
390 |
||
391 |
if (! (__bothSmallInteger(width, height) |
|
392 |
&& __isByteArray(aByteArray))) { |
|
393 |
RETURN (false); |
|
394 |
} |
|
395 |
||
396 |
{ |
|
397 |
int w = __intVal(width); |
|
398 |
int h = __intVal(height); |
|
399 |
int comp = __intVal(__INST(compression)); |
|
400 |
unsigned char *dest = __ByteArrayInstPtr(aByteArray)->ba_element; |
|
401 |
int szDest = __byteArraySize(aByteArray); |
|
402 |
int i, j, c, c1, nibnum, padw, x, y; |
|
403 |
unsigned char *pp; |
|
404 |
||
405 |
c = c1 = 0; |
|
406 |
if (comp == 0) { /* read uncompressed data */ |
|
407 |
padw = ((w + 7)/8) * 8; /* 'w', padded to be a multiple of 8pix (32bits) */ |
|
408 |
||
409 |
for (i=h-1; i>=0; i--) { |
|
410 |
pp = dest + (i * w); |
|
411 |
for (j=nibnum=0; j<padw; j++,nibnum++) { |
|
412 |
if ((nibnum&1) == 0) { /* read the next byte */ |
|
413 |
c = GETC(__fp, __cp); |
|
414 |
console_printf("\nch: %02x ", c); |
|
415 |
if (c == EOF) { |
|
416 |
if (@global(InfoPrinting) == true) { |
|
417 |
console_fprintf(stderr, "ImageReader [warning]: premature EOF\n"); |
|
418 |
} |
|
419 |
RETURN (false); |
|
420 |
} |
|
421 |
nibnum = 0; |
|
422 |
} |
|
423 |
if (j<w) { |
|
424 |
if (pp >= (dest+szDest)) { |
|
425 |
if (@global(InfoPrinting) == true) { |
|
426 |
console_fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n"); |
|
427 |
} |
|
428 |
{ RETURN (false) }; |
|
429 |
} |
|
430 |
*pp++ = (c & 0xF0) >> 4; |
|
431 |
console_printf("%d ", pp[-1]); |
|
432 |
c <<= 4; |
|
433 |
} |
|
434 |
} |
|
435 |
if (FERROR(__fp)) { RETURN (false) }; |
|
436 |
} |
|
437 |
RETURN (true); |
|
438 |
} |
|
439 |
if (comp == 2) { /* read RLE4 compressed data */ |
|
440 |
x = y = 0; |
|
441 |
pp = dest + x + (h-y-1)*w; |
|
442 |
||
443 |
while (y<h) { |
|
444 |
c = GETC(__fp, __cp); if (c == EOF) { RETURN (false); } |
|
445 |
if (c) { /* encoded mode */ |
|
446 |
c &= 0xFF; |
|
447 |
c1 = GETC(__fp, __cp); if (c1 == EOF) { RETURN (false); } |
|
448 |
for (i=0; i<c; i++,x++,pp++) { |
|
449 |
if (pp >= (dest+szDest)) { |
|
450 |
if (@global(InfoPrinting) == true) { |
|
451 |
console_fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n"); |
|
452 |
} |
|
453 |
return 0; |
|
454 |
} |
|
455 |
*pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); |
|
456 |
} |
|
457 |
} else { |
|
458 |
/* c==0x00 : escape codes */ |
|
459 |
c = GETC(__fp, __cp); if (c == EOF) { RETURN (false); } |
|
460 |
||
461 |
if (c == 0x00) { /* end of line */ |
|
462 |
x=0; y++; pp = dest + x + (h-y-1)*w; |
|
463 |
} else |
|
464 |
if (c == 0x01) break; /* end of pic8 */ |
|
465 |
||
466 |
else if (c == 0x02) { /* delta */ |
|
467 |
c = GETC(__fp, __cp); if (c == EOF) { RETURN (false); } |
|
468 |
x += (c & 0xFF); |
|
469 |
c = GETC(__fp, __cp); if (c == EOF) { RETURN (false); } |
|
470 |
y += (c & 0xFF); |
|
471 |
pp = dest + x + (h-y-1)*w; |
|
472 |
} else { /* absolute mode */ |
|
473 |
c &= 0xFF; |
|
474 |
for (i=0; i<c; i++, x++, pp++) { |
|
475 |
if ((i&1) == 0) { |
|
476 |
c1 = GETC(__fp, __cp); if (c1 == EOF) { RETURN (false); } |
|
477 |
} |
|
478 |
if (pp >= (dest+szDest)) { |
|
479 |
if (@global(InfoPrinting) == true) { |
|
480 |
console_fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n"); |
|
481 |
} |
|
482 |
return 0; |
|
483 |
} |
|
484 |
*pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f); |
|
485 |
} |
|
486 |
||
487 |
if (((c&3)==1) || ((c&3)==2)) { |
|
488 |
GETC(__fp, __cp); /* read pad byte */ |
|
489 |
} |
|
490 |
} |
|
491 |
} /* escape processing */ |
|
492 |
if (FERROR(__fp)) RETURN (false); |
|
493 |
} /* while */ |
|
494 |
RETURN (true); |
|
495 |
} |
|
496 |
} |
|
497 |
%}. |
|
498 |
^ false |
|
499 |
||
500 |
"Modified: 22.4.1996 / 19:14:32 / cg" |
|
501 |
! |
|
502 |
||
503 |
loadBMP8Width:width height:height from:aStream into:aByteArray |
|
504 |
"load bmp-4 bit per pixel imagedata. A helper for BMP image reader. |
|
505 |
Primitive c function for speed" |
|
506 |
||
507 |
|f bytes offset| |
|
508 |
||
509 |
aStream isExternalStream ifTrue:[ |
|
510 |
f := aStream filePointer. |
|
511 |
f isNil ifTrue:[^ false]. |
|
512 |
] ifFalse:[ |
|
513 |
aStream isInternalByteStream ifTrue:[ |
|
514 |
bytes := aStream collection. |
|
515 |
bytes isNil ifTrue:[^ false]. |
|
516 |
offset := aStream position. |
|
517 |
] ifFalse:[ |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
518 |
^ false |
2571 | 519 |
] |
103 | 520 |
]. |
2571 | 521 |
%{ |
522 |
#define GETC(fp, cp) (fp ? getc(fp) : *cp++) |
|
523 |
#define FERROR(fp) (fp && ferror(fp)) |
|
524 |
||
525 |
FILE *__fp = NULL; |
|
526 |
char *__cp = NULL; |
|
527 |
||
528 |
if (f != nil) { |
|
529 |
__fp = __FILEVal(f); |
|
530 |
} else { |
|
531 |
__cp = __stringVal(bytes); |
|
532 |
__cp += __intVal(offset); |
|
533 |
} |
|
534 |
||
535 |
if (! (__bothSmallInteger(width, height) |
|
536 |
&& __isByteArray(aByteArray))) { |
|
537 |
RETURN (false); |
|
538 |
} |
|
539 |
||
540 |
{ |
|
541 |
int w = __intVal(width); |
|
542 |
int h = __intVal(height); |
|
543 |
int comp = __intVal(__INST(compression)); |
|
544 |
unsigned char *dest = __ByteArrayInstPtr(aByteArray)->ba_element; |
|
545 |
int szDest = __byteArraySize(aByteArray); |
|
546 |
int i,j,c,c1,padw,x,y; |
|
547 |
unsigned char *pp; |
|
548 |
||
549 |
if (comp == 0) { /* uncompressed data */ |
|
550 |
padw = ((w + 3)/4) * 4; |
|
551 |
||
552 |
for (i=h-1; i>=0; i--) { |
|
553 |
pp = dest + (i * w); |
|
554 |
||
555 |
for (j=0; j<padw; j++) { |
|
556 |
c = GETC(__fp, __cp); |
|
557 |
if (c==EOF) { |
|
558 |
if (@global(InfoPrinting) == true) { |
|
559 |
console_fprintf(stderr, "ImageReader [warning]: BMP premature EOF [%d]\n", __LINE__); |
|
560 |
console_fprintf(stderr, "h=%d i=%d w=%d j=%d padw=%d\n", h, i, w, j, padw); |
|
561 |
} |
|
562 |
RETURN(false); |
|
563 |
} |
|
564 |
if (j<w) { |
|
565 |
if (pp >= (dest+szDest)) { |
|
566 |
if (@global(InfoPrinting) == true) { |
|
567 |
console_fprintf(stderr, "ImageReader [warning]: BMP outBuffer overrun\n"); |
|
568 |
} |
|
569 |
RETURN(false); |
|
570 |
} |
|
571 |
*pp++ = c; |
|
572 |
} |
|
573 |
} |
|
574 |
if (FERROR(__fp)) { |
|
575 |
if (@global(InfoPrinting) == true) { |
|
576 |
console_fprintf(stderr, "ImageReader [warning]: BMP ferror set\n"); |
|
577 |
} |
|
578 |
RETURN(false); |
|
579 |
} |
|
580 |
} |
|
581 |
if (FERROR(__fp)) { |
|
582 |
if (@global(InfoPrinting) == true) { |
|
583 |
console_fprintf(stderr, "ImageReader [warning]: BMP ferror set at end\n"); |
|
584 |
} |
|
585 |
RETURN(false); |
|
586 |
} |
|
587 |
RETURN(true); |
|
588 |
} |
|
589 |
if (comp == 1) { /* RLE8 compressed */ |
|
590 |
x = y = 0; |
|
591 |
pp = dest + x + (h-y-1)*w; |
|
592 |
||
593 |
while (y<h) { |
|
594 |
c = GETC(__fp, __cp); |
|
595 |
if (c == EOF) { |
|
596 |
if (@global(InfoPrinting) == true) { |
|
597 |
console_fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__); |
|
598 |
} |
|
599 |
RETURN(false); |
|
600 |
} |
|
601 |
if (c) { /* encoded mode */ |
|
602 |
c1 = GETC(__fp, __cp); |
|
603 |
if (c1 == EOF) { |
|
604 |
if (@global(InfoPrinting) == true) { |
|
605 |
console_fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__); |
|
606 |
} |
|
607 |
RETURN(false); |
|
608 |
} |
|
609 |
c &= 0xFF; |
|
610 |
||
611 |
for (i=0; i<c; i++,x++,pp++) { |
|
612 |
if (pp >= (dest+szDest)) { |
|
613 |
if (@global(InfoPrinting) == true) { |
|
614 |
console_fprintf(stderr, "ImageReader [warning]: BMP/RLE8 outBuffer overrun\n"); |
|
615 |
} |
|
616 |
RETURN(false); |
|
617 |
} |
|
618 |
*pp = c1; |
|
619 |
} |
|
620 |
} else { |
|
621 |
/* c==0x00 : escape codes */ |
|
622 |
c = GETC(__fp, __cp); |
|
623 |
if (c == EOF) { |
|
624 |
if (@global(InfoPrinting) == true) { |
|
625 |
console_fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__); |
|
626 |
} |
|
627 |
RETURN(false); |
|
628 |
} |
|
629 |
if (c == 0x00) { /* end of line */ |
|
630 |
x=0; y++; pp = dest + x + (h-y-1)*w; |
|
631 |
} else |
|
632 |
if (c == 0x01) break; /* end of pic8 */ |
|
633 |
else if (c == 0x02) { /* delta */ |
|
634 |
c = GETC(__fp, __cp); |
|
635 |
if (c == EOF) { |
|
636 |
if (@global(InfoPrinting) == true) { |
|
637 |
console_fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__); |
|
638 |
} |
|
639 |
RETURN(false); |
|
640 |
} |
|
641 |
x += (c & 0xFF); |
|
642 |
c = GETC(__fp, __cp); |
|
643 |
if (c == EOF) { |
|
644 |
if (@global(InfoPrinting) == true) { |
|
645 |
console_fprintf(stderr, "ImageReader [warning]: BMP/RLE8 premature EOF [%d]\n", __LINE__); |
|
646 |
} |
|
647 |
RETURN(false); |
|
648 |
} |
|
649 |
y += (c & 0xFF); |
|
650 |
pp = dest + x + (h-y-1)*w; |
|
651 |
} else { /* absolute mode */ |
|
652 |
c &= 0xFF; |
|
653 |
for (i=0; i<c; i++, x++, pp++) { |
|
654 |
c1 = GETC(__fp, __cp); |
|
655 |
if (c1 == EOF) { RETURN(false); } |
|
656 |
if (pp >= (dest+szDest)) { |
|
657 |
if (@global(InfoPrinting) == true) { |
|
658 |
console_fprintf(stderr, "ImageReader [warning]: BMP/RLE8 outBuffer overrun\n"); |
|
659 |
} |
|
660 |
RETURN(false); |
|
661 |
} |
|
662 |
*pp = c1; |
|
663 |
} |
|
664 |
||
665 |
if (c & 1) GETC(__fp, __cp); /* odd length run: read an extra pad byte */ |
|
666 |
} |
|
667 |
} |
|
668 |
if (FERROR(__fp)) { |
|
669 |
if (@global(InfoPrinting) == true) { |
|
670 |
console_fprintf(stderr, "ImageReader [warning]: BMP/RLE8 ferror set\n"); |
|
671 |
} |
|
672 |
RETURN(false); |
|
673 |
} |
|
674 |
} |
|
675 |
if (FERROR(__fp)) { |
|
676 |
if (@global(InfoPrinting) == true) { |
|
677 |
console_fprintf(stderr, "ImageReader [warning]: BMP/RLE8 ferror set at end\n"); |
|
678 |
} |
|
679 |
RETURN(false); |
|
680 |
} |
|
681 |
RETURN(true); |
|
682 |
} |
|
683 |
if (@global(InfoPrinting) == true) { |
|
684 |
console_fprintf(stderr, "ImageReader [warning]: BMP unhandled compression: %d\n", comp); |
|
685 |
} |
|
686 |
RETURN(false); |
|
687 |
} |
|
688 |
%}. |
|
689 |
^ true |
|
690 |
||
691 |
"Modified: 22.4.1996 / 19:14:32 / cg" |
|
692 |
! |
|
693 |
||
694 |
loadBMPWidth:w height:h bytesPerPixel:bpp from:aStream into:data |
|
695 |
|buff idx fileBytesPerRow imgBytesPerRow align| |
|
696 |
||
697 |
align := 4. |
|
698 |
||
699 |
compression == 0 ifTrue:[ |
|
700 |
imgBytesPerRow := w * bpp. |
|
713 | 701 |
fileBytesPerRow := imgBytesPerRow. |
2571 | 702 |
(fileBytesPerRow bitAnd:(align-1)) ~~ 0 ifTrue:[ |
703 |
fileBytesPerRow := (fileBytesPerRow bitAnd:((align-1) bitInvert)) + align. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
704 |
]. |
713 | 705 |
"/ |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
706 |
"/ stupid - last row comes first |
713 | 707 |
"/ |
2571 | 708 |
idx := imgBytesPerRow * (h - 1) + 1. |
713 | 709 |
buff := ByteArray uninitializedNew:fileBytesPerRow. |
160
ee4d64b12c94
handle depth 24 rgb BMP files
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
710 |
|
2571 | 711 |
1 to:h do:[:row | |
713 | 712 |
(aStream nextBytes:fileBytesPerRow into:buff) ~~ fileBytesPerRow ifTrue:[ |
713 |
^ false |
|
714 |
]. |
|
715 |
data replaceFrom:idx to:idx+imgBytesPerRow-1 with:buff. |
|
716 |
idx := idx - imgBytesPerRow. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
717 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
718 |
^ true |
160
ee4d64b12c94
handle depth 24 rgb BMP files
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
719 |
]. |
2571 | 720 |
^ false. |
721 |
! |
|
722 |
||
723 |
loadBMPWidth:w height:h depth:d from:aStream into:data |
|
724 |
"helper: load a BMP image" |
|
725 |
||
726 |
d == 8 ifTrue:[ |
|
727 |
compression == 0 ifTrue:[ |
|
728 |
^ self loadUncompressedFrom:aStream into:data. |
|
729 |
]. |
|
730 |
compression == 1 ifTrue:[ |
|
731 |
^ self loadRLECompressedBMP8From:aStream into:data. |
|
732 |
]. |
|
733 |
self halt. |
|
734 |
^ false |
|
735 |
]. |
|
736 |
d == 4 ifTrue:[ |
|
737 |
^ self loadBMP4From:aStream into:data |
|
738 |
]. |
|
739 |
d == 2 ifTrue:[ |
|
740 |
^ self loadBMP2From:aStream into:data |
|
741 |
]. |
|
742 |
d == 1 ifTrue:[ |
|
743 |
^ self loadBMP1From:aStream into:data |
|
744 |
]. |
|
745 |
((d == 16) |
|
746 |
or:[ (d == 24) |
|
747 |
or:[ (d == 32) ]]) ifTrue:[ |
|
748 |
(self loadBMPWidth:w height:h bytesPerPixel:(d // 8) from:aStream into:data) ifFalse:[ |
|
749 |
^ false |
|
750 |
]. |
|
751 |
inDepth == 16 ifTrue:[ |
|
752 |
"/ Depth16Image keeps its data MSB (sigh); here they come LSB. |
|
753 |
data swapBytes. |
|
754 |
]. |
|
755 |
inDepth == 24 ifTrue:[ |
|
756 |
"/ Depth24Image keeps its data r/g/b; BMP has it b/g/r (sigh) |
|
757 |
self swapBytesFromRGB_to_BGR. |
|
758 |
]. |
|
759 |
||
760 |
^ true |
|
761 |
]. |
|
819 | 762 |
^ false |
103 | 763 |
|
819 | 764 |
"Created: / 17.9.1995 / 18:48:11 / claus" |
765 |
"Modified: / 3.2.1998 / 20:21:16 / cg" |
|
2571 | 766 |
! |
767 |
||
768 |
loadRLECompressedBMP8From:aStream into:aByteArray |
|
769 |
"load bmp-8 bit per pixel imagedata" |
|
770 |
||
771 |
|bytesPerRowInData x y dstIndex lineStartIndex cnt clr code n| |
|
772 |
||
773 |
bytesPerRowInData := self bytesPerRow. |
|
774 |
x := 0. |
|
775 |
y := height - 1. |
|
776 |
lineStartIndex := (y * bytesPerRowInData) + 1. |
|
777 |
dstIndex := lineStartIndex. |
|
778 |
||
779 |
[ y < height ] whileTrue:[ |
|
780 |
cnt := aStream nextByte. |
|
781 |
clr := aStream nextByte. |
|
782 |
cnt ~~ 0 ifTrue:[ |
|
783 |
aByteArray from:dstIndex to:dstIndex+cnt-1 put:clr. |
|
784 |
x := x + cnt. |
|
785 |
dstIndex := dstIndex + cnt. |
|
786 |
] ifFalse:[ |
|
787 |
"/ cnt == 0: escape codes */ |
|
788 |
code := clr. |
|
789 |
code == 0 ifTrue:[ |
|
790 |
"/ end of line |
|
791 |
x := 0. |
|
792 |
y := y - 1. |
|
793 |
lineStartIndex := lineStartIndex - bytesPerRowInData. |
|
794 |
dstIndex := lineStartIndex. |
|
795 |
] ifFalse:[ |
|
796 |
code == 1 ifTrue:[ |
|
797 |
"/ end of pic |
|
798 |
^ true |
|
799 |
]. |
|
800 |
code == 2 ifTrue:[ |
|
801 |
"/ delta |
|
802 |
x := x + aStream nextSignedByte. |
|
803 |
y := y - aStream nextSignedByte. |
|
804 |
lineStartIndex := (y * bytesPerRowInData) + 1. |
|
805 |
dstIndex := lineStartIndex + x. |
|
806 |
] ifFalse:[ |
|
807 |
"/ absolute; cnt pixels coming |
|
808 |
cnt := code. |
|
809 |
n := aStream nextBytes:cnt into:aByteArray startingAt:dstIndex. |
|
810 |
n ~~ cnt ifTrue:[self halt. ^ false]. |
|
811 |
x := x + cnt. |
|
812 |
dstIndex := dstIndex + cnt. |
|
813 |
"/ odd count - padd |
|
814 |
cnt odd ifTrue:[ |
|
815 |
aStream skip:1. |
|
816 |
]. |
|
817 |
]. |
|
818 |
]. |
|
819 |
]. |
|
820 |
]. |
|
821 |
^ true. |
|
822 |
! |
|
823 |
||
824 |
loadUncompressedFrom:aStream into:aByteArray |
|
825 |
"load bmp-1,2,4 and 8 bit per pixel imagedata." |
|
826 |
||
827 |
|bytesPerRowInStream bytesPerRowInData skip dstIndex n| |
|
828 |
||
829 |
compression == 0 ifFalse:[ |
|
830 |
^ false |
|
831 |
]. |
|
832 |
||
833 |
bytesPerRowInStream := Image bytesPerRowForWidth:width depth:inDepth padding:32. |
|
834 |
bytesPerRowInData := self bytesPerRow. |
|
835 |
skip := bytesPerRowInStream - bytesPerRowInData. |
|
836 |
||
837 |
"/ bottom row first... |
|
838 |
dstIndex := (height - 1) * bytesPerRowInData + 1. |
|
839 |
height to:1 by:-1 do:[:y | |
|
840 |
n := aStream nextBytes:bytesPerRowInData into:aByteArray startingAt:dstIndex. |
|
841 |
n ~~ bytesPerRowInData ifTrue:[ |
|
842 |
^ false. |
|
843 |
]. |
|
844 |
skip ~~ 0 ifTrue:[ |
|
845 |
aStream skip:skip. |
|
846 |
]. |
|
847 |
dstIndex := dstIndex - bytesPerRowInData. |
|
848 |
]. |
|
849 |
^ true. |
|
850 |
! |
|
851 |
||
852 |
swapBytesFromRGB_to_BGR |
|
853 |
|idx bytesPerRow| |
|
854 |
||
855 |
"/ Depth24Image keeps its data r/g/b; BMP has it b/g/r (sigh) |
|
856 |
idx := 1. |
|
857 |
bytesPerRow := self bytesPerRow. |
|
858 |
1 to:height do:[:y | |
|
859 |
self class swap:bytesPerRow bytesFromRGB_to_BGR_in:data startingAt:idx. |
|
860 |
idx := idx + bytesPerRow. |
|
861 |
]. |
|
103 | 862 |
! ! |
863 |
||
1848 | 864 |
!WindowsIconReader methodsFor:'private-reading'! |
865 |
||
866 |
readColorMap:nColors numBytesPerColor:nRawBytesPerColor from:aStream |
|
867 |
"read the colormap; notice: its in BGR order (sigh)." |
|
868 |
||
869 |
|rawMap rMap gMap bMap |
|
870 |
srcIndex "{ Class: SmallInteger }" |
|
871 |
skipDelta "{ Class: SmallInteger }"| |
|
872 |
||
873 |
rawMap := ByteArray uninitializedNew:(nColors*nRawBytesPerColor). |
|
874 |
aStream nextBytes:(nColors*nRawBytesPerColor) into:rawMap. |
|
875 |
||
876 |
rMap := ByteArray new:nColors. |
|
877 |
gMap := ByteArray new:nColors. |
|
878 |
bMap := ByteArray new:nColors. |
|
879 |
srcIndex := 1. |
|
880 |
skipDelta := nRawBytesPerColor - 3. |
|
881 |
||
882 |
"/ stupid: this is a BGR-ordered map (otherwise, could use #rgbBytesVector:-message) |
|
883 |
"/ also, there might be a fourth byte (alpha ?) which is (currently) skipped. |
|
884 |
1 to:nColors do:[:i | |
|
885 |
bMap at:i put:(rawMap at:srcIndex). |
|
886 |
srcIndex := srcIndex + 1. |
|
887 |
gMap at:i put:(rawMap at:srcIndex). |
|
888 |
srcIndex := srcIndex + 1. |
|
889 |
rMap at:i put:(rawMap at:srcIndex). |
|
890 |
srcIndex := srcIndex + 1. |
|
891 |
srcIndex := srcIndex + skipDelta. |
|
892 |
]. |
|
893 |
||
894 |
^ MappedPalette |
|
895 |
redVector:rMap |
|
896 |
greenVector:gMap |
|
897 |
blueVector:bMap. |
|
898 |
! ! |
|
899 |
||
1805 | 900 |
!WindowsIconReader methodsFor:'reading'! |
0 | 901 |
|
211 | 902 |
fromOS2File:aFilename |
903 |
"read an image from an OS/2 BMP file" |
|
904 |
||
41 | 905 |
|reader stream| |
906 |
||
1790 | 907 |
stream := self class streamReadingFile:aFilename. |
41 | 908 |
stream isNil ifTrue:[^ nil]. |
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
909 |
reader := self class new. |
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
910 |
reader fromOS2Stream:stream. |
41 | 911 |
stream close. |
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
912 |
^ reader image |
211 | 913 |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
914 |
"Modified: / 30-05-2007 / 16:52:50 / cg" |
21 | 915 |
! |
0 | 916 |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
917 |
fromOS2Stream:aStream |
211 | 918 |
"read an image from an OS/2 BMP stream" |
919 |
||
2571 | 920 |
^ self fromOS2Stream:aStream alreadyRead:nil |
921 |
! |
|
922 |
||
923 |
fromOS2Stream:aStream alreadyRead:bytesAlreadyRead |
|
924 |
"read an image from an OS/2 BMP stream" |
|
925 |
||
926 |
|header inBytesPerRow mask bytesPerRow nColors nByte| |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
927 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
928 |
inStream := aStream. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
929 |
aStream binary. |
2031 | 930 |
byteOrder := #lsb. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
931 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
932 |
"read the header" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
933 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
934 |
header := ByteArray uninitializedNew:8r110. |
2571 | 935 |
bytesAlreadyRead size > 0 ifTrue:[ |
936 |
header replaceFrom:1 with:bytesAlreadyRead |
|
937 |
]. |
|
938 |
aStream nextBytes:(16-bytesAlreadyRead size) into:header startingAt:(1+bytesAlreadyRead size). |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
939 |
|
445 | 940 |
(header startsWith:#(73 67)) ifTrue:[ "IC" |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
941 |
"IC format" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
942 |
aStream nextBytes:10 into:header startingAt:17. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
943 |
width := header at:7. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
944 |
height := header at:9. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
945 |
inDepth := 2 "header at:11". "where is it" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
946 |
] ifFalse:[ |
445 | 947 |
(header startsWith:#(67 73)) ifTrue:[ "CI" |
819 | 948 |
^ self fileFormatError:'unsupported format: CI'. |
445 | 949 |
] ifFalse:[ |
950 |
aStream nextBytes:(8r110-16) into:header startingAt:17. |
|
951 |
width := header at:8r101. |
|
952 |
height := header at:8r103. |
|
953 |
inDepth := header at:8r107. |
|
954 |
] |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
955 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
956 |
|
1846 | 957 |
self reportDimension. |
958 |
||
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
959 |
"read the colormap; notice: its in BGR order (sigh)" |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
960 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
961 |
nColors := 1 bitShift:inDepth. |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
962 |
colorMap := self readColorMap:nColors numBytesPerColor:3 from:aStream. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
963 |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
964 |
"read the mask" |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
965 |
|
2571 | 966 |
nByte := ((width * height) + 7) // 8. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
967 |
mask := ByteArray uninitializedNew:nByte. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
968 |
aStream nextBytes:nByte into:mask. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
969 |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
970 |
"what is this ?" |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
971 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
972 |
aStream nextBytes:nByte into:mask. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
973 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
974 |
"/ "read the data bits" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
975 |
"/ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
976 |
"/ bytesPerRow := width * inDepth + 7 // 8. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
977 |
"/ data4 := ByteArray uninitializedNew:(height * bytesPerRow). |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
978 |
"/ inDepth == 8 ifTrue:[ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
979 |
"/ ]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
980 |
"/ aStream nextBytes:(height * bytesPerRow) into:data4. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
981 |
"/ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
982 |
"/ "stupid: last row first" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
983 |
"/ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
984 |
"/ tmp := ByteArray new:(height * bytesPerRow). |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
985 |
"/ srcIndex := 1. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
986 |
"/ dstIndex := (height - 1) * bytesPerRow + 1. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
987 |
"/ 1 to:height do:[:row | |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
988 |
"/ tmp replaceFrom:dstIndex to:(dstIndex + bytesPerRow - 1) |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
989 |
"/ with:data4 startingAt:srcIndex. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
990 |
"/ srcIndex := srcIndex + bytesPerRow. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
991 |
"/ dstIndex := dstIndex - bytesPerRow. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
992 |
"/ ]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
993 |
"/ data4 := tmp. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
994 |
"/ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
995 |
"/ "expand into bytes" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
996 |
"/ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
997 |
"/ data := ByteArray new:(width * height). |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
998 |
"/ data4 expandPixels:inDepth width:width height:height |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
999 |
"/ into:data mapping:nil. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1000 |
"/ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1001 |
|
2571 | 1002 |
bytesPerRow := ((width * inDepth) + 7) // 8. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1003 |
"/ bmp data is always 32bit aligned; if required, |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1004 |
inBytesPerRow := ((bytesPerRow + 3) // 4) * 4. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1005 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1006 |
data := ByteArray uninitializedNew:(height * width "bytesPerRow"). |
2571 | 1007 |
compression := 0. |
1008 |
(self loadBMPWidth:width height:height depth:inDepth from:aStream into:data) ifFalse:[ |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1009 |
^ nil |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1010 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1011 |
photometric := #palette. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1012 |
samplesPerPixel := 1. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1013 |
bitsPerSample := #(8). |
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1014 |
^ self image |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1015 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1016 |
" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1017 |
|i f| |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1018 |
i := Image fromFile:'/LocalLibrary/Images/OS2/dos3.ico'. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1019 |
f := i asFormOn:Display. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1020 |
v displayOpaqueForm:(f magnifyBy:2@2) x:5 y:5 |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1021 |
" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1022 |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1023 |
"Modified: / 17-09-1995 / 18:49:24 / claus" |
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1024 |
"Modified: / 30-05-2007 / 16:53:24 / cg" |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1025 |
! |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1026 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1027 |
fromStream:aStream |
211 | 1028 |
"figure out which format the stream contains |
1029 |
(there are various different bmp/ico formats around) |
|
1030 |
and read the image." |
|
1031 |
||
1032 |
|fileSize header| |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1033 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1034 |
inStream := aStream. |
2031 | 1035 |
byteOrder := #lsb. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1036 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1037 |
aStream binary. |
2571 | 1038 |
aStream isFileStream ifTrue:[ |
1039 |
fileSize := aStream fileSize. |
|
1040 |
fileSize < 16 ifTrue:[ |
|
1041 |
^ self fileFormatError:'short file'. |
|
1042 |
]. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1043 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1044 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1045 |
header := ByteArray uninitializedNew:4. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1046 |
aStream nextBytes:4 into:header. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1047 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1048 |
(header startsWith:#(66 77)) ifTrue:[ "BM" |
387 | 1049 |
"/ 'WinIconReader [info]: Win3.x or OS/2 vsn 2 BM format' infoPrintNL. |
2571 | 1050 |
^ self fromWindowsBMPStream:aStream alreadyRead:header |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1051 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1052 |
(header startsWith:#(66 65)) ifTrue:[ "BA" |
387 | 1053 |
"/ 'WinIconReader [info]: OS/2 vsn 2 BA format' infoPrintNL. |
2571 | 1054 |
^ self fromOS2Stream:aStream alreadyRead:header |
445 | 1055 |
]. |
1056 |
(header startsWith:#(67 73)) ifTrue:[ "CI" |
|
1057 |
"/ 'WinIconReader [info]: OS/2 vsn 2 BA format' infoPrintNL. |
|
2031 | 1058 |
"/ ^ self fromOS2Stream:aStream |
1059 |
^ self fileFormatError:'OS/2 CI format not supported'. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1060 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1061 |
(header startsWith:#(73 67)) ifTrue:[ "IC" |
387 | 1062 |
"/ 'WinIconReader [info]: OS/2 IC format' infoPrintNL. |
2571 | 1063 |
^ self fromOS2Stream:aStream alreadyRead:header |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1064 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1065 |
(header startsWith:#(80 84)) ifTrue:[ "PT" |
387 | 1066 |
"/ 'WinIconReader [info]: OS/2 PT format' infoPrintNL. |
2571 | 1067 |
^ self fromOS2Stream:aStream alreadyRead:header |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1068 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1069 |
(header startsWith:#(16r53 16r5A)) ifTrue:[ "SZ" |
1167
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1070 |
"/ 'WinIconReader [info]: OS/2 SZ format' infoPrintNL. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1071 |
"/ ^ self fromOS2Stream:aStream |
2031 | 1072 |
^ self fileFormatError:'OS/2 SZ format not supported'. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1073 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1074 |
(header startsWith:#(0 0 1 0)) ifTrue:[ |
387 | 1075 |
"/ 'WinIconReader [info]: Win3.x ICO format' infoPrintNL. |
2571 | 1076 |
^ self fromWindowsICOStream:aStream alreadyRead:header |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1077 |
]. |
819 | 1078 |
^ self fileFormatError:('format not supported:' |
1079 |
, ((header at:1) printStringRadix:16) |
|
1080 |
, ' ' |
|
1081 |
, ((header at:2) printStringRadix:16)). |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1082 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1083 |
" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1084 |
Image fromFile:'/phys/clam//LocalLibrary/Images/OS2_icons/dos.ico' |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1085 |
" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1086 |
|
819 | 1087 |
"Modified: / 17.9.1995 / 18:59:07 / claus" |
1088 |
"Modified: / 3.2.1998 / 20:18:14 / cg" |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1089 |
! |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1090 |
|
21 | 1091 |
fromWindowsBMPFile: aFilename |
211 | 1092 |
"read an image from a windows BMP file" |
1093 |
||
41 | 1094 |
|reader stream| |
1095 |
||
1096 |
stream := self class streamReadingFile:aFilename. |
|
1097 |
stream isNil ifTrue:[^ nil]. |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1098 |
reader := self class new. |
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1099 |
reader fromWindowsBMPStream:stream. |
41 | 1100 |
stream close. |
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1101 |
^ reader image. |
211 | 1102 |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1103 |
"Modified: / 30-05-2007 / 16:53:48 / cg" |
21 | 1104 |
! |
1105 |
||
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1106 |
fromWindowsBMPStream:aStream |
211 | 1107 |
"read an image from a windows BMP stream" |
1108 |
||
2571 | 1109 |
^ self fromWindowsBMPStream:aStream alreadyRead:nil |
1110 |
! |
|
1111 |
||
1112 |
fromWindowsBMPStream:aStream alreadyRead:bytesAlreadyRead |
|
1113 |
"read an image from a windows BMP stream" |
|
1114 |
||
1115 |
| header iSize inPlanes |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1116 |
imgSize resH resV numColor numImportantColor |
2571 | 1117 |
dataStart redMask greenMask blueMask alphaMask |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1118 |
bytesPerRow numBytesPerColorInColormap| |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1119 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1120 |
inStream := aStream. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1121 |
aStream binary. |
2031 | 1122 |
byteOrder := #lsb. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1123 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1124 |
"read the header" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1125 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1126 |
header := ByteArray uninitializedNew:16r54. |
2571 | 1127 |
bytesAlreadyRead size > 0 ifTrue:[ |
1128 |
header replaceFrom:1 with:bytesAlreadyRead |
|
1129 |
]. |
|
1130 |
aStream nextBytes:(18-bytesAlreadyRead size) into:header startingAt:(1+bytesAlreadyRead size). |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1131 |
|
518 | 1132 |
iSize := header at:(16r0E + 1). |
1133 |
(iSize == 40) ifTrue:[ "header-size" |
|
2571 | 1134 |
"/ |
1135 |
"/ a Windows3.x BMP file |
|
1136 |
"/ |
|
1137 |
"/ 'WinIconReader [info]: Win3.x format' infoPrintCR. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1138 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1139 |
aStream nextBytes:(40-4) into:header startingAt:19. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1140 |
|
2571 | 1141 |
width := header wordAt:(16r12 + 1) MSB:false. |
1142 |
height := header wordAt:(16r16 + 1) MSB:false. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1143 |
inPlanes := header wordAt:(16r1A + 1) MSB:false. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1144 |
inDepth := header wordAt:(16r1C + 1) MSB:false. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1145 |
compression := header wordAt:(16r1E + 1) MSB:false. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1146 |
imgSize := header doubleWordAt:(16r22 + 1) MSB:false. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1147 |
resH := header doubleWordAt:(16r26 + 1) MSB:false. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1148 |
resV := header doubleWordAt:(16r2A + 1) MSB:false. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1149 |
numColor := header doubleWordAt:(16r2E + 1) MSB:false. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1150 |
numImportantColor := header doubleWordAt:(16r32 + 1) MSB:false. |
2571 | 1151 |
redMask := header doubleWordAt:(16r36 + 1) MSB:false. |
1152 |
greenMask := header doubleWordAt:(16r3A + 1) MSB:false. |
|
1153 |
blueMask := header doubleWordAt:(16r3E + 1) MSB:false. |
|
1154 |
alphaMask := header doubleWordAt:(16r42 + 1) MSB:false. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1155 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1156 |
numColor == 0 ifTrue:[ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1157 |
" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1158 |
some bmp-writers seem to leave this as zero (which is wrong) |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1159 |
" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1160 |
inDepth <= 8 ifTrue:[ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1161 |
numColor := 1 bitShift:inDepth. |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1162 |
"/ 'WinIconReader [warning]: missing nColor in header - assume ' infoPrint. numColor infoPrintCR |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1163 |
] |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1164 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1165 |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1166 |
numBytesPerColorInColormap := 4. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1167 |
dataStart := header wordAt:(16r0A + 1) MSB:false |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1168 |
] ifFalse:[ |
2571 | 1169 |
((iSize == 12) or:[iSize >= 64]) ifTrue:[ |
1170 |
"/ |
|
1171 |
"/ its an OS/2 BMP file |
|
1172 |
"/ |
|
1173 |
"/ 'WinIconReader [info]: OS/2 format' infoPrintCR. |
|
1174 |
aStream nextBytes:(iSize-4) into:header startingAt:19. |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1175 |
|
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1176 |
numBytesPerColorInColormap := 3. |
565 | 1177 |
dataStart := nil. |
2571 | 1178 |
|
1179 |
iSize == 12 ifTrue:[ |
|
1180 |
width := header wordAt:(16r12 + 1) MSB:false. |
|
1181 |
height := header wordAt:(16r14 + 1) MSB:false. |
|
1182 |
inPlanes := header wordAt:(16r16 + 1) MSB:false. |
|
1183 |
inDepth := header wordAt:(16r18 + 1) MSB:false. |
|
1184 |
"/ dataStart := header wordAt:(16r0A + 1) MSB:false. |
|
1185 |
compression := 0. |
|
1186 |
]. |
|
1187 |
iSize >= 64 ifTrue:[ |
|
1188 |
"/ |
|
1189 |
"/ its an OS/2 (vsn2) BMP file |
|
1190 |
"/ |
|
1191 |
width := header doubleWordAt:(16r12 + 1) MSB:false. |
|
1192 |
height := header doubleWordAt:(16r16 + 1) MSB:false. |
|
1193 |
inPlanes := header wordAt:(16r1A + 1) MSB:false. |
|
1194 |
inDepth := header wordAt:(16r1c + 1) MSB:false. |
|
1195 |
compression := header doubleWordAt:(16r1e + 1) MSB:false. |
|
1196 |
numColor := header doubleWordAt:(16r2E + 1) MSB:false. |
|
1197 |
numImportantColor := header doubleWordAt:(16r32 + 1) MSB:false. |
|
1198 |
dataStart := header wordAt:(16r0A + 1) MSB:false. |
|
1199 |
]. |
|
1200 |
numColor := 1 bitShift:inDepth. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1201 |
] ifFalse:[ |
819 | 1202 |
^ self fileFormatError:'unknown format'. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1203 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1204 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1205 |
|
1846 | 1206 |
self reportDimension. |
1207 |
||
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1208 |
numColor ~~ 0 ifTrue:[ |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1209 |
"read the colormap - notice: its in BGR order (sigh)" |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1210 |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1211 |
colorMap := self |
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1212 |
readColorMap:numColor |
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1213 |
numBytesPerColor:numBytesPerColorInColormap |
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1214 |
from:aStream. |
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1215 |
|
898
cdf6cc132b53
care for funny number of colors (nColor > (1<<depth))
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
1216 |
numColor > (1 bitShift:inDepth) ifTrue:[ |
cdf6cc132b53
care for funny number of colors (nColor > (1<<depth))
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
1217 |
'funny number of colors in image' infoPrintCR. |
cdf6cc132b53
care for funny number of colors (nColor > (1<<depth))
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
1218 |
numColor := 1 bitShift:inDepth. |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1219 |
colorMap := colorMap copyTo:numColor. |
898
cdf6cc132b53
care for funny number of colors (nColor > (1<<depth))
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
1220 |
]. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1221 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1222 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1223 |
"/ check for valid compression |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1224 |
compression ~~ 0 ifTrue:[ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1225 |
"/ some compression |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1226 |
compression == 1 ifTrue:[ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1227 |
"/ RLE8 - must be depth-8 |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1228 |
inDepth ~~ 8 ifTrue:[ |
819 | 1229 |
^ self fileFormatError:'RLE8 compression only supported with depth8 images'. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1230 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1231 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1232 |
compression == 2 ifTrue:[ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1233 |
"/ RLE4 - must be depth-4 |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1234 |
inDepth ~~ 4 ifTrue:[ |
819 | 1235 |
^ self fileFormatError:'RLE4 compression only supported with depth4 images'. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1236 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1237 |
]. |
2571 | 1238 |
compression == 3 ifTrue:[ |
1239 |
"/ BITFIELDS - must be depth-16 or 32 |
|
1240 |
((inDepth ~~ 16) and:[inDepth ~~ 32]) ifTrue:[ |
|
1241 |
^ self fileFormatError:'BITFIELDS compression only supported with depth16/32 images'. |
|
1242 |
]. |
|
1243 |
]. |
|
1244 |
compression >= 4 ifTrue:[ |
|
1245 |
^ self fileFormatError:'unsupported compression'. |
|
1246 |
]. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1247 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1248 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1249 |
inPlanes ~~ 1 ifTrue:[ |
819 | 1250 |
^ self fileFormatError:'only 1 plane images supported'. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1251 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1252 |
|
565 | 1253 |
dataStart notNil ifTrue:[ |
2571 | 1254 |
aStream position:dataStart. |
1255 |
]. |
|
1256 |
||
1257 |
inDepth <= 8 ifTrue:[ |
|
1258 |
samplesPerPixel := 1. |
|
1259 |
bitsPerSample := Array with:inDepth. |
|
1260 |
photometric := #palette. |
|
1261 |
] ifFalse:[ |
|
1262 |
inDepth == 16 ifTrue:[ |
|
1263 |
photometric := #palette. |
|
1264 |
samplesPerPixel := 3. |
|
1265 |
bitsPerSample := #(5 5 5). |
|
1266 |
colorMap := FixedPalette |
|
1267 |
redShift:10 redMask:16r1f |
|
1268 |
greenShift:5 greenMask:16r1f |
|
1269 |
blueShift:0 blueMask:16r1F. |
|
1270 |
||
1271 |
] ifFalse:[ |
|
1272 |
inDepth == 24 ifTrue:[ |
|
1273 |
photometric := #rgb. |
|
1274 |
samplesPerPixel := 3. |
|
1275 |
bitsPerSample := #(8 8 8). |
|
1276 |
] ifFalse:[ |
|
1277 |
inDepth == 32 ifTrue:[ |
|
1278 |
photometric := #rgb. |
|
1279 |
samplesPerPixel := 4. |
|
1280 |
bitsPerSample := #(8 8 8 8). |
|
1281 |
] ifFalse:[ |
|
1282 |
^ self fileFormatError:'unsupported depth'. |
|
1283 |
] |
|
1284 |
] |
|
1285 |
] |
|
565 | 1286 |
]. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1287 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1288 |
inDepth == 24 ifTrue:[ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1289 |
bytesPerRow := width * 3 |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1290 |
] ifFalse:[ |
2571 | 1291 |
inDepth == 16 ifTrue:[ |
1292 |
bytesPerRow := width * 2 |
|
1293 |
] ifFalse:[ |
|
1294 |
inDepth == 32 ifTrue:[ |
|
1295 |
bytesPerRow := width * 4 |
|
1296 |
] ifFalse:[ |
|
1297 |
bytesPerRow := self bytesPerRow |
|
1298 |
]. |
|
1299 |
]. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1300 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1301 |
data := ByteArray uninitializedNew:(height * bytesPerRow). |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1302 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1303 |
"/ read & possibly decompress |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1304 |
|
2571 | 1305 |
(self loadBMPWidth:width height:height depth:inDepth from:aStream into:data) ifFalse:[ |
1306 |
self fileFormatError:('read/decompression error'). |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1307 |
^ nil |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1308 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1309 |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1310 |
^ self image |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1311 |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1312 |
"Modified: / 17-09-1995 / 18:48:46 / claus" |
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1313 |
"Modified: / 30-05-2007 / 16:57:39 / cg" |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1314 |
! |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1315 |
|
211 | 1316 |
fromWindowsICOFile:aFilename |
1317 |
"read an image from a windows ICO file" |
|
1318 |
||
41 | 1319 |
|reader stream| |
1320 |
||
1321 |
stream := self class streamReadingFile:aFilename. |
|
1322 |
stream isNil ifTrue:[^ nil]. |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1323 |
reader := self class new. |
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1324 |
reader fromWindowsICOStream:stream. |
41 | 1325 |
stream close. |
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1326 |
^ reader image. |
21 | 1327 |
|
1328 |
" |
|
1329 |
Image fromFile:'/phys/clam2//LocalLibrary/Images/WIN_icons/ibm.ico'. |
|
1330 |
" |
|
211 | 1331 |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1332 |
"Modified: / 30-05-2007 / 16:57:52 / cg" |
21 | 1333 |
! |
1334 |
||
41 | 1335 |
fromWindowsICOStream:aStream |
211 | 1336 |
"read an image from a windows ICO stream" |
1337 |
||
2571 | 1338 |
^ self fromWindowsICOStream:aStream alreadyRead:nil |
1339 |
! |
|
1340 |
||
1341 |
fromWindowsICOStream:aStream alreadyRead:bytesAlreadyRead |
|
1342 |
"read an image from a windows ICO stream" |
|
1343 |
||
1344 |
|header |
|
2034 | 1345 |
srcIndex dstIndex |
1346 |
rawData tmp bytesPerRow nColor cmapSize| |
|
0 | 1347 |
|
41 | 1348 |
inStream := aStream. |
1349 |
aStream binary. |
|
0 | 1350 |
|
1351 |
"read the header" |
|
1352 |
||
2034 | 1353 |
header := ByteArray uninitializedNew:(6 + 16 + 40). |
2571 | 1354 |
bytesAlreadyRead size > 0 ifTrue:[ |
1355 |
header replaceFrom:1 with:bytesAlreadyRead |
|
1356 |
]. |
|
1357 |
aStream nextBytes:((6 + 16 + 40)-bytesAlreadyRead size) into:header startingAt:(1+bytesAlreadyRead size). |
|
1358 |
||
21 | 1359 |
width := header at:(6+1). |
1360 |
height := header at:(7+1). |
|
1361 |
nColor := header at:(8+1). |
|
430 | 1362 |
"/ reserved := header at:(9+1). |
2034 | 1363 |
"/ nPlanes := header wordAt:(10+1). |
1364 |
"/ nBitsPerPel := header wordAt:(12+1). |
|
1365 |
"/ nBytesInResource := header doubleWordAt:(14+1). |
|
1366 |
"/ ordinal := header wordAt:(18+1). |
|
1367 |
"21, 22 ?" |
|
1368 |
"23, ... , 62 ?" |
|
21 | 1369 |
|
2034 | 1370 |
inDepth := header at:16r25. |
1371 |
"/ mhmh - some depth4 icons seem to have a 0 in the depth field ... |
|
1372 |
inDepth == 0 ifTrue:[ |
|
1373 |
inDepth := 4 |
|
1386 | 1374 |
]. |
1569 | 1375 |
(#(4 8) includes:inDepth) ifFalse:[ |
1376 |
"/ only tested for depth 4/8 images. |
|
1377 |
^ self fileFormatError:'only depth 4/8 ico-images supported (depth is ' , inDepth printString , ')'. |
|
1378 |
"/ self halt:'only depth 4 ico-images supported (depth is ' , inDepth printString , ')'. |
|
1171
d848c46349ab
sorry: only depth4 images are handled.
Claus Gittinger <cg@exept.de>
parents:
1168
diff
changeset
|
1379 |
]. |
1846 | 1380 |
self reportDimension. |
0 | 1381 |
|
2034 | 1382 |
"read the colormap" |
1383 |
cmapSize := (1 bitShift:inDepth). |
|
1384 |
||
1385 |
colorMap := self |
|
1386 |
readColorMap:cmapSize |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1387 |
numBytesPerColor:4 |
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1388 |
from:aStream. |
0 | 1389 |
|
2034 | 1390 |
"read the data bits" |
0 | 1391 |
|
2571 | 1392 |
bytesPerRow := ((width * inDepth) + 7) // 8. |
1569 | 1393 |
rawData := ByteArray uninitializedNew:(height * bytesPerRow). |
1394 |
aStream nextBytes:(height * bytesPerRow) into:rawData. |
|
0 | 1395 |
|
1396 |
"read mask" |
|
2034 | 1397 |
|
0 | 1398 |
" |
1399 |
mask := ByteArray new:(width * height / 8). |
|
41 | 1400 |
aStream nextBytes:(width * height / 8) into:mask. |
0 | 1401 |
" |
1402 |
||
1403 |
"stupid: last row first" |
|
1404 |
||
3 | 1405 |
tmp := ByteArray uninitializedNew:(height * bytesPerRow). |
0 | 1406 |
srcIndex := 1. |
1407 |
dstIndex := (height - 1) * bytesPerRow + 1. |
|
1408 |
1 to:height do:[:row | |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1409 |
tmp replaceFrom:dstIndex to:(dstIndex + bytesPerRow - 1) |
1569 | 1410 |
with:rawData startingAt:srcIndex. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1411 |
srcIndex := srcIndex + bytesPerRow. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1412 |
dstIndex := dstIndex - bytesPerRow. |
0 | 1413 |
]. |
1569 | 1414 |
rawData := tmp. |
0 | 1415 |
|
1416 |
photometric := #palette. |
|
1417 |
samplesPerPixel := 1. |
|
298
cd59c474524a
return .ico files as Depth4Images (not Depth8)
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
1418 |
|
cd59c474524a
return .ico files as Depth4Images (not Depth8)
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
1419 |
false ifTrue:[ |
1569 | 1420 |
"expand into bytes" |
298
cd59c474524a
return .ico files as Depth4Images (not Depth8)
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
1421 |
|
cd59c474524a
return .ico files as Depth4Images (not Depth8)
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
1422 |
data := ByteArray new:(width * height). |
1569 | 1423 |
rawData expandPixels:inDepth width:width height:height |
298
cd59c474524a
return .ico files as Depth4Images (not Depth8)
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
1424 |
into:data mapping:nil. |
cd59c474524a
return .ico files as Depth4Images (not Depth8)
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
1425 |
bitsPerSample := #(8). |
cd59c474524a
return .ico files as Depth4Images (not Depth8)
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
1426 |
] ifFalse:[ |
1569 | 1427 |
data := rawData. |
1428 |
bitsPerSample := (Array with:inDepth). |
|
298
cd59c474524a
return .ico files as Depth4Images (not Depth8)
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
1429 |
]. |
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1430 |
^ self image |
298
cd59c474524a
return .ico files as Depth4Images (not Depth8)
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
1431 |
|
0 | 1432 |
" |
21 | 1433 |
WindowsIconReader new fromWindowsICOFile:'/phys/clam2//LocalLibrary/Images/WIN_icons/ibm.ico'. |
0 | 1434 |
" |
211 | 1435 |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1436 |
"Modified: / 30-05-2007 / 16:58:11 / cg" |
1805 | 1437 |
! ! |
1438 |
||
1439 |
!WindowsIconReader methodsFor:'writing'! |
|
713 | 1440 |
|
1441 |
save:image onFile:aFileName |
|
1442 |
"save image as BMP file on aFileName. |
|
1443 |
Only depth 1,4,8 and 24 images can be represented in this format." |
|
1444 |
||
1168 | 1445 |
aFileName asFilename suffix asLowercase = 'ico' ifTrue:[ |
1446 |
"/ (image depth == 4 |
|
1447 |
"/ and:[image width == 32 |
|
1448 |
"/ and:[image height == 32]]) ifTrue:[ |
|
1449 |
^ self saveICO:image onFile:aFileName. |
|
1450 |
"/ ] |
|
1451 |
]. |
|
713 | 1452 |
self saveBMP:image onFile:aFileName. |
1453 |
||
1454 |
"Modified: 17.10.1997 / 20:16:53 / cg" |
|
1455 |
! |
|
1456 |
||
1457 |
saveBMP:image onFile:fileName |
|
1458 |
"save image as BMP file on aFileName. |
|
1459 |
Only depth 1,4,8 and 24 images can be represented in this format." |
|
1460 |
||
1507
9bae072abf64
fixed bmp saving 24bit image (red & blue channels exchanged)
Claus Gittinger <cg@exept.de>
parents:
1386
diff
changeset
|
1461 |
|depth bhSize biSize biClrUsed biSizeImage bfOffBits rowBytes imgBytesPerRow data srcIndex row t| |
713 | 1462 |
|
1463 |
depth := image depth. |
|
1464 |
width := image width. |
|
1465 |
height := image height. |
|
1466 |
||
1467 |
(#(1 4 8 24) includes:depth) ifFalse:[ |
|
1468 |
^ Image cannotRepresentImageSignal |
|
1469 |
raiseWith:image |
|
1470 |
errorString:('BMP format only supports depths 1,4,8 and 24'). |
|
1471 |
]. |
|
1472 |
image mask notNil ifTrue:[ |
|
1473 |
Image informationLostQuerySignal |
|
1474 |
raiseWith:image |
|
1475 |
errorString:('BMP format does not support an imageMask'). |
|
1476 |
]. |
|
1477 |
||
1478 |
bhSize := 14. "# bytes in file header" |
|
1479 |
biSize := 40. "info header size in bytes" |
|
1734 | 1480 |
biClrUsed := (depth >= 24) ifTrue:[0] ifFalse:[1 bitShift: depth]. "No. color table entries" |
713 | 1481 |
bfOffBits := biSize + bhSize + (4*biClrUsed). |
1482 |
"/ bmp aligns rows on a longword boundary |
|
1483 |
rowBytes := ((depth min:24) * width + 31 // 32) * 4. |
|
1484 |
biSizeImage := height * rowBytes. |
|
1485 |
||
1486 |
outStream := fileName asFilename writeStream. |
|
1487 |
outStream binary. |
|
1488 |
byteOrder := #lsb. |
|
1489 |
||
1490 |
"Write the file header" |
|
1491 |
self writeShort:19778. "bfType = BM" |
|
1492 |
self writeLong:(bfOffBits + biSizeImage). "Entire file size in bytes" |
|
1493 |
self writeLong:0. "bfReserved" |
|
1494 |
self writeLong:bfOffBits. "Offset of bitmap data from start of hdr (and file)" |
|
1495 |
||
1496 |
"Write the bitmap info header" |
|
1754 | 1497 |
outStream position1Based: bhSize+1. |
713 | 1498 |
self writeLong:biSize. "info header size in bytes" |
1499 |
self writeLong:width. "biWidth" |
|
1500 |
self writeLong:height. "biHeight" |
|
1501 |
self writeShort:1. "biPlanes" |
|
1502 |
self writeShort:(depth min:24). "biBitCount" |
|
1503 |
self writeLong:0. "biCompression" |
|
1504 |
self writeLong:biSizeImage. "size of image section in bytes" |
|
1505 |
self writeLong:2800. "biXPelsPerMeter" |
|
1506 |
self writeLong:2800. "biYPelsPerMeter" |
|
1507 |
self writeLong:biClrUsed. |
|
1508 |
self writeLong:0. "biClrImportant" |
|
1509 |
1 to:biClrUsed do:[:i | "Color map" |
|
1510 |
|clr r g b| |
|
1511 |
||
1512 |
clr := image colorFromValue:i-1. |
|
1513 |
clr isNil ifTrue:[ |
|
1514 |
r := g := b := 0. |
|
1515 |
] ifFalse:[ |
|
1516 |
r := clr redByte. |
|
1517 |
g := clr greenByte. |
|
1518 |
b := clr blueByte. |
|
1519 |
]. |
|
1520 |
||
1521 |
"/ put B,G,R |
|
1522 |
outStream nextPut:b. |
|
1523 |
outStream nextPut:g. |
|
1524 |
outStream nextPut:r. |
|
1525 |
outStream nextPut:0. |
|
1526 |
]. |
|
1527 |
||
1528 |
imgBytesPerRow := image bytesPerRow. |
|
1529 |
data := image data. |
|
1530 |
||
1531 |
||
1532 |
"/ sorry, must extract rows individually |
|
1533 |
"/ (even if alignment is correct), |
|
1534 |
"/ since BMP saves rows bottom-to-top |
|
1535 |
||
1536 |
row := ByteArray new:rowBytes. |
|
1537 |
||
1538 |
srcIndex := 1 + (height * imgBytesPerRow). |
|
1539 |
1 to:height do:[:i | |
|
1540 |
srcIndex := srcIndex - imgBytesPerRow. |
|
1541 |
row replaceFrom:1 to:imgBytesPerRow with:data startingAt:srcIndex. |
|
1507
9bae072abf64
fixed bmp saving 24bit image (red & blue channels exchanged)
Claus Gittinger <cg@exept.de>
parents:
1386
diff
changeset
|
1542 |
depth >= 24 ifTrue:[ |
9bae072abf64
fixed bmp saving 24bit image (red & blue channels exchanged)
Claus Gittinger <cg@exept.de>
parents:
1386
diff
changeset
|
1543 |
"/ stupid must swap red & blue bytes |
9bae072abf64
fixed bmp saving 24bit image (red & blue channels exchanged)
Claus Gittinger <cg@exept.de>
parents:
1386
diff
changeset
|
1544 |
|
9bae072abf64
fixed bmp saving 24bit image (red & blue channels exchanged)
Claus Gittinger <cg@exept.de>
parents:
1386
diff
changeset
|
1545 |
1 to:row size by:3 do:[:i | |
9bae072abf64
fixed bmp saving 24bit image (red & blue channels exchanged)
Claus Gittinger <cg@exept.de>
parents:
1386
diff
changeset
|
1546 |
t := row at:i. |
9bae072abf64
fixed bmp saving 24bit image (red & blue channels exchanged)
Claus Gittinger <cg@exept.de>
parents:
1386
diff
changeset
|
1547 |
row at:i put:(row at:i+2). |
9bae072abf64
fixed bmp saving 24bit image (red & blue channels exchanged)
Claus Gittinger <cg@exept.de>
parents:
1386
diff
changeset
|
1548 |
row at:i+2 put:t |
9bae072abf64
fixed bmp saving 24bit image (red & blue channels exchanged)
Claus Gittinger <cg@exept.de>
parents:
1386
diff
changeset
|
1549 |
]. |
9bae072abf64
fixed bmp saving 24bit image (red & blue channels exchanged)
Claus Gittinger <cg@exept.de>
parents:
1386
diff
changeset
|
1550 |
]. |
713 | 1551 |
outStream nextPutAll:row. |
1552 |
]. |
|
1553 |
||
1554 |
outStream close. |
|
1555 |
||
1556 |
" |
|
1557 |
|i| |
|
1558 |
||
1559 |
i := Image fromFile:'bitmaps/SBrowser.xbm'. |
|
1560 |
WindowsIconReader save:i onFile:'test.bmp'. |
|
1561 |
" |
|
1562 |
" |
|
1563 |
|i| |
|
1564 |
||
1565 |
i := Image fromFile:'bitmaps/gifImages/garfield.gif'. |
|
1566 |
WindowsIconReader save:i onFile:'test.bmp'. |
|
1567 |
" |
|
1568 |
||
1569 |
"Modified: 21.10.1997 / 05:02:02 / cg" |
|
1167
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1570 |
! |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1571 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1572 |
saveICO:image onFile:fileName |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1573 |
"save image as ICO file on aFileName. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1574 |
Only depth 4 images of size 32x32 can be represented in this format." |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1575 |
|
1233 | 1576 |
|depth biSizeImage rowBytes imgBytesPerRow data srcIndex row| |
1167
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1577 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1578 |
depth := image depth. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1579 |
width := image width. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1580 |
height := image height. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1581 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1582 |
depth ~~ 4 ifTrue:[ |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1583 |
^ Image cannotRepresentImageSignal |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1584 |
raiseWith:image |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1585 |
errorString:('ICO format only supports depths 4'). |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1586 |
]. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1587 |
(width ~~ 32 or:[height ~~ 32]) ifTrue:[ |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1588 |
^ Image cannotRepresentImageSignal |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1589 |
raiseWith:image |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1590 |
errorString:('ICO format (currently) only supports 32x32 bitmaps'). |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1591 |
]. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1592 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1593 |
"/ align rows on a longword boundary |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1594 |
rowBytes := (depth * width + 31 // 32) * 4. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1595 |
biSizeImage := height * rowBytes. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1596 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1597 |
outStream := fileName asFilename writeStream. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1598 |
outStream binary. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1599 |
byteOrder := #lsb. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1600 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1601 |
"Write the file header" |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1602 |
outStream nextPutAll:#[0 0 1 0]. "/ ICO magic |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1603 |
self writeShort:1. "/ # of images in file |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1604 |
outStream nextPut:image width. "/ |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1605 |
outStream nextPut:image height. "/ |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1606 |
outStream nextPut:(1 bitShift:image depth). "/ # of colors |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1607 |
outStream nextPutAll:#[0 0 0 0 0 ]. "/ reserved |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1608 |
self writeLong:16rE802. "/ size pixels |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1609 |
self writeLong:16r26. "/ offset in file |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1610 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1611 |
"/ 40 bytes - unknown format |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1612 |
outStream nextPutAll:(ByteArray new:40). |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1613 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1614 |
"/ 16-entry RGB map |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1615 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1616 |
1 to:16 do:[:i | "Color map" |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1617 |
|clr r g b| |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1618 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1619 |
clr := image colorFromValue:i-1. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1620 |
clr isNil ifTrue:[ |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1621 |
r := g := b := 0. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1622 |
] ifFalse:[ |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1623 |
r := clr redByte. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1624 |
g := clr greenByte. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1625 |
b := clr blueByte. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1626 |
]. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1627 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1628 |
"/ put B,G,R |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1629 |
outStream nextPut:b. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1630 |
outStream nextPut:g. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1631 |
outStream nextPut:r. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1632 |
outStream nextPut:0. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1633 |
]. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1634 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1635 |
imgBytesPerRow := image bytesPerRow. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1636 |
data := image data. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1637 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1638 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1639 |
"/ sorry, must extract rows individually |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1640 |
"/ (even if alignment is correct), |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1641 |
"/ since ICO saves rows bottom-to-top |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1642 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1643 |
row := ByteArray new:rowBytes. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1644 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1645 |
srcIndex := 1 + (height * imgBytesPerRow). |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1646 |
1 to:height do:[:i | |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1647 |
srcIndex := srcIndex - imgBytesPerRow. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1648 |
row replaceFrom:1 to:imgBytesPerRow with:data startingAt:srcIndex. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1649 |
outStream nextPutAll:row. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1650 |
]. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1651 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1652 |
"/ the mask ... |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1653 |
image mask isNil ifTrue:[ |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1654 |
outStream next:128 put:16rFF |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1655 |
] ifFalse:[ |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1656 |
imgBytesPerRow := image mask bytesPerRow. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1657 |
data := image mask data. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1658 |
row := ByteArray new:4. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1659 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1660 |
srcIndex := 1 + (height * imgBytesPerRow). |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1661 |
1 to:height do:[:i | |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1662 |
srcIndex := srcIndex - imgBytesPerRow. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1663 |
row replaceFrom:1 to:imgBytesPerRow with:data startingAt:srcIndex. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1664 |
outStream nextPutAll:row. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1665 |
]. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1666 |
]. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1667 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1668 |
outStream close. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1669 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1670 |
" |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1671 |
|i| |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1672 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1673 |
i := Image fromFile:'bitmaps/xpmBitmaps/SmalltalkX_clr.xpm'. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1674 |
i := Depth4Image fromImage:i. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1675 |
i := i magnifiedTo:32@32. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1676 |
WindowsIconReader new saveICO:i onFile:'test.ico'. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1677 |
" |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1678 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1679 |
"Modified: 21.10.1997 / 05:02:02 / cg" |
713 | 1680 |
! ! |
1681 |
||
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1682 |
!WindowsIconReader class methodsFor:'documentation'! |
0 | 1683 |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1684 |
version |
2571 | 1685 |
^ '$Header: /cvs/stx/stx/libview2/WindowsIconReader.st,v 1.62 2008-12-02 21:02:23 cg Exp $' |
0 | 1686 |
! ! |
1734 | 1687 |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1688 |
WindowsIconReader initialize! |