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