]>
Commit | Line | Data |
---|---|---|
e65ed37a | 1 | ///////////////////////////////////////////////////////////////////////////// |
8898456d | 2 | // Name: src/common/imagbmp.cpp |
05813ada VS |
3 | // Purpose: wxImage BMP,ICO and CUR handlers |
4 | // Author: Robert Roebling, Chris Elliott | |
e65ed37a | 5 | // RCS-ID: $Id$ |
05813ada | 6 | // Copyright: (c) Robert Roebling, Chris Elliott |
65571936 | 7 | // Licence: wxWindows licence |
e65ed37a RR |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
e65ed37a RR |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #ifdef __BORLANDC__ | |
8898456d | 14 | #pragma hdrstop |
e65ed37a RR |
15 | #endif |
16 | ||
e30285ab | 17 | #if wxUSE_IMAGE |
c96ea657 | 18 | |
5ff14574 PC |
19 | #include "wx/imagbmp.h" |
20 | ||
8898456d | 21 | #ifndef WX_PRECOMP |
57bd4c60 WS |
22 | #ifdef __WXMSW__ |
23 | #include "wx/msw/wrapwin.h" | |
24 | #endif | |
8898456d WS |
25 | #include "wx/log.h" |
26 | #include "wx/app.h" | |
0bca0373 | 27 | #include "wx/bitmap.h" |
5ff14574 PC |
28 | #include "wx/palette.h" |
29 | #include "wx/intl.h" | |
8898456d WS |
30 | #endif |
31 | ||
e65ed37a RR |
32 | #include "wx/filefn.h" |
33 | #include "wx/wfstream.h" | |
1971d23c | 34 | #include "wx/quantize.h" |
e65ed37a RR |
35 | |
36 | // For memcpy | |
37 | #include <string.h> | |
38 | ||
39 | #ifdef __SALFORDC__ | |
40 | #ifdef FAR | |
41 | #undef FAR | |
42 | #endif | |
43 | #endif | |
44 | ||
e65ed37a | 45 | //----------------------------------------------------------------------------- |
05813ada | 46 | // wxBMPHandler |
e65ed37a RR |
47 | //----------------------------------------------------------------------------- |
48 | ||
e65ed37a | 49 | IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler,wxImageHandler) |
e65ed37a | 50 | |
e30285ab VZ |
51 | #if wxUSE_STREAMS |
52 | ||
37b83ca6 | 53 | #ifndef BI_RGB |
35b463c0 VZ |
54 | #define BI_RGB 0 |
55 | #endif | |
56 | ||
57 | #ifndef BI_RLE8 | |
37b83ca6 | 58 | #define BI_RLE8 1 |
35b463c0 VZ |
59 | #endif |
60 | ||
61 | #ifndef BI_RLE4 | |
37b83ca6 VS |
62 | #define BI_RLE4 2 |
63 | #endif | |
64 | ||
65 | #ifndef BI_BITFIELDS | |
66 | #define BI_BITFIELDS 3 | |
67 | #endif | |
68 | ||
69 | #define poffset (line * width * 3 + column * 3) | |
70 | ||
f6bcfd97 BP |
71 | bool wxBMPHandler::SaveFile(wxImage *image, |
72 | wxOutputStream& stream, | |
73 | bool verbose) | |
574c939e | 74 | { |
7beb59f3 | 75 | return SaveDib(image, stream, verbose, true/*IsBmp*/, false/*IsMask*/); |
37b83ca6 VS |
76 | } |
77 | ||
78 | bool wxBMPHandler::SaveDib(wxImage *image, | |
05813ada VS |
79 | wxOutputStream& stream, |
80 | bool verbose, | |
81 | bool IsBmp, | |
82 | bool IsMask) | |
37b83ca6 | 83 | |
f6bcfd97 | 84 | { |
7beb59f3 | 85 | wxCHECK_MSG( image, false, _T("invalid pointer in wxBMPHandler::SaveFile") ); |
f6bcfd97 | 86 | |
05813ada | 87 | if ( !image->Ok() ) |
f6bcfd97 | 88 | { |
574c939e | 89 | if ( verbose ) |
05813ada | 90 | wxLogError(_("BMP: Couldn't save invalid image.")); |
7beb59f3 | 91 | return false; |
f6bcfd97 BP |
92 | } |
93 | ||
1971d23c VZ |
94 | // get the format of the BMP file to save, else use 24bpp |
95 | unsigned format = wxBMP_24BPP; | |
fd94e8aa VS |
96 | if ( image->HasOption(wxIMAGE_OPTION_BMP_FORMAT) ) |
97 | format = image->GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT); | |
1971d23c | 98 | |
574c939e | 99 | wxUint16 bpp; // # of bits per pixel |
1971d23c VZ |
100 | int palette_size; // # of color map entries, ie. 2^bpp colors |
101 | ||
102 | // set the bpp and appropriate palette_size, and do additional checks | |
05813ada | 103 | if ( (format == wxBMP_1BPP) || (format == wxBMP_1BPP_BW) ) |
1971d23c VZ |
104 | { |
105 | bpp = 1; | |
106 | palette_size = 2; | |
107 | } | |
05813ada | 108 | else if ( format == wxBMP_4BPP ) |
1971d23c VZ |
109 | { |
110 | bpp = 4; | |
111 | palette_size = 16; | |
112 | } | |
05813ada VS |
113 | else if ( (format == wxBMP_8BPP) || (format == wxBMP_8BPP_GREY) || |
114 | (format == wxBMP_8BPP_RED) || (format == wxBMP_8BPP_PALETTE) ) | |
1971d23c VZ |
115 | { |
116 | // need to set a wxPalette to use this, HOW TO CHECK IF VALID, SIZE? | |
b11e8fb6 VZ |
117 | if ((format == wxBMP_8BPP_PALETTE) |
118 | #if wxUSE_PALETTE | |
119 | && !image->HasPalette() | |
120 | #endif // wxUSE_PALETTE | |
121 | ) | |
1971d23c | 122 | { |
05813ada | 123 | if ( verbose ) |
dc9b623b | 124 | wxLogError(_("BMP: wxImage doesn't have own wxPalette.")); |
7beb59f3 | 125 | return false; |
1971d23c VZ |
126 | } |
127 | bpp = 8; | |
128 | palette_size = 256; | |
129 | } | |
130 | else // you get 24bpp | |
131 | { | |
132 | format = wxBMP_24BPP; | |
133 | bpp = 24; | |
134 | palette_size = 0; | |
135 | } | |
136 | ||
f6bcfd97 | 137 | unsigned width = image->GetWidth(); |
1971d23c VZ |
138 | unsigned row_padding = (4 - int(width*bpp/8.0) % 4) % 4; // # bytes to pad to dword |
139 | unsigned row_width = int(width * bpp/8.0) + row_padding; // # of bytes per row | |
140 | ||
f6bcfd97 BP |
141 | struct |
142 | { | |
143 | // BitmapHeader: | |
144 | wxUint16 magic; // format magic, always 'BM' | |
145 | wxUint32 filesize; // total file size, inc. headers | |
146 | wxUint32 reserved; // for future use | |
147 | wxUint32 data_offset; // image data offset in the file | |
33ac7e6f | 148 | |
f6bcfd97 BP |
149 | // BitmapInfoHeader: |
150 | wxUint32 bih_size; // 2nd part's size | |
151 | wxUint32 width, height; // bitmap's dimensions | |
152 | wxUint16 planes; // num of planes | |
153 | wxUint16 bpp; // bits per pixel | |
154 | wxUint32 compression; // compression method | |
155 | wxUint32 size_of_bmp; // size of the bitmap | |
156 | wxUint32 h_res, v_res; // image resolution in dpi | |
157 | wxUint32 num_clrs; // number of colors used | |
158 | wxUint32 num_signif_clrs;// number of significant colors | |
159 | } hdr; | |
1971d23c | 160 | |
f6bcfd97 BP |
161 | wxUint32 hdr_size = 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/; |
162 | ||
163 | hdr.magic = wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/); | |
1971d23c VZ |
164 | hdr.filesize = wxUINT32_SWAP_ON_BE( hdr_size + palette_size*4 + |
165 | row_width * image->GetHeight() ); | |
f6bcfd97 | 166 | hdr.reserved = 0; |
1971d23c | 167 | hdr.data_offset = wxUINT32_SWAP_ON_BE(hdr_size + palette_size*4); |
33ac7e6f | 168 | |
f6bcfd97 BP |
169 | hdr.bih_size = wxUINT32_SWAP_ON_BE(hdr_size - 14); |
170 | hdr.width = wxUINT32_SWAP_ON_BE(image->GetWidth()); | |
05813ada | 171 | if ( IsBmp ) |
37b83ca6 | 172 | { |
05813ada | 173 | hdr.height = wxUINT32_SWAP_ON_BE(image->GetHeight()); |
37b83ca6 VS |
174 | } |
175 | else | |
176 | { | |
177 | hdr.height = wxUINT32_SWAP_ON_BE(2 * image->GetHeight()); | |
05813ada | 178 | } |
f6bcfd97 | 179 | hdr.planes = wxUINT16_SWAP_ON_BE(1); // always 1 plane |
1971d23c | 180 | hdr.bpp = wxUINT16_SWAP_ON_BE(bpp); |
f6bcfd97 | 181 | hdr.compression = 0; // RGB uncompressed |
33ac7e6f | 182 | hdr.size_of_bmp = wxUINT32_SWAP_ON_BE(row_width * image->GetHeight()); |
1971d23c VZ |
183 | hdr.h_res = hdr.v_res = wxUINT32_SWAP_ON_BE(72); // 72dpi is standard |
184 | hdr.num_clrs = wxUINT32_SWAP_ON_BE(palette_size); // # colors in colormap | |
185 | hdr.num_signif_clrs = 0; // all colors are significant | |
574c939e | 186 | |
05813ada | 187 | if ( IsBmp ) |
37b83ca6 | 188 | { |
05813ada VS |
189 | if (// VS: looks ugly but compilers tend to do ugly things with structs, |
190 | // like aligning hdr.filesize's ofset to dword :( | |
191 | // VZ: we should add padding then... | |
192 | !stream.Write(&hdr.magic, 2) || | |
193 | !stream.Write(&hdr.filesize, 4) || | |
194 | !stream.Write(&hdr.reserved, 4) || | |
574c939e | 195 | !stream.Write(&hdr.data_offset, 4) |
05813ada VS |
196 | ) |
197 | { | |
198 | if (verbose) | |
199 | wxLogError(_("BMP: Couldn't write the file (Bitmap) header.")); | |
7beb59f3 | 200 | return false; |
05813ada VS |
201 | } |
202 | } | |
203 | if ( !IsMask ) | |
f6bcfd97 | 204 | { |
05813ada VS |
205 | if ( |
206 | !stream.Write(&hdr.bih_size, 4) || | |
207 | !stream.Write(&hdr.width, 4) || | |
208 | !stream.Write(&hdr.height, 4) || | |
209 | !stream.Write(&hdr.planes, 2) || | |
210 | !stream.Write(&hdr.bpp, 2) || | |
211 | !stream.Write(&hdr.compression, 4) || | |
212 | !stream.Write(&hdr.size_of_bmp, 4) || | |
213 | !stream.Write(&hdr.h_res, 4) || | |
214 | !stream.Write(&hdr.v_res, 4) || | |
215 | !stream.Write(&hdr.num_clrs, 4) || | |
216 | !stream.Write(&hdr.num_signif_clrs, 4) | |
217 | ) | |
218 | { | |
219 | if (verbose) | |
220 | wxLogError(_("BMP: Couldn't write the file (BitmapInfo) header.")); | |
7beb59f3 | 221 | return false; |
05813ada | 222 | } |
f6bcfd97 BP |
223 | } |
224 | ||
1971d23c VZ |
225 | wxPalette *palette = NULL; // entries for quantized images |
226 | wxUint8 *rgbquad = NULL; // for the RGBQUAD bytes for the colormap | |
227 | wxImage *q_image = NULL; // destination for quantized image | |
228 | ||
229 | // if <24bpp use quantization to reduce colors for *some* of the formats | |
230 | if ( (format == wxBMP_1BPP) || (format == wxBMP_4BPP) || | |
05813ada | 231 | (format == wxBMP_8BPP) || (format == wxBMP_8BPP_PALETTE) ) |
1971d23c VZ |
232 | { |
233 | // make a new palette and quantize the image | |
234 | if (format != wxBMP_8BPP_PALETTE) | |
235 | { | |
236 | q_image = new wxImage(); | |
237 | ||
238 | // I get a delete error using Quantize when desired colors > 236 | |
239 | int quantize = ((palette_size > 236) ? 236 : palette_size); | |
240 | // fill the destination too, it gives much nicer 4bpp images | |
241 | wxQuantize::Quantize( *image, *q_image, &palette, quantize, 0, | |
242 | wxQUANTIZE_FILL_DESTINATION_IMAGE ); | |
243 | } | |
244 | else | |
245 | { | |
b11e8fb6 | 246 | #if wxUSE_PALETTE |
1971d23c | 247 | palette = new wxPalette(image->GetPalette()); |
b11e8fb6 | 248 | #endif // wxUSE_PALETTE |
1971d23c VZ |
249 | } |
250 | ||
251 | int i; | |
252 | unsigned char r, g, b; | |
253 | rgbquad = new wxUint8 [palette_size*4]; | |
254 | ||
05813ada | 255 | for (i = 0; i < palette_size; i++) |
1971d23c | 256 | { |
b11e8fb6 | 257 | #if wxUSE_PALETTE |
05813ada | 258 | if ( !palette->GetRGB(i, &r, &g, &b) ) |
b11e8fb6 VZ |
259 | #endif // wxUSE_PALETTE |
260 | r = g = b = 0; | |
1971d23c VZ |
261 | |
262 | rgbquad[i*4] = b; | |
263 | rgbquad[i*4+1] = g; | |
264 | rgbquad[i*4+2] = r; | |
265 | rgbquad[i*4+3] = 0; | |
266 | } | |
267 | } | |
268 | // make a 256 entry greyscale colormap or 2 entry black & white | |
05813ada VS |
269 | else if ( (format == wxBMP_8BPP_GREY) || (format == wxBMP_8BPP_RED) || |
270 | (format == wxBMP_1BPP_BW) ) | |
1971d23c | 271 | { |
1971d23c VZ |
272 | rgbquad = new wxUint8 [palette_size*4]; |
273 | ||
0ce5a3a8 | 274 | for ( int i = 0; i < palette_size; i++ ) |
1971d23c | 275 | { |
0ce5a3a8 | 276 | // if 1BPP_BW then the value should be either 0 or 255 |
11846af3 | 277 | wxUint8 c = (wxUint8)((i > 0) && (format == wxBMP_1BPP_BW) ? 255 : i); |
0ce5a3a8 VZ |
278 | |
279 | rgbquad[i*4] = | |
280 | rgbquad[i*4+1] = | |
281 | rgbquad[i*4+2] = c; | |
1971d23c VZ |
282 | rgbquad[i*4+3] = 0; |
283 | } | |
284 | } | |
285 | ||
286 | // if the colormap was made, then it needs to be written | |
287 | if (rgbquad) | |
288 | { | |
05813ada | 289 | if ( !IsMask ) |
1971d23c | 290 | { |
05813ada VS |
291 | if ( !stream.Write(rgbquad, palette_size*4) ) |
292 | { | |
293 | if (verbose) | |
294 | wxLogError(_("BMP: Couldn't write RGB color map.")); | |
295 | delete[] rgbquad; | |
b11e8fb6 | 296 | #if wxUSE_PALETTE |
05813ada | 297 | delete palette; |
b11e8fb6 | 298 | #endif // wxUSE_PALETTE |
05813ada | 299 | delete q_image; |
7beb59f3 | 300 | return false; |
05813ada VS |
301 | } |
302 | } | |
1971d23c VZ |
303 | delete []rgbquad; |
304 | } | |
305 | ||
306 | // pointer to the image data, use quantized if available | |
f6bcfd97 | 307 | wxUint8 *data = (wxUint8*) image->GetData(); |
1971d23c VZ |
308 | if (q_image) if (q_image->Ok()) data = (wxUint8*) q_image->GetData(); |
309 | ||
f6bcfd97 | 310 | wxUint8 *buffer = new wxUint8[row_width]; |
f6bcfd97 BP |
311 | memset(buffer, 0, row_width); |
312 | int y; unsigned x; | |
1971d23c | 313 | long int pixel; |
f6bcfd97 | 314 | |
05813ada | 315 | for (y = image->GetHeight() -1; y >= 0; y--) |
f6bcfd97 | 316 | { |
05813ada | 317 | if ( format == wxBMP_24BPP ) // 3 bytes per pixel red,green,blue |
1971d23c | 318 | { |
05813ada | 319 | for ( x = 0; x < width; x++ ) |
1971d23c VZ |
320 | { |
321 | pixel = 3*(y*width + x); | |
322 | ||
323 | buffer[3*x ] = data[pixel+2]; | |
324 | buffer[3*x + 1] = data[pixel+1]; | |
325 | buffer[3*x + 2] = data[pixel]; | |
326 | } | |
327 | } | |
328 | else if ((format == wxBMP_8BPP) || // 1 byte per pixel in color | |
329 | (format == wxBMP_8BPP_PALETTE)) | |
330 | { | |
331 | for (x = 0; x < width; x++) | |
332 | { | |
333 | pixel = 3*(y*width + x); | |
b11e8fb6 | 334 | #if wxUSE_PALETTE |
9a431f4d WS |
335 | buffer[x] = (wxUint8)palette->GetPixel( data[pixel], |
336 | data[pixel+1], | |
337 | data[pixel+2] ); | |
b11e8fb6 VZ |
338 | #else |
339 | // FIXME: what should this be? use some std palette maybe? | |
340 | buffer[x] = 0; | |
341 | #endif // wxUSE_PALETTE | |
1971d23c VZ |
342 | } |
343 | } | |
05813ada | 344 | else if ( format == wxBMP_8BPP_GREY ) // 1 byte per pix, rgb ave to grey |
1971d23c VZ |
345 | { |
346 | for (x = 0; x < width; x++) | |
347 | { | |
348 | pixel = 3*(y*width + x); | |
349 | buffer[x] = (wxUint8)(.299*data[pixel] + | |
350 | .587*data[pixel+1] + | |
351 | .114*data[pixel+2]); | |
352 | } | |
353 | } | |
05813ada | 354 | else if ( format == wxBMP_8BPP_RED ) // 1 byte per pixel, red as greys |
f6bcfd97 | 355 | { |
1971d23c VZ |
356 | for (x = 0; x < width; x++) |
357 | { | |
358 | buffer[x] = (wxUint8)data[3*(y*width + x)]; | |
359 | } | |
360 | } | |
05813ada | 361 | else if ( format == wxBMP_4BPP ) // 4 bpp in color |
1971d23c VZ |
362 | { |
363 | for (x = 0; x < width; x+=2) | |
364 | { | |
365 | pixel = 3*(y*width + x); | |
366 | ||
367 | // fill buffer, ignore if > width | |
b11e8fb6 | 368 | #if wxUSE_PALETTE |
9a431f4d | 369 | buffer[x/2] = (wxUint8)( |
b11e8fb6 VZ |
370 | ((wxUint8)palette->GetPixel(data[pixel], |
371 | data[pixel+1], | |
372 | data[pixel+2]) << 4) | | |
373 | (((x+1) > width) | |
374 | ? 0 | |
375 | : ((wxUint8)palette->GetPixel(data[pixel+3], | |
376 | data[pixel+4], | |
9a431f4d | 377 | data[pixel+5]) )) ); |
b11e8fb6 VZ |
378 | #else |
379 | // FIXME: what should this be? use some std palette maybe? | |
380 | buffer[x/2] = 0; | |
381 | #endif // wxUSE_PALETTE | |
1971d23c VZ |
382 | } |
383 | } | |
05813ada | 384 | else if ( format == wxBMP_1BPP ) // 1 bpp in "color" |
1971d23c VZ |
385 | { |
386 | for (x = 0; x < width; x+=8) | |
387 | { | |
388 | pixel = 3*(y*width + x); | |
389 | ||
b11e8fb6 | 390 | #if wxUSE_PALETTE |
9a431f4d WS |
391 | buffer[x/8] = (wxUint8)( |
392 | ((wxUint8)palette->GetPixel(data[pixel], data[pixel+1], data[pixel+2]) << 7) | | |
1971d23c VZ |
393 | (((x+1) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+3], data[pixel+4], data[pixel+5]) << 6)) | |
394 | (((x+2) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+6], data[pixel+7], data[pixel+8]) << 5)) | | |
395 | (((x+3) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+9], data[pixel+10], data[pixel+11]) << 4)) | | |
396 | (((x+4) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+12], data[pixel+13], data[pixel+14]) << 3)) | | |
397 | (((x+5) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+15], data[pixel+16], data[pixel+17]) << 2)) | | |
398 | (((x+6) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+18], data[pixel+19], data[pixel+20]) << 1)) | | |
9a431f4d | 399 | (((x+7) > width) ? 0 : ((wxUint8)palette->GetPixel(data[pixel+21], data[pixel+22], data[pixel+23]) )) ); |
b11e8fb6 VZ |
400 | #else |
401 | // FIXME: what should this be? use some std palette maybe? | |
402 | buffer[x/8] = 0; | |
403 | #endif // wxUSE_PALETTE | |
1971d23c VZ |
404 | } |
405 | } | |
05813ada | 406 | else if ( format == wxBMP_1BPP_BW ) // 1 bpp B&W colormap from red color ONLY |
1971d23c VZ |
407 | { |
408 | for (x = 0; x < width; x+=8) | |
409 | { | |
410 | pixel = 3*(y*width + x); | |
411 | ||
9a431f4d | 412 | buffer[x/8] = (wxUint8)( |
05813ada VS |
413 | (((wxUint8)(data[pixel] /128.)) << 7) | |
414 | (((x+1) > width) ? 0 : (((wxUint8)(data[pixel+3] /128.)) << 6)) | | |
415 | (((x+2) > width) ? 0 : (((wxUint8)(data[pixel+6] /128.)) << 5)) | | |
416 | (((x+3) > width) ? 0 : (((wxUint8)(data[pixel+9] /128.)) << 4)) | | |
417 | (((x+4) > width) ? 0 : (((wxUint8)(data[pixel+12]/128.)) << 3)) | | |
418 | (((x+5) > width) ? 0 : (((wxUint8)(data[pixel+15]/128.)) << 2)) | | |
419 | (((x+6) > width) ? 0 : (((wxUint8)(data[pixel+18]/128.)) << 1)) | | |
9a431f4d | 420 | (((x+7) > width) ? 0 : (((wxUint8)(data[pixel+21]/128.)) )) ); |
1971d23c | 421 | } |
f6bcfd97 | 422 | } |
33ac7e6f | 423 | |
05813ada | 424 | if ( !stream.Write(buffer, row_width) ) |
f6bcfd97 BP |
425 | { |
426 | if (verbose) | |
427 | wxLogError(_("BMP: Couldn't write data.")); | |
428 | delete[] buffer; | |
b11e8fb6 VZ |
429 | #if wxUSE_PALETTE |
430 | delete palette; | |
431 | #endif // wxUSE_PALETTE | |
432 | delete q_image; | |
7beb59f3 | 433 | return false; |
f6bcfd97 BP |
434 | } |
435 | } | |
436 | delete[] buffer; | |
b11e8fb6 VZ |
437 | #if wxUSE_PALETTE |
438 | delete palette; | |
439 | #endif // wxUSE_PALETTE | |
440 | delete q_image; | |
f6bcfd97 | 441 | |
7beb59f3 | 442 | return true; |
f6bcfd97 BP |
443 | } |
444 | ||
445 | ||
05813ada VS |
446 | typedef struct |
447 | { | |
448 | unsigned char r, g, b; | |
449 | } _cmap; | |
f6bcfd97 | 450 | |
574c939e | 451 | bool wxBMPHandler::DoLoadDib(wxImage * image, int width, int height, |
05813ada | 452 | int bpp, int ncolors, int comp, |
1b941f2d | 453 | wxFileOffset bmpOffset, wxInputStream& stream, |
05813ada VS |
454 | bool verbose, bool IsBmp, bool hasPalette) |
455 | { | |
52b64b0a RR |
456 | wxInt32 aDword, rmask = 0, gmask = 0, bmask = 0; |
457 | int rshift = 0, gshift = 0, bshift = 0; | |
9042a017 | 458 | int rbits = 0, gbits = 0, bbits = 0; |
52b64b0a RR |
459 | wxInt32 dbuf[4]; |
460 | wxInt8 bbuf[4]; | |
461 | wxUint8 aByte; | |
462 | wxUint16 aWord; | |
31528cd3 | 463 | |
05813ada | 464 | // allocate space for palette if needed: |
999836aa | 465 | _cmap *cmap; |
31528cd3 | 466 | |
05813ada | 467 | if ( bpp < 16 ) |
e65ed37a | 468 | { |
05813ada VS |
469 | cmap = new _cmap[ncolors]; |
470 | if ( !cmap ) | |
e65ed37a | 471 | { |
58c837a4 | 472 | if (verbose) |
05813ada | 473 | wxLogError(_("BMP: Couldn't allocate memory.")); |
7beb59f3 | 474 | return false; |
e65ed37a RR |
475 | } |
476 | } | |
477 | else | |
478 | cmap = NULL; | |
479 | ||
05813ada | 480 | // destroy existing here instead of: |
52b64b0a | 481 | image->Destroy(); |
05813ada | 482 | image->Create(width, height); |
574c939e | 483 | |
e65ed37a | 484 | unsigned char *ptr = image->GetData(); |
05813ada VS |
485 | |
486 | if ( !ptr ) | |
e65ed37a | 487 | { |
05813ada VS |
488 | if ( verbose ) |
489 | wxLogError( _("BMP: Couldn't allocate memory.") ); | |
490 | if ( cmap ) | |
491 | delete[] cmap; | |
7beb59f3 | 492 | return false; |
e65ed37a | 493 | } |
05813ada VS |
494 | |
495 | // Reading the palette, if it exists: | |
496 | if ( bpp < 16 && ncolors != 0 ) | |
e65ed37a | 497 | { |
3f4fc796 JS |
498 | unsigned char* r = new unsigned char[ncolors]; |
499 | unsigned char* g = new unsigned char[ncolors]; | |
500 | unsigned char* b = new unsigned char[ncolors]; | |
e65ed37a RR |
501 | for (int j = 0; j < ncolors; j++) |
502 | { | |
52b64b0a RR |
503 | if (hasPalette) |
504 | { | |
05813ada VS |
505 | stream.Read(bbuf, 4); |
506 | cmap[j].b = bbuf[0]; | |
507 | cmap[j].g = bbuf[1]; | |
508 | cmap[j].r = bbuf[2]; | |
509 | ||
510 | r[j] = cmap[j].r; | |
511 | g[j] = cmap[j].g; | |
512 | b[j] = cmap[j].b; | |
513 | } | |
52b64b0a RR |
514 | else |
515 | { | |
516 | //used in reading .ico file mask | |
9a431f4d WS |
517 | r[j] = cmap[j].r = |
518 | g[j] = cmap[j].g = | |
519 | b[j] = cmap[j].b = ( j ? 255 : 0 ); | |
52b64b0a RR |
520 | } |
521 | } | |
b11e8fb6 VZ |
522 | |
523 | #if wxUSE_PALETTE | |
3f4fc796 JS |
524 | // Set the palette for the wxImage |
525 | image->SetPalette(wxPalette(ncolors, r, g, b)); | |
b11e8fb6 | 526 | #endif // wxUSE_PALETTE |
3f4fc796 JS |
527 | |
528 | delete[] r; | |
529 | delete[] g; | |
530 | delete[] b; | |
e65ed37a | 531 | } |
05813ada | 532 | else if ( bpp == 16 || bpp == 32 ) |
e65ed37a | 533 | { |
05813ada | 534 | if ( comp == BI_BITFIELDS ) |
e65ed37a RR |
535 | { |
536 | int bit = 0; | |
05813ada | 537 | stream.Read(dbuf, 4 * 3); |
9042a017 | 538 | rmask = wxINT32_SWAP_ON_BE(dbuf[0]); |
05813ada | 539 | gmask = wxINT32_SWAP_ON_BE(dbuf[1]); |
9042a017 VZ |
540 | bmask = wxINT32_SWAP_ON_BE(dbuf[2]); |
541 | // find shift amount (Least significant bit of mask) | |
542 | for (bit = bpp-1; bit>=0; bit--) | |
e65ed37a RR |
543 | { |
544 | if (bmask & (1 << bit)) | |
545 | bshift = bit; | |
546 | if (gmask & (1 << bit)) | |
547 | gshift = bit; | |
548 | if (rmask & (1 << bit)) | |
549 | rshift = bit; | |
550 | } | |
9042a017 VZ |
551 | // Find number of bits in mask (MSB-LSB+1) |
552 | for (bit = 0; bit < bpp; bit++) | |
553 | { | |
554 | if (bmask & (1 << bit)) | |
555 | bbits = bit-bshift+1; | |
556 | if (gmask & (1 << bit)) | |
557 | gbits = bit-gshift+1; | |
558 | if (rmask & (1 << bit)) | |
559 | rbits = bit-rshift+1; | |
560 | } | |
e65ed37a | 561 | } |
05813ada | 562 | else if ( bpp == 16 ) |
e65ed37a RR |
563 | { |
564 | rmask = 0x7C00; | |
565 | gmask = 0x03E0; | |
566 | bmask = 0x001F; | |
567 | rshift = 10; | |
568 | gshift = 5; | |
569 | bshift = 0; | |
9042a017 VZ |
570 | rbits = 5; |
571 | gbits = 5; | |
572 | bbits = 5; | |
e65ed37a | 573 | } |
05813ada | 574 | else if ( bpp == 32 ) |
e65ed37a RR |
575 | { |
576 | rmask = 0x00FF0000; | |
577 | gmask = 0x0000FF00; | |
578 | bmask = 0x000000FF; | |
579 | rshift = 16; | |
580 | gshift = 8; | |
581 | bshift = 0; | |
9042a017 VZ |
582 | rbits = 8; |
583 | gbits = 8; | |
584 | bbits = 8; | |
e65ed37a RR |
585 | } |
586 | } | |
587 | ||
588 | /* | |
589 | * Reading the image data | |
590 | */ | |
05813ada VS |
591 | if ( IsBmp ) |
592 | stream.SeekI(bmpOffset); // else icon, just carry on | |
52b64b0a | 593 | |
e65ed37a RR |
594 | unsigned char *data = ptr; |
595 | ||
596 | /* set the whole image to the background color */ | |
05813ada | 597 | if ( bpp < 16 && (comp == BI_RLE4 || comp == BI_RLE8) ) |
e65ed37a RR |
598 | { |
599 | for (int i = 0; i < width * height; i++) | |
600 | { | |
601 | *ptr++ = cmap[0].r; | |
602 | *ptr++ = cmap[0].g; | |
603 | *ptr++ = cmap[0].b; | |
604 | } | |
605 | ptr = data; | |
606 | } | |
31528cd3 | 607 | |
e65ed37a RR |
608 | int linesize = ((width * bpp + 31) / 32) * 4; |
609 | ||
610 | /* BMPs are stored upside down */ | |
999836aa | 611 | for ( int line = (height - 1); line >= 0; line-- ) |
e65ed37a RR |
612 | { |
613 | int linepos = 0; | |
999836aa | 614 | for ( int column = 0; column < width ; ) |
e65ed37a | 615 | { |
05813ada | 616 | if ( bpp < 16 ) |
e65ed37a | 617 | { |
e65ed37a RR |
618 | linepos++; |
619 | aByte = stream.GetC(); | |
05813ada | 620 | if ( bpp == 1 ) |
e65ed37a | 621 | { |
999836aa | 622 | for (int bit = 0; bit < 8 && column < width; bit++) |
e65ed37a | 623 | { |
999836aa | 624 | int index = ((aByte & (0x80 >> bit)) ? 1 : 0); |
e65ed37a RR |
625 | ptr[poffset] = cmap[index].r; |
626 | ptr[poffset + 1] = cmap[index].g; | |
627 | ptr[poffset + 2] = cmap[index].b; | |
628 | column++; | |
629 | } | |
630 | } | |
05813ada | 631 | else if ( bpp == 4 ) |
e65ed37a | 632 | { |
05813ada | 633 | if ( comp == BI_RLE4 ) |
e65ed37a | 634 | { |
2b5f62a0 VZ |
635 | wxUint8 first; |
636 | first = aByte; | |
637 | aByte = stream.GetC(); | |
638 | if ( first == 0 ) | |
639 | { | |
640 | if ( aByte == 0 ) | |
641 | { | |
642 | if ( column > 0 ) | |
643 | column = width; | |
644 | } | |
645 | else if ( aByte == 1 ) | |
646 | { | |
647 | column = width; | |
648 | line = -1; | |
649 | } | |
650 | else if ( aByte == 2 ) | |
651 | { | |
652 | aByte = stream.GetC(); | |
653 | column += aByte; | |
654 | linepos = column * bpp / 4; | |
655 | aByte = stream.GetC(); | |
656 | line -= aByte; // upside down | |
657 | } | |
658 | else | |
659 | { | |
660 | int absolute = aByte; | |
661 | wxUint8 nibble[2] ; | |
662 | int readBytes = 0 ; | |
663 | for (int k = 0; k < absolute; k++) | |
664 | { | |
665 | if ( !(k % 2 ) ) | |
666 | { | |
667 | ++readBytes ; | |
668 | aByte = stream.GetC(); | |
9a431f4d WS |
669 | nibble[0] = (wxUint8)( (aByte & 0xF0) >> 4 ) ; |
670 | nibble[1] = (wxUint8)( aByte & 0x0F ) ; | |
2b5f62a0 VZ |
671 | } |
672 | ptr[poffset ] = cmap[nibble[k%2]].r; | |
673 | ptr[poffset + 1] = cmap[nibble[k%2]].g; | |
674 | ptr[poffset + 2] = cmap[nibble[k%2]].b; | |
675 | column++; | |
676 | if ( k % 2 ) | |
677 | linepos++; | |
678 | } | |
679 | if ( readBytes & 0x01 ) | |
680 | aByte = stream.GetC(); | |
681 | } | |
682 | } | |
683 | else | |
684 | { | |
685 | wxUint8 nibble[2] ; | |
9a431f4d WS |
686 | nibble[0] = (wxUint8)( (aByte & 0xF0) >> 4 ) ; |
687 | nibble[1] = (wxUint8)( aByte & 0x0F ) ; | |
2b5f62a0 VZ |
688 | |
689 | for ( int l = 0; l < first && column < width; l++ ) | |
690 | { | |
691 | ptr[poffset ] = cmap[nibble[l%2]].r; | |
692 | ptr[poffset + 1] = cmap[nibble[l%2]].g; | |
693 | ptr[poffset + 2] = cmap[nibble[l%2]].b; | |
694 | column++; | |
695 | if ( l % 2 ) | |
696 | linepos++; | |
697 | } | |
698 | } | |
e65ed37a RR |
699 | } |
700 | else | |
701 | { | |
999836aa | 702 | for (int nibble = 0; nibble < 2 && column < width; nibble++) |
e65ed37a | 703 | { |
999836aa | 704 | int index = ((aByte & (0xF0 >> nibble * 4)) >> (!nibble * 4)); |
05813ada | 705 | if ( index >= 16 ) |
e65ed37a RR |
706 | index = 15; |
707 | ptr[poffset] = cmap[index].r; | |
708 | ptr[poffset + 1] = cmap[index].g; | |
709 | ptr[poffset + 2] = cmap[index].b; | |
710 | column++; | |
711 | } | |
712 | } | |
713 | } | |
05813ada | 714 | else if ( bpp == 8 ) |
e65ed37a | 715 | { |
05813ada | 716 | if ( comp == BI_RLE8 ) |
e65ed37a RR |
717 | { |
718 | unsigned char first; | |
719 | first = aByte; | |
720 | aByte = stream.GetC(); | |
05813ada | 721 | if ( first == 0 ) |
e65ed37a | 722 | { |
05813ada | 723 | if ( aByte == 0 ) |
e65ed37a RR |
724 | { |
725 | /* column = width; */ | |
726 | } | |
05813ada | 727 | else if ( aByte == 1 ) |
e65ed37a RR |
728 | { |
729 | column = width; | |
730 | line = -1; | |
731 | } | |
05813ada | 732 | else if ( aByte == 2 ) |
e65ed37a RR |
733 | { |
734 | aByte = stream.GetC(); | |
735 | column += aByte; | |
736 | linepos = column * bpp / 8; | |
737 | aByte = stream.GetC(); | |
738 | line += aByte; | |
739 | } | |
740 | else | |
741 | { | |
742 | int absolute = aByte; | |
743 | for (int k = 0; k < absolute; k++) | |
744 | { | |
745 | linepos++; | |
746 | aByte = stream.GetC(); | |
747 | ptr[poffset ] = cmap[aByte].r; | |
748 | ptr[poffset + 1] = cmap[aByte].g; | |
749 | ptr[poffset + 2] = cmap[aByte].b; | |
750 | column++; | |
751 | } | |
05813ada | 752 | if ( absolute & 0x01 ) |
e65ed37a RR |
753 | aByte = stream.GetC(); |
754 | } | |
755 | } | |
756 | else | |
757 | { | |
05813ada | 758 | for ( int l = 0; l < first && column < width; l++ ) |
e65ed37a RR |
759 | { |
760 | ptr[poffset ] = cmap[aByte].r; | |
761 | ptr[poffset + 1] = cmap[aByte].g; | |
762 | ptr[poffset + 2] = cmap[aByte].b; | |
763 | column++; | |
764 | linepos++; | |
765 | } | |
766 | } | |
767 | } | |
768 | else | |
769 | { | |
770 | ptr[poffset ] = cmap[aByte].r; | |
771 | ptr[poffset + 1] = cmap[aByte].g; | |
772 | ptr[poffset + 2] = cmap[aByte].b; | |
773 | column++; | |
953704c1 | 774 | // linepos += size; seems to be wrong, RR |
e65ed37a RR |
775 | } |
776 | } | |
2b5f62a0 VZ |
777 | } |
778 | else if ( bpp == 24 ) | |
779 | { | |
780 | stream.Read(bbuf, 3); | |
781 | linepos += 3; | |
782 | ptr[poffset ] = (unsigned char)bbuf[2]; | |
783 | ptr[poffset + 1] = (unsigned char)bbuf[1]; | |
784 | ptr[poffset + 2] = (unsigned char)bbuf[0]; | |
785 | column++; | |
786 | } | |
787 | else if ( bpp == 16 ) | |
788 | { | |
789 | unsigned char temp; | |
790 | stream.Read(&aWord, 2); | |
791 | aWord = wxUINT16_SWAP_ON_BE(aWord); | |
792 | linepos += 2; | |
793 | /* use the masks and calculated amonut of shift | |
9042a017 VZ |
794 | to retrieve the color data out of the word. Then |
795 | shift it left by (8 - number of bits) such that | |
796 | the image has the proper dynamic range */ | |
9a431f4d | 797 | temp = (unsigned char)((aWord & rmask) >> rshift << (8-rbits)); |
2b5f62a0 | 798 | ptr[poffset] = temp; |
9a431f4d | 799 | temp = (unsigned char)((aWord & gmask) >> gshift << (8-gbits)); |
2b5f62a0 | 800 | ptr[poffset + 1] = temp; |
9a431f4d | 801 | temp = (unsigned char)((aWord & bmask) >> bshift << (8-bbits)); |
2b5f62a0 VZ |
802 | ptr[poffset + 2] = temp; |
803 | column++; | |
804 | } | |
805 | else | |
806 | { | |
807 | unsigned char temp; | |
808 | stream.Read(&aDword, 4); | |
809 | aDword = wxINT32_SWAP_ON_BE(aDword); | |
810 | linepos += 4; | |
9a431f4d | 811 | temp = (unsigned char)((aDword & rmask) >> rshift); |
2b5f62a0 | 812 | ptr[poffset] = temp; |
9a431f4d | 813 | temp = (unsigned char)((aDword & gmask) >> gshift); |
2b5f62a0 | 814 | ptr[poffset + 1] = temp; |
9a431f4d | 815 | temp = (unsigned char)((aDword & bmask) >> bshift); |
2b5f62a0 VZ |
816 | ptr[poffset + 2] = temp; |
817 | column++; | |
818 | } | |
819 | } | |
820 | while ( (linepos < linesize) && (comp != 1) && (comp != 2) ) | |
821 | { | |
822 | stream.Read(&aByte, 1); | |
823 | linepos += 1; | |
824 | if ( !stream ) | |
825 | break; | |
826 | } | |
827 | } | |
828 | ||
829 | delete[] cmap; | |
830 | ||
7beb59f3 | 831 | image->SetMask(false); |
2b5f62a0 VZ |
832 | |
833 | const wxStreamError err = stream.GetLastError(); | |
834 | return err == wxSTREAM_NO_ERROR || err == wxSTREAM_EOF; | |
52b64b0a RR |
835 | } |
836 | ||
574c939e | 837 | bool wxBMPHandler::LoadDib(wxImage *image, wxInputStream& stream, |
05813ada | 838 | bool verbose, bool IsBmp) |
52b64b0a | 839 | { |
52b64b0a RR |
840 | wxUint16 aWord; |
841 | wxInt32 dbuf[4]; | |
842 | wxInt8 bbuf[4]; | |
52b64b0a | 843 | |
30984dea | 844 | wxFileOffset offset = 0; // keep gcc quiet |
52b64b0a RR |
845 | if ( IsBmp ) |
846 | { | |
847 | // read the header off the .BMP format file | |
848 | ||
849 | offset = stream.TellI(); | |
67c20e13 VZ |
850 | if (offset == wxInvalidOffset) |
851 | offset = 0; | |
52b64b0a | 852 | |
05813ada VS |
853 | stream.Read(bbuf, 2); |
854 | stream.Read(dbuf, 16); | |
52b64b0a RR |
855 | } |
856 | else | |
857 | { | |
05813ada | 858 | stream.Read(dbuf, 4); |
52b64b0a RR |
859 | } |
860 | #if 0 // unused | |
05813ada | 861 | wxInt32 size = wxINT32_SWAP_ON_BE(dbuf[0]); |
52b64b0a | 862 | #endif |
05813ada | 863 | offset = offset + wxINT32_SWAP_ON_BE(dbuf[2]); |
52b64b0a RR |
864 | |
865 | stream.Read(dbuf, 4 * 2); | |
ecae80b2 WS |
866 | int width = wxINT32_SWAP_ON_BE((int)dbuf[0]); |
867 | int height = wxINT32_SWAP_ON_BE((int)dbuf[1]); | |
05813ada | 868 | if ( !IsBmp)height = height / 2; // for icons divide by 2 |
574c939e | 869 | |
05813ada | 870 | if ( width > 32767 ) |
52b64b0a RR |
871 | { |
872 | if (verbose) | |
873 | wxLogError( _("DIB Header: Image width > 32767 pixels for file.") ); | |
7beb59f3 | 874 | return false; |
52b64b0a | 875 | } |
05813ada | 876 | if ( height > 32767 ) |
52b64b0a RR |
877 | { |
878 | if (verbose) | |
879 | wxLogError( _("DIB Header: Image height > 32767 pixels for file.") ); | |
7beb59f3 | 880 | return false; |
52b64b0a RR |
881 | } |
882 | ||
05813ada | 883 | stream.Read(&aWord, 2); |
52b64b0a RR |
884 | /* |
885 | TODO | |
886 | int planes = (int)wxUINT16_SWAP_ON_BE( aWord ); | |
887 | */ | |
05813ada | 888 | stream.Read(&aWord, 2); |
ecae80b2 | 889 | int bpp = wxUINT16_SWAP_ON_BE((int)aWord); |
05813ada | 890 | if ( bpp != 1 && bpp != 4 && bpp != 8 && bpp != 16 && bpp != 24 && bpp != 32 ) |
52b64b0a RR |
891 | { |
892 | if (verbose) | |
893 | wxLogError( _("DIB Header: Unknown bitdepth in file.") ); | |
7beb59f3 | 894 | return false; |
52b64b0a RR |
895 | } |
896 | ||
05813ada | 897 | stream.Read(dbuf, 4 * 4); |
ecae80b2 | 898 | int comp = wxINT32_SWAP_ON_BE((int)dbuf[0]); |
574c939e | 899 | if ( comp != BI_RGB && comp != BI_RLE4 && comp != BI_RLE8 && |
05813ada | 900 | comp != BI_BITFIELDS ) |
52b64b0a RR |
901 | { |
902 | if (verbose) | |
903 | wxLogError( _("DIB Header: Unknown encoding in file.") ); | |
7beb59f3 | 904 | return false; |
52b64b0a RR |
905 | } |
906 | ||
05813ada | 907 | stream.Read(dbuf, 4 * 2); |
ecae80b2 | 908 | int ncolors = wxINT32_SWAP_ON_BE( (int)dbuf[0] ); |
52b64b0a RR |
909 | if (ncolors == 0) |
910 | ncolors = 1 << bpp; | |
911 | /* some more sanity checks */ | |
912 | if (((comp == BI_RLE4) && (bpp != 4)) || | |
913 | ((comp == BI_RLE8) && (bpp != 8)) || | |
914 | ((comp == BI_BITFIELDS) && (bpp != 16 && bpp != 32))) | |
915 | { | |
916 | if (verbose) | |
917 | wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") ); | |
7beb59f3 | 918 | return false; |
52b64b0a RR |
919 | } |
920 | ||
921 | //read DIB; this is the BMP image or the XOR part of an icon image | |
05813ada | 922 | if ( !DoLoadDib(image, width, height, bpp, ncolors, comp, offset, stream, |
7beb59f3 | 923 | verbose, IsBmp, true) ) |
52b64b0a RR |
924 | { |
925 | if (verbose) | |
eab77b59 | 926 | wxLogError( _("Error in reading image DIB.") ); |
7beb59f3 | 927 | return false; |
52b64b0a RR |
928 | } |
929 | ||
930 | if ( !IsBmp ) | |
931 | { | |
932 | //read Icon mask which is monochrome | |
933 | //there is no palette, so we will create one | |
1f5b2017 | 934 | wxImage mask; |
05813ada | 935 | if ( !DoLoadDib(&mask, width, height, 1, 2, BI_RGB, offset, stream, |
7beb59f3 | 936 | verbose, IsBmp, false) ) |
52b64b0a RR |
937 | { |
938 | if (verbose) | |
939 | wxLogError( _("ICO: Error in reading mask DIB.") ); | |
7beb59f3 | 940 | return false; |
52b64b0a | 941 | } |
1f5b2017 | 942 | image->SetMaskFromImage(mask, 255, 255, 255); |
52b64b0a RR |
943 | |
944 | } | |
05813ada | 945 | |
7beb59f3 | 946 | return true; |
e65ed37a RR |
947 | } |
948 | ||
574c939e | 949 | bool wxBMPHandler::LoadFile(wxImage *image, wxInputStream& stream, |
05813ada VS |
950 | bool verbose, int WXUNUSED(index)) |
951 | { | |
952 | // Read a single DIB fom the file: | |
7beb59f3 | 953 | return LoadDib(image, stream, verbose, true/*isBmp*/); |
05813ada | 954 | } |
52b64b0a | 955 | |
05813ada | 956 | bool wxBMPHandler::DoCanRead(wxInputStream& stream) |
52b64b0a | 957 | { |
05813ada VS |
958 | unsigned char hdr[2]; |
959 | ||
79fa2374 | 960 | if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) |
7beb59f3 | 961 | return false; |
79fa2374 | 962 | |
79fa2374 VZ |
963 | // do we have the BMP file signature? |
964 | return hdr[0] == 'B' && hdr[1] == 'M'; | |
52b64b0a RR |
965 | } |
966 | ||
e30285ab VZ |
967 | #endif // wxUSE_STREAMS |
968 | ||
52b64b0a | 969 | |
658974ae | 970 | #if wxUSE_ICO_CUR |
05813ada VS |
971 | //----------------------------------------------------------------------------- |
972 | // wxICOHandler | |
973 | //----------------------------------------------------------------------------- | |
974 | ||
975 | IMPLEMENT_DYNAMIC_CLASS(wxICOHandler, wxBMPHandler) | |
976 | ||
e30285ab VZ |
977 | #if wxUSE_STREAMS |
978 | ||
05813ada VS |
979 | struct ICONDIRENTRY |
980 | { | |
981 | wxUint8 bWidth; // Width of the image | |
982 | wxUint8 bHeight; // Height of the image (times 2) | |
983 | wxUint8 bColorCount; // Number of colors in image (0 if >=8bpp) | |
984 | wxUint8 bReserved; // Reserved | |
985 | ||
986 | // these two are different in icons and cursors: | |
987 | // icon or cursor | |
988 | wxUint16 wPlanes; // Color Planes or XHotSpot | |
989 | wxUint16 wBitCount; // Bits per pixel or YHotSpot | |
990 | ||
991 | wxUint32 dwBytesInRes; // how many bytes in this resource? | |
992 | wxUint32 dwImageOffset; // where in the file is this image | |
993 | }; | |
994 | ||
995 | struct ICONDIR | |
996 | { | |
997 | wxUint16 idReserved; // Reserved | |
998 | wxUint16 idType; // resource type (1 for icons, 2 for cursors) | |
999 | wxUint16 idCount; // how many images? | |
1000 | }; | |
1001 | ||
1002 | ||
1003 | bool wxICOHandler::SaveFile(wxImage *image, | |
1004 | wxOutputStream& stream, | |
1005 | bool verbose) | |
1006 | ||
52b64b0a | 1007 | { |
05813ada VS |
1008 | //sanity check; icon must be less than 127 pixels high and 255 wide |
1009 | if ( image->GetHeight () > 127 ) | |
1010 | { | |
1011 | if ( verbose ) | |
1012 | wxLogError(_("ICO: Image too tall for an icon.")); | |
7beb59f3 | 1013 | return false; |
05813ada VS |
1014 | } |
1015 | if ( image->GetWidth () > 255 ) | |
1016 | { | |
1017 | if ( verbose ) | |
1018 | wxLogError(_("ICO: Image too wide for an icon.")); | |
7beb59f3 | 1019 | return false; |
05813ada VS |
1020 | } |
1021 | ||
1229a1e7 | 1022 | const int images = 1; // only generate one image |
05813ada | 1023 | |
574c939e | 1024 | // VS: This is a hack of sort - since ICO and CUR files are almost |
05813ada VS |
1025 | // identical, we have all the meat in wxICOHandler and check for |
1026 | // the actual (handler) type when the code has to distinguish between | |
1027 | // the two formats | |
1028 | int type = (this->GetType() == wxBITMAP_TYPE_CUR) ? 2 : 1; | |
1029 | ||
1030 | // write a header, (ICONDIR) | |
1031 | // Calculate the header size | |
1032 | wxUint32 offset = 3 * sizeof(wxUint16); | |
1033 | ||
1034 | ICONDIR IconDir; | |
1035 | IconDir.idReserved = 0; | |
ecae80b2 WS |
1036 | IconDir.idType = wxUINT16_SWAP_ON_BE((wxUint16)type); |
1037 | IconDir.idCount = wxUINT16_SWAP_ON_BE((wxUint16)images); | |
05813ada VS |
1038 | stream.Write(&IconDir.idReserved, sizeof(IconDir.idReserved)); |
1039 | stream.Write(&IconDir.idType, sizeof(IconDir.idType)); | |
1040 | stream.Write(&IconDir.idCount, sizeof(IconDir.idCount)); | |
1041 | if ( !stream.IsOk() ) | |
1042 | { | |
1043 | if ( verbose ) | |
1044 | wxLogError(_("ICO: Error writing the image file!")); | |
7beb59f3 | 1045 | return false; |
05813ada VS |
1046 | } |
1047 | ||
1048 | // for each iamage write a description ICONDIRENTRY: | |
1049 | ICONDIRENTRY icondirentry; | |
1229a1e7 | 1050 | for (int img = 0; img < images; img++) |
05813ada VS |
1051 | { |
1052 | wxImage mask; | |
1053 | ||
1054 | if ( image->HasMask() ) | |
574c939e | 1055 | { |
05813ada VS |
1056 | // make another image with black/white: |
1057 | mask = image->ConvertToMono (image->GetMaskRed(), image->GetMaskGreen(), image->GetMaskBlue() ); | |
1058 | ||
1059 | // now we need to change the masked regions to black: | |
1060 | unsigned char r = image->GetMaskRed(); | |
1061 | unsigned char g = image->GetMaskGreen(); | |
1062 | unsigned char b = image->GetMaskBlue(); | |
1063 | if ( (r != 0) || (g != 0) || (b != 0) ) | |
1064 | { | |
1065 | // Go round and apply black to the masked bits: | |
1066 | int i, j; | |
1067 | for (i = 0; i < mask.GetWidth(); i++) | |
1068 | { | |
1069 | for (j = 0; j < mask.GetHeight(); j++) | |
1070 | { | |
574c939e KB |
1071 | if ((r == mask.GetRed(i, j)) && |
1072 | (g == mask.GetGreen(i, j))&& | |
05813ada VS |
1073 | (b == mask.GetBlue(i, j)) ) |
1074 | image->SetRGB(i, j, 0, 0, 0 ); | |
1075 | } | |
1076 | } | |
1077 | } | |
1078 | } | |
1079 | else | |
1080 | { | |
1081 | // just make a black mask all over: | |
1082 | mask = image->Copy(); | |
1229a1e7 WS |
1083 | int i, j; |
1084 | for (i = 0; i < mask.GetWidth(); i++) | |
05813ada | 1085 | for (j = 0; j < mask.GetHeight(); j++) |
1229a1e7 | 1086 | mask.SetRGB(i, j, 0, 0, 0 ); |
05813ada VS |
1087 | } |
1088 | // Set the formats for image and mask | |
1089 | // (Windows never saves with more than 8 colors): | |
fd94e8aa | 1090 | image->SetOption(wxIMAGE_OPTION_BMP_FORMAT, wxBMP_8BPP); |
05813ada VS |
1091 | |
1092 | // monochome bitmap: | |
fd94e8aa | 1093 | mask.SetOption(wxIMAGE_OPTION_BMP_FORMAT, wxBMP_1BPP_BW); |
7beb59f3 WS |
1094 | bool IsBmp = false; |
1095 | bool IsMask = false; | |
05813ada VS |
1096 | |
1097 | //calculate size and offset of image and mask | |
1098 | wxCountingOutputStream cStream; | |
5cb598ae | 1099 | bool bResult = SaveDib(image, cStream, verbose, IsBmp, IsMask); |
05813ada VS |
1100 | if ( !bResult ) |
1101 | { | |
1102 | if ( verbose ) | |
1103 | wxLogError(_("ICO: Error writing the image file!")); | |
7beb59f3 | 1104 | return false; |
05813ada | 1105 | } |
7beb59f3 | 1106 | IsMask = true; |
05813ada VS |
1107 | |
1108 | bResult = SaveDib(&mask, cStream, verbose, IsBmp, IsMask); | |
1109 | if ( !bResult ) | |
1110 | { | |
1111 | if ( verbose ) | |
1112 | wxLogError(_("ICO: Error writing the image file!")); | |
7beb59f3 | 1113 | return false; |
05813ada VS |
1114 | } |
1115 | wxUint32 Size = cStream.GetSize(); | |
1116 | ||
7beb59f3 | 1117 | // wxCountingOutputStream::Ok() always returns true for now and this |
05813ada VS |
1118 | // "if" provokes VC++ warnings in optimized build |
1119 | #if 0 | |
1120 | if ( !cStream.Ok() ) | |
1121 | { | |
1122 | if ( verbose ) | |
1123 | wxLogError(_("ICO: Error writing the image file!")); | |
7beb59f3 | 1124 | return false; |
05813ada VS |
1125 | } |
1126 | #endif // 0 | |
1127 | ||
1128 | offset = offset + sizeof(ICONDIRENTRY); | |
1129 | ||
f12a90c7 DE |
1130 | icondirentry.bWidth = (wxUint8)image->GetWidth(); |
1131 | icondirentry.bHeight = (wxUint8)(2 * image->GetHeight()); | |
05813ada VS |
1132 | icondirentry.bColorCount = 0; |
1133 | icondirentry.bReserved = 0; | |
1134 | icondirentry.wPlanes = wxUINT16_SWAP_ON_BE(1); | |
1135 | icondirentry.wBitCount = wxUINT16_SWAP_ON_BE(wxBMP_8BPP); | |
1136 | if ( type == 2 /*CUR*/) | |
1137 | { | |
fd94e8aa VS |
1138 | int hx = image->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ? |
1139 | image->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) : | |
05813ada | 1140 | image->GetWidth() / 2; |
fd94e8aa VS |
1141 | int hy = image->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ? |
1142 | image->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) : | |
05813ada VS |
1143 | image->GetHeight() / 2; |
1144 | ||
1145 | // actually write the values of the hot spot here: | |
1146 | icondirentry.wPlanes = wxUINT16_SWAP_ON_BE((wxUint16)hx); | |
1147 | icondirentry.wBitCount = wxUINT16_SWAP_ON_BE((wxUint16)hy); | |
1148 | } | |
1149 | icondirentry.dwBytesInRes = wxUINT32_SWAP_ON_BE(Size); | |
1150 | icondirentry.dwImageOffset = wxUINT32_SWAP_ON_BE(offset); | |
1151 | ||
1152 | // increase size to allow for the data written: | |
1153 | offset += Size; | |
1154 | ||
1155 | // write to stream: | |
1156 | stream.Write(&icondirentry.bWidth, sizeof(icondirentry.bWidth)); | |
1157 | stream.Write(&icondirentry.bHeight, sizeof(icondirentry.bHeight)); | |
1158 | stream.Write(&icondirentry.bColorCount, sizeof(icondirentry.bColorCount)); | |
1159 | stream.Write(&icondirentry.bReserved, sizeof(icondirentry.bReserved)); | |
1160 | stream.Write(&icondirentry.wPlanes, sizeof(icondirentry.wPlanes)); | |
1161 | stream.Write(&icondirentry.wBitCount, sizeof(icondirentry.wBitCount)); | |
1162 | stream.Write(&icondirentry.dwBytesInRes, sizeof(icondirentry.dwBytesInRes)); | |
1163 | stream.Write(&icondirentry.dwImageOffset, sizeof(icondirentry.dwImageOffset)); | |
1164 | if ( !stream.IsOk() ) | |
1165 | { | |
1166 | if ( verbose ) | |
1167 | wxLogError(_("ICO: Error writing the image file!")); | |
7beb59f3 | 1168 | return false; |
05813ada VS |
1169 | } |
1170 | ||
1171 | // actually save it: | |
7beb59f3 | 1172 | IsMask = false; |
05813ada VS |
1173 | bResult = SaveDib(image, stream, verbose, IsBmp, IsMask); |
1174 | if ( !bResult ) | |
1175 | { | |
1176 | if ( verbose ) | |
1177 | wxLogError(_("ICO: Error writing the image file!")); | |
7beb59f3 | 1178 | return false; |
05813ada | 1179 | } |
7beb59f3 | 1180 | IsMask = true; |
05813ada VS |
1181 | |
1182 | bResult = SaveDib(&mask, stream, verbose, IsBmp, IsMask); | |
1183 | if ( !bResult ) | |
1184 | { | |
1185 | if ( verbose ) | |
1186 | wxLogError(_("ICO: Error writing the image file!")); | |
7beb59f3 | 1187 | return false; |
05813ada VS |
1188 | } |
1189 | ||
1190 | } // end of for loop | |
1191 | ||
7beb59f3 | 1192 | return true; |
05813ada VS |
1193 | } |
1194 | ||
574c939e | 1195 | bool wxICOHandler::LoadFile(wxImage *image, wxInputStream& stream, |
05813ada | 1196 | bool verbose, int index) |
658974ae VS |
1197 | { |
1198 | stream.SeekI(0); | |
1199 | return DoLoadFile(image, stream, verbose, index); | |
1200 | } | |
1201 | ||
574c939e | 1202 | bool wxICOHandler::DoLoadFile(wxImage *image, wxInputStream& stream, |
5c235ea0 | 1203 | bool WXUNUSED(verbose), int index) |
05813ada | 1204 | { |
5cb598ae WS |
1205 | bool bResult wxDUMMY_INITIALIZE(false); |
1206 | bool IsBmp = false; | |
52b64b0a | 1207 | |
05813ada | 1208 | ICONDIR IconDir; |
658974ae | 1209 | |
1b941f2d | 1210 | wxFileOffset iPos = stream.TellI(); |
05813ada VS |
1211 | stream.Read(&IconDir, sizeof(IconDir)); |
1212 | wxUint16 nIcons = wxUINT16_SWAP_ON_BE(IconDir.idCount); | |
1213 | // nType is 1 for Icons, 2 for Cursors: | |
1214 | wxUint16 nType = wxUINT16_SWAP_ON_BE(IconDir.idType); | |
574c939e | 1215 | |
05813ada VS |
1216 | // loop round the icons and choose the best one: |
1217 | ICONDIRENTRY *pIconDirEntry = new ICONDIRENTRY[nIcons]; | |
1218 | ICONDIRENTRY *pCurrentEntry = pIconDirEntry; | |
1219 | int wMax = 0; | |
1220 | int colmax = 0; | |
1221 | int iSel = wxNOT_FOUND; | |
1222 | ||
1223 | for (int i = 0; i < nIcons; i++ ) | |
52b64b0a RR |
1224 | { |
1225 | stream.Read(pCurrentEntry, sizeof(ICONDIRENTRY)); | |
05813ada VS |
1226 | // bHeight and bColorCount are wxUint8 |
1227 | if ( pCurrentEntry->bWidth >= wMax ) | |
52b64b0a | 1228 | { |
05813ada VS |
1229 | // see if we have more colors, ==0 indicates > 8bpp: |
1230 | if ( pCurrentEntry->bColorCount == 0 ) | |
1231 | pCurrentEntry->bColorCount = 255; | |
1232 | if ( pCurrentEntry->bColorCount >= colmax ) | |
574c939e | 1233 | { |
05813ada VS |
1234 | iSel = i; |
1235 | wMax = pCurrentEntry->bWidth; | |
1236 | colmax = pCurrentEntry->bColorCount; | |
52b64b0a RR |
1237 | } |
1238 | } | |
05813ada VS |
1239 | pCurrentEntry++; |
1240 | } | |
574c939e | 1241 | |
05813ada VS |
1242 | if ( index != -1 ) |
1243 | { | |
1244 | // VS: Note that we *have* to run the loop above even if index != -1, because | |
1245 | // it reads ICONDIRENTRies. | |
1246 | iSel = index; | |
52b64b0a | 1247 | } |
574c939e | 1248 | |
05813ada | 1249 | if ( iSel == wxNOT_FOUND || iSel < 0 || iSel >= nIcons ) |
52b64b0a | 1250 | { |
05813ada | 1251 | wxLogError(_("ICO: Invalid icon index.")); |
5cb598ae | 1252 | bResult = false; |
52b64b0a RR |
1253 | } |
1254 | else | |
1255 | { | |
05813ada VS |
1256 | // seek to selected icon: |
1257 | pCurrentEntry = pIconDirEntry + iSel; | |
658974ae | 1258 | stream.SeekI(iPos + wxUINT32_SWAP_ON_BE(pCurrentEntry->dwImageOffset), wxFromStart); |
7beb59f3 | 1259 | bResult = LoadDib(image, stream, true, IsBmp); |
658974ae VS |
1260 | bool bIsCursorType = (this->GetType() == wxBITMAP_TYPE_CUR) || (this->GetType() == wxBITMAP_TYPE_ANI); |
1261 | if ( bResult && bIsCursorType && nType == 2 ) | |
05813ada VS |
1262 | { |
1263 | // it is a cursor, so let's set the hotspot: | |
fd94e8aa VS |
1264 | image->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, wxUINT16_SWAP_ON_BE(pCurrentEntry->wPlanes)); |
1265 | image->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, wxUINT16_SWAP_ON_BE(pCurrentEntry->wBitCount)); | |
05813ada | 1266 | } |
52b64b0a | 1267 | } |
05813ada VS |
1268 | delete[] pIconDirEntry; |
1269 | return bResult; | |
52b64b0a RR |
1270 | } |
1271 | ||
649d13e8 | 1272 | int wxICOHandler::GetImageCount(wxInputStream& stream) |
0828c087 | 1273 | { |
05813ada | 1274 | ICONDIR IconDir; |
1b941f2d | 1275 | wxFileOffset iPos = stream.TellI(); |
05813ada VS |
1276 | stream.SeekI(0); |
1277 | stream.Read(&IconDir, sizeof(IconDir)); | |
1278 | wxUint16 nIcons = wxUINT16_SWAP_ON_BE(IconDir.idCount); | |
658974ae | 1279 | stream.SeekI(iPos); |
05813ada | 1280 | return (int)nIcons; |
0828c087 VS |
1281 | } |
1282 | ||
05813ada | 1283 | bool wxICOHandler::DoCanRead(wxInputStream& stream) |
52b64b0a | 1284 | { |
2b5f62a0 | 1285 | stream.SeekI(0); |
52b64b0a | 1286 | unsigned char hdr[4]; |
39d16996 | 1287 | if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) |
7beb59f3 | 1288 | return false; |
39d16996 VZ |
1289 | |
1290 | // hdr[2] is one for an icon and two for a cursor | |
1291 | return hdr[0] == '\0' && hdr[1] == '\0' && hdr[2] == '\1' && hdr[3] == '\0'; | |
52b64b0a RR |
1292 | } |
1293 | ||
e30285ab | 1294 | #endif // wxUSE_STREAMS |
e65ed37a | 1295 | |
05813ada VS |
1296 | |
1297 | //----------------------------------------------------------------------------- | |
1298 | // wxCURHandler | |
1299 | //----------------------------------------------------------------------------- | |
1300 | ||
1301 | IMPLEMENT_DYNAMIC_CLASS(wxCURHandler, wxICOHandler) | |
1302 | ||
e30285ab VZ |
1303 | #if wxUSE_STREAMS |
1304 | ||
05813ada VS |
1305 | bool wxCURHandler::DoCanRead(wxInputStream& stream) |
1306 | { | |
8dcfaa40 | 1307 | stream.SeekI(0); |
05813ada | 1308 | unsigned char hdr[4]; |
39d16996 | 1309 | if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) |
7beb59f3 | 1310 | return false; |
39d16996 VZ |
1311 | |
1312 | // hdr[2] is one for an icon and two for a cursor | |
1313 | return hdr[0] == '\0' && hdr[1] == '\0' && hdr[2] == '\2' && hdr[3] == '\0'; | |
05813ada VS |
1314 | } |
1315 | ||
e30285ab VZ |
1316 | #endif // wxUSE_STREAMS |
1317 | ||
658974ae VS |
1318 | //----------------------------------------------------------------------------- |
1319 | // wxANIHandler | |
1320 | //----------------------------------------------------------------------------- | |
1321 | ||
1322 | IMPLEMENT_DYNAMIC_CLASS(wxANIHandler, wxCURHandler) | |
1323 | ||
e30285ab VZ |
1324 | #if wxUSE_STREAMS |
1325 | ||
574c939e | 1326 | bool wxANIHandler::LoadFile(wxImage *image, wxInputStream& stream, |
658974ae VS |
1327 | bool verbose, int index) |
1328 | { | |
574c939e KB |
1329 | wxInt32 FCC1, FCC2; |
1330 | wxUint32 datalen; | |
658974ae | 1331 | |
2b5f62a0 VZ |
1332 | wxInt32 riff32; |
1333 | memcpy( &riff32, "RIFF", 4 ); | |
1334 | wxInt32 list32; | |
1335 | memcpy( &list32, "LIST", 4 ); | |
1336 | wxInt32 ico32; | |
1337 | memcpy( &ico32, "icon", 4 ); | |
658974ae | 1338 | int iIcon = 0; |
574c939e | 1339 | |
658974ae VS |
1340 | stream.SeekI(0); |
1341 | stream.Read(&FCC1, 4); | |
cff81649 | 1342 | if ( FCC1 != riff32 ) |
7beb59f3 | 1343 | return false; |
658974ae VS |
1344 | |
1345 | // we have a riff file: | |
1346 | while (stream.IsOk()) | |
574c939e | 1347 | { |
658974ae VS |
1348 | // we always have a data size |
1349 | stream.Read(&datalen, 4); | |
1af5934b | 1350 | datalen = wxINT32_SWAP_ON_BE(datalen) ; |
8dcfaa40 CE |
1351 | //data should be padded to make even number of bytes |
1352 | if (datalen % 2 == 1) datalen ++ ; | |
658974ae | 1353 | //now either data or a FCC |
cff81649 | 1354 | if ( (FCC1 == riff32) || (FCC1 == list32) ) |
658974ae | 1355 | { |
79fa2374 | 1356 | stream.Read(&FCC2, 4); |
658974ae | 1357 | } |
574c939e | 1358 | else |
658974ae | 1359 | { |
cff81649 | 1360 | if (FCC1 == ico32 && iIcon >= index) |
658974ae VS |
1361 | { |
1362 | return DoLoadFile(image, stream, verbose, -1); | |
574c939e | 1363 | } |
658974ae VS |
1364 | else |
1365 | { | |
1366 | stream.SeekI(stream.TellI() + datalen); | |
cff81649 | 1367 | if ( FCC1 == ico32 ) |
658974ae VS |
1368 | iIcon ++; |
1369 | } | |
1370 | } | |
574c939e | 1371 | |
658974ae VS |
1372 | // try to read next data chunk: |
1373 | stream.Read(&FCC1, 4); | |
1374 | } | |
7beb59f3 | 1375 | return false; |
658974ae VS |
1376 | } |
1377 | ||
1378 | bool wxANIHandler::DoCanRead(wxInputStream& stream) | |
1379 | { | |
574c939e KB |
1380 | wxInt32 FCC1, FCC2; |
1381 | wxUint32 datalen ; | |
7beb59f3 | 1382 | |
2b5f62a0 VZ |
1383 | wxInt32 riff32; |
1384 | memcpy( &riff32, "RIFF", 4 ); | |
1385 | wxInt32 list32; | |
1386 | memcpy( &list32, "LIST", 4 ); | |
1387 | wxInt32 ico32; | |
7beb59f3 | 1388 | memcpy( &ico32, "icon", 4 ); |
2b5f62a0 VZ |
1389 | wxInt32 anih32; |
1390 | memcpy( &anih32, "anih", 4 ); | |
7beb59f3 | 1391 | |
8dcfaa40 | 1392 | stream.SeekI(0); |
39d16996 | 1393 | if ( !stream.Read(&FCC1, 4) ) |
7beb59f3 | 1394 | return false; |
39d16996 | 1395 | |
cff81649 | 1396 | if ( FCC1 != riff32 ) |
7beb59f3 | 1397 | return false; |
658974ae VS |
1398 | |
1399 | // we have a riff file: | |
1400 | while ( stream.IsOk() ) | |
1401 | { | |
cff81649 | 1402 | if ( FCC1 == anih32 ) |
7beb59f3 | 1403 | return true; |
658974ae VS |
1404 | // we always have a data size: |
1405 | stream.Read(&datalen, 4); | |
79fa2374 | 1406 | datalen = wxINT32_SWAP_ON_BE(datalen) ; |
8dcfaa40 CE |
1407 | //data should be padded to make even number of bytes |
1408 | if (datalen % 2 == 1) datalen ++ ; | |
658974ae | 1409 | // now either data or a FCC: |
cff81649 | 1410 | if ( (FCC1 == riff32) || (FCC1 == list32) ) |
658974ae | 1411 | { |
79fa2374 | 1412 | stream.Read(&FCC2, 4); |
658974ae | 1413 | } |
574c939e | 1414 | else |
658974ae VS |
1415 | { |
1416 | stream.SeekI(stream.TellI() + datalen); | |
1417 | } | |
574c939e | 1418 | |
658974ae | 1419 | // try to read next data chunk: |
79fa2374 VZ |
1420 | if ( !stream.Read(&FCC1, 4) ) |
1421 | { | |
1422 | // reading failed -- either EOF or IO error, bail out anyhow | |
7beb59f3 | 1423 | return false; |
79fa2374 | 1424 | } |
658974ae VS |
1425 | } |
1426 | ||
7beb59f3 | 1427 | return false; |
658974ae VS |
1428 | } |
1429 | ||
1430 | int wxANIHandler::GetImageCount(wxInputStream& stream) | |
1431 | { | |
574c939e KB |
1432 | wxInt32 FCC1, FCC2; |
1433 | wxUint32 datalen ; | |
574c939e | 1434 | |
2b5f62a0 VZ |
1435 | wxInt32 riff32; |
1436 | memcpy( &riff32, "RIFF", 4 ); | |
1437 | wxInt32 list32; | |
1438 | memcpy( &list32, "LIST", 4 ); | |
1439 | wxInt32 ico32; | |
1440 | memcpy( &ico32, "icon", 4 ); | |
1441 | wxInt32 anih32; | |
1442 | memcpy( &anih32, "anih", 4 ); | |
7beb59f3 | 1443 | |
658974ae VS |
1444 | stream.SeekI(0); |
1445 | stream.Read(&FCC1, 4); | |
cff81649 | 1446 | if ( FCC1 != riff32 ) |
658974ae VS |
1447 | return wxNOT_FOUND; |
1448 | ||
1449 | // we have a riff file: | |
1450 | while ( stream.IsOk() ) | |
1451 | { | |
1452 | // we always have a data size: | |
1453 | stream.Read(&datalen, 4); | |
1af5934b | 1454 | datalen = wxINT32_SWAP_ON_BE(datalen) ; |
8dcfaa40 CE |
1455 | //data should be padded to make even number of bytes |
1456 | if (datalen % 2 == 1) datalen ++ ; | |
658974ae | 1457 | // now either data or a FCC: |
cff81649 | 1458 | if ( (FCC1 == riff32) || (FCC1 == list32) ) |
79fa2374 VZ |
1459 | { |
1460 | stream.Read(&FCC2, 4); | |
658974ae VS |
1461 | } |
1462 | else | |
1463 | { | |
cff81649 | 1464 | if ( FCC1 == anih32 ) |
658974ae VS |
1465 | { |
1466 | wxUint32 *pData = new wxUint32[datalen/4]; | |
1467 | stream.Read(pData, datalen); | |
1af5934b | 1468 | int nIcons = wxINT32_SWAP_ON_BE(*(pData + 1)); |
658974ae VS |
1469 | delete[] pData; |
1470 | return nIcons; | |
1471 | } | |
574c939e | 1472 | else |
658974ae VS |
1473 | stream.SeekI(stream.TellI() + datalen); |
1474 | } | |
574c939e | 1475 | |
658974ae VS |
1476 | // try to read next data chunk: |
1477 | stream.Read(&FCC1, 4); | |
1478 | } | |
1479 | ||
1480 | return wxNOT_FOUND; | |
1481 | } | |
1482 | ||
e30285ab VZ |
1483 | #endif // wxUSE_STREAMS |
1484 | ||
658974ae VS |
1485 | #endif // wxUSE_ICO_CUR |
1486 | ||
e30285ab | 1487 | #endif // wxUSE_IMAGE |