author | Claus Gittinger <cg@exept.de> |
Mon, 07 Mar 2016 01:56:08 +0100 | |
changeset 3590 | 104bc11d3845 |
parent 3589 | ef1e5b12715f |
child 3591 | 87336f7e44cf |
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 |
3589 | 17 |
instanceVariableNames:'compression inDepth topDown redMask greenMask blueMask alphaMask' |
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 |
||
3589 | 64 |
Name Size Offset Description |
1385 | 65 |
|
66 |
Header 14 bytes Windows Structure: BITMAPFILEHEADER |
|
3589 | 67 |
Signature 2 bytes 0 'BM' |
68 |
FileSize 4 bytes 2 File size in bytes |
|
69 |
reserved 4 bytes 6 unused (=0) |
|
70 |
DataOffset 4 bytes 10 File offset to Raster Data |
|
1385 | 71 |
|
72 |
InfoHeader 40 bytes Windows Structure: BITMAPINFOHEADER |
|
73 |
||
3589 | 74 |
Size 4 bytes 14 Size of InfoHeader =40 |
75 |
Width 4 bytes 18 Bitmap Width |
|
76 |
Height 4 bytes 22 Bitmap Height |
|
77 |
Planes 2 bytes 26 Number of Planes (=1) |
|
78 |
BitCount 2 bytes 28 Bits per Pixel |
|
3583 | 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 |
|
3589 | 84 |
Compression 4 bytes 30 Type of Compression |
3583 | 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 |
||
3589 | 96 |
ImageSize 4 bytes 34 (compressed) Size of Image |
3583 | 97 |
It is valid to set this =0 if Compression = 0 |
3589 | 98 |
XpixelsPerM 4 bytes 38 horizontal resolution: Pixels/meter |
99 |
YpixelsPerM 4 bytes 42 vertical resolution: Pixels/meter |
|
100 |
ColorsUsed 4 bytes 46 Number of actually used colors |
|
3583 | 101 |
ColorsImportant |
3589 | 102 |
4 bytes 50 Number of important colors |
3583 | 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. |
3589 | 240 |
MIMETypes defineImageType:'image/x-ms-bitmap' suffix:'bmp' reader:self. |
2578 | 241 |
MIMETypes defineImageType:'image/bmp' suffix:'bmp' reader:self. |
647 | 242 |
MIMETypes defineImageType:nil suffix:'ico' reader:self. |
211 | 243 |
|
398 | 244 |
"Modified: 1.2.1997 / 15:03:59 / cg" |
28 | 245 |
! ! |
246 |
||
102 | 247 |
!WindowsIconReader class methodsFor:'testing'! |
248 |
||
713 | 249 |
canRepresent:anImage |
250 |
"return true, if anImage can be represented in my file format. |
|
251 |
BMP supports depth 1,4,8 and 24." |
|
252 |
||
3571 | 253 |
(#(1 4 8 24) includes:anImage depth) ifTrue:[^true]. |
254 |
('WindwsIconReader [info]: image depth is not 1,4,8 or 24.') infoPrintCR. |
|
255 |
^ false |
|
713 | 256 |
|
257 |
"Created: 17.10.1997 / 20:18:23 / cg" |
|
258 |
! |
|
259 |
||
102 | 260 |
isValidImageFile:aFileName |
261 |
"return true, if aFileName contains a valid windows bitmap-file image" |
|
262 |
||
263 |
|inStream header ok| |
|
264 |
||
265 |
inStream := self streamReadingFile:aFileName. |
|
266 |
inStream isNil ifTrue:[^ false]. |
|
267 |
||
268 |
inStream binary. |
|
104 | 269 |
ok := false. |
692
e6af4c70e066
size is going to be eliminated from the stream
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
270 |
inStream fileSize > 16 ifTrue:[ |
2578 | 271 |
header := ByteArray uninitializedNew:4. |
272 |
inStream nextBytes:4 into:header. |
|
102 | 273 |
|
2578 | 274 |
(header startsWith:#(66 77)) ifTrue:[ "BM" |
275 |
ok := true. |
|
104 | 276 |
"/ 'WINREADER: Win3.x or OS/2 vsn 2 BM format' infoPrintNL. |
2578 | 277 |
]. |
278 |
(header startsWith:#(66 65)) ifTrue:[ "BA" |
|
279 |
ok := true. |
|
104 | 280 |
"/ 'WINREADER: OS/2 vsn 2 BA format' infoPrintNL. |
2578 | 281 |
]. |
282 |
(header startsWith:#(73 67)) ifTrue:[ "IC" |
|
283 |
ok := true. |
|
104 | 284 |
"/ 'WINREADER: OS/2 IC format' infoPrintNL. |
2578 | 285 |
]. |
286 |
(header startsWith:#(80 84)) ifTrue:[ "PT" |
|
287 |
ok := true. |
|
104 | 288 |
"/ 'WINREADER: OS/2 PT format' infoPrintNL. |
2578 | 289 |
]. |
290 |
(header startsWith:#(0 0 1 0)) ifTrue:[ |
|
291 |
ok := true. |
|
104 | 292 |
"/ 'WINREADER: Win3.x ICO format' infoPrintNL. |
2578 | 293 |
]. |
102 | 294 |
]. |
104 | 295 |
inStream close. |
296 |
^ ok |
|
102 | 297 |
|
298 |
" |
|
299 |
WindowsIconReader isValidImageFile:'/phys/clam2/LocalLibrary/Images/OS2_icons/dos.ico' |
|
300 |
" |
|
301 |
||
302 |
"Created: 17.9.1995 / 17:14:20 / claus" |
|
303 |
! ! |
|
304 |
||
103 | 305 |
!WindowsIconReader methodsFor:'private'! |
306 |
||
3589 | 307 |
bitsPerPixel |
308 |
^ inDepth |
|
3584 | 309 |
! |
310 |
||
3589 | 311 |
convertPixels |
312 |
"/ figure out, how pixels should be processed, |
|
313 |
"/ according to the order of r/g/b components and masks |
|
314 |
"/ bring the pixels to a standard format: |
|
315 |
"/ rgb888/rgba8888 if possible and useful |
|
316 |
"/ |
|
317 |
|swapAction redShift greenShift blueShift alphaShift pixelAction |
|
318 |
numRedBits numGreenBits numBlueBits numAlphaBits needPixelProcessing| |
|
319 |
||
320 |
inDepth == 1 ifTrue:[ |
|
321 |
colorMap isNil ifTrue:[ |
|
322 |
photometric := #blackIs0 |
|
323 |
] ifFalse:[ |
|
324 |
photometric := #palette. |
|
325 |
colorMap size == 2 ifTrue:[ |
|
326 |
((colorMap at:1) = (Color white) |
|
327 |
and:[(colorMap at:2) = (Color black)]) ifTrue:[ |
|
328 |
photometric := #whiteIs0. |
|
329 |
colorMap := nil. |
|
330 |
] ifFalse:[ |
|
331 |
((colorMap at:1) = (Color black) |
|
332 |
and:[(colorMap at:2) = (Color white)]) ifTrue:[ |
|
333 |
photometric := #blackIs0. |
|
334 |
colorMap := nil. |
|
335 |
] |
|
336 |
]. |
|
337 |
]. |
|
338 |
]. |
|
339 |
samplesPerPixel := 1. |
|
340 |
bitsPerSample := Array with:inDepth. |
|
341 |
^ self. |
|
342 |
]. |
|
343 |
||
344 |
((inDepth > 8) and:[redMask notNil]) ifTrue:[ |
|
345 |
numRedBits := redMask bitCount. |
|
346 |
numGreenBits := greenMask bitCount. |
|
347 |
numBlueBits := blueMask bitCount. |
|
348 |
numAlphaBits := alphaMask bitCount. |
|
349 |
redShift := redMask lowBit - 1. |
|
350 |
greenShift := greenMask lowBit - 1. |
|
351 |
blueShift := blueMask lowBit - 1. |
|
352 |
alphaShift := alphaMask lowBit - 1. |
|
2578 | 353 |
|
3589 | 354 |
alphaMask == 0 ifTrue:[ |
355 |
photometric := #rgb. |
|
356 |
samplesPerPixel := 3 |
|
357 |
] ifFalse:[ |
|
358 |
photometric := #rgba. |
|
359 |
samplesPerPixel := 4 |
|
360 |
]. |
|
361 |
needPixelProcessing := false. |
|
362 |
||
363 |
inDepth == 16 ifTrue:[ |
|
3590 | 364 |
alphaMask == 0 ifTrue:[ |
365 |
bitsPerSample := {numRedBits. numGreenBits. numBlueBits}. |
|
366 |
colorMap := FixedPalette |
|
367 |
redShift:redShift redMask:(redMask >> redShift) |
|
368 |
greenShift:greenShift greenMask:(greenMask >> greenShift) |
|
369 |
blueShift:blueShift blueMask:(blueMask >> blueShift). |
|
370 |
photometric := #palette. |
|
3589 | 371 |
] ifFalse:[ |
3590 | 372 |
bitsPerSample := {numRedBits. numGreenBits. numBlueBits. numAlphaBits }. |
373 |
colorMap := FixedPaletteWithAlpha |
|
374 |
redShift:redShift redMask:(redMask >> redShift) |
|
375 |
greenShift:greenShift greenMask:(greenMask >> greenShift) |
|
376 |
blueShift:blueShift blueMask:(blueMask >> blueShift). |
|
377 |
colorMap alphaShift:alphaShift alphaMask:(alphaMask >> alphaShift). |
|
378 |
photometric := #palette. |
|
379 |
]. |
|
380 |
swapAction := [:bytes | data swapBytes]. |
|
3589 | 381 |
]. |
382 |
inDepth == 24 ifTrue:[ |
|
383 |
(redShift ~~ 0 |
|
384 |
or:[ greenShift ~~ 8 |
|
385 |
or:[ blueShift ~~ 16 |
|
386 |
or:[ numRedBits ~~ 8 |
|
387 |
or:[ numGreenBits ~~ 8 |
|
388 |
or:[ numBlueBits ~~ 8 |
|
389 |
or:[ alphaMask ~~ 0]]]]]] |
|
390 |
) ifTrue:[ |
|
391 |
(redShift == 16 |
|
392 |
and:[greenShift == 8 |
|
393 |
and:[blueShift == 0 |
|
394 |
and:[numRedBits == 8 |
|
395 |
and:[numGreenBits == 8 |
|
396 |
and:[numBlueBits == 8 |
|
397 |
and:[alphaMask == 0]]]]]] |
|
398 |
) ifTrue:[ |
|
399 |
"/ common case: swap r and b |
|
400 |
swapAction := [:data | self swapBytesFromRGB_to_BGR]. |
|
401 |
] ifFalse:[ |
|
402 |
needPixelProcessing := true |
|
403 |
]. |
|
404 |
]. |
|
405 |
bitsPerSample := #(8 8 8). |
|
406 |
photometric := #rgb. |
|
407 |
]. |
|
408 |
inDepth == 32 ifTrue:[ |
|
409 |
(redShift ~~ 0 |
|
410 |
or:[ greenShift ~~ 8 |
|
411 |
or:[ redShift ~~ 16 |
|
412 |
or:[ numRedBits ~~ 8 |
|
413 |
or:[ numGreenBits ~~ 8 |
|
414 |
or:[ numBlueBits ~~ 8 |
|
415 |
or:[ alphaMask ~~ 0 and:[ alphaShift ~~ 24]]]]]]] |
|
416 |
) ifTrue:[ |
|
417 |
(redShift == 16 |
|
418 |
and:[greenShift == 8 |
|
419 |
and:[blueShift == 0 |
|
420 |
and:[alphaShift == 24 |
|
421 |
and:[numRedBits == 8 |
|
422 |
and:[numGreenBits == 8 |
|
423 |
and:[numBlueBits == 8 |
|
424 |
and:[numAlphaBits == 8]]]]]]] |
|
425 |
) ifTrue:[ |
|
426 |
"/ common case: swap r and b |
|
427 |
swapAction := [:data | self swapBytesFromRGBA_to_BGRA]. |
|
428 |
] ifFalse:[ |
|
429 |
needPixelProcessing := true |
|
430 |
]. |
|
431 |
]. |
|
432 |
bitsPerSample := #(8 8 8 8). |
|
433 |
photometric := #rgba. |
|
434 |
]. |
|
435 |
||
436 |
needPixelProcessing ifTrue:[ |
|
437 |
alphaMask == 0 ifTrue:[ |
|
3590 | 438 |
pixelAction := [:v | |
439 |
|r g b| |
|
440 |
r := (v bitAnd:redMask) >> redShift. |
|
441 |
g := (v bitAnd:greenMask) >> greenShift. |
|
442 |
b := (v bitAnd:blueMask) >> blueShift. |
|
443 |
(((b bitShift:8) bitOr:g) bitShift:8) bitOr:r |
|
444 |
]. |
|
3589 | 445 |
] ifFalse:[ |
3590 | 446 |
pixelAction := [:v | |
447 |
|r g b a| |
|
448 |
r := (v bitAnd:redMask) >> redShift. |
|
449 |
g := (v bitAnd:greenMask) >> greenShift. |
|
450 |
b := (v bitAnd:blueMask) >> blueShift. |
|
451 |
a := (v bitAnd:alphaMask) >> alphaShift. |
|
452 |
(((((a bitShift:8) bitOr:b) bitShift:8) bitOr:g) bitShift:8) bitOr:r |
|
453 |
]. |
|
3589 | 454 |
]. |
455 |
]. |
|
456 |
] ifFalse:[ |
|
457 |
inDepth <= 8 ifTrue:[ |
|
458 |
photometric := #palette. |
|
459 |
] ifFalse:[ |
|
460 |
inDepth == 16 ifTrue:[ |
|
461 |
bitsPerSample := #(5 5 5). |
|
462 |
colorMap := FixedPalette |
|
463 |
redShift:10 redMask:16r1f |
|
464 |
greenShift:5 greenMask:16r1f |
|
465 |
blueShift:0 blueMask:16r1F. |
|
466 |
photometric := #palette. |
|
467 |
swapAction := [:bytes | data swapBytes]. |
|
468 |
]. |
|
469 |
inDepth == 24 ifTrue:[ |
|
470 |
bitsPerSample := #(8 8 8). |
|
471 |
photometric := #rgb. |
|
472 |
swapAction := [:data | self swapBytesFromRGB_to_BGR]. |
|
473 |
]. |
|
474 |
inDepth == 32 ifTrue:[ |
|
475 |
bitsPerSample := #(8 8 8 8). |
|
476 |
photometric := #rgba. |
|
477 |
swapAction := [:data | self swapBytesFromRGBA_to_BGRA]. |
|
478 |
]. |
|
479 |
]. |
|
480 |
]. |
|
481 |
||
482 |
"/ Depth32Image keeps its data r/g/b/a; BMP has it b/g/r/a (sigh) |
|
483 |
pixelAction notNil ifTrue:[ |
|
484 |
inDepth == 16 ifTrue:[ |
|
485 |
swapAction := |
|
486 |
[:byteArray | |
|
487 |
|idx rowIdx oldValue newValue bpr| |
|
488 |
rowIdx := 1. |
|
489 |
bpr := self bytesPerRow. |
|
490 |
||
491 |
1 to:height do:[:y | |
|
492 |
idx := rowIdx. |
|
493 |
1 to:width do:[:x | |
|
494 |
oldValue := data wordAt:idx MSB:false. |
|
495 |
newValue := pixelAction value:oldValue. |
|
496 |
data wordAt:idx put:newValue MSB:true. |
|
497 |
idx := idx + 2. |
|
498 |
]. |
|
499 |
rowIdx := rowIdx + bpr. |
|
500 |
] |
|
501 |
]. |
|
502 |
]. |
|
503 |
inDepth == 24 ifTrue:[ |
|
504 |
swapAction := |
|
505 |
[:byteArray | |
|
506 |
|idx rowIdx oldValue newValue bpr| |
|
507 |
rowIdx := 1. |
|
508 |
bpr := self bytesPerRow. |
|
509 |
||
510 |
1 to:height do:[:y | |
|
511 |
idx := rowIdx. |
|
512 |
1 to:width do:[:x | |
|
513 |
oldValue := data unsignedIntegerAt:idx length:3 bigEndian:false. |
|
514 |
newValue := pixelAction value:oldValue. |
|
515 |
data unsignedIntegerAt:idx put:newValue length:3 bigEndian:true. |
|
516 |
idx := idx + 3. |
|
517 |
]. |
|
518 |
rowIdx := rowIdx + bpr. |
|
519 |
] |
|
520 |
]. |
|
521 |
]. |
|
522 |
inDepth == 32 ifTrue:[ |
|
523 |
swapAction := |
|
524 |
[:byteArray | |
|
525 |
|idx rowIdx oldValue newValue bpr| |
|
526 |
rowIdx := 1. |
|
527 |
bpr := self bytesPerRow. |
|
528 |
||
529 |
1 to:height do:[:y | |
|
530 |
idx := rowIdx. |
|
531 |
1 to:width do:[:x | |
|
532 |
oldValue := data doubleWordAt:idx MSB:false. |
|
533 |
newValue := pixelAction value:oldValue. |
|
534 |
data doubleWordAt:idx put:newValue MSB:false. |
|
535 |
idx := idx + 4. |
|
536 |
]. |
|
537 |
rowIdx := rowIdx + bpr. |
|
538 |
] |
|
539 |
]. |
|
540 |
]. |
|
2578 | 541 |
]. |
3589 | 542 |
swapAction notNil ifTrue:[ |
543 |
swapAction value:data |
|
544 |
]. |
|
545 |
! |
|
2578 | 546 |
|
2571 | 547 |
loadBMP1From:aStream into:aByteArray |
548 |
"load bmp-1 bit per pixel imagedata." |
|
549 |
||
550 |
compression == 0 ifFalse:[ |
|
2578 | 551 |
^ false |
552 |
]. |
|
2571 | 553 |
|
554 |
^ self loadUncompressedFrom:aStream into:aByteArray |
|
555 |
! |
|
556 |
||
557 |
loadBMP2From:aStream into:aByteArray |
|
558 |
"load bmp-2 bit per pixel imagedata." |
|
559 |
||
560 |
compression == 0 ifFalse:[ |
|
2578 | 561 |
^ false |
562 |
]. |
|
2571 | 563 |
|
564 |
^ self loadUncompressedFrom:aStream into:aByteArray |
|
565 |
! |
|
566 |
||
567 |
loadBMP4From:aStream into:aByteArray |
|
568 |
"load bmp-4 bit per pixel imagedata." |
|
211 | 569 |
|
2571 | 570 |
compression == 0 ifTrue:[ |
2578 | 571 |
^ self loadUncompressedFrom:aStream into:aByteArray |
572 |
]. |
|
2571 | 573 |
compression == 2 ifTrue:[ |
2578 | 574 |
^ self loadRLECompressedBMP4From:aStream into:aByteArray |
575 |
]. |
|
2571 | 576 |
|
577 |
^ false |
|
578 |
! |
|
160
ee4d64b12c94
handle depth 24 rgb BMP files
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
579 |
|
3589 | 580 |
loadBMPWidth:w height:h bytesPerPixel:bpp from:aStream into:aByteArray |
3583 | 581 |
"load bmp-16/24/32 bit per pixel imagedata." |
582 |
||
3589 | 583 |
|dstIdx fileBytesPerRow imgBytesPerRow skip align lineDelta| |
2571 | 584 |
|
585 |
align := 4. |
|
586 |
||
3589 | 587 |
((compression == 0) or:[compression == 3]) ifFalse:[ |
588 |
"/ 'BMPReader: unsupported compression: ' infoPrint. compression infoPrintCR. |
|
589 |
self fileFormatError:('unsupported compression:', compression printString). |
|
590 |
^ false. |
|
591 |
]. |
|
160
ee4d64b12c94
handle depth 24 rgb BMP files
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
592 |
|
3589 | 593 |
imgBytesPerRow := w * bpp. |
594 |
fileBytesPerRow := imgBytesPerRow. |
|
595 |
(fileBytesPerRow bitAnd:(align-1)) ~~ 0 ifTrue:[ |
|
596 |
fileBytesPerRow := (fileBytesPerRow bitAnd:((align-1) bitInvert)) + align. |
|
597 |
]. |
|
598 |
||
599 |
topDown ifTrue:[ |
|
600 |
dstIdx := 1. |
|
601 |
lineDelta := imgBytesPerRow |
|
602 |
] ifFalse:[ |
|
603 |
dstIdx := imgBytesPerRow * (h - 1) + 1. |
|
604 |
lineDelta := imgBytesPerRow negated |
|
605 |
]. |
|
606 |
||
607 |
(lineDelta == fileBytesPerRow) ifTrue:[ |
|
608 |
(aStream nextBytes:(fileBytesPerRow*h) into:aByteArray startingAt:dstIdx) == (fileBytesPerRow*h) ifFalse:[ |
|
609 |
^ false |
|
3583 | 610 |
]. |
3589 | 611 |
] ifFalse:[ |
612 |
skip := fileBytesPerRow - imgBytesPerRow. |
|
3578 | 613 |
1 to:h do:[:row | |
3589 | 614 |
(aStream nextBytes:imgBytesPerRow into:aByteArray startingAt:dstIdx) == imgBytesPerRow ifFalse:[ |
3578 | 615 |
^ false |
616 |
]. |
|
3589 | 617 |
skip ~~ 0 ifTrue:[ aStream skip:skip ]. |
3583 | 618 |
dstIdx := dstIdx + lineDelta. |
3578 | 619 |
]. |
160
ee4d64b12c94
handle depth 24 rgb BMP files
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
620 |
]. |
3589 | 621 |
^ true |
2571 | 622 |
! |
623 |
||
3589 | 624 |
loadBMPWidth:w height:h depth:d from:aStream into:aByteArray |
2571 | 625 |
"helper: load a BMP image" |
626 |
||
627 |
d == 8 ifTrue:[ |
|
2756
e8bf6a023191
halt in: #loadBMPWidth:height:depth:from:into:
Claus Gittinger <cg@exept.de>
parents:
2578
diff
changeset
|
628 |
compression == 0 ifTrue:[ |
3589 | 629 |
^ self loadUncompressedFrom:aStream into:aByteArray. |
2756
e8bf6a023191
halt in: #loadBMPWidth:height:depth:from:into:
Claus Gittinger <cg@exept.de>
parents:
2578
diff
changeset
|
630 |
]. |
e8bf6a023191
halt in: #loadBMPWidth:height:depth:from:into:
Claus Gittinger <cg@exept.de>
parents:
2578
diff
changeset
|
631 |
compression == 1 ifTrue:[ |
3589 | 632 |
^ self loadRLECompressedBMP8From:aStream into:aByteArray. |
2756
e8bf6a023191
halt in: #loadBMPWidth:height:depth:from:into:
Claus Gittinger <cg@exept.de>
parents:
2578
diff
changeset
|
633 |
]. |
3578 | 634 |
"/ self breakPoint:#cg info:'unhandled compression'. |
635 |
self fileFormatError:('unsupported compression:', compression printString). |
|
2756
e8bf6a023191
halt in: #loadBMPWidth:height:depth:from:into:
Claus Gittinger <cg@exept.de>
parents:
2578
diff
changeset
|
636 |
^ false |
2571 | 637 |
]. |
638 |
d == 4 ifTrue:[ |
|
3589 | 639 |
^ self loadBMP4From:aStream into:aByteArray |
2571 | 640 |
]. |
641 |
d == 2 ifTrue:[ |
|
3589 | 642 |
^ self loadBMP2From:aStream into:aByteArray |
2571 | 643 |
]. |
644 |
d == 1 ifTrue:[ |
|
3589 | 645 |
^ self loadBMP1From:aStream into:aByteArray |
2571 | 646 |
]. |
2578 | 647 |
((d == 16) |
648 |
or:[ (d == 24) |
|
2571 | 649 |
or:[ (d == 32) ]]) ifTrue:[ |
3589 | 650 |
(self loadBMPWidth:w height:h bytesPerPixel:(d // 8) from:aStream into:aByteArray) ifFalse:[ |
2756
e8bf6a023191
halt in: #loadBMPWidth:height:depth:from:into:
Claus Gittinger <cg@exept.de>
parents:
2578
diff
changeset
|
651 |
^ false |
e8bf6a023191
halt in: #loadBMPWidth:height:depth:from:into:
Claus Gittinger <cg@exept.de>
parents:
2578
diff
changeset
|
652 |
]. |
e8bf6a023191
halt in: #loadBMPWidth:height:depth:from:into:
Claus Gittinger <cg@exept.de>
parents:
2578
diff
changeset
|
653 |
^ true |
2571 | 654 |
]. |
3578 | 655 |
self fileFormatError:('unsupported depth:', d printString). |
819 | 656 |
^ false |
103 | 657 |
|
819 | 658 |
"Created: / 17.9.1995 / 18:48:11 / claus" |
659 |
"Modified: / 3.2.1998 / 20:21:16 / cg" |
|
2571 | 660 |
! |
661 |
||
3578 | 662 |
loadRLECompressedBMP4From:aStream into:aByteArray |
663 |
"load bmp-rle-4 pixel imagedata" |
|
664 |
||
3589 | 665 |
|bytesPerRow x y dstIndex lineStartIndex cnt pair clr1 clr2 code nbyte byte bytes byteIdx rev| |
3578 | 666 |
|
3589 | 667 |
bytesPerRow := self bytesPerRow. |
3578 | 668 |
x := 0. |
3583 | 669 |
|
670 |
topDown ifTrue:[ |
|
671 |
y := 0. |
|
672 |
lineStartIndex := 1. |
|
673 |
] ifFalse:[ |
|
674 |
y := height - 1. |
|
3589 | 675 |
lineStartIndex := (y * bytesPerRow) + 1. |
3583 | 676 |
]. |
3578 | 677 |
dstIndex := lineStartIndex. |
3589 | 678 |
"/ data atAllPut:16rBB. |
679 |
||
3583 | 680 |
[ y between:0 and:height-1 ] whileTrue:[ |
3578 | 681 |
cnt := aStream nextByte. |
682 |
pair := aStream nextByte. |
|
683 |
cnt ~~ 0 ifTrue:[ |
|
3583 | 684 |
((x between:0 and:width-1) and:[y between:0 and:height-1]) ifFalse:[ |
685 |
self breakPoint:#cg. |
|
686 |
self fileFormatError:('invalid delta'). |
|
687 |
]. |
|
3589 | 688 |
"/ Transcript printf:'RUN y: %d x: %d cnt: %d pair: %02x\n' with:y with:x with:cnt with:pair. |
3583 | 689 |
x even ifTrue:[ |
690 |
(cnt >= 2) ifTrue:[ |
|
691 |
nbyte := cnt // 2. |
|
3589 | 692 |
aByteArray from:dstIndex to:(dstIndex+nbyte-1) put:pair. |
3583 | 693 |
dstIndex := dstIndex + nbyte. |
694 |
]. |
|
695 |
cnt odd ifTrue:[ |
|
696 |
"/ got odd count |
|
3589 | 697 |
byte := aByteArray at:dstIndex. |
698 |
aByteArray at:dstIndex put:((byte bitAnd:16r0F) bitOr:(pair bitAnd:16rF0)). |
|
3583 | 699 |
"/ self halt. |
700 |
]. |
|
701 |
] ifFalse:[ |
|
3589 | 702 |
clr1 := pair bitShift:-4. "/ left bit |
703 |
clr2 := pair bitAnd:16rF. "/ right bit |
|
704 |
||
705 |
"/ the first odd-x halfbyte |
|
3583 | 706 |
byte := aByteArray at:dstIndex. |
3589 | 707 |
aByteArray at:dstIndex put:((byte bitAnd:16rF0) bitOr:clr1). |
3578 | 708 |
dstIndex := dstIndex + 1. |
3589 | 709 |
|
3583 | 710 |
nbyte := (cnt-1) // 2. |
711 |
nbyte > 0 ifTrue:[ |
|
3589 | 712 |
rev := (clr2 << 4) bitOr:clr1. |
713 |
aByteArray from:dstIndex to:(dstIndex+nbyte-1) put:rev. |
|
3583 | 714 |
dstIndex := dstIndex + nbyte. |
715 |
]. |
|
716 |
cnt even ifTrue:[ |
|
3589 | 717 |
"/ the final odd-x halfbyte |
3583 | 718 |
byte := aByteArray at:dstIndex. |
3589 | 719 |
aByteArray at:dstIndex put:((byte bitAnd:16r0F) bitOr:(clr2 bitShift:4)). |
3583 | 720 |
]. |
3578 | 721 |
]. |
3583 | 722 |
x := x + cnt. |
3578 | 723 |
] ifFalse:[ |
724 |
"/ cnt == 0: escape codes */ |
|
725 |
code := pair. |
|
726 |
code == 0 ifTrue:[ |
|
727 |
"/ end of line |
|
3589 | 728 |
"/ Transcript printf:'EOL\n'. |
3578 | 729 |
x := 0. |
3583 | 730 |
topDown ifTrue:[ |
731 |
y := y + 1. |
|
3589 | 732 |
lineStartIndex := lineStartIndex + bytesPerRow. |
3583 | 733 |
] ifFalse:[ |
734 |
y := y - 1. |
|
3589 | 735 |
lineStartIndex := lineStartIndex - bytesPerRow. |
3583 | 736 |
]. |
3578 | 737 |
dstIndex := lineStartIndex. |
738 |
] ifFalse:[ |
|
739 |
code == 1 ifTrue:[ |
|
3589 | 740 |
"/ Transcript printf:'END\n'. |
3578 | 741 |
"/ end of pic |
742 |
^ true |
|
743 |
]. |
|
744 |
code == 2 ifTrue:[ |
|
745 |
"/ delta |
|
746 |
x := x + aStream nextSignedByte. |
|
747 |
y := y - aStream nextSignedByte. |
|
3589 | 748 |
"/ Transcript printf:'MOVE y: %d x: %d\n' with:y with:x with:code. |
749 |
lineStartIndex := (y * bytesPerRow) + 1. |
|
750 |
dstIndex := lineStartIndex + (x//2). |
|
3578 | 751 |
] ifFalse:[ |
752 |
"/ absolute; cnt pixels coming |
|
3583 | 753 |
((x between:0 and:width-1) and:[y between:0 and:height-1]) ifFalse:[ |
754 |
self breakPoint:#cg. |
|
755 |
self fileFormatError:('invalid delta'). |
|
756 |
]. |
|
3578 | 757 |
cnt := code. |
3583 | 758 |
nbyte := (cnt+1) // 2. |
759 |
bytes := aStream nextBytes:nbyte. |
|
760 |
"/ odd byte count - padd |
|
3578 | 761 |
nbyte odd ifTrue:[ |
762 |
aStream skip:1. |
|
763 |
]. |
|
3583 | 764 |
x + cnt > width ifTrue:[self halt]. |
765 |
byteIdx := 1. |
|
3589 | 766 |
"/ Transcript printf:'PIXELS: %d x: %d count: %d nbyte: %d\n' with:y with:x with:cnt with:nbyte. |
3583 | 767 |
x even ifTrue:[ |
3589 | 768 |
nbyte := cnt // 2. |
769 |
aByteArray replaceFrom:dstIndex to:(dstIndex+nbyte-1) with:bytes startingAt:byteIdx. |
|
770 |
byteIdx := byteIdx + nbyte. |
|
771 |
dstIndex := dstIndex + nbyte. |
|
3583 | 772 |
cnt odd ifTrue:[ |
3589 | 773 |
"/ the last half-byte |
774 |
pair := bytes at:byteIdx. |
|
3583 | 775 |
byte := aByteArray at:dstIndex. |
776 |
aByteArray at:dstIndex put:((byte bitAnd:16r0F) bitOr:(pair bitAnd:16rF0)). |
|
777 |
"/ self halt. |
|
778 |
]. |
|
3589 | 779 |
] ifFalse:[ |
780 |
nbyte := (cnt // 2). |
|
781 |
[nbyte > 0] whileTrue:[ |
|
782 |
pair := bytes at:byteIdx. byteIdx := byteIdx + 1. |
|
783 |
byte := aByteArray at:dstIndex. |
|
784 |
aByteArray at:dstIndex put:((byte bitAnd:16rF0) bitOr:((pair bitShift:-4) bitAnd:16r0F)). |
|
785 |
dstIndex := dstIndex + 1. |
|
786 |
byte := aByteArray at:dstIndex. |
|
787 |
aByteArray at:dstIndex put:((byte bitAnd:16r0F) bitOr:((pair bitShift:4) bitAnd:16rF0)). |
|
788 |
nbyte := nbyte - 1. |
|
789 |
]. |
|
790 |
cnt odd ifTrue:[ |
|
791 |
pair := bytes at:byteIdx. byteIdx := byteIdx + 1. |
|
792 |
byte := aByteArray at:dstIndex. |
|
793 |
aByteArray at:dstIndex put:((byte bitAnd:16rF0) bitOr:((pair bitShift:-4) bitAnd:16r0F)). |
|
794 |
dstIndex := dstIndex + 1. |
|
795 |
]. |
|
3583 | 796 |
]. |
797 |
x := x + cnt. |
|
3578 | 798 |
]. |
799 |
]. |
|
800 |
]. |
|
801 |
]. |
|
802 |
^ true. |
|
803 |
! |
|
804 |
||
2571 | 805 |
loadRLECompressedBMP8From:aStream into:aByteArray |
806 |
"load bmp-8 bit per pixel imagedata" |
|
807 |
||
808 |
|bytesPerRowInData x y dstIndex lineStartIndex cnt clr code n| |
|
809 |
||
810 |
bytesPerRowInData := self bytesPerRow. |
|
811 |
x := 0. |
|
3583 | 812 |
|
813 |
topDown ifTrue:[ |
|
814 |
y := 0. |
|
815 |
lineStartIndex := 1. |
|
816 |
] ifFalse:[ |
|
817 |
y := height - 1. |
|
818 |
lineStartIndex := (y * bytesPerRowInData) + 1. |
|
819 |
]. |
|
2571 | 820 |
dstIndex := lineStartIndex. |
3583 | 821 |
|
822 |
[ y between:0 and:height-1 ] whileTrue:[ |
|
2571 | 823 |
cnt := aStream nextByte. |
824 |
clr := aStream nextByte. |
|
825 |
cnt ~~ 0 ifTrue:[ |
|
826 |
aByteArray from:dstIndex to:dstIndex+cnt-1 put:clr. |
|
827 |
x := x + cnt. |
|
828 |
dstIndex := dstIndex + cnt. |
|
829 |
] ifFalse:[ |
|
830 |
"/ cnt == 0: escape codes */ |
|
831 |
code := clr. |
|
832 |
code == 0 ifTrue:[ |
|
833 |
"/ end of line |
|
834 |
x := 0. |
|
3583 | 835 |
topDown ifTrue:[ |
836 |
y := y + 1. |
|
837 |
lineStartIndex := lineStartIndex + bytesPerRowInData. |
|
838 |
] ifFalse:[ |
|
839 |
y := y - 1. |
|
840 |
lineStartIndex := lineStartIndex - bytesPerRowInData. |
|
841 |
]. |
|
2571 | 842 |
dstIndex := lineStartIndex. |
843 |
] ifFalse:[ |
|
844 |
code == 1 ifTrue:[ |
|
845 |
"/ end of pic |
|
846 |
^ true |
|
847 |
]. |
|
848 |
code == 2 ifTrue:[ |
|
849 |
"/ delta |
|
850 |
x := x + aStream nextSignedByte. |
|
851 |
y := y - aStream nextSignedByte. |
|
852 |
lineStartIndex := (y * bytesPerRowInData) + 1. |
|
853 |
dstIndex := lineStartIndex + x. |
|
854 |
] ifFalse:[ |
|
855 |
"/ absolute; cnt pixels coming |
|
856 |
cnt := code. |
|
857 |
n := aStream nextBytes:cnt into:aByteArray startingAt:dstIndex. |
|
2578 | 858 |
n ~~ cnt ifTrue:[^ false]. |
2571 | 859 |
x := x + cnt. |
860 |
dstIndex := dstIndex + cnt. |
|
861 |
"/ odd count - padd |
|
862 |
cnt odd ifTrue:[ |
|
863 |
aStream skip:1. |
|
864 |
]. |
|
865 |
]. |
|
866 |
]. |
|
867 |
]. |
|
868 |
]. |
|
869 |
^ true. |
|
870 |
! |
|
871 |
||
872 |
loadUncompressedFrom:aStream into:aByteArray |
|
873 |
"load bmp-1,2,4 and 8 bit per pixel imagedata." |
|
874 |
||
3583 | 875 |
|bytesPerRowInStream bytesPerRowInData skip n |
876 |
dstIndex "{ Class: SmallInteger }" |
|
877 |
lineDelta "{ Class: SmallInteger }" |
|
878 |
nRows "{ Class: SmallInteger }" | |
|
2571 | 879 |
|
880 |
compression == 0 ifFalse:[ |
|
3583 | 881 |
^ false |
2578 | 882 |
]. |
2571 | 883 |
|
2578 | 884 |
bytesPerRowInStream := Image bytesPerRowForWidth:width depth:inDepth padding:32. |
2571 | 885 |
bytesPerRowInData := self bytesPerRow. |
886 |
skip := bytesPerRowInStream - bytesPerRowInData. |
|
887 |
||
3583 | 888 |
topDown ifTrue:[ |
889 |
dstIndex := 1. |
|
890 |
lineDelta := bytesPerRowInData. |
|
891 |
] ifFalse:[ |
|
892 |
"/ bottom row first... |
|
893 |
dstIndex := (height - 1) * bytesPerRowInData + 1. |
|
894 |
lineDelta := bytesPerRowInData negated. |
|
895 |
]. |
|
896 |
nRows := height. |
|
897 |
||
898 |
1 to:nRows do:[:row | |
|
899 |
n := aStream nextBytes:bytesPerRowInData into:aByteArray startingAt:dstIndex. |
|
3589 | 900 |
n ~~ bytesPerRowInData ifTrue:[^ false]. |
901 |
skip ~~ 0 ifTrue:[aStream skip:skip]. |
|
3583 | 902 |
dstIndex := dstIndex + lineDelta. |
2571 | 903 |
]. |
904 |
^ true. |
|
905 |
! |
|
906 |
||
1848 | 907 |
readColorMap:nColors numBytesPerColor:nRawBytesPerColor from:aStream |
908 |
"read the colormap; notice: its in BGR order (sigh)." |
|
909 |
||
2578 | 910 |
|rawMap rMap gMap bMap |
3589 | 911 |
srcIndex "{ Class: SmallInteger }"| |
1848 | 912 |
|
913 |
rawMap := ByteArray uninitializedNew:(nColors*nRawBytesPerColor). |
|
914 |
aStream nextBytes:(nColors*nRawBytesPerColor) into:rawMap. |
|
915 |
||
916 |
rMap := ByteArray new:nColors. |
|
917 |
gMap := ByteArray new:nColors. |
|
918 |
bMap := ByteArray new:nColors. |
|
919 |
srcIndex := 1. |
|
920 |
||
921 |
"/ stupid: this is a BGR-ordered map (otherwise, could use #rgbBytesVector:-message) |
|
922 |
"/ also, there might be a fourth byte (alpha ?) which is (currently) skipped. |
|
923 |
1 to:nColors do:[:i | |
|
3589 | 924 |
bMap at:i put:(rawMap at:srcIndex). |
925 |
gMap at:i put:(rawMap at:srcIndex+1). |
|
926 |
rMap at:i put:(rawMap at:srcIndex+2). |
|
927 |
srcIndex := srcIndex + nRawBytesPerColor. |
|
1848 | 928 |
]. |
929 |
||
2578 | 930 |
^ MappedPalette |
3589 | 931 |
redVector:rMap |
932 |
greenVector:gMap |
|
933 |
blueVector:bMap. |
|
934 |
! |
|
935 |
||
936 |
swapBytesFromRGBA_to_BGRA |
|
937 |
|idx bytesPerRow| |
|
938 |
||
939 |
"/ Depth32Image keeps its data r/g/b/a; BMP has it b/g/r/a (sigh) |
|
940 |
idx := 1. |
|
941 |
bytesPerRow := self bytesPerRow. |
|
942 |
1 to:height do:[:y | |
|
943 |
self class swap:width pixelsFromRGB_to_BGR_in:data startingAt:idx bytesPerPixel:4. |
|
944 |
idx := idx + bytesPerRow. |
|
945 |
]. |
|
946 |
! |
|
947 |
||
948 |
swapBytesFromRGB_to_BGR |
|
949 |
|idx bytesPerRow| |
|
950 |
||
951 |
"/ Depth24Image keeps its data r/g/b; BMP has it b/g/r (sigh) |
|
952 |
idx := 1. |
|
953 |
bytesPerRow := self bytesPerRow. |
|
954 |
1 to:height do:[:y | |
|
955 |
self class swap:width pixelsFromRGB_to_BGR_in:data startingAt:idx bytesPerPixel:3. |
|
956 |
idx := idx + bytesPerRow. |
|
957 |
]. |
|
1848 | 958 |
! ! |
959 |
||
1805 | 960 |
!WindowsIconReader methodsFor:'reading'! |
0 | 961 |
|
211 | 962 |
fromOS2File:aFilename |
963 |
"read an image from an OS/2 BMP file" |
|
964 |
||
41 | 965 |
|reader stream| |
966 |
||
1790 | 967 |
stream := self class streamReadingFile:aFilename. |
41 | 968 |
stream isNil ifTrue:[^ nil]. |
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
969 |
reader := self class new. |
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
970 |
reader fromOS2Stream:stream. |
41 | 971 |
stream close. |
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
972 |
^ reader image |
211 | 973 |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
974 |
"Modified: / 30-05-2007 / 16:52:50 / cg" |
21 | 975 |
! |
0 | 976 |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
977 |
fromOS2Stream:aStream |
211 | 978 |
"read an image from an OS/2 BMP stream" |
979 |
||
2571 | 980 |
^ self fromOS2Stream:aStream alreadyRead:nil |
981 |
! |
|
982 |
||
983 |
fromOS2Stream:aStream alreadyRead:bytesAlreadyRead |
|
984 |
"read an image from an OS/2 BMP stream" |
|
985 |
||
986 |
|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
|
987 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
988 |
inStream := aStream. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
989 |
aStream binary. |
2031 | 990 |
byteOrder := #lsb. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
991 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
992 |
"read the header" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
993 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
994 |
header := ByteArray uninitializedNew:8r110. |
2571 | 995 |
bytesAlreadyRead size > 0 ifTrue:[ |
3583 | 996 |
header replaceFrom:1 with:bytesAlreadyRead |
2571 | 997 |
]. |
998 |
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
|
999 |
|
445 | 1000 |
(header startsWith:#(73 67)) ifTrue:[ "IC" |
3583 | 1001 |
"IC format" |
1002 |
aStream nextBytes:10 into:header startingAt:17. |
|
1003 |
width := header at:7. |
|
1004 |
height := header at:9. |
|
1005 |
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
|
1006 |
] ifFalse:[ |
3583 | 1007 |
(header startsWith:#(67 73)) ifTrue:[ "CI" |
1008 |
^ self fileFormatError:'unsupported format: CI'. |
|
1009 |
] ifFalse:[ |
|
1010 |
aStream nextBytes:(8r110-16) into:header startingAt:17. |
|
1011 |
width := header at:8r101. |
|
1012 |
height := header at:8r103. |
|
1013 |
inDepth := header at:8r107. |
|
1014 |
] |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1015 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1016 |
|
1846 | 1017 |
self reportDimension. |
1018 |
||
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1019 |
"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
|
1020 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1021 |
nColors := 1 bitShift:inDepth. |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1022 |
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
|
1023 |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1024 |
"read the mask" |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1025 |
|
2571 | 1026 |
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
|
1027 |
mask := ByteArray uninitializedNew:nByte. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1028 |
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
|
1029 |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1030 |
"what is this ?" |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1031 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1032 |
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
|
1033 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1034 |
"/ "read the data bits" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1035 |
"/ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1036 |
"/ 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
|
1037 |
"/ 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
|
1038 |
"/ inDepth == 8 ifTrue:[ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1039 |
"/ ]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1040 |
"/ 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
|
1041 |
"/ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1042 |
"/ "stupid: last row first" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1043 |
"/ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1044 |
"/ 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
|
1045 |
"/ srcIndex := 1. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1046 |
"/ 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
|
1047 |
"/ 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
|
1048 |
"/ 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
|
1049 |
"/ with:data4 startingAt:srcIndex. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1050 |
"/ srcIndex := srcIndex + bytesPerRow. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1051 |
"/ dstIndex := dstIndex - bytesPerRow. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1052 |
"/ ]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1053 |
"/ data4 := tmp. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1054 |
"/ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1055 |
"/ "expand into bytes" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1056 |
"/ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1057 |
"/ 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
|
1058 |
"/ 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
|
1059 |
"/ into:data mapping:nil. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1060 |
"/ |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1061 |
|
2571 | 1062 |
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
|
1063 |
"/ 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
|
1064 |
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
|
1065 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1066 |
data := ByteArray uninitializedNew:(height * width "bytesPerRow"). |
2578 | 1067 |
compression := 0. |
2571 | 1068 |
(self loadBMPWidth:width height:height depth:inDepth from:aStream into:data) ifFalse:[ |
3583 | 1069 |
^ nil |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1070 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1071 |
photometric := #palette. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1072 |
samplesPerPixel := 1. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1073 |
bitsPerSample := #(8). |
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1074 |
^ self image |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1075 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1076 |
" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1077 |
|i f| |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1078 |
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
|
1079 |
f := i asFormOn:Display. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1080 |
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
|
1081 |
" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1082 |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1083 |
"Modified: / 17-09-1995 / 18:49:24 / claus" |
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1084 |
"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
|
1085 |
! |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1086 |
|
2578 | 1087 |
fromStream:aStream |
211 | 1088 |
"figure out which format the stream contains |
1089 |
(there are various different bmp/ico formats around) |
|
1090 |
and read the image." |
|
1091 |
||
1092 |
|fileSize header| |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1093 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1094 |
inStream := aStream. |
2031 | 1095 |
byteOrder := #lsb. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1096 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1097 |
aStream binary. |
3583 | 1098 |
aStream signalAtEnd:true. |
2571 | 1099 |
aStream isFileStream ifTrue:[ |
3583 | 1100 |
fileSize := aStream fileSize. |
1101 |
fileSize < 16 ifTrue:[ |
|
1102 |
^ self fileFormatError:'short/corrupted file'. |
|
1103 |
]. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1104 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1105 |
|
3583 | 1106 |
EndOfStreamError handle:[:ex | |
1107 |
^ self fileFormatError:'unexpected EOF while reading (short/corrupted file)'. |
|
1108 |
] do:[ |
|
1109 |
header := ByteArray uninitializedNew:4. |
|
1110 |
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
|
1111 |
|
3583 | 1112 |
(header startsWith:#(66 77)) ifTrue:[ "BM" |
1113 |
"/ 'WinIconReader [info]: Win3.x or OS/2 vsn 2 BM format' infoPrintNL. |
|
1114 |
^ self fromWindowsBMPStream:aStream alreadyRead:header |
|
1115 |
]. |
|
1116 |
(header startsWith:#(66 65)) ifTrue:[ "BA" |
|
1117 |
"/ 'WinIconReader [info]: OS/2 vsn 2 BA format' infoPrintNL. |
|
1118 |
^ self fromOS2Stream:aStream alreadyRead:header |
|
1119 |
]. |
|
1120 |
(header startsWith:#(67 73)) ifTrue:[ "CI" |
|
1121 |
"/ 'WinIconReader [info]: OS/2 vsn 2 BA format' infoPrintNL. |
|
1122 |
"/ ^ self fromOS2Stream:aStream |
|
1123 |
^ self fileFormatError:'OS/2 CI format not supported'. |
|
1124 |
]. |
|
1125 |
(header startsWith:#(73 67)) ifTrue:[ "IC" |
|
1126 |
"/ 'WinIconReader [info]: OS/2 IC format' infoPrintNL. |
|
1127 |
^ self fromOS2Stream:aStream alreadyRead:header |
|
1128 |
]. |
|
1129 |
(header startsWith:#(80 84)) ifTrue:[ "PT" |
|
1130 |
"/ 'WinIconReader [info]: OS/2 PT format' infoPrintNL. |
|
1131 |
^ self fromOS2Stream:aStream alreadyRead:header |
|
1132 |
]. |
|
1133 |
(header startsWith:#(16r53 16r5A)) ifTrue:[ "SZ" |
|
1134 |
"/ 'WinIconReader [info]: OS/2 SZ format' infoPrintNL. |
|
1135 |
"/ ^ self fromOS2Stream:aStream |
|
1136 |
^ self fileFormatError:'OS/2 SZ format not supported'. |
|
1137 |
]. |
|
1138 |
(header startsWith:#(0 0 1 0)) ifTrue:[ |
|
1139 |
"/ 'WinIconReader [info]: Win3.x ICO format' infoPrintNL. |
|
1140 |
^ self fromWindowsICOStream:aStream alreadyRead:header |
|
1141 |
]. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1142 |
]. |
3583 | 1143 |
|
1144 |
^ 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
|
1145 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1146 |
" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1147 |
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
|
1148 |
" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1149 |
|
819 | 1150 |
"Modified: / 17.9.1995 / 18:59:07 / claus" |
1151 |
"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
|
1152 |
! |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1153 |
|
2578 | 1154 |
fromWindowsBMPFile: aFilename |
211 | 1155 |
"read an image from a windows BMP file" |
1156 |
||
41 | 1157 |
|reader stream| |
1158 |
||
1159 |
stream := self class streamReadingFile:aFilename. |
|
1160 |
stream isNil ifTrue:[^ nil]. |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1161 |
reader := self class new. |
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1162 |
reader fromWindowsBMPStream:stream. |
41 | 1163 |
stream close. |
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1164 |
^ reader image. |
211 | 1165 |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1166 |
"Modified: / 30-05-2007 / 16:53:48 / cg" |
21 | 1167 |
! |
1168 |
||
2578 | 1169 |
fromWindowsBMPStream:aStream |
211 | 1170 |
"read an image from a windows BMP stream" |
1171 |
||
2571 | 1172 |
^ self fromWindowsBMPStream:aStream alreadyRead:nil |
1173 |
! |
|
1174 |
||
1175 |
fromWindowsBMPStream:aStream alreadyRead:bytesAlreadyRead |
|
1176 |
"read an image from a windows BMP stream" |
|
1177 |
||
3589 | 1178 |
| fileHeader bitmapHeader iSize inPlanes |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1179 |
imgSize resH resV numColor numImportantColor |
3589 | 1180 |
dataStart |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1181 |
bytesPerRow numBytesPerColorInColormap| |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1182 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1183 |
inStream := aStream. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1184 |
aStream binary. |
2031 | 1185 |
byteOrder := #lsb. |
3583 | 1186 |
topDown := false. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1187 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1188 |
"read the header" |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1189 |
|
3589 | 1190 |
fileHeader := ByteArray new:14. |
2571 | 1191 |
bytesAlreadyRead size > 0 ifTrue:[ |
3589 | 1192 |
fileHeader replaceFrom:1 with:bytesAlreadyRead |
2571 | 1193 |
]. |
3589 | 1194 |
aStream nextBytes:(14-bytesAlreadyRead size) into:fileHeader 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
|
1195 |
|
3589 | 1196 |
iSize := aStream nextUnsignedLongMSB:false. |
1197 |
iSize > 124 ifTrue:[ |
|
1198 |
^ self fileFormatError:'unknown format'. |
|
1199 |
]. |
|
1200 |
||
1201 |
bitmapHeader := ByteArray new:iSize+16. "/ reserve to allow masks to be read with iSize=40 |
|
1202 |
aStream nextBytes:(iSize-4) into:bitmapHeader startingAt:(1+4). |
|
1203 |
||
1204 |
dataStart := fileHeader doubleWordAt:(10 + 1) MSB:false. |
|
3590 | 1205 |
|
3583 | 1206 |
((iSize == 40) or:[iSize == 108 or:[iSize == 124]]) ifTrue:[ "header-size" |
3579 | 1207 |
"/ |
3583 | 1208 |
"/ a Windows3.x BMP file (40) |
1209 |
"/ a Windows4.x BMP file (108) |
|
1210 |
"/ a Windows5.x BMP file (124) |
|
3579 | 1211 |
"/ |
3583 | 1212 |
"/ '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
|
1213 |
|
3589 | 1214 |
width := bitmapHeader doubleWordAt:(4 + 1) MSB:false. |
1215 |
height := bitmapHeader signedDoubleWordAt:(8 + 1) MSB:false. |
|
1216 |
inPlanes := bitmapHeader wordAt:(12 + 1) MSB:false. |
|
1217 |
inDepth := bitmapHeader wordAt:(14 + 1) MSB:false. |
|
1218 |
compression := bitmapHeader doubleWordAt:(16 + 1) MSB:false. |
|
1219 |
imgSize := bitmapHeader doubleWordAt:(20 + 1) MSB:false. |
|
1220 |
resH := bitmapHeader doubleWordAt:(24 + 1) MSB:false. |
|
1221 |
resV := bitmapHeader doubleWordAt:(28 + 1) MSB:false. |
|
1222 |
numColor := bitmapHeader doubleWordAt:(32 + 1) MSB:false. |
|
1223 |
numImportantColor := bitmapHeader doubleWordAt:(36 + 1) MSB:false. |
|
3583 | 1224 |
|
1225 |
(compression > 3) ifTrue:[ |
|
1226 |
^ self fileFormatError:'unhandled compression'. |
|
3589 | 1227 |
]. |
1228 |
||
1229 |
(iSize == 40 and:[compression == 3]) ifTrue:[ |
|
1230 |
"/ masks are not counted in header (sigh) |
|
1231 |
aStream next:4*3 into:bitmapHeader startingAt:iSize+1. |
|
1232 |
redMask := bitmapHeader doubleWordAt:(40 + 1) MSB:false. |
|
1233 |
greenMask := bitmapHeader doubleWordAt:(44 + 1) MSB:false. |
|
1234 |
blueMask := bitmapHeader doubleWordAt:(48 + 1) MSB:false. |
|
1235 |
alphaMask := 0. |
|
1236 |
] ifFalse:[ |
|
1237 |
iSize > 40 ifTrue:[ |
|
1238 |
((compression == 3) or:[iSize == 108]) ifTrue:[ |
|
1239 |
redMask := bitmapHeader doubleWordAt:(40 + 1) MSB:false. |
|
1240 |
greenMask := bitmapHeader doubleWordAt:(44 + 1) MSB:false. |
|
1241 |
blueMask := bitmapHeader doubleWordAt:(48 + 1) MSB:false. |
|
1242 |
alphaMask := bitmapHeader doubleWordAt:(52 + 1) MSB:false. |
|
1243 |
]. |
|
1244 |
]. |
|
3583 | 1245 |
]. |
1246 |
||
3579 | 1247 |
numColor == 0 ifTrue:[ |
3583 | 1248 |
"if 0 and depth is 8 or smaller, then the colormap has the size for the depth" |
3579 | 1249 |
inDepth <= 8 ifTrue:[ |
1250 |
numColor := 1 bitShift:inDepth. |
|
1251 |
] |
|
1252 |
]. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1253 |
|
3579 | 1254 |
numBytesPerColorInColormap := 4. |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1255 |
] ifFalse:[ |
3589 | 1256 |
((iSize == 12) or:[ (iSize == 16) or:[iSize == 64]]) ifTrue:[ |
3579 | 1257 |
"/ |
3583 | 1258 |
"/ its a Win2.x or OS/2 BMP file |
3579 | 1259 |
"/ |
3583 | 1260 |
"/ 'WinIconReader [info]: Win2.x or OS/2 format' infoPrintCR. |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1261 |
|
3579 | 1262 |
numBytesPerColorInColormap := 3. |
2571 | 1263 |
|
3589 | 1264 |
iSize >= 64 ifTrue:[ |
1265 |
"/ |
|
1266 |
"/ its an OS/2 (vsn2) BMP file |
|
1267 |
"/ |
|
1268 |
width := bitmapHeader doubleWordAt:(4 + 1) MSB:false. |
|
1269 |
height := bitmapHeader signedDoubleWordAt:(8 + 1) MSB:false. |
|
1270 |
inPlanes := bitmapHeader wordAt:(12 + 1) MSB:false. |
|
1271 |
inDepth := bitmapHeader wordAt:(14 + 1) MSB:false. |
|
1272 |
compression := bitmapHeader doubleWordAt:(16 + 1) MSB:false. |
|
1273 |
numColor := bitmapHeader doubleWordAt:(32 + 1) MSB:false. |
|
1274 |
numImportantColor := bitmapHeader doubleWordAt:(36 + 1) MSB:false. |
|
1275 |
(compression > 2) ifTrue:[ |
|
1276 |
^ self fileFormatError:'unhandled OS2 compression'. |
|
1277 |
]. |
|
1278 |
numColor == 0 ifTrue:[ |
|
1279 |
self halt. |
|
1280 |
"if 0 and depth is 8 or smaller, then the colormap has the size for the depth" |
|
1281 |
inDepth <= 8 ifTrue:[ |
|
1282 |
numColor := 1 bitShift:inDepth. |
|
1283 |
]. |
|
1284 |
]. |
|
1285 |
numBytesPerColorInColormap := 4. |
|
1286 |
] ifFalse:[ |
|
1287 |
iSize == 16 ifTrue:[ |
|
1288 |
"/ an OS2 bitmap with large dimensions |
|
1289 |
width := bitmapHeader doubleWordAt:(4 + 1) MSB:false. |
|
1290 |
height := bitmapHeader signedDoubleWordAt:(8 + 1) MSB:false. |
|
1291 |
inPlanes := bitmapHeader wordAt:(12 + 1) MSB:false. |
|
1292 |
inDepth := bitmapHeader wordAt:(14 + 1) MSB:false. |
|
1293 |
numBytesPerColorInColormap := 4. |
|
1294 |
] ifFalse:[ |
|
1295 |
"/ size == 12: |
|
1296 |
"/ a Win2.x bitmap |
|
1297 |
width := bitmapHeader wordAt:(4 + 1) MSB:false. |
|
1298 |
height := bitmapHeader signedWordAt:(6 + 1) MSB:false. |
|
1299 |
inPlanes := bitmapHeader wordAt:(8 + 1) MSB:false. |
|
1300 |
inDepth := bitmapHeader wordAt:(10 + 1) MSB:false. |
|
1301 |
]. |
|
3579 | 1302 |
compression := 0. |
3583 | 1303 |
inDepth <= 8 ifTrue:[ |
1304 |
numColor := 1 bitShift:inDepth. |
|
1305 |
]. |
|
3589 | 1306 |
]. |
3579 | 1307 |
] ifFalse:[ |
1308 |
^ self fileFormatError:'unknown format'. |
|
1309 |
]. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1310 |
]. |
3589 | 1311 |
|
3583 | 1312 |
topDown := false. |
1313 |
height < 0 ifTrue:[ |
|
1314 |
height := height negated. |
|
1315 |
topDown := true. |
|
1316 |
]. |
|
1317 |
((width > 10000) or:[height > 10000]) ifTrue:[ |
|
1318 |
^ self fileFormatError:'unreasonable width or height'. |
|
3579 | 1319 |
]. |
1320 |
||
1846 | 1321 |
self reportDimension. |
1322 |
||
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1323 |
numColor ~~ 0 ifTrue:[ |
3579 | 1324 |
"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
|
1325 |
|
3581 | 1326 |
numColor > 4096 ifTrue:[ |
1327 |
"/ colormap only allowed up to 12bit |
|
3579 | 1328 |
^ self fileFormatError:'unreasonable colormap size'. |
3589 | 1329 |
]. |
3590 | 1330 |
"/ (dataStart - inStream position) ~= (numBytesPerColorInColormap*numColor) ifTrue:[ |
1331 |
"/ self halt |
|
1332 |
"/ ]. |
|
3589 | 1333 |
|
3579 | 1334 |
colorMap := self |
1335 |
readColorMap:numColor |
|
1336 |
numBytesPerColor:numBytesPerColorInColormap |
|
1337 |
from:aStream. |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1734
diff
changeset
|
1338 |
|
3579 | 1339 |
numColor > (1 bitShift:inDepth) ifTrue:[ |
1340 |
'funny number of colors in image' infoPrintCR. |
|
1341 |
numColor := 1 bitShift:inDepth. |
|
1342 |
colorMap := colorMap copyTo:numColor. |
|
1343 |
]. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1344 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1345 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1346 |
"/ check for valid compression |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1347 |
compression ~~ 0 ifTrue:[ |
3579 | 1348 |
"/ some compression |
1349 |
compression == 1 ifTrue:[ |
|
1350 |
"/ RLE8 - must be depth-8 |
|
1351 |
inDepth ~~ 8 ifTrue:[ |
|
1352 |
^ self fileFormatError:'RLE8 compression only supported with depth8 images'. |
|
1353 |
]. |
|
1354 |
]. |
|
1355 |
compression == 2 ifTrue:[ |
|
1356 |
"/ RLE4 - must be depth-4 |
|
1357 |
inDepth ~~ 4 ifTrue:[ |
|
1358 |
^ self fileFormatError:'RLE4 compression only supported with depth4 images'. |
|
1359 |
]. |
|
1360 |
]. |
|
1361 |
compression == 3 ifTrue:[ |
|
1362 |
"/ BITFIELDS - must be depth-16 or 32 |
|
3589 | 1363 |
(inDepth < 16) ifTrue:[ |
3579 | 1364 |
^ self fileFormatError:'BITFIELDS compression only supported with depth16/32 images'. |
1365 |
]. |
|
1366 |
]. |
|
1367 |
compression >= 4 ifTrue:[ |
|
1368 |
^ self fileFormatError:'unsupported compression'. |
|
1369 |
]. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1370 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1371 |
|
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1372 |
inPlanes ~~ 1 ifTrue:[ |
3579 | 1373 |
^ 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
|
1374 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1375 |
|
565 | 1376 |
dataStart notNil ifTrue:[ |
3590 | 1377 |
"/ (dataStart - inStream position) ~~ 0 ifTrue:[self halt]. |
3589 | 1378 |
|
3579 | 1379 |
aStream position:dataStart. |
2571 | 1380 |
]. |
3581 | 1381 |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1382 |
inDepth == 24 ifTrue:[ |
3579 | 1383 |
bytesPerRow := width * 3 |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1384 |
] ifFalse:[ |
3579 | 1385 |
inDepth == 16 ifTrue:[ |
1386 |
bytesPerRow := width * 2 |
|
1387 |
] ifFalse:[ |
|
1388 |
inDepth == 32 ifTrue:[ |
|
1389 |
bytesPerRow := width * 4 |
|
1390 |
] ifFalse:[ |
|
3589 | 1391 |
bitsPerSample := Array with:inDepth. |
3579 | 1392 |
bytesPerRow := self bytesPerRow |
1393 |
]. |
|
1394 |
]. |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1395 |
]. |
3583 | 1396 |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1397 |
data := ByteArray uninitializedNew:(height * bytesPerRow). |
3583 | 1398 |
"/ when compressed, there may be holes, which need to be filled with zeros |
1399 |
compression ~~ 0 ifTrue:[ |
|
1400 |
data atAllPut:0 |
|
1401 |
]. |
|
1402 |
||
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1403 |
"/ read & possibly decompress |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1404 |
|
2571 | 1405 |
(self loadBMPWidth:width height:height depth:inDepth from:aStream into:data) ifFalse:[ |
3579 | 1406 |
self fileFormatError:('read/decompression error'). |
1407 |
^ nil |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1408 |
]. |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1409 |
|
3589 | 1410 |
self convertPixels. |
1411 |
||
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1412 |
^ self image |
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1413 |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1414 |
"Modified: / 17-09-1995 / 18:48:46 / claus" |
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1415 |
"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
|
1416 |
! |
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1417 |
|
2578 | 1418 |
fromWindowsICOFile:aFilename |
211 | 1419 |
"read an image from a windows ICO file" |
1420 |
||
41 | 1421 |
|reader stream| |
1422 |
||
1423 |
stream := self class streamReadingFile:aFilename. |
|
1424 |
stream isNil ifTrue:[^ nil]. |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1425 |
reader := self class new. |
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1426 |
reader fromWindowsICOStream:stream. |
41 | 1427 |
stream close. |
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1428 |
^ reader image. |
21 | 1429 |
|
1430 |
" |
|
1431 |
Image fromFile:'/phys/clam2//LocalLibrary/Images/WIN_icons/ibm.ico'. |
|
1432 |
" |
|
211 | 1433 |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1434 |
"Modified: / 30-05-2007 / 16:57:52 / cg" |
21 | 1435 |
! |
1436 |
||
41 | 1437 |
fromWindowsICOStream:aStream |
211 | 1438 |
"read an image from a windows ICO stream" |
1439 |
||
2571 | 1440 |
^ self fromWindowsICOStream:aStream alreadyRead:nil |
1441 |
! |
|
1442 |
||
1443 |
fromWindowsICOStream:aStream alreadyRead:bytesAlreadyRead |
|
1444 |
"read an image from a windows ICO stream" |
|
1445 |
||
2578 | 1446 |
|header |
2034 | 1447 |
srcIndex dstIndex |
1448 |
rawData tmp bytesPerRow nColor cmapSize| |
|
0 | 1449 |
|
41 | 1450 |
inStream := aStream. |
1451 |
aStream binary. |
|
0 | 1452 |
|
1453 |
"read the header" |
|
1454 |
||
2034 | 1455 |
header := ByteArray uninitializedNew:(6 + 16 + 40). |
3404 | 1456 |
bytesAlreadyRead notEmptyOrNil ifTrue:[ |
1457 |
header replaceFrom:1 with:bytesAlreadyRead |
|
2571 | 1458 |
]. |
1459 |
aStream nextBytes:((6 + 16 + 40)-bytesAlreadyRead size) into:header startingAt:(1+bytesAlreadyRead size). |
|
1460 |
||
21 | 1461 |
width := header at:(6+1). |
1462 |
height := header at:(7+1). |
|
1463 |
nColor := header at:(8+1). |
|
430 | 1464 |
"/ reserved := header at:(9+1). |
2034 | 1465 |
"/ nPlanes := header wordAt:(10+1). |
1466 |
"/ nBitsPerPel := header wordAt:(12+1). |
|
1467 |
"/ nBytesInResource := header doubleWordAt:(14+1). |
|
1468 |
"/ ordinal := header wordAt:(18+1). |
|
1469 |
"21, 22 ?" |
|
1470 |
"23, ... , 62 ?" |
|
21 | 1471 |
|
2034 | 1472 |
inDepth := header at:16r25. |
1473 |
"/ mhmh - some depth4 icons seem to have a 0 in the depth field ... |
|
1474 |
inDepth == 0 ifTrue:[ |
|
3404 | 1475 |
inDepth := 4 |
1386 | 1476 |
]. |
3404 | 1477 |
(#(4 8 32) includes:inDepth) ifFalse:[ |
1478 |
"/ only tested for depth 4/8 images. |
|
1479 |
^ self fileFormatError:'only depth 4/8/32 ico-images supported (depth is ' , inDepth printString , ')'. |
|
1569 | 1480 |
"/ 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
|
1481 |
]. |
1846 | 1482 |
self reportDimension. |
0 | 1483 |
|
3404 | 1484 |
nColor > 0 ifTrue:[ |
1485 |
"read the colormap" |
|
1486 |
cmapSize := (1 bitShift:inDepth). |
|
2034 | 1487 |
|
3404 | 1488 |
colorMap := self readColorMap:cmapSize numBytesPerColor:4 from:aStream. |
1489 |
]. |
|
0 | 1490 |
|
2034 | 1491 |
"read the data bits" |
0 | 1492 |
|
2571 | 1493 |
bytesPerRow := ((width * inDepth) + 7) // 8. |
1569 | 1494 |
rawData := ByteArray uninitializedNew:(height * bytesPerRow). |
1495 |
aStream nextBytes:(height * bytesPerRow) into:rawData. |
|
0 | 1496 |
|
1497 |
"read mask" |
|
2034 | 1498 |
|
0 | 1499 |
" |
1500 |
mask := ByteArray new:(width * height / 8). |
|
41 | 1501 |
aStream nextBytes:(width * height / 8) into:mask. |
0 | 1502 |
" |
1503 |
||
1504 |
"stupid: last row first" |
|
1505 |
||
3 | 1506 |
tmp := ByteArray uninitializedNew:(height * bytesPerRow). |
0 | 1507 |
srcIndex := 1. |
1508 |
dstIndex := (height - 1) * bytesPerRow + 1. |
|
1509 |
1 to:height do:[:row | |
|
3404 | 1510 |
tmp replaceFrom:dstIndex to:(dstIndex + bytesPerRow - 1) |
1511 |
with:rawData startingAt:srcIndex. |
|
1512 |
srcIndex := srcIndex + bytesPerRow. |
|
1513 |
dstIndex := dstIndex - bytesPerRow. |
|
0 | 1514 |
]. |
3568 | 1515 |
|
1516 |
inDepth == 32 ifTrue:[ |
|
1517 |
srcIndex := 1. |
|
1518 |
1 to:height do:[:row | |
|
1519 |
1 to:width do:[:row | |
|
1520 |
|b1 b2 b3 b4| |
|
1521 |
b1 := tmp at:srcIndex. |
|
1522 |
b2 := tmp at:srcIndex+1. |
|
1523 |
b3 := tmp at:srcIndex+2. |
|
1524 |
b4 := tmp at:srcIndex+3. |
|
1525 |
tmp at:srcIndex put:b3. |
|
1526 |
tmp at:srcIndex+2 put:b1. |
|
1527 |
||
1528 |
srcIndex := srcIndex + 4. |
|
1529 |
]. |
|
1530 |
]. |
|
1531 |
]. |
|
1532 |
||
1569 | 1533 |
rawData := tmp. |
0 | 1534 |
|
3404 | 1535 |
nColor > 0 ifTrue:[ |
1536 |
photometric := #palette. |
|
1537 |
samplesPerPixel := 1. |
|
1538 |
bitsPerSample := (Array with:inDepth). |
|
1539 |
] ifFalse:[ |
|
1540 |
inDepth == 32 ifTrue:[ |
|
1541 |
photometric := #rgba. |
|
1542 |
samplesPerPixel := 4. |
|
1543 |
bitsPerSample := #(8 8 8 8). |
|
1544 |
] ifFalse:[ |
|
1545 |
^ self fileFormatError:'unsupported image depth: ' , inDepth printString. |
|
1546 |
] |
|
1547 |
]. |
|
298
cd59c474524a
return .ico files as Depth4Images (not Depth8)
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
1548 |
|
3404 | 1549 |
data := rawData. |
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1550 |
^ self image |
298
cd59c474524a
return .ico files as Depth4Images (not Depth8)
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
1551 |
|
0 | 1552 |
" |
21 | 1553 |
WindowsIconReader new fromWindowsICOFile:'/phys/clam2//LocalLibrary/Images/WIN_icons/ibm.ico'. |
0 | 1554 |
" |
211 | 1555 |
|
2333
a93f1f5392bd
from*Stream: methods return the image
Claus Gittinger <cg@exept.de>
parents:
2034
diff
changeset
|
1556 |
"Modified: / 30-05-2007 / 16:58:11 / cg" |
1805 | 1557 |
! ! |
1558 |
||
1559 |
!WindowsIconReader methodsFor:'writing'! |
|
713 | 1560 |
|
1561 |
save:image onFile:aFileName |
|
1562 |
"save image as BMP file on aFileName. |
|
1563 |
Only depth 1,4,8 and 24 images can be represented in this format." |
|
1564 |
||
1168 | 1565 |
aFileName asFilename suffix asLowercase = 'ico' ifTrue:[ |
1566 |
"/ (image depth == 4 |
|
1567 |
"/ and:[image width == 32 |
|
1568 |
"/ and:[image height == 32]]) ifTrue:[ |
|
2578 | 1569 |
^ self saveICO:image onFile:aFileName. |
1168 | 1570 |
"/ ] |
1571 |
]. |
|
713 | 1572 |
self saveBMP:image onFile:aFileName. |
1573 |
||
1574 |
"Modified: 17.10.1997 / 20:16:53 / cg" |
|
1575 |
! |
|
1576 |
||
2578 | 1577 |
saveBMP:image onFile:fileName |
713 | 1578 |
"save image as BMP file on aFileName. |
1579 |
Only depth 1,4,8 and 24 images can be represented in this format." |
|
1580 |
||
2578 | 1581 |
|depth bhSize biSize biClrUsed biSizeImage bfOffBits rowBytes imgBytesPerRow |
1582 |
bits srcIndex row| |
|
713 | 1583 |
|
1584 |
depth := image depth. |
|
1585 |
width := image width. |
|
1586 |
height := image height. |
|
1587 |
||
1588 |
(#(1 4 8 24) includes:depth) ifFalse:[ |
|
2578 | 1589 |
^ Image cannotRepresentImageSignal |
713 | 1590 |
raiseWith:image |
1591 |
errorString:('BMP format only supports depths 1,4,8 and 24'). |
|
1592 |
]. |
|
1593 |
image mask notNil ifTrue:[ |
|
1594 |
Image informationLostQuerySignal |
|
1595 |
raiseWith:image |
|
1596 |
errorString:('BMP format does not support an imageMask'). |
|
1597 |
]. |
|
1598 |
||
1599 |
bhSize := 14. "# bytes in file header" |
|
2578 | 1600 |
biSize := 40. "info header size in bytes" |
1734 | 1601 |
biClrUsed := (depth >= 24) ifTrue:[0] ifFalse:[1 bitShift: depth]. "No. color table entries" |
713 | 1602 |
bfOffBits := biSize + bhSize + (4*biClrUsed). |
1603 |
"/ bmp aligns rows on a longword boundary |
|
2578 | 1604 |
rowBytes := (((depth min:24) * width + 31) // 32) * 4. |
713 | 1605 |
biSizeImage := height * rowBytes. |
1606 |
||
1607 |
outStream := fileName asFilename writeStream. |
|
1608 |
outStream binary. |
|
1609 |
byteOrder := #lsb. |
|
1610 |
||
1611 |
"Write the file header" |
|
2578 | 1612 |
self writeShort:19778. "bfType = BM" |
713 | 1613 |
self writeLong:(bfOffBits + biSizeImage). "Entire file size in bytes" |
2578 | 1614 |
self writeLong:0. "bfReserved" |
713 | 1615 |
self writeLong:bfOffBits. "Offset of bitmap data from start of hdr (and file)" |
1616 |
||
1617 |
"Write the bitmap info header" |
|
3213 | 1618 |
outStream position: bhSize. |
2578 | 1619 |
self writeLong:biSize. "info header size in bytes" |
1620 |
self writeLong:width. "biWidth" |
|
1621 |
self writeLong:height. "biHeight" |
|
1622 |
self writeShort:1. "biPlanes" |
|
1623 |
self writeShort:(depth min:24). "biBitCount" |
|
1624 |
self writeLong:0. "biCompression" |
|
713 | 1625 |
self writeLong:biSizeImage. "size of image section in bytes" |
2578 | 1626 |
self writeLong:2800. "biXPelsPerMeter" |
1627 |
self writeLong:2800. "biYPelsPerMeter" |
|
713 | 1628 |
self writeLong:biClrUsed. |
2578 | 1629 |
self writeLong:0. "biClrImportant" |
713 | 1630 |
1 to:biClrUsed do:[:i | "Color map" |
1631 |
|clr r g b| |
|
1632 |
||
1633 |
clr := image colorFromValue:i-1. |
|
1634 |
clr isNil ifTrue:[ |
|
1635 |
r := g := b := 0. |
|
1636 |
] ifFalse:[ |
|
1637 |
r := clr redByte. |
|
1638 |
g := clr greenByte. |
|
1639 |
b := clr blueByte. |
|
1640 |
]. |
|
1641 |
||
1642 |
"/ put B,G,R |
|
1643 |
outStream nextPut:b. |
|
1644 |
outStream nextPut:g. |
|
1645 |
outStream nextPut:r. |
|
1646 |
outStream nextPut:0. |
|
1647 |
]. |
|
1648 |
||
1649 |
imgBytesPerRow := image bytesPerRow. |
|
2578 | 1650 |
bits := image bits. |
713 | 1651 |
|
1652 |
||
1653 |
"/ sorry, must extract rows individually |
|
1654 |
"/ (even if alignment is correct), |
|
1655 |
"/ since BMP saves rows bottom-to-top |
|
1656 |
||
1657 |
row := ByteArray new:rowBytes. |
|
1658 |
||
1659 |
srcIndex := 1 + (height * imgBytesPerRow). |
|
1660 |
1 to:height do:[:i | |
|
1661 |
srcIndex := srcIndex - imgBytesPerRow. |
|
2578 | 1662 |
row replaceFrom:1 to:imgBytesPerRow with:bits startingAt:srcIndex. |
1663 |
depth == 24 ifTrue:[ |
|
1507
9bae072abf64
fixed bmp saving 24bit image (red & blue channels exchanged)
Claus Gittinger <cg@exept.de>
parents:
1386
diff
changeset
|
1664 |
"/ stupid must swap red & blue bytes |
2578 | 1665 |
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
|
1666 |
]. |
713 | 1667 |
outStream nextPutAll:row. |
1668 |
]. |
|
1669 |
||
1670 |
outStream close. |
|
1671 |
||
1672 |
" |
|
1673 |
|i| |
|
1674 |
||
1675 |
i := Image fromFile:'bitmaps/SBrowser.xbm'. |
|
1676 |
WindowsIconReader save:i onFile:'test.bmp'. |
|
1677 |
" |
|
2578 | 1678 |
|
713 | 1679 |
" |
2578 | 1680 |
|i i2| |
713 | 1681 |
|
2578 | 1682 |
i := Image fromFile:'../../goodies/bitmaps/gifImages/garfield.gif'. |
1683 |
i inspect. |
|
1684 |
WindowsIconReader save:i onFile:'garfield.bmp'. |
|
1685 |
i2 := Image fromFile:'garfield.bmp'. |
|
1686 |
i2 inspect. |
|
713 | 1687 |
" |
1688 |
||
1689 |
"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
|
1690 |
! |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1691 |
|
2578 | 1692 |
saveICO:image onFile:fileName |
1167
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1693 |
"save image as ICO file on aFileName. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1694 |
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
|
1695 |
|
1233 | 1696 |
|depth biSizeImage rowBytes imgBytesPerRow data srcIndex row| |
1167
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1697 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1698 |
depth := image depth. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1699 |
width := image width. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1700 |
height := image height. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1701 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1702 |
depth ~~ 4 ifTrue:[ |
2812 | 1703 |
^ Image cannotRepresentImageSignal |
1704 |
raiseWith:image |
|
1705 |
errorString:('ICO format only supports depths 4'). |
|
1167
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1706 |
]. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1707 |
(width ~~ 32 or:[height ~~ 32]) ifTrue:[ |
2812 | 1708 |
^ Image cannotRepresentImageSignal |
1709 |
raiseWith:image |
|
1710 |
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
|
1711 |
]. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1712 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1713 |
"/ align rows on a longword boundary |
2812 | 1714 |
rowBytes := ((depth * width + 31) // 32) * 4. |
1167
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1715 |
biSizeImage := height * rowBytes. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1716 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1717 |
outStream := fileName asFilename writeStream. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1718 |
outStream binary. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1719 |
byteOrder := #lsb. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1720 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1721 |
"Write the file header" |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1722 |
outStream nextPutAll:#[0 0 1 0]. "/ ICO magic |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1723 |
self writeShort:1. "/ # of images in file |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1724 |
outStream nextPut:image width. "/ |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1725 |
outStream nextPut:image height. "/ |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1726 |
outStream nextPut:(1 bitShift:image depth). "/ # of colors |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1727 |
outStream nextPutAll:#[0 0 0 0 0 ]. "/ reserved |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1728 |
self writeLong:16rE802. "/ size pixels |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1729 |
self writeLong:16r26. "/ offset in file |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1730 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1731 |
"/ 40 bytes - unknown format |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1732 |
outStream nextPutAll:(ByteArray new:40). |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1733 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1734 |
"/ 16-entry RGB map |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1735 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1736 |
1 to:16 do:[:i | "Color map" |
2812 | 1737 |
|clr r g b| |
1167
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1738 |
|
2812 | 1739 |
clr := image colorFromValue:i-1. |
1740 |
clr isNil ifTrue:[ |
|
1741 |
r := g := b := 0. |
|
1742 |
] ifFalse:[ |
|
1743 |
r := clr redByte. |
|
1744 |
g := clr greenByte. |
|
1745 |
b := clr blueByte. |
|
1746 |
]. |
|
1167
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1747 |
|
2812 | 1748 |
"/ put B,G,R |
1749 |
outStream nextPut:b. |
|
1750 |
outStream nextPut:g. |
|
1751 |
outStream nextPut:r. |
|
1752 |
outStream nextPut:0. |
|
1167
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1753 |
]. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1754 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1755 |
imgBytesPerRow := image bytesPerRow. |
2812 | 1756 |
data := image bits. |
1167
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1757 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1758 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1759 |
"/ sorry, must extract rows individually |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1760 |
"/ (even if alignment is correct), |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1761 |
"/ since ICO saves rows bottom-to-top |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1762 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1763 |
row := ByteArray new:rowBytes. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1764 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1765 |
srcIndex := 1 + (height * imgBytesPerRow). |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1766 |
1 to:height do:[:i | |
2812 | 1767 |
srcIndex := srcIndex - imgBytesPerRow. |
1768 |
row replaceFrom:1 to:imgBytesPerRow with:data startingAt:srcIndex. |
|
1769 |
outStream nextPutAll:row. |
|
1167
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1770 |
]. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1771 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1772 |
"/ the mask ... |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1773 |
image mask isNil ifTrue:[ |
2812 | 1774 |
outStream next:128 put:16rFF |
1167
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1775 |
] ifFalse:[ |
2812 | 1776 |
imgBytesPerRow := image mask bytesPerRow. |
1777 |
data := image mask data. |
|
1778 |
row := ByteArray new:4. |
|
1167
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1779 |
|
2812 | 1780 |
srcIndex := 1 + (height * imgBytesPerRow). |
1781 |
1 to:height do:[:i | |
|
1782 |
srcIndex := srcIndex - imgBytesPerRow. |
|
1783 |
row replaceFrom:1 to:imgBytesPerRow with:data startingAt:srcIndex. |
|
1784 |
outStream nextPutAll:row. |
|
1785 |
]. |
|
1167
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1786 |
]. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1787 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1788 |
outStream close. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1789 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1790 |
" |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1791 |
|i| |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1792 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1793 |
i := Image fromFile:'bitmaps/xpmBitmaps/SmalltalkX_clr.xpm'. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1794 |
i := Depth4Image fromImage:i. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1795 |
i := i magnifiedTo:32@32. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1796 |
WindowsIconReader new saveICO:i onFile:'test.ico'. |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1797 |
" |
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1798 |
|
78084516ae7c
first attempt in saving ICO files.
Claus Gittinger <cg@exept.de>
parents:
1073
diff
changeset
|
1799 |
"Modified: 21.10.1997 / 05:02:02 / cg" |
713 | 1800 |
! ! |
1801 |
||
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1802 |
!WindowsIconReader class methodsFor:'documentation'! |
0 | 1803 |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1804 |
version |
3568 | 1805 |
^ '$Header$' |
2756
e8bf6a023191
halt in: #loadBMPWidth:height:depth:from:into:
Claus Gittinger <cg@exept.de>
parents:
2578
diff
changeset
|
1806 |
! |
e8bf6a023191
halt in: #loadBMPWidth:height:depth:from:into:
Claus Gittinger <cg@exept.de>
parents:
2578
diff
changeset
|
1807 |
|
e8bf6a023191
halt in: #loadBMPWidth:height:depth:from:into:
Claus Gittinger <cg@exept.de>
parents:
2578
diff
changeset
|
1808 |
version_CVS |
3568 | 1809 |
^ '$Header$' |
0 | 1810 |
! ! |
1734 | 1811 |
|
3158 | 1812 |
|
172
ee7d84977c86
use inforPrint instead of errorPrint (these are not fatal errors)
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
1813 |
WindowsIconReader initialize! |