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