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