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