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