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