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