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