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