1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage BMP,ICO and CUR handlers
4 // Author: Robert Roebling, Chris Elliott
6 // Copyright: (c) Robert Roebling, Chris Elliott
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "imagbmp.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
23 #if wxUSE_IMAGE && wxUSE_STREAMS
25 #include "wx/imagbmp.h"
26 #include "wx/bitmap.h"
30 #include "wx/filefn.h"
31 #include "wx/wfstream.h"
33 #include "wx/module.h"
34 #include "wx/quantize.h"
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
62 #define BI_BITFIELDS 3
65 #define poffset (line * width * 3 + column * 3)
67 bool wxBMPHandler::SaveFile(wxImage
*image
,
68 wxOutputStream
& stream
,
71 return SaveDib(image
, stream
, verbose
, TRUE
/*IsBmp*/, FALSE
/*IsMask*/);
74 bool wxBMPHandler::SaveDib(wxImage
*image
,
75 wxOutputStream
& stream
,
81 wxCHECK_MSG( image
, FALSE
, _T("invalid pointer in wxBMPHandler::SaveFile") );
86 wxLogError(_("BMP: Couldn't save invalid image."));
90 // get the format of the BMP file to save, else use 24bpp
91 unsigned format
= wxBMP_24BPP
;
92 if ( image
->HasOption(wxIMAGE_OPTION_BMP_FORMAT
) )
93 format
= image
->GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT
);
95 wxUint16 bpp
; // # of bits per pixel
96 int palette_size
; // # of color map entries, ie. 2^bpp colors
98 // set the bpp and appropriate palette_size, and do additional checks
99 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_1BPP_BW
) )
104 else if ( format
== wxBMP_4BPP
)
109 else if ( (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_GREY
) ||
110 (format
== wxBMP_8BPP_RED
) || (format
== wxBMP_8BPP_PALETTE
) )
112 // need to set a wxPalette to use this, HOW TO CHECK IF VALID, SIZE?
113 if ((format
== wxBMP_8BPP_PALETTE
)
115 && !image
->HasPalette()
116 #endif // wxUSE_PALETTE
120 wxLogError(_("BMP: wxImage doesn't have own wxPalette."));
126 else // you get 24bpp
128 format
= wxBMP_24BPP
;
133 unsigned width
= image
->GetWidth();
134 unsigned row_padding
= (4 - int(width
*bpp
/8.0) % 4) % 4; // # bytes to pad to dword
135 unsigned row_width
= int(width
* bpp
/8.0) + row_padding
; // # of bytes per row
140 wxUint16 magic
; // format magic, always 'BM'
141 wxUint32 filesize
; // total file size, inc. headers
142 wxUint32 reserved
; // for future use
143 wxUint32 data_offset
; // image data offset in the file
146 wxUint32 bih_size
; // 2nd part's size
147 wxUint32 width
, height
; // bitmap's dimensions
148 wxUint16 planes
; // num of planes
149 wxUint16 bpp
; // bits per pixel
150 wxUint32 compression
; // compression method
151 wxUint32 size_of_bmp
; // size of the bitmap
152 wxUint32 h_res
, v_res
; // image resolution in dpi
153 wxUint32 num_clrs
; // number of colors used
154 wxUint32 num_signif_clrs
;// number of significant colors
157 wxUint32 hdr_size
= 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/;
159 hdr
.magic
= wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/);
160 hdr
.filesize
= wxUINT32_SWAP_ON_BE( hdr_size
+ palette_size
*4 +
161 row_width
* image
->GetHeight() );
163 hdr
.data_offset
= wxUINT32_SWAP_ON_BE(hdr_size
+ palette_size
*4);
165 hdr
.bih_size
= wxUINT32_SWAP_ON_BE(hdr_size
- 14);
166 hdr
.width
= wxUINT32_SWAP_ON_BE(image
->GetWidth());
169 hdr
.height
= wxUINT32_SWAP_ON_BE(image
->GetHeight());
173 hdr
.height
= wxUINT32_SWAP_ON_BE(2 * image
->GetHeight());
175 hdr
.planes
= wxUINT16_SWAP_ON_BE(1); // always 1 plane
176 hdr
.bpp
= wxUINT16_SWAP_ON_BE(bpp
);
177 hdr
.compression
= 0; // RGB uncompressed
178 hdr
.size_of_bmp
= wxUINT32_SWAP_ON_BE(row_width
* image
->GetHeight());
179 hdr
.h_res
= hdr
.v_res
= wxUINT32_SWAP_ON_BE(72); // 72dpi is standard
180 hdr
.num_clrs
= wxUINT32_SWAP_ON_BE(palette_size
); // # colors in colormap
181 hdr
.num_signif_clrs
= 0; // all colors are significant
185 if (// VS: looks ugly but compilers tend to do ugly things with structs,
186 // like aligning hdr.filesize's ofset to dword :(
187 // VZ: we should add padding then...
188 !stream
.Write(&hdr
.magic
, 2) ||
189 !stream
.Write(&hdr
.filesize
, 4) ||
190 !stream
.Write(&hdr
.reserved
, 4) ||
191 !stream
.Write(&hdr
.data_offset
, 4)
195 wxLogError(_("BMP: Couldn't write the file (Bitmap) header."));
202 !stream
.Write(&hdr
.bih_size
, 4) ||
203 !stream
.Write(&hdr
.width
, 4) ||
204 !stream
.Write(&hdr
.height
, 4) ||
205 !stream
.Write(&hdr
.planes
, 2) ||
206 !stream
.Write(&hdr
.bpp
, 2) ||
207 !stream
.Write(&hdr
.compression
, 4) ||
208 !stream
.Write(&hdr
.size_of_bmp
, 4) ||
209 !stream
.Write(&hdr
.h_res
, 4) ||
210 !stream
.Write(&hdr
.v_res
, 4) ||
211 !stream
.Write(&hdr
.num_clrs
, 4) ||
212 !stream
.Write(&hdr
.num_signif_clrs
, 4)
216 wxLogError(_("BMP: Couldn't write the file (BitmapInfo) header."));
221 wxPalette
*palette
= NULL
; // entries for quantized images
222 wxUint8
*rgbquad
= NULL
; // for the RGBQUAD bytes for the colormap
223 wxImage
*q_image
= NULL
; // destination for quantized image
225 // if <24bpp use quantization to reduce colors for *some* of the formats
226 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_4BPP
) ||
227 (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_PALETTE
) )
229 // make a new palette and quantize the image
230 if (format
!= wxBMP_8BPP_PALETTE
)
232 q_image
= new wxImage();
234 // I get a delete error using Quantize when desired colors > 236
235 int quantize
= ((palette_size
> 236) ? 236 : palette_size
);
236 // fill the destination too, it gives much nicer 4bpp images
237 wxQuantize::Quantize( *image
, *q_image
, &palette
, quantize
, 0,
238 wxQUANTIZE_FILL_DESTINATION_IMAGE
);
243 palette
= new wxPalette(image
->GetPalette());
244 #endif // wxUSE_PALETTE
248 unsigned char r
, g
, b
;
249 rgbquad
= new wxUint8
[palette_size
*4];
251 for (i
= 0; i
< palette_size
; i
++)
254 if ( !palette
->GetRGB(i
, &r
, &g
, &b
) )
255 #endif // wxUSE_PALETTE
264 // make a 256 entry greyscale colormap or 2 entry black & white
265 else if ( (format
== wxBMP_8BPP_GREY
) || (format
== wxBMP_8BPP_RED
) ||
266 (format
== wxBMP_1BPP_BW
) )
269 rgbquad
= new wxUint8
[palette_size
*4];
271 for (i
= 0; i
< palette_size
; i
++)
273 // if 1BPP_BW then just 0 and 255 then exit
274 if (( i
> 0) && (format
== wxBMP_1BPP_BW
)) i
= 255;
282 // if the colormap was made, then it needs to be written
287 if ( !stream
.Write(rgbquad
, palette_size
*4) )
290 wxLogError(_("BMP: Couldn't write RGB color map."));
294 #endif // wxUSE_PALETTE
302 // pointer to the image data, use quantized if available
303 wxUint8
*data
= (wxUint8
*) image
->GetData();
304 if (q_image
) if (q_image
->Ok()) data
= (wxUint8
*) q_image
->GetData();
306 wxUint8
*buffer
= new wxUint8
[row_width
];
307 memset(buffer
, 0, row_width
);
311 for (y
= image
->GetHeight() -1; y
>= 0; y
--)
313 if ( format
== wxBMP_24BPP
) // 3 bytes per pixel red,green,blue
315 for ( x
= 0; x
< width
; x
++ )
317 pixel
= 3*(y
*width
+ x
);
319 buffer
[3*x
] = data
[pixel
+2];
320 buffer
[3*x
+ 1] = data
[pixel
+1];
321 buffer
[3*x
+ 2] = data
[pixel
];
324 else if ((format
== wxBMP_8BPP
) || // 1 byte per pixel in color
325 (format
== wxBMP_8BPP_PALETTE
))
327 for (x
= 0; x
< width
; x
++)
329 pixel
= 3*(y
*width
+ x
);
331 buffer
[x
] = palette
->GetPixel( data
[pixel
],
335 // FIXME: what should this be? use some std palette maybe?
337 #endif // wxUSE_PALETTE
340 else if ( format
== wxBMP_8BPP_GREY
) // 1 byte per pix, rgb ave to grey
342 for (x
= 0; x
< width
; x
++)
344 pixel
= 3*(y
*width
+ x
);
345 buffer
[x
] = (wxUint8
)(.299*data
[pixel
] +
350 else if ( format
== wxBMP_8BPP_RED
) // 1 byte per pixel, red as greys
352 for (x
= 0; x
< width
; x
++)
354 buffer
[x
] = (wxUint8
)data
[3*(y
*width
+ x
)];
357 else if ( format
== wxBMP_4BPP
) // 4 bpp in color
359 for (x
= 0; x
< width
; x
+=2)
361 pixel
= 3*(y
*width
+ x
);
363 // fill buffer, ignore if > width
366 ((wxUint8
)palette
->GetPixel(data
[pixel
],
368 data
[pixel
+2]) << 4) |
371 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3],
375 // FIXME: what should this be? use some std palette maybe?
377 #endif // wxUSE_PALETTE
380 else if ( format
== wxBMP_1BPP
) // 1 bpp in "color"
382 for (x
= 0; x
< width
; x
+=8)
384 pixel
= 3*(y
*width
+ x
);
387 buffer
[x
/8] = ((wxUint8
)palette
->GetPixel(data
[pixel
], data
[pixel
+1], data
[pixel
+2]) << 7) |
388 (((x
+1) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3], data
[pixel
+4], data
[pixel
+5]) << 6)) |
389 (((x
+2) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+6], data
[pixel
+7], data
[pixel
+8]) << 5)) |
390 (((x
+3) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+9], data
[pixel
+10], data
[pixel
+11]) << 4)) |
391 (((x
+4) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+12], data
[pixel
+13], data
[pixel
+14]) << 3)) |
392 (((x
+5) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+15], data
[pixel
+16], data
[pixel
+17]) << 2)) |
393 (((x
+6) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+18], data
[pixel
+19], data
[pixel
+20]) << 1)) |
394 (((x
+7) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+21], data
[pixel
+22], data
[pixel
+23]) ));
396 // FIXME: what should this be? use some std palette maybe?
398 #endif // wxUSE_PALETTE
401 else if ( format
== wxBMP_1BPP_BW
) // 1 bpp B&W colormap from red color ONLY
403 for (x
= 0; x
< width
; x
+=8)
405 pixel
= 3*(y
*width
+ x
);
408 (((wxUint8
)(data
[pixel
] /128.)) << 7) |
409 (((x
+1) > width
) ? 0 : (((wxUint8
)(data
[pixel
+3] /128.)) << 6)) |
410 (((x
+2) > width
) ? 0 : (((wxUint8
)(data
[pixel
+6] /128.)) << 5)) |
411 (((x
+3) > width
) ? 0 : (((wxUint8
)(data
[pixel
+9] /128.)) << 4)) |
412 (((x
+4) > width
) ? 0 : (((wxUint8
)(data
[pixel
+12]/128.)) << 3)) |
413 (((x
+5) > width
) ? 0 : (((wxUint8
)(data
[pixel
+15]/128.)) << 2)) |
414 (((x
+6) > width
) ? 0 : (((wxUint8
)(data
[pixel
+18]/128.)) << 1)) |
415 (((x
+7) > width
) ? 0 : (((wxUint8
)(data
[pixel
+21]/128.)) ));
419 if ( !stream
.Write(buffer
, row_width
) )
422 wxLogError(_("BMP: Couldn't write data."));
426 #endif // wxUSE_PALETTE
434 #endif // wxUSE_PALETTE
443 unsigned char r
, g
, b
;
446 bool wxBMPHandler::DoLoadDib(wxImage
* image
, int width
, int height
,
447 int bpp
, int ncolors
, int comp
,
448 off_t bmpOffset
, wxInputStream
& stream
,
449 bool verbose
, bool IsBmp
, bool hasPalette
)
451 wxInt32 aDword
, rmask
= 0, gmask
= 0, bmask
= 0;
452 int rshift
= 0, gshift
= 0, bshift
= 0;
453 int rbits
= 0, gbits
= 0, bbits
= 0;
459 // allocate space for palette if needed:
464 cmap
= new _cmap
[ncolors
];
468 wxLogError(_("BMP: Couldn't allocate memory."));
475 // destroy existing here instead of:
477 image
->Create(width
, height
);
479 unsigned char *ptr
= image
->GetData();
484 wxLogError( _("BMP: Couldn't allocate memory.") );
490 // Reading the palette, if it exists:
491 if ( bpp
< 16 && ncolors
!= 0 )
493 unsigned char* r
= new unsigned char[ncolors
];
494 unsigned char* g
= new unsigned char[ncolors
];
495 unsigned char* b
= new unsigned char[ncolors
];
496 for (int j
= 0; j
< ncolors
; j
++)
500 stream
.Read(bbuf
, 4);
511 //used in reading .ico file mask
512 r
[j
] = cmap
[j
].r
= j
* 255;
513 g
[j
] = cmap
[j
].g
= j
* 255;
514 b
[j
] = cmap
[j
].b
= j
* 255;
519 // Set the palette for the wxImage
520 image
->SetPalette(wxPalette(ncolors
, r
, g
, b
));
521 #endif // wxUSE_PALETTE
527 else if ( bpp
== 16 || bpp
== 32 )
529 if ( comp
== BI_BITFIELDS
)
532 stream
.Read(dbuf
, 4 * 3);
533 rmask
= wxINT32_SWAP_ON_BE(dbuf
[0]);
534 gmask
= wxINT32_SWAP_ON_BE(dbuf
[1]);
535 bmask
= wxINT32_SWAP_ON_BE(dbuf
[2]);
536 // find shift amount (Least significant bit of mask)
537 for (bit
= bpp
-1; bit
>=0; bit
--)
539 if (bmask
& (1 << bit
))
541 if (gmask
& (1 << bit
))
543 if (rmask
& (1 << bit
))
546 // Find number of bits in mask (MSB-LSB+1)
547 for (bit
= 0; bit
< bpp
; bit
++)
549 if (bmask
& (1 << bit
))
550 bbits
= bit
-bshift
+1;
551 if (gmask
& (1 << bit
))
552 gbits
= bit
-gshift
+1;
553 if (rmask
& (1 << bit
))
554 rbits
= bit
-rshift
+1;
557 else if ( bpp
== 16 )
569 else if ( bpp
== 32 )
584 * Reading the image data
587 stream
.SeekI(bmpOffset
); // else icon, just carry on
589 unsigned char *data
= ptr
;
591 /* set the whole image to the background color */
592 if ( bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
) )
594 for (int i
= 0; i
< width
* height
; i
++)
605 int linesize
= ((width
* bpp
+ 31) / 32) * 4;
607 /* BMPs are stored upside down */
608 for ( line
= (height
- 1); line
>= 0; line
-- )
611 for ( column
= 0; column
< width
; )
617 aByte
= stream
.GetC();
621 for (bit
= 0; bit
< 8 && column
< width
; bit
++)
623 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
;
632 if ( comp
== BI_RLE4
)
635 wxLogError(_("DIB Header: Cannot deal with 4bit encoded yet."));
643 for (nibble
= 0; nibble
< 2 && column
< width
; nibble
++)
645 index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
648 ptr
[poffset
] = cmap
[index
].r
;
649 ptr
[poffset
+ 1] = cmap
[index
].g
;
650 ptr
[poffset
+ 2] = cmap
[index
].b
;
657 if ( comp
== BI_RLE8
)
661 aByte
= stream
.GetC();
666 /* column = width; */
668 else if ( aByte
== 1 )
673 else if ( aByte
== 2 )
675 aByte
= stream
.GetC();
677 linepos
= column
* bpp
/ 8;
678 aByte
= stream
.GetC();
683 int absolute
= aByte
;
684 for (int k
= 0; k
< absolute
; k
++)
687 aByte
= stream
.GetC();
688 ptr
[poffset
] = cmap
[aByte
].r
;
689 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
690 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
693 if ( absolute
& 0x01 )
694 aByte
= stream
.GetC();
699 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
701 ptr
[poffset
] = cmap
[aByte
].r
;
702 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
703 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
711 ptr
[poffset
] = cmap
[aByte
].r
;
712 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
713 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
715 // linepos += size; seems to be wrong, RR
719 else if ( bpp
== 24 )
721 stream
.Read(bbuf
, 3);
723 ptr
[poffset
] = (unsigned char)bbuf
[2];
724 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
725 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
728 else if ( bpp
== 16 )
731 stream
.Read(&aWord
, 2);
732 aWord
= wxUINT16_SWAP_ON_BE(aWord
);
734 /* use the masks and calculated amonut of shift
735 to retrieve the color data out of the word. Then
736 shift it left by (8 - number of bits) such that
737 the image has the proper dynamic range */
738 temp
= (aWord
& rmask
) >> rshift
<< (8-rbits
);
740 temp
= (aWord
& gmask
) >> gshift
<< (8-gbits
);
741 ptr
[poffset
+ 1] = temp
;
742 temp
= (aWord
& bmask
) >> bshift
<< (8-bbits
);
743 ptr
[poffset
+ 2] = temp
;
749 stream
.Read(&aDword
, 4);
750 aDword
= wxINT32_SWAP_ON_BE(aDword
);
752 temp
= (aDword
& rmask
) >> rshift
;
754 temp
= (aDword
& gmask
) >> gshift
;
755 ptr
[poffset
+ 1] = temp
;
756 temp
= (aDword
& bmask
) >> bshift
;
757 ptr
[poffset
+ 2] = temp
;
761 while ( (linepos
< linesize
) && (comp
!= 1) && (comp
!= 2) )
763 stream
.Read(&aByte
, 1);
765 if ( stream
.LastError() != wxStream_NOERROR
)
772 image
->SetMask(FALSE
);
774 return ( stream
.LastError() == wxSTREAM_NO_ERROR
|| stream
.LastError() == wxSTREAM_EOF
);
777 bool wxBMPHandler::LoadDib(wxImage
*image
, wxInputStream
& stream
,
778 bool verbose
, bool IsBmp
)
785 offset
= 0; // keep gcc quiet
788 // read the header off the .BMP format file
790 offset
= stream
.TellI();
791 if (offset
== wxInvalidOffset
) offset
= 0;
793 stream
.Read(bbuf
, 2);
794 stream
.Read(dbuf
, 16);
798 stream
.Read(dbuf
, 4);
801 wxInt32 size
= wxINT32_SWAP_ON_BE(dbuf
[0]);
803 offset
= offset
+ wxINT32_SWAP_ON_BE(dbuf
[2]);
805 stream
.Read(dbuf
, 4 * 2);
806 int width
= (int)wxINT32_SWAP_ON_BE(dbuf
[0]);
807 int height
= (int)wxINT32_SWAP_ON_BE(dbuf
[1]);
808 if ( !IsBmp
)height
= height
/ 2; // for icons divide by 2
813 wxLogError( _("DIB Header: Image width > 32767 pixels for file.") );
816 if ( height
> 32767 )
819 wxLogError( _("DIB Header: Image height > 32767 pixels for file.") );
823 stream
.Read(&aWord
, 2);
826 int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
828 stream
.Read(&aWord
, 2);
829 int bpp
= (int)wxUINT16_SWAP_ON_BE(aWord
);
830 if ( bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
!= 16 && bpp
!= 24 && bpp
!= 32 )
833 wxLogError( _("DIB Header: Unknown bitdepth in file.") );
837 stream
.Read(dbuf
, 4 * 4);
838 int comp
= (int)wxINT32_SWAP_ON_BE(dbuf
[0]);
839 if ( comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&&
840 comp
!= BI_BITFIELDS
)
843 wxLogError( _("DIB Header: Unknown encoding in file.") );
847 stream
.Read(dbuf
, 4 * 2);
848 int ncolors
= (int)wxINT32_SWAP_ON_BE( dbuf
[0] );
851 /* some more sanity checks */
852 if (((comp
== BI_RLE4
) && (bpp
!= 4)) ||
853 ((comp
== BI_RLE8
) && (bpp
!= 8)) ||
854 ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
857 wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") );
861 //read DIB; this is the BMP image or the XOR part of an icon image
862 if ( !DoLoadDib(image
, width
, height
, bpp
, ncolors
, comp
, offset
, stream
,
863 verbose
, IsBmp
, TRUE
) )
866 wxLogError( _("Error in reading image DIB .") );
872 //read Icon mask which is monochrome
873 //there is no palette, so we will create one
875 if ( !DoLoadDib(&mask
, width
, height
, 1, 2, BI_RGB
, offset
, stream
,
876 verbose
, IsBmp
, FALSE
) )
879 wxLogError( _("ICO: Error in reading mask DIB.") );
882 image
->SetMaskFromImage(mask
, 255, 255, 255);
889 bool wxBMPHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
890 bool verbose
, int WXUNUSED(index
))
892 // Read a single DIB fom the file:
893 return LoadDib(image
, stream
, verbose
, TRUE
/*isBmp*/);
896 bool wxBMPHandler::DoCanRead(wxInputStream
& stream
)
898 unsigned char hdr
[2];
900 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
903 // do we have the BMP file signature?
904 return hdr
[0] == 'B' && hdr
[1] == 'M';
909 //-----------------------------------------------------------------------------
911 //-----------------------------------------------------------------------------
913 IMPLEMENT_DYNAMIC_CLASS(wxICOHandler
, wxBMPHandler
)
917 wxUint8 bWidth
; // Width of the image
918 wxUint8 bHeight
; // Height of the image (times 2)
919 wxUint8 bColorCount
; // Number of colors in image (0 if >=8bpp)
920 wxUint8 bReserved
; // Reserved
922 // these two are different in icons and cursors:
924 wxUint16 wPlanes
; // Color Planes or XHotSpot
925 wxUint16 wBitCount
; // Bits per pixel or YHotSpot
927 wxUint32 dwBytesInRes
; // how many bytes in this resource?
928 wxUint32 dwImageOffset
; // where in the file is this image
933 wxUint16 idReserved
; // Reserved
934 wxUint16 idType
; // resource type (1 for icons, 2 for cursors)
935 wxUint16 idCount
; // how many images?
939 bool wxICOHandler::SaveFile(wxImage
*image
,
940 wxOutputStream
& stream
,
944 bool bResult
= FALSE
;
945 //sanity check; icon must be less than 127 pixels high and 255 wide
946 if ( image
->GetHeight () > 127 )
949 wxLogError(_("ICO: Image too tall for an icon."));
952 if ( image
->GetWidth () > 255 )
955 wxLogError(_("ICO: Image too wide for an icon."));
959 int images
= 1; // only generate one image
961 // VS: This is a hack of sort - since ICO and CUR files are almost
962 // identical, we have all the meat in wxICOHandler and check for
963 // the actual (handler) type when the code has to distinguish between
965 int type
= (this->GetType() == wxBITMAP_TYPE_CUR
) ? 2 : 1;
967 // write a header, (ICONDIR)
968 // Calculate the header size
969 wxUint32 offset
= 3 * sizeof(wxUint16
);
972 IconDir
.idReserved
= 0;
973 IconDir
.idType
= wxUINT16_SWAP_ON_BE(type
);
974 IconDir
.idCount
= wxUINT16_SWAP_ON_BE(images
);
975 stream
.Write(&IconDir
.idReserved
, sizeof(IconDir
.idReserved
));
976 stream
.Write(&IconDir
.idType
, sizeof(IconDir
.idType
));
977 stream
.Write(&IconDir
.idCount
, sizeof(IconDir
.idCount
));
978 if ( !stream
.IsOk() )
981 wxLogError(_("ICO: Error writing the image file!"));
985 // for each iamage write a description ICONDIRENTRY:
986 ICONDIRENTRY icondirentry
;
987 for (int i
= 0; i
< images
; i
++)
991 if ( image
->HasMask() )
993 // make another image with black/white:
994 mask
= image
->ConvertToMono (image
->GetMaskRed(), image
->GetMaskGreen(), image
->GetMaskBlue() );
996 // now we need to change the masked regions to black:
997 unsigned char r
= image
->GetMaskRed();
998 unsigned char g
= image
->GetMaskGreen();
999 unsigned char b
= image
->GetMaskBlue();
1000 if ( (r
!= 0) || (g
!= 0) || (b
!= 0) )
1002 // Go round and apply black to the masked bits:
1004 for (i
= 0; i
< mask
.GetWidth(); i
++)
1006 for (j
= 0; j
< mask
.GetHeight(); j
++)
1008 if ((r
== mask
.GetRed(i
, j
)) &&
1009 (g
== mask
.GetGreen(i
, j
))&&
1010 (b
== mask
.GetBlue(i
, j
)) )
1011 image
->SetRGB(i
, j
, 0, 0, 0 );
1018 // just make a black mask all over:
1019 mask
= image
->Copy();
1021 for (i
= 0; i
< mask
.GetWidth(); i
++)
1022 for (j
= 0; j
< mask
.GetHeight(); j
++)
1023 mask
.SetRGB(i
, j
, 0, 0, 0 );
1025 // Set the formats for image and mask
1026 // (Windows never saves with more than 8 colors):
1027 image
->SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_8BPP
);
1029 // monochome bitmap:
1030 mask
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_1BPP_BW
);
1032 bool IsMask
= FALSE
;
1034 //calculate size and offset of image and mask
1035 wxCountingOutputStream cStream
;
1036 bResult
= SaveDib(image
, cStream
, verbose
, IsBmp
, IsMask
);
1040 wxLogError(_("ICO: Error writing the image file!"));
1045 bResult
= SaveDib(&mask
, cStream
, verbose
, IsBmp
, IsMask
);
1049 wxLogError(_("ICO: Error writing the image file!"));
1052 wxUint32 Size
= cStream
.GetSize();
1054 // wxCountingOutputStream::Ok() always returns TRUE for now and this
1055 // "if" provokes VC++ warnings in optimized build
1057 if ( !cStream
.Ok() )
1060 wxLogError(_("ICO: Error writing the image file!"));
1065 offset
= offset
+ sizeof(ICONDIRENTRY
);
1067 icondirentry
.bWidth
= image
->GetWidth();
1068 icondirentry
.bHeight
= 2 * image
->GetHeight();
1069 icondirentry
.bColorCount
= 0;
1070 icondirentry
.bReserved
= 0;
1071 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE(1);
1072 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE(wxBMP_8BPP
);
1073 if ( type
== 2 /*CUR*/)
1075 int hx
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ?
1076 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) :
1077 image
->GetWidth() / 2;
1078 int hy
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ?
1079 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) :
1080 image
->GetHeight() / 2;
1082 // actually write the values of the hot spot here:
1083 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE((wxUint16
)hx
);
1084 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE((wxUint16
)hy
);
1086 icondirentry
.dwBytesInRes
= wxUINT32_SWAP_ON_BE(Size
);
1087 icondirentry
.dwImageOffset
= wxUINT32_SWAP_ON_BE(offset
);
1089 // increase size to allow for the data written:
1093 stream
.Write(&icondirentry
.bWidth
, sizeof(icondirentry
.bWidth
));
1094 stream
.Write(&icondirentry
.bHeight
, sizeof(icondirentry
.bHeight
));
1095 stream
.Write(&icondirentry
.bColorCount
, sizeof(icondirentry
.bColorCount
));
1096 stream
.Write(&icondirentry
.bReserved
, sizeof(icondirentry
.bReserved
));
1097 stream
.Write(&icondirentry
.wPlanes
, sizeof(icondirentry
.wPlanes
));
1098 stream
.Write(&icondirentry
.wBitCount
, sizeof(icondirentry
.wBitCount
));
1099 stream
.Write(&icondirentry
.dwBytesInRes
, sizeof(icondirentry
.dwBytesInRes
));
1100 stream
.Write(&icondirentry
.dwImageOffset
, sizeof(icondirentry
.dwImageOffset
));
1101 if ( !stream
.IsOk() )
1104 wxLogError(_("ICO: Error writing the image file!"));
1108 // actually save it:
1110 bResult
= SaveDib(image
, stream
, verbose
, IsBmp
, IsMask
);
1114 wxLogError(_("ICO: Error writing the image file!"));
1119 bResult
= SaveDib(&mask
, stream
, verbose
, IsBmp
, IsMask
);
1123 wxLogError(_("ICO: Error writing the image file!"));
1127 } // end of for loop
1132 bool wxICOHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1133 bool verbose
, int index
)
1136 return DoLoadFile(image
, stream
, verbose
, index
);
1139 bool wxICOHandler::DoLoadFile(wxImage
*image
, wxInputStream
& stream
,
1140 bool WXUNUSED(verbose
), int index
)
1142 bool bResult
= FALSE
;
1147 off_t iPos
= stream
.TellI();
1148 stream
.Read(&IconDir
, sizeof(IconDir
));
1149 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1150 // nType is 1 for Icons, 2 for Cursors:
1151 wxUint16 nType
= wxUINT16_SWAP_ON_BE(IconDir
.idType
);
1153 // loop round the icons and choose the best one:
1154 ICONDIRENTRY
*pIconDirEntry
= new ICONDIRENTRY
[nIcons
];
1155 ICONDIRENTRY
*pCurrentEntry
= pIconDirEntry
;
1158 int iSel
= wxNOT_FOUND
;
1160 for (int i
= 0; i
< nIcons
; i
++ )
1162 stream
.Read(pCurrentEntry
, sizeof(ICONDIRENTRY
));
1163 // bHeight and bColorCount are wxUint8
1164 if ( pCurrentEntry
->bWidth
>= wMax
)
1166 // see if we have more colors, ==0 indicates > 8bpp:
1167 if ( pCurrentEntry
->bColorCount
== 0 )
1168 pCurrentEntry
->bColorCount
= 255;
1169 if ( pCurrentEntry
->bColorCount
>= colmax
)
1172 wMax
= pCurrentEntry
->bWidth
;
1173 colmax
= pCurrentEntry
->bColorCount
;
1181 // VS: Note that we *have* to run the loop above even if index != -1, because
1182 // it reads ICONDIRENTRies.
1186 if ( iSel
== wxNOT_FOUND
|| iSel
< 0 || iSel
>= nIcons
)
1188 wxLogError(_("ICO: Invalid icon index."));
1193 // seek to selected icon:
1194 pCurrentEntry
= pIconDirEntry
+ iSel
;
1195 stream
.SeekI(iPos
+ wxUINT32_SWAP_ON_BE(pCurrentEntry
->dwImageOffset
), wxFromStart
);
1196 bResult
= LoadDib(image
, stream
, TRUE
, IsBmp
);
1197 bool bIsCursorType
= (this->GetType() == wxBITMAP_TYPE_CUR
) || (this->GetType() == wxBITMAP_TYPE_ANI
);
1198 if ( bResult
&& bIsCursorType
&& nType
== 2 )
1200 // it is a cursor, so let's set the hotspot:
1201 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wPlanes
));
1202 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wBitCount
));
1205 delete[] pIconDirEntry
;
1209 int wxICOHandler::GetImageCount(wxInputStream
& stream
)
1212 off_t iPos
= stream
.TellI();
1214 stream
.Read(&IconDir
, sizeof(IconDir
));
1215 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1220 bool wxICOHandler::DoCanRead(wxInputStream
& stream
)
1223 unsigned char hdr
[4];
1224 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
1227 // hdr[2] is one for an icon and two for a cursor
1228 return hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\1' && hdr
[3] == '\0';
1233 //-----------------------------------------------------------------------------
1235 //-----------------------------------------------------------------------------
1237 IMPLEMENT_DYNAMIC_CLASS(wxCURHandler
, wxICOHandler
)
1239 bool wxCURHandler::DoCanRead(wxInputStream
& stream
)
1242 unsigned char hdr
[4];
1243 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
1246 // hdr[2] is one for an icon and two for a cursor
1247 return hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\2' && hdr
[3] == '\0';
1250 //-----------------------------------------------------------------------------
1252 //-----------------------------------------------------------------------------
1254 IMPLEMENT_DYNAMIC_CLASS(wxANIHandler
, wxCURHandler
)
1256 bool wxANIHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1257 bool verbose
, int index
)
1261 static const char *rifftxt
= "RIFF";
1262 static const char *listtxt
= "LIST";
1263 static const char *icotxt
= "icon";
1265 wxInt32 riff32
= (wxInt32
) rifftxt
;
1266 wxInt32 list32
= (wxInt32
) listtxt
;
1267 wxInt32 ico32
= (wxInt32
) icotxt
;
1272 stream
.Read(&FCC1
, 4);
1273 if ( FCC1
!= riff32
)
1276 // we have a riff file:
1277 while (stream
.IsOk())
1279 // we always have a data size
1280 stream
.Read(&datalen
, 4);
1281 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
1282 //data should be padded to make even number of bytes
1283 if (datalen
% 2 == 1) datalen
++ ;
1284 //now either data or a FCC
1285 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
1287 stream
.Read(&FCC2
, 4);
1291 if (FCC1
== ico32
&& iIcon
>= index
)
1293 return DoLoadFile(image
, stream
, verbose
, -1);
1297 stream
.SeekI(stream
.TellI() + datalen
);
1298 if ( FCC1
== ico32
)
1303 // try to read next data chunk:
1304 stream
.Read(&FCC1
, 4);
1309 bool wxANIHandler::DoCanRead(wxInputStream
& stream
)
1313 static const char *rifftxt
= "RIFF";
1314 static const char *listtxt
= "LIST";
1315 static const char *anihtxt
= "anih";
1317 wxInt32 riff32
= (wxInt32
) rifftxt
;
1318 wxInt32 list32
= (wxInt32
) listtxt
;
1319 wxInt32 anih32
= (wxInt32
) anihtxt
;
1322 if ( !stream
.Read(&FCC1
, 4) )
1325 if ( FCC1
!= riff32
)
1328 // we have a riff file:
1329 while ( stream
.IsOk() )
1331 if ( FCC1
== anih32
)
1333 // we always have a data size:
1334 stream
.Read(&datalen
, 4);
1335 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
1336 //data should be padded to make even number of bytes
1337 if (datalen
% 2 == 1) datalen
++ ;
1338 // now either data or a FCC:
1339 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
1341 stream
.Read(&FCC2
, 4);
1345 stream
.SeekI(stream
.TellI() + datalen
);
1348 // try to read next data chunk:
1349 if ( !stream
.Read(&FCC1
, 4) )
1351 // reading failed -- either EOF or IO error, bail out anyhow
1359 int wxANIHandler::GetImageCount(wxInputStream
& stream
)
1363 static const char *rifftxt
= "RIFF";
1364 static const char *listtxt
= "LIST";
1365 static const char *anihtxt
= "anih";
1367 wxInt32 riff32
= (wxInt32
) rifftxt
;
1368 wxInt32 list32
= (wxInt32
) listtxt
;
1369 wxInt32 anih32
= (wxInt32
) anihtxt
;
1372 stream
.Read(&FCC1
, 4);
1373 if ( FCC1
!= riff32
)
1376 // we have a riff file:
1377 while ( stream
.IsOk() )
1379 // we always have a data size:
1380 stream
.Read(&datalen
, 4);
1381 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
1382 //data should be padded to make even number of bytes
1383 if (datalen
% 2 == 1) datalen
++ ;
1384 // now either data or a FCC:
1385 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
1387 stream
.Read(&FCC2
, 4);
1391 if ( FCC1
== anih32
)
1393 wxUint32
*pData
= new wxUint32
[datalen
/4];
1394 stream
.Read(pData
, datalen
);
1395 int nIcons
= wxINT32_SWAP_ON_BE(*(pData
+ 1));
1400 stream
.SeekI(stream
.TellI() + datalen
);
1403 // try to read next data chunk:
1404 stream
.Read(&FCC1
, 4);
1410 #endif // wxUSE_ICO_CUR
1412 #endif // wxUSE_IMAGE && wxUSE_STREAMS