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