]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagbmp.cpp
fc67b7731307c719f5972214b05f943708c4929b
[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/anidecod.h"
37
38 // For memcpy
39 #include <string.h>
40
41 //-----------------------------------------------------------------------------
42 // wxBMPHandler
43 //-----------------------------------------------------------------------------
44
45 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler,wxImageHandler)
46
47 #if wxUSE_STREAMS
48
49 #ifndef BI_RGB
50 #define BI_RGB 0
51 #endif
52
53 #ifndef BI_RLE8
54 #define BI_RLE8 1
55 #endif
56
57 #ifndef BI_RLE4
58 #define BI_RLE4 2
59 #endif
60
61 #ifndef BI_BITFIELDS
62 #define BI_BITFIELDS 3
63 #endif
64
65 #define poffset (line * width * 3 + column * 3)
66
67 bool wxBMPHandler::SaveFile(wxImage *image,
68 wxOutputStream& stream,
69 bool verbose)
70 {
71 return SaveDib(image, stream, verbose, true/*IsBmp*/, false/*IsMask*/);
72 }
73
74 bool wxBMPHandler::SaveDib(wxImage *image,
75 wxOutputStream& stream,
76 bool verbose,
77 bool IsBmp,
78 bool IsMask)
79
80 {
81 wxCHECK_MSG( image, false, _T("invalid pointer in wxBMPHandler::SaveFile") );
82
83 if ( !image->Ok() )
84 {
85 if ( verbose )
86 wxLogError(_("BMP: Couldn't save invalid image."));
87 return false;
88 }
89
90 // get the format of the BMP file to save, else use 24bpp
91 unsigned format = wxBMP_24BPP;
92 if ( image->HasOption(wxIMAGE_OPTION_BMP_FORMAT) )
93 format = image->GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT);
94
95 wxUint16 bpp; // # of bits per pixel
96 int palette_size; // # of color map entries, ie. 2^bpp colors
97
98 // set the bpp and appropriate palette_size, and do additional checks
99 if ( (format == wxBMP_1BPP) || (format == wxBMP_1BPP_BW) )
100 {
101 bpp = 1;
102 palette_size = 2;
103 }
104 else if ( format == wxBMP_4BPP )
105 {
106 bpp = 4;
107 palette_size = 16;
108 }
109 else if ( (format == wxBMP_8BPP) || (format == wxBMP_8BPP_GREY) ||
110 (format == wxBMP_8BPP_RED) || (format == wxBMP_8BPP_PALETTE) )
111 {
112 // need to set a wxPalette to use this, HOW TO CHECK IF VALID, SIZE?
113 if ((format == wxBMP_8BPP_PALETTE)
114 #if wxUSE_PALETTE
115 && !image->HasPalette()
116 #endif // wxUSE_PALETTE
117 )
118 {
119 if ( verbose )
120 wxLogError(_("BMP: wxImage doesn't have own wxPalette."));
121 return false;
122 }
123 bpp = 8;
124 palette_size = 256;
125 }
126 else // you get 24bpp
127 {
128 format = wxBMP_24BPP;
129 bpp = 24;
130 palette_size = 0;
131 }
132
133 unsigned width = image->GetWidth();
134 unsigned row_padding = (4 - int(width*bpp/8.0) % 4) % 4; // # bytes to pad to dword
135 unsigned row_width = int(width * bpp/8.0) + row_padding; // # of bytes per row
136
137 struct
138 {
139 // BitmapHeader:
140 wxUint16 magic; // format magic, always 'BM'
141 wxUint32 filesize; // total file size, inc. headers
142 wxUint32 reserved; // for future use
143 wxUint32 data_offset; // image data offset in the file
144
145 // BitmapInfoHeader:
146 wxUint32 bih_size; // 2nd part's size
147 wxUint32 width, height; // bitmap's dimensions
148 wxUint16 planes; // num of planes
149 wxUint16 bpp; // bits per pixel
150 wxUint32 compression; // compression method
151 wxUint32 size_of_bmp; // size of the bitmap
152 wxUint32 h_res, v_res; // image resolution in pixels-per-meter
153 wxUint32 num_clrs; // number of colors used
154 wxUint32 num_signif_clrs;// number of significant colors
155 } hdr;
156
157 wxUint32 hdr_size = 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/;
158
159 hdr.magic = wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/);
160 hdr.filesize = wxUINT32_SWAP_ON_BE( hdr_size + palette_size*4 +
161 row_width * image->GetHeight() );
162 hdr.reserved = 0;
163 hdr.data_offset = wxUINT32_SWAP_ON_BE(hdr_size + palette_size*4);
164
165 hdr.bih_size = wxUINT32_SWAP_ON_BE(hdr_size - 14);
166 hdr.width = wxUINT32_SWAP_ON_BE(image->GetWidth());
167 if ( IsBmp )
168 {
169 hdr.height = wxUINT32_SWAP_ON_BE(image->GetHeight());
170 }
171 else
172 {
173 hdr.height = wxUINT32_SWAP_ON_BE(2 * image->GetHeight());
174 }
175 hdr.planes = wxUINT16_SWAP_ON_BE(1); // always 1 plane
176 hdr.bpp = wxUINT16_SWAP_ON_BE(bpp);
177 hdr.compression = 0; // RGB uncompressed
178 hdr.size_of_bmp = wxUINT32_SWAP_ON_BE(row_width * image->GetHeight());
179
180 // get the resolution from the image options or fall back to 72dpi standard
181 // for the BMP format if not specified
182 wxUint32 hres = image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX),
183 vres = image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY);
184 switch ( image->GetOptionInt(wxIMAGE_OPTION_RESOLUTION) )
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 = (wxUint32)(100*mm2inches*hres);
198 vres = (wxUint32)(100*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 typedef struct
473 {
474 unsigned char r, g, b;
475 } _cmap;
476
477 bool wxBMPHandler::DoLoadDib(wxImage * image, int width, int height,
478 int bpp, int ncolors, int comp,
479 wxFileOffset bmpOffset, wxInputStream& stream,
480 bool verbose, bool IsBmp, bool hasPalette)
481 {
482 wxInt32 aDword, rmask = 0, gmask = 0, bmask = 0, amask = 0;
483 int rshift = 0, gshift = 0, bshift = 0, ashift = 0;
484 int rbits = 0, gbits = 0, bbits = 0, abits = 0;
485 wxInt32 dbuf[4];
486 wxInt8 bbuf[4];
487 wxUint8 aByte;
488 wxUint16 aWord;
489
490 // allocate space for palette if needed:
491 _cmap *cmap;
492
493 if ( bpp < 16 )
494 {
495 cmap = new _cmap[ncolors];
496 if ( !cmap )
497 {
498 if (verbose)
499 wxLogError(_("BMP: Couldn't allocate memory."));
500 return false;
501 }
502 }
503 else
504 cmap = NULL;
505
506 // destroy existing here instead of:
507 image->Destroy();
508 image->Create(width, height);
509
510 unsigned char *ptr = image->GetData();
511
512 if ( !ptr )
513 {
514 if ( verbose )
515 wxLogError( _("BMP: Couldn't allocate memory.") );
516 delete[] cmap;
517 return false;
518 }
519
520 unsigned char *alpha;
521 if ( bpp == 32 )
522 {
523 // tell the image to allocate an alpha buffer
524 image->SetAlpha();
525 alpha = image->GetAlpha();
526 if ( !alpha )
527 {
528 if ( verbose )
529 wxLogError(_("BMP: Couldn't allocate memory."));
530 delete[] cmap;
531 return false;
532 }
533 }
534 else // no alpha
535 {
536 alpha = NULL;
537 }
538
539 // Reading the palette, if it exists:
540 if ( bpp < 16 && ncolors != 0 )
541 {
542 unsigned char* r = new unsigned char[ncolors];
543 unsigned char* g = new unsigned char[ncolors];
544 unsigned char* b = new unsigned char[ncolors];
545 for (int j = 0; j < ncolors; j++)
546 {
547 if (hasPalette)
548 {
549 stream.Read(bbuf, 4);
550 cmap[j].b = bbuf[0];
551 cmap[j].g = bbuf[1];
552 cmap[j].r = bbuf[2];
553
554 r[j] = cmap[j].r;
555 g[j] = cmap[j].g;
556 b[j] = cmap[j].b;
557 }
558 else
559 {
560 //used in reading .ico file mask
561 r[j] = cmap[j].r =
562 g[j] = cmap[j].g =
563 b[j] = cmap[j].b = ( j ? 255 : 0 );
564 }
565 }
566
567 #if wxUSE_PALETTE
568 // Set the palette for the wxImage
569 image->SetPalette(wxPalette(ncolors, r, g, b));
570 #endif // wxUSE_PALETTE
571
572 delete[] r;
573 delete[] g;
574 delete[] b;
575 }
576 else if ( bpp == 16 || bpp == 32 )
577 {
578 if ( comp == BI_BITFIELDS )
579 {
580 int bit = 0;
581 stream.Read(dbuf, 4 * 3);
582 rmask = wxINT32_SWAP_ON_BE(dbuf[0]);
583 gmask = wxINT32_SWAP_ON_BE(dbuf[1]);
584 bmask = wxINT32_SWAP_ON_BE(dbuf[2]);
585 // find shift amount (Least significant bit of mask)
586 for (bit = bpp-1; bit>=0; bit--)
587 {
588 if (bmask & (1 << bit))
589 bshift = bit;
590 if (gmask & (1 << bit))
591 gshift = bit;
592 if (rmask & (1 << bit))
593 rshift = bit;
594 }
595 // Find number of bits in mask (MSB-LSB+1)
596 for (bit = 0; bit < bpp; bit++)
597 {
598 if (bmask & (1 << bit))
599 bbits = bit-bshift+1;
600 if (gmask & (1 << bit))
601 gbits = bit-gshift+1;
602 if (rmask & (1 << bit))
603 rbits = bit-rshift+1;
604 }
605 }
606 else if ( bpp == 16 )
607 {
608 rmask = 0x7C00;
609 gmask = 0x03E0;
610 bmask = 0x001F;
611 rshift = 10;
612 gshift = 5;
613 bshift = 0;
614 rbits = 5;
615 gbits = 5;
616 bbits = 5;
617 }
618 else if ( bpp == 32 )
619 {
620 rmask = 0x00FF0000;
621 gmask = 0x0000FF00;
622 bmask = 0x000000FF;
623 amask = 0xFF000000;
624
625 ashift = 24;
626 rshift = 16;
627 gshift = 8;
628 bshift = 0;
629 abits = 8;
630 rbits = 8;
631 gbits = 8;
632 bbits = 8;
633 }
634 }
635
636 /*
637 * Reading the image data
638 */
639 if ( IsBmp )
640 stream.SeekI(bmpOffset); // else icon, just carry on
641
642 unsigned char *data = ptr;
643
644 /* set the whole image to the background color */
645 if ( bpp < 16 && (comp == BI_RLE4 || comp == BI_RLE8) )
646 {
647 for (int i = 0; i < width * height; i++)
648 {
649 *ptr++ = cmap[0].r;
650 *ptr++ = cmap[0].g;
651 *ptr++ = cmap[0].b;
652 }
653 ptr = data;
654 }
655
656 int linesize = ((width * bpp + 31) / 32) * 4;
657
658 /* BMPs are stored upside down */
659 for ( int line = (height - 1); line >= 0; line-- )
660 {
661 int linepos = 0;
662 for ( int column = 0; column < width ; )
663 {
664 if ( bpp < 16 )
665 {
666 linepos++;
667 aByte = stream.GetC();
668 if ( bpp == 1 )
669 {
670 for (int bit = 0; bit < 8 && column < width; bit++)
671 {
672 int index = ((aByte & (0x80 >> bit)) ? 1 : 0);
673 ptr[poffset] = cmap[index].r;
674 ptr[poffset + 1] = cmap[index].g;
675 ptr[poffset + 2] = cmap[index].b;
676 column++;
677 }
678 }
679 else if ( bpp == 4 )
680 {
681 if ( comp == BI_RLE4 )
682 {
683 wxUint8 first;
684 first = aByte;
685 aByte = stream.GetC();
686 if ( first == 0 )
687 {
688 if ( aByte == 0 )
689 {
690 if ( column > 0 )
691 column = width;
692 }
693 else if ( aByte == 1 )
694 {
695 column = width;
696 line = -1;
697 }
698 else if ( aByte == 2 )
699 {
700 aByte = stream.GetC();
701 column += aByte;
702 linepos = column * bpp / 4;
703 aByte = stream.GetC();
704 line -= aByte; // upside down
705 }
706 else
707 {
708 int absolute = aByte;
709 wxUint8 nibble[2] ;
710 int readBytes = 0 ;
711 for (int k = 0; k < absolute; k++)
712 {
713 if ( !(k % 2 ) )
714 {
715 ++readBytes ;
716 aByte = stream.GetC();
717 nibble[0] = (wxUint8)( (aByte & 0xF0) >> 4 ) ;
718 nibble[1] = (wxUint8)( aByte & 0x0F ) ;
719 }
720 ptr[poffset ] = cmap[nibble[k%2]].r;
721 ptr[poffset + 1] = cmap[nibble[k%2]].g;
722 ptr[poffset + 2] = cmap[nibble[k%2]].b;
723 column++;
724 if ( k % 2 )
725 linepos++;
726 }
727 if ( readBytes & 0x01 )
728 aByte = stream.GetC();
729 }
730 }
731 else
732 {
733 wxUint8 nibble[2] ;
734 nibble[0] = (wxUint8)( (aByte & 0xF0) >> 4 ) ;
735 nibble[1] = (wxUint8)( aByte & 0x0F ) ;
736
737 for ( int l = 0; l < first && column < width; l++ )
738 {
739 ptr[poffset ] = cmap[nibble[l%2]].r;
740 ptr[poffset + 1] = cmap[nibble[l%2]].g;
741 ptr[poffset + 2] = cmap[nibble[l%2]].b;
742 column++;
743 if ( l % 2 )
744 linepos++;
745 }
746 }
747 }
748 else
749 {
750 for (int nibble = 0; nibble < 2 && column < width; nibble++)
751 {
752 int index = ((aByte & (0xF0 >> nibble * 4)) >> (!nibble * 4));
753 if ( index >= 16 )
754 index = 15;
755 ptr[poffset] = cmap[index].r;
756 ptr[poffset + 1] = cmap[index].g;
757 ptr[poffset + 2] = cmap[index].b;
758 column++;
759 }
760 }
761 }
762 else if ( bpp == 8 )
763 {
764 if ( comp == BI_RLE8 )
765 {
766 unsigned char first;
767 first = aByte;
768 aByte = stream.GetC();
769 if ( first == 0 )
770 {
771 if ( aByte == 0 )
772 {
773 /* column = width; */
774 }
775 else if ( aByte == 1 )
776 {
777 column = width;
778 line = -1;
779 }
780 else if ( aByte == 2 )
781 {
782 aByte = stream.GetC();
783 column += aByte;
784 linepos = column * bpp / 8;
785 aByte = stream.GetC();
786 line += aByte;
787 }
788 else
789 {
790 int absolute = aByte;
791 for (int k = 0; k < absolute; k++)
792 {
793 linepos++;
794 aByte = stream.GetC();
795 ptr[poffset ] = cmap[aByte].r;
796 ptr[poffset + 1] = cmap[aByte].g;
797 ptr[poffset + 2] = cmap[aByte].b;
798 column++;
799 }
800 if ( absolute & 0x01 )
801 aByte = stream.GetC();
802 }
803 }
804 else
805 {
806 for ( int l = 0; l < first && column < width; l++ )
807 {
808 ptr[poffset ] = cmap[aByte].r;
809 ptr[poffset + 1] = cmap[aByte].g;
810 ptr[poffset + 2] = cmap[aByte].b;
811 column++;
812 linepos++;
813 }
814 }
815 }
816 else
817 {
818 ptr[poffset ] = cmap[aByte].r;
819 ptr[poffset + 1] = cmap[aByte].g;
820 ptr[poffset + 2] = cmap[aByte].b;
821 column++;
822 // linepos += size; seems to be wrong, RR
823 }
824 }
825 }
826 else if ( bpp == 24 )
827 {
828 stream.Read(bbuf, 3);
829 linepos += 3;
830 ptr[poffset ] = (unsigned char)bbuf[2];
831 ptr[poffset + 1] = (unsigned char)bbuf[1];
832 ptr[poffset + 2] = (unsigned char)bbuf[0];
833 column++;
834 }
835 else if ( bpp == 16 )
836 {
837 unsigned char temp;
838 stream.Read(&aWord, 2);
839 aWord = wxUINT16_SWAP_ON_BE(aWord);
840 linepos += 2;
841 /* use the masks and calculated amonut of shift
842 to retrieve the color data out of the word. Then
843 shift it left by (8 - number of bits) such that
844 the image has the proper dynamic range */
845 temp = (unsigned char)((aWord & rmask) >> rshift << (8-rbits));
846 ptr[poffset] = temp;
847 temp = (unsigned char)((aWord & gmask) >> gshift << (8-gbits));
848 ptr[poffset + 1] = temp;
849 temp = (unsigned char)((aWord & bmask) >> bshift << (8-bbits));
850 ptr[poffset + 2] = temp;
851 column++;
852 }
853 else
854 {
855 unsigned char temp;
856 stream.Read(&aDword, 4);
857 aDword = wxINT32_SWAP_ON_BE(aDword);
858 linepos += 4;
859 temp = (unsigned char)((aDword & rmask) >> rshift);
860 ptr[poffset] = temp;
861 temp = (unsigned char)((aDword & gmask) >> gshift);
862 ptr[poffset + 1] = temp;
863 temp = (unsigned char)((aDword & bmask) >> bshift);
864 ptr[poffset + 2] = temp;
865 if ( alpha )
866 {
867 temp = (unsigned char)((aDword & amask) >> ashift);
868 alpha[line * width + column] = temp;
869 }
870 column++;
871 }
872 }
873 while ( (linepos < linesize) && (comp != 1) && (comp != 2) )
874 {
875 stream.Read(&aByte, 1);
876 linepos += 1;
877 if ( !stream )
878 break;
879 }
880 }
881
882 delete[] cmap;
883
884 image->SetMask(false);
885
886 const wxStreamError err = stream.GetLastError();
887 return err == wxSTREAM_NO_ERROR || err == wxSTREAM_EOF;
888 }
889
890 bool wxBMPHandler::LoadDib(wxImage *image, wxInputStream& stream,
891 bool verbose, bool IsBmp)
892 {
893 wxUint16 aWord;
894 wxInt32 dbuf[4];
895 wxInt8 bbuf[4];
896
897 wxFileOffset offset = 0; // keep gcc quiet
898 if ( IsBmp )
899 {
900 // read the header off the .BMP format file
901
902 offset = stream.TellI();
903 if (offset == wxInvalidOffset)
904 offset = 0;
905
906 stream.Read(bbuf, 2);
907 stream.Read(dbuf, 16);
908 }
909 else
910 {
911 stream.Read(dbuf, 4);
912 }
913 #if 0 // unused
914 wxInt32 size = wxINT32_SWAP_ON_BE(dbuf[0]);
915 #endif
916 offset = offset + wxINT32_SWAP_ON_BE(dbuf[2]);
917
918 stream.Read(dbuf, 4 * 2);
919 int width = wxINT32_SWAP_ON_BE((int)dbuf[0]);
920 int height = wxINT32_SWAP_ON_BE((int)dbuf[1]);
921 if ( !IsBmp)height = height / 2; // for icons divide by 2
922
923 if ( width > 32767 )
924 {
925 if (verbose)
926 wxLogError( _("DIB Header: Image width > 32767 pixels for file.") );
927 return false;
928 }
929 if ( height > 32767 )
930 {
931 if (verbose)
932 wxLogError( _("DIB Header: Image height > 32767 pixels for file.") );
933 return false;
934 }
935
936 stream.Read(&aWord, 2);
937 /*
938 TODO
939 int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
940 */
941 stream.Read(&aWord, 2);
942 int bpp = wxUINT16_SWAP_ON_BE((int)aWord);
943 if ( bpp != 1 && bpp != 4 && bpp != 8 && bpp != 16 && bpp != 24 && bpp != 32 )
944 {
945 if (verbose)
946 wxLogError( _("DIB Header: Unknown bitdepth in file.") );
947 return false;
948 }
949
950 stream.Read(dbuf, 4 * 4);
951 int comp = wxINT32_SWAP_ON_BE((int)dbuf[0]);
952 if ( comp != BI_RGB && comp != BI_RLE4 && comp != BI_RLE8 &&
953 comp != BI_BITFIELDS )
954 {
955 if (verbose)
956 wxLogError( _("DIB Header: Unknown encoding in file.") );
957 return false;
958 }
959
960 stream.Read(dbuf, 4 * 2);
961
962 int ncolors = wxINT32_SWAP_ON_BE( (int)dbuf[0] );
963 if (ncolors == 0)
964 ncolors = 1 << bpp;
965 /* some more sanity checks */
966 if (((comp == BI_RLE4) && (bpp != 4)) ||
967 ((comp == BI_RLE8) && (bpp != 8)) ||
968 ((comp == BI_BITFIELDS) && (bpp != 16 && bpp != 32)))
969 {
970 if (verbose)
971 wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") );
972 return false;
973 }
974
975 //read DIB; this is the BMP image or the XOR part of an icon image
976 if ( !DoLoadDib(image, width, height, bpp, ncolors, comp, offset, stream,
977 verbose, IsBmp, true) )
978 {
979 if (verbose)
980 wxLogError( _("Error in reading image DIB.") );
981 return false;
982 }
983
984 if ( !IsBmp )
985 {
986 //read Icon mask which is monochrome
987 //there is no palette, so we will create one
988 wxImage mask;
989 if ( !DoLoadDib(&mask, width, height, 1, 2, BI_RGB, offset, stream,
990 verbose, IsBmp, false) )
991 {
992 if (verbose)
993 wxLogError( _("ICO: Error in reading mask DIB.") );
994 return false;
995 }
996 image->SetMaskFromImage(mask, 255, 255, 255);
997
998 }
999
1000 // the resolution in the bitmap header is in meters, convert to centimeters
1001 image->SetOption(wxIMAGE_OPTION_RESOLUTIONUNIT, wxIMAGE_RESOLUTION_CM);
1002 image->SetOption(wxIMAGE_OPTION_RESOLUTIONX, dbuf[2]/100);
1003 image->SetOption(wxIMAGE_OPTION_RESOLUTIONY, dbuf[3]/100);
1004
1005 return true;
1006 }
1007
1008 bool wxBMPHandler::LoadFile(wxImage *image, wxInputStream& stream,
1009 bool verbose, int WXUNUSED(index))
1010 {
1011 // Read a single DIB fom the file:
1012 return LoadDib(image, stream, verbose, true/*isBmp*/);
1013 }
1014
1015 bool wxBMPHandler::DoCanRead(wxInputStream& stream)
1016 {
1017 unsigned char hdr[2];
1018
1019 if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
1020 return false;
1021
1022 // do we have the BMP file signature?
1023 return hdr[0] == 'B' && hdr[1] == 'M';
1024 }
1025
1026 #endif // wxUSE_STREAMS
1027
1028
1029 #if wxUSE_ICO_CUR
1030 //-----------------------------------------------------------------------------
1031 // wxICOHandler
1032 //-----------------------------------------------------------------------------
1033
1034 IMPLEMENT_DYNAMIC_CLASS(wxICOHandler, wxBMPHandler)
1035
1036 #if wxUSE_STREAMS
1037
1038 struct ICONDIRENTRY
1039 {
1040 wxUint8 bWidth; // Width of the image
1041 wxUint8 bHeight; // Height of the image (times 2)
1042 wxUint8 bColorCount; // Number of colors in image (0 if >=8bpp)
1043 wxUint8 bReserved; // Reserved
1044
1045 // these two are different in icons and cursors:
1046 // icon or cursor
1047 wxUint16 wPlanes; // Color Planes or XHotSpot
1048 wxUint16 wBitCount; // Bits per pixel or YHotSpot
1049
1050 wxUint32 dwBytesInRes; // how many bytes in this resource?
1051 wxUint32 dwImageOffset; // where in the file is this image
1052 };
1053
1054 struct ICONDIR
1055 {
1056 wxUint16 idReserved; // Reserved
1057 wxUint16 idType; // resource type (1 for icons, 2 for cursors)
1058 wxUint16 idCount; // how many images?
1059 };
1060
1061
1062 bool wxICOHandler::SaveFile(wxImage *image,
1063 wxOutputStream& stream,
1064 bool verbose)
1065
1066 {
1067 //sanity check; icon must be less than 127 pixels high and 255 wide
1068 if ( image->GetHeight () > 127 )
1069 {
1070 if ( verbose )
1071 wxLogError(_("ICO: Image too tall for an icon."));
1072 return false;
1073 }
1074 if ( image->GetWidth () > 255 )
1075 {
1076 if ( verbose )
1077 wxLogError(_("ICO: Image too wide for an icon."));
1078 return false;
1079 }
1080
1081 const int images = 1; // only generate one image
1082
1083 // VS: This is a hack of sort - since ICO and CUR files are almost
1084 // identical, we have all the meat in wxICOHandler and check for
1085 // the actual (handler) type when the code has to distinguish between
1086 // the two formats
1087 int type = (this->GetType() == wxBITMAP_TYPE_CUR) ? 2 : 1;
1088
1089 // write a header, (ICONDIR)
1090 // Calculate the header size
1091 wxUint32 offset = 3 * sizeof(wxUint16);
1092
1093 ICONDIR IconDir;
1094 IconDir.idReserved = 0;
1095 IconDir.idType = wxUINT16_SWAP_ON_BE((wxUint16)type);
1096 IconDir.idCount = wxUINT16_SWAP_ON_BE((wxUint16)images);
1097 stream.Write(&IconDir.idReserved, sizeof(IconDir.idReserved));
1098 stream.Write(&IconDir.idType, sizeof(IconDir.idType));
1099 stream.Write(&IconDir.idCount, sizeof(IconDir.idCount));
1100 if ( !stream.IsOk() )
1101 {
1102 if ( verbose )
1103 wxLogError(_("ICO: Error writing the image file!"));
1104 return false;
1105 }
1106
1107 // for each iamage write a description ICONDIRENTRY:
1108 ICONDIRENTRY icondirentry;
1109 for (int img = 0; img < images; img++)
1110 {
1111 wxImage mask;
1112
1113 if ( image->HasMask() )
1114 {
1115 // make another image with black/white:
1116 mask = image->ConvertToMono (image->GetMaskRed(), image->GetMaskGreen(), image->GetMaskBlue() );
1117
1118 // now we need to change the masked regions to black:
1119 unsigned char r = image->GetMaskRed();
1120 unsigned char g = image->GetMaskGreen();
1121 unsigned char b = image->GetMaskBlue();
1122 if ( (r != 0) || (g != 0) || (b != 0) )
1123 {
1124 // Go round and apply black to the masked bits:
1125 int i, j;
1126 for (i = 0; i < mask.GetWidth(); i++)
1127 {
1128 for (j = 0; j < mask.GetHeight(); j++)
1129 {
1130 if ((r == mask.GetRed(i, j)) &&
1131 (g == mask.GetGreen(i, j))&&
1132 (b == mask.GetBlue(i, j)) )
1133 image->SetRGB(i, j, 0, 0, 0 );
1134 }
1135 }
1136 }
1137 }
1138 else
1139 {
1140 // just make a black mask all over:
1141 mask = image->Copy();
1142 int i, j;
1143 for (i = 0; i < mask.GetWidth(); i++)
1144 for (j = 0; j < mask.GetHeight(); j++)
1145 mask.SetRGB(i, j, 0, 0, 0 );
1146 }
1147 // Set the formats for image and mask
1148 // (Windows never saves with more than 8 colors):
1149 image->SetOption(wxIMAGE_OPTION_BMP_FORMAT, wxBMP_8BPP);
1150
1151 // monochome bitmap:
1152 mask.SetOption(wxIMAGE_OPTION_BMP_FORMAT, wxBMP_1BPP_BW);
1153 bool IsBmp = false;
1154 bool IsMask = false;
1155
1156 //calculate size and offset of image and mask
1157 wxCountingOutputStream cStream;
1158 bool bResult = SaveDib(image, cStream, verbose, IsBmp, IsMask);
1159 if ( !bResult )
1160 {
1161 if ( verbose )
1162 wxLogError(_("ICO: Error writing the image file!"));
1163 return false;
1164 }
1165 IsMask = true;
1166
1167 bResult = SaveDib(&mask, cStream, verbose, IsBmp, IsMask);
1168 if ( !bResult )
1169 {
1170 if ( verbose )
1171 wxLogError(_("ICO: Error writing the image file!"));
1172 return false;
1173 }
1174 wxUint32 Size = cStream.GetSize();
1175
1176 // wxCountingOutputStream::IsOk() always returns true for now and this
1177 // "if" provokes VC++ warnings in optimized build
1178 #if 0
1179 if ( !cStream.Ok() )
1180 {
1181 if ( verbose )
1182 wxLogError(_("ICO: Error writing the image file!"));
1183 return false;
1184 }
1185 #endif // 0
1186
1187 offset = offset + sizeof(ICONDIRENTRY);
1188
1189 icondirentry.bWidth = (wxUint8)image->GetWidth();
1190 icondirentry.bHeight = (wxUint8)(2 * image->GetHeight());
1191 icondirentry.bColorCount = 0;
1192 icondirentry.bReserved = 0;
1193 icondirentry.wPlanes = wxUINT16_SWAP_ON_BE(1);
1194 icondirentry.wBitCount = wxUINT16_SWAP_ON_BE(wxBMP_8BPP);
1195 if ( type == 2 /*CUR*/)
1196 {
1197 int hx = image->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X) ?
1198 image->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) :
1199 image->GetWidth() / 2;
1200 int hy = image->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ?
1201 image->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) :
1202 image->GetHeight() / 2;
1203
1204 // actually write the values of the hot spot here:
1205 icondirentry.wPlanes = wxUINT16_SWAP_ON_BE((wxUint16)hx);
1206 icondirentry.wBitCount = wxUINT16_SWAP_ON_BE((wxUint16)hy);
1207 }
1208 icondirentry.dwBytesInRes = wxUINT32_SWAP_ON_BE(Size);
1209 icondirentry.dwImageOffset = wxUINT32_SWAP_ON_BE(offset);
1210
1211 // increase size to allow for the data written:
1212 offset += Size;
1213
1214 // write to stream:
1215 stream.Write(&icondirentry.bWidth, sizeof(icondirentry.bWidth));
1216 stream.Write(&icondirentry.bHeight, sizeof(icondirentry.bHeight));
1217 stream.Write(&icondirentry.bColorCount, sizeof(icondirentry.bColorCount));
1218 stream.Write(&icondirentry.bReserved, sizeof(icondirentry.bReserved));
1219 stream.Write(&icondirentry.wPlanes, sizeof(icondirentry.wPlanes));
1220 stream.Write(&icondirentry.wBitCount, sizeof(icondirentry.wBitCount));
1221 stream.Write(&icondirentry.dwBytesInRes, sizeof(icondirentry.dwBytesInRes));
1222 stream.Write(&icondirentry.dwImageOffset, sizeof(icondirentry.dwImageOffset));
1223 if ( !stream.IsOk() )
1224 {
1225 if ( verbose )
1226 wxLogError(_("ICO: Error writing the image file!"));
1227 return false;
1228 }
1229
1230 // actually save it:
1231 IsMask = false;
1232 bResult = SaveDib(image, stream, verbose, IsBmp, IsMask);
1233 if ( !bResult )
1234 {
1235 if ( verbose )
1236 wxLogError(_("ICO: Error writing the image file!"));
1237 return false;
1238 }
1239 IsMask = true;
1240
1241 bResult = SaveDib(&mask, stream, verbose, IsBmp, IsMask);
1242 if ( !bResult )
1243 {
1244 if ( verbose )
1245 wxLogError(_("ICO: Error writing the image file!"));
1246 return false;
1247 }
1248
1249 } // end of for loop
1250
1251 return true;
1252 }
1253
1254 bool wxICOHandler::LoadFile(wxImage *image, wxInputStream& stream,
1255 bool verbose, int index)
1256 {
1257 stream.SeekI(0);
1258 return DoLoadFile(image, stream, verbose, index);
1259 }
1260
1261 bool wxICOHandler::DoLoadFile(wxImage *image, wxInputStream& stream,
1262 bool WXUNUSED(verbose), int index)
1263 {
1264 bool bResult wxDUMMY_INITIALIZE(false);
1265 bool IsBmp = false;
1266
1267 ICONDIR IconDir;
1268
1269 wxFileOffset iPos = stream.TellI();
1270 stream.Read(&IconDir, sizeof(IconDir));
1271 wxUint16 nIcons = wxUINT16_SWAP_ON_BE(IconDir.idCount);
1272 // nType is 1 for Icons, 2 for Cursors:
1273 wxUint16 nType = wxUINT16_SWAP_ON_BE(IconDir.idType);
1274
1275 // loop round the icons and choose the best one:
1276 ICONDIRENTRY *pIconDirEntry = new ICONDIRENTRY[nIcons];
1277 ICONDIRENTRY *pCurrentEntry = pIconDirEntry;
1278 int wMax = 0;
1279 int colmax = 0;
1280 int iSel = wxNOT_FOUND;
1281
1282 for (int i = 0; i < nIcons; i++ )
1283 {
1284 stream.Read(pCurrentEntry, sizeof(ICONDIRENTRY));
1285 // bHeight and bColorCount are wxUint8
1286 if ( pCurrentEntry->bWidth >= wMax )
1287 {
1288 // see if we have more colors, ==0 indicates > 8bpp:
1289 if ( pCurrentEntry->bColorCount == 0 )
1290 pCurrentEntry->bColorCount = 255;
1291 if ( pCurrentEntry->bColorCount >= colmax )
1292 {
1293 iSel = i;
1294 wMax = pCurrentEntry->bWidth;
1295 colmax = pCurrentEntry->bColorCount;
1296 }
1297 }
1298 pCurrentEntry++;
1299 }
1300
1301 if ( index != -1 )
1302 {
1303 // VS: Note that we *have* to run the loop above even if index != -1, because
1304 // it reads ICONDIRENTRies.
1305 iSel = index;
1306 }
1307
1308 if ( iSel == wxNOT_FOUND || iSel < 0 || iSel >= nIcons )
1309 {
1310 wxLogError(_("ICO: Invalid icon index."));
1311 bResult = false;
1312 }
1313 else
1314 {
1315 // seek to selected icon:
1316 pCurrentEntry = pIconDirEntry + iSel;
1317 stream.SeekI(iPos + wxUINT32_SWAP_ON_BE(pCurrentEntry->dwImageOffset), wxFromStart);
1318 bResult = LoadDib(image, stream, true, IsBmp);
1319 bool bIsCursorType = (this->GetType() == wxBITMAP_TYPE_CUR) || (this->GetType() == wxBITMAP_TYPE_ANI);
1320 if ( bResult && bIsCursorType && nType == 2 )
1321 {
1322 // it is a cursor, so let's set the hotspot:
1323 image->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, wxUINT16_SWAP_ON_BE(pCurrentEntry->wPlanes));
1324 image->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, wxUINT16_SWAP_ON_BE(pCurrentEntry->wBitCount));
1325 }
1326 }
1327 delete[] pIconDirEntry;
1328 return bResult;
1329 }
1330
1331 int wxICOHandler::GetImageCount(wxInputStream& stream)
1332 {
1333 ICONDIR IconDir;
1334 wxFileOffset iPos = stream.TellI();
1335 stream.SeekI(0);
1336 stream.Read(&IconDir, sizeof(IconDir));
1337 wxUint16 nIcons = wxUINT16_SWAP_ON_BE(IconDir.idCount);
1338 stream.SeekI(iPos);
1339 return (int)nIcons;
1340 }
1341
1342 bool wxICOHandler::DoCanRead(wxInputStream& stream)
1343 {
1344 stream.SeekI(0);
1345 unsigned char hdr[4];
1346 if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
1347 return false;
1348
1349 // hdr[2] is one for an icon and two for a cursor
1350 return hdr[0] == '\0' && hdr[1] == '\0' && hdr[2] == '\1' && hdr[3] == '\0';
1351 }
1352
1353 #endif // wxUSE_STREAMS
1354
1355
1356 //-----------------------------------------------------------------------------
1357 // wxCURHandler
1358 //-----------------------------------------------------------------------------
1359
1360 IMPLEMENT_DYNAMIC_CLASS(wxCURHandler, wxICOHandler)
1361
1362 #if wxUSE_STREAMS
1363
1364 bool wxCURHandler::DoCanRead(wxInputStream& stream)
1365 {
1366 stream.SeekI(0);
1367 unsigned char hdr[4];
1368 if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
1369 return false;
1370
1371 // hdr[2] is one for an icon and two for a cursor
1372 return hdr[0] == '\0' && hdr[1] == '\0' && hdr[2] == '\2' && hdr[3] == '\0';
1373 }
1374
1375 #endif // wxUSE_STREAMS
1376
1377 //-----------------------------------------------------------------------------
1378 // wxANIHandler
1379 //-----------------------------------------------------------------------------
1380
1381 IMPLEMENT_DYNAMIC_CLASS(wxANIHandler, wxCURHandler)
1382
1383 #if wxUSE_STREAMS
1384
1385 bool wxANIHandler::LoadFile(wxImage *image, wxInputStream& stream,
1386 bool WXUNUSED(verbose), int index)
1387 {
1388 wxANIDecoder decoder;
1389 if (!decoder.Load(stream))
1390 return false;
1391
1392 return decoder.ConvertToImage(index != -1 ? (size_t)index : 0, image);
1393 }
1394
1395 bool wxANIHandler::DoCanRead(wxInputStream& stream)
1396 {
1397 wxANIDecoder decod;
1398 return decod.CanRead(stream);
1399 }
1400
1401 int wxANIHandler::GetImageCount(wxInputStream& stream)
1402 {
1403 wxANIDecoder decoder;
1404 if (!decoder.Load(stream))
1405 return wxNOT_FOUND;
1406
1407 return decoder.GetFrameCount();
1408 }
1409
1410 #endif // wxUSE_STREAMS
1411
1412 #endif // wxUSE_ICO_CUR
1413
1414 #endif // wxUSE_IMAGE