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