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