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