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