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