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