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