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