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