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