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