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