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