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