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