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