1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/imagbmp.cpp
3 // Purpose: wxImage BMP,ICO and CUR handlers
4 // Author: Robert Roebling, Chris Elliott
5 // Copyright: (c) Robert Roebling, Chris Elliott
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
18 #include "wx/imagbmp.h"
22 #include "wx/msw/wrapwin.h"
26 #include "wx/bitmap.h"
27 #include "wx/palette.h"
32 #include "wx/filefn.h"
33 #include "wx/wfstream.h"
34 #include "wx/quantize.h"
35 #include "wx/scopeguard.h"
36 #include "wx/anidecod.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
47 static bool CanReadICOOrCUR(wxInputStream
*stream
, wxUint16 resourceType
);
49 #endif // wxUSE_ICO_CUR
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
72 #define BI_BITFIELDS 3
75 #define poffset (line * width * 3 + column * 3)
77 bool wxBMPHandler::SaveFile(wxImage
*image
,
78 wxOutputStream
& stream
,
81 return SaveDib(image
, stream
, verbose
, true/*IsBmp*/, false/*IsMask*/);
84 bool wxBMPHandler::SaveDib(wxImage
*image
,
85 wxOutputStream
& stream
,
91 wxCHECK_MSG( image
, false, wxT("invalid pointer in wxBMPHandler::SaveFile") );
97 wxLogError(_("BMP: Couldn't save invalid image."));
102 // get the format of the BMP file to save, else use 24bpp
103 unsigned format
= wxBMP_24BPP
;
104 if ( image
->HasOption(wxIMAGE_OPTION_BMP_FORMAT
) )
105 format
= image
->GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT
);
107 wxUint16 bpp
; // # of bits per pixel
108 int palette_size
; // # of color map entries, ie. 2^bpp colors
110 // set the bpp and appropriate palette_size, and do additional checks
111 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_1BPP_BW
) )
116 else if ( format
== wxBMP_4BPP
)
121 else if ( (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_GREY
) ||
122 (format
== wxBMP_8BPP_RED
) || (format
== wxBMP_8BPP_PALETTE
) )
124 // need to set a wxPalette to use this, HOW TO CHECK IF VALID, SIZE?
125 if ((format
== wxBMP_8BPP_PALETTE
)
127 && !image
->HasPalette()
128 #endif // wxUSE_PALETTE
133 wxLogError(_("BMP: wxImage doesn't have own wxPalette."));
140 else // you get 24bpp
142 format
= wxBMP_24BPP
;
147 unsigned width
= image
->GetWidth();
148 unsigned row_padding
= (4 - ((width
* bpp
+ 7) / 8) % 4) % 4; // # bytes to pad to dword
149 unsigned row_width
= (width
* bpp
+ 7) / 8 + row_padding
; // # of bytes per row
154 wxUint16 magic
; // format magic, always 'BM'
155 wxUint32 filesize
; // total file size, inc. headers
156 wxUint32 reserved
; // for future use
157 wxUint32 data_offset
; // image data offset in the file
160 wxUint32 bih_size
; // 2nd part's size
161 wxUint32 width
, height
; // bitmap's dimensions
162 wxUint16 planes
; // num of planes
163 wxUint16 bpp
; // bits per pixel
164 wxUint32 compression
; // compression method
165 wxUint32 size_of_bmp
; // size of the bitmap
166 wxUint32 h_res
, v_res
; // image resolution in pixels-per-meter
167 wxUint32 num_clrs
; // number of colors used
168 wxUint32 num_signif_clrs
;// number of significant colors
171 wxUint32 hdr_size
= 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/;
173 hdr
.magic
= wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/);
174 hdr
.filesize
= wxUINT32_SWAP_ON_BE( hdr_size
+ palette_size
*4 +
175 row_width
* image
->GetHeight() );
177 hdr
.data_offset
= wxUINT32_SWAP_ON_BE(hdr_size
+ palette_size
*4);
179 hdr
.bih_size
= wxUINT32_SWAP_ON_BE(hdr_size
- 14);
180 hdr
.width
= wxUINT32_SWAP_ON_BE(image
->GetWidth());
183 hdr
.height
= wxUINT32_SWAP_ON_BE(image
->GetHeight());
187 hdr
.height
= wxUINT32_SWAP_ON_BE(2 * image
->GetHeight());
189 hdr
.planes
= wxUINT16_SWAP_ON_BE(1); // always 1 plane
190 hdr
.bpp
= wxUINT16_SWAP_ON_BE(bpp
);
191 hdr
.compression
= 0; // RGB uncompressed
192 hdr
.size_of_bmp
= wxUINT32_SWAP_ON_BE(row_width
* image
->GetHeight());
194 // get the resolution from the image options or fall back to 72dpi standard
195 // for the BMP format if not specified
197 switch ( GetResolutionFromOptions(*image
, &hres
, &vres
) )
200 wxFAIL_MSG( wxT("unexpected image resolution units") );
203 case wxIMAGE_RESOLUTION_NONE
:
206 // fall through to convert it to correct units
208 case wxIMAGE_RESOLUTION_INCHES
:
209 // convert resolution in inches to resolution in centimeters
210 hres
= (int)(10*mm2inches
*hres
);
211 vres
= (int)(10*mm2inches
*vres
);
212 // fall through to convert it to resolution in meters
214 case wxIMAGE_RESOLUTION_CM
:
215 // convert resolution in centimeters to resolution in meters
221 hdr
.h_res
= wxUINT32_SWAP_ON_BE(hres
);
222 hdr
.v_res
= wxUINT32_SWAP_ON_BE(vres
);
223 hdr
.num_clrs
= wxUINT32_SWAP_ON_BE(palette_size
); // # colors in colormap
224 hdr
.num_signif_clrs
= 0; // all colors are significant
228 if (// VS: looks ugly but compilers tend to do ugly things with structs,
229 // like aligning hdr.filesize's ofset to dword :(
230 // VZ: we should add padding then...
231 !stream
.WriteAll(&hdr
.magic
, 2) ||
232 !stream
.WriteAll(&hdr
.filesize
, 4) ||
233 !stream
.WriteAll(&hdr
.reserved
, 4) ||
234 !stream
.WriteAll(&hdr
.data_offset
, 4)
239 wxLogError(_("BMP: Couldn't write the file (Bitmap) header."));
247 !stream
.WriteAll(&hdr
.bih_size
, 4) ||
248 !stream
.WriteAll(&hdr
.width
, 4) ||
249 !stream
.WriteAll(&hdr
.height
, 4) ||
250 !stream
.WriteAll(&hdr
.planes
, 2) ||
251 !stream
.WriteAll(&hdr
.bpp
, 2) ||
252 !stream
.WriteAll(&hdr
.compression
, 4) ||
253 !stream
.WriteAll(&hdr
.size_of_bmp
, 4) ||
254 !stream
.WriteAll(&hdr
.h_res
, 4) ||
255 !stream
.WriteAll(&hdr
.v_res
, 4) ||
256 !stream
.WriteAll(&hdr
.num_clrs
, 4) ||
257 !stream
.WriteAll(&hdr
.num_signif_clrs
, 4)
262 wxLogError(_("BMP: Couldn't write the file (BitmapInfo) header."));
268 wxPalette
*palette
= NULL
; // entries for quantized images
269 wxUint8
*rgbquad
= NULL
; // for the RGBQUAD bytes for the colormap
270 wxImage
*q_image
= NULL
; // destination for quantized image
272 // if <24bpp use quantization to reduce colors for *some* of the formats
273 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_4BPP
) ||
274 (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_PALETTE
) )
276 // make a new palette and quantize the image
277 if (format
!= wxBMP_8BPP_PALETTE
)
279 q_image
= new wxImage();
281 // I get a delete error using Quantize when desired colors > 236
282 int quantize
= ((palette_size
> 236) ? 236 : palette_size
);
283 // fill the destination too, it gives much nicer 4bpp images
284 wxQuantize::Quantize( *image
, *q_image
, &palette
, quantize
, 0,
285 wxQUANTIZE_FILL_DESTINATION_IMAGE
);
290 palette
= new wxPalette(image
->GetPalette());
291 #endif // wxUSE_PALETTE
295 unsigned char r
, g
, b
;
296 rgbquad
= new wxUint8
[palette_size
*4];
298 for (i
= 0; i
< palette_size
; i
++)
301 if ( !palette
->GetRGB(i
, &r
, &g
, &b
) )
302 #endif // wxUSE_PALETTE
311 // make a 256 entry greyscale colormap or 2 entry black & white
312 else if ( (format
== wxBMP_8BPP_GREY
) || (format
== wxBMP_8BPP_RED
) ||
313 (format
== wxBMP_1BPP_BW
) )
315 rgbquad
= new wxUint8
[palette_size
*4];
317 for ( int i
= 0; i
< palette_size
; i
++ )
319 // if 1BPP_BW then the value should be either 0 or 255
320 wxUint8 c
= (wxUint8
)((i
> 0) && (format
== wxBMP_1BPP_BW
) ? 255 : i
);
329 // if the colormap was made, then it needs to be written
334 if ( !stream
.WriteAll(rgbquad
, palette_size
*4) )
338 wxLogError(_("BMP: Couldn't write RGB color map."));
343 #endif // wxUSE_PALETTE
351 // pointer to the image data, use quantized if available
352 wxUint8
*data
= (wxUint8
*) image
->GetData();
353 if (q_image
) if (q_image
->IsOk()) data
= (wxUint8
*) q_image
->GetData();
355 wxUint8
*buffer
= new wxUint8
[row_width
];
356 memset(buffer
, 0, row_width
);
360 for (y
= image
->GetHeight() -1; y
>= 0; y
--)
362 if ( format
== wxBMP_24BPP
) // 3 bytes per pixel red,green,blue
364 for ( x
= 0; x
< width
; x
++ )
366 pixel
= 3*(y
*width
+ x
);
368 buffer
[3*x
] = data
[pixel
+2];
369 buffer
[3*x
+ 1] = data
[pixel
+1];
370 buffer
[3*x
+ 2] = data
[pixel
];
373 else if ((format
== wxBMP_8BPP
) || // 1 byte per pixel in color
374 (format
== wxBMP_8BPP_PALETTE
))
376 for (x
= 0; x
< width
; x
++)
378 pixel
= 3*(y
*width
+ x
);
380 buffer
[x
] = (wxUint8
)palette
->GetPixel( data
[pixel
],
384 // FIXME: what should this be? use some std palette maybe?
386 #endif // wxUSE_PALETTE
389 else if ( format
== wxBMP_8BPP_GREY
) // 1 byte per pix, rgb ave to grey
391 for (x
= 0; x
< width
; x
++)
393 pixel
= 3*(y
*width
+ x
);
394 buffer
[x
] = (wxUint8
)(.299*data
[pixel
] +
399 else if ( format
== wxBMP_8BPP_RED
) // 1 byte per pixel, red as greys
401 for (x
= 0; x
< width
; x
++)
403 buffer
[x
] = (wxUint8
)data
[3*(y
*width
+ x
)];
406 else if ( format
== wxBMP_4BPP
) // 4 bpp in color
408 for (x
= 0; x
< width
; x
+=2)
410 pixel
= 3*(y
*width
+ x
);
412 // fill buffer, ignore if > width
414 buffer
[x
/2] = (wxUint8
)(
415 ((wxUint8
)palette
->GetPixel(data
[pixel
],
417 data
[pixel
+2]) << 4) |
420 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3],
424 // FIXME: what should this be? use some std palette maybe?
426 #endif // wxUSE_PALETTE
429 else if ( format
== wxBMP_1BPP
) // 1 bpp in "color"
431 for (x
= 0; x
< width
; x
+=8)
433 pixel
= 3*(y
*width
+ x
);
436 buffer
[x
/8] = (wxUint8
)(
437 ((wxUint8
)palette
->GetPixel(data
[pixel
], data
[pixel
+1], data
[pixel
+2]) << 7) |
438 (((x
+1) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3], data
[pixel
+4], data
[pixel
+5]) << 6)) |
439 (((x
+2) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+6], data
[pixel
+7], data
[pixel
+8]) << 5)) |
440 (((x
+3) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+9], data
[pixel
+10], data
[pixel
+11]) << 4)) |
441 (((x
+4) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+12], data
[pixel
+13], data
[pixel
+14]) << 3)) |
442 (((x
+5) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+15], data
[pixel
+16], data
[pixel
+17]) << 2)) |
443 (((x
+6) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+18], data
[pixel
+19], data
[pixel
+20]) << 1)) |
444 (((x
+7) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+21], data
[pixel
+22], data
[pixel
+23]) )) );
446 // FIXME: what should this be? use some std palette maybe?
448 #endif // wxUSE_PALETTE
451 else if ( format
== wxBMP_1BPP_BW
) // 1 bpp B&W colormap from red color ONLY
453 for (x
= 0; x
< width
; x
+=8)
455 pixel
= 3*(y
*width
+ x
);
457 buffer
[x
/8] = (wxUint8
)(
458 (((wxUint8
)(data
[pixel
] /128.)) << 7) |
459 (((x
+1) > width
) ? 0 : (((wxUint8
)(data
[pixel
+3] /128.)) << 6)) |
460 (((x
+2) > width
) ? 0 : (((wxUint8
)(data
[pixel
+6] /128.)) << 5)) |
461 (((x
+3) > width
) ? 0 : (((wxUint8
)(data
[pixel
+9] /128.)) << 4)) |
462 (((x
+4) > width
) ? 0 : (((wxUint8
)(data
[pixel
+12]/128.)) << 3)) |
463 (((x
+5) > width
) ? 0 : (((wxUint8
)(data
[pixel
+15]/128.)) << 2)) |
464 (((x
+6) > width
) ? 0 : (((wxUint8
)(data
[pixel
+18]/128.)) << 1)) |
465 (((x
+7) > width
) ? 0 : (((wxUint8
)(data
[pixel
+21]/128.)) )) );
469 if ( !stream
.WriteAll(buffer
, row_width
) )
473 wxLogError(_("BMP: Couldn't write data."));
478 #endif // wxUSE_PALETTE
486 #endif // wxUSE_PALETTE
495 static void Free(BMPPalette
* pal
) { delete [] pal
; }
497 unsigned char r
, g
, b
;
500 bool wxBMPHandler::DoLoadDib(wxImage
* image
, int width
, int height
,
501 int bpp
, int ncolors
, int comp
,
502 wxFileOffset bmpOffset
, wxInputStream
& stream
,
503 bool verbose
, bool IsBmp
, bool hasPalette
)
505 wxInt32 aDword
, rmask
= 0, gmask
= 0, bmask
= 0, amask
= 0;
506 int rshift
= 0, gshift
= 0, bshift
= 0, ashift
= 0;
507 int rbits
= 0, gbits
= 0, bbits
= 0;
513 // allocate space for palette if needed:
518 cmap
= new BMPPalette
[ncolors
];
523 wxLogError(_("BMP: Couldn't allocate memory."));
533 wxON_BLOCK_EXIT1(&BMPPalette::Free
, cmap
);
535 bool isUpsideDown
= true;
539 isUpsideDown
= false;
543 // destroy existing here instead of:
545 image
->Create(width
, height
);
547 unsigned char *ptr
= image
->GetData();
553 wxLogError( _("BMP: Couldn't allocate memory.") );
558 unsigned char *alpha
;
561 // tell the image to allocate an alpha buffer
563 alpha
= image
->GetAlpha();
568 wxLogError(_("BMP: Couldn't allocate memory."));
578 // Reading the palette, if it exists:
579 if ( bpp
< 16 && ncolors
!= 0 )
581 unsigned char* r
= new unsigned char[ncolors
];
582 unsigned char* g
= new unsigned char[ncolors
];
583 unsigned char* b
= new unsigned char[ncolors
];
584 for (int j
= 0; j
< ncolors
; j
++)
588 if ( !stream
.ReadAll(bbuf
, 4) )
601 //used in reading .ico file mask
604 b
[j
] = cmap
[j
].b
= ( j
? 255 : 0 );
609 // Set the palette for the wxImage
610 image
->SetPalette(wxPalette(ncolors
, r
, g
, b
));
611 #endif // wxUSE_PALETTE
617 else if ( bpp
== 16 || bpp
== 32 )
619 if ( comp
== BI_BITFIELDS
)
622 if ( !stream
.ReadAll(dbuf
, 4 * 3) )
625 rmask
= wxINT32_SWAP_ON_BE(dbuf
[0]);
626 gmask
= wxINT32_SWAP_ON_BE(dbuf
[1]);
627 bmask
= wxINT32_SWAP_ON_BE(dbuf
[2]);
628 // find shift amount (Least significant bit of mask)
629 for (bit
= bpp
-1; bit
>=0; bit
--)
631 if (bmask
& (1 << bit
))
633 if (gmask
& (1 << bit
))
635 if (rmask
& (1 << bit
))
638 // Find number of bits in mask (MSB-LSB+1)
639 for (bit
= 0; bit
< bpp
; bit
++)
641 if (bmask
& (1 << bit
))
642 bbits
= bit
-bshift
+1;
643 if (gmask
& (1 << bit
))
644 gbits
= bit
-gshift
+1;
645 if (rmask
& (1 << bit
))
646 rbits
= bit
-rshift
+1;
649 else if ( bpp
== 16 )
661 else if ( bpp
== 32 )
679 * Reading the image data
683 // NOTE: seeking a positive amount in wxFromCurrent mode allows us to
684 // load even non-seekable streams (see wxInputStream::SeekI docs)!
685 const wxFileOffset pos
= stream
.TellI();
686 if ( pos
== wxInvalidOffset
||
688 stream
.SeekI(bmpOffset
- pos
, wxFromCurrent
) == wxInvalidOffset
) )
690 //else: icon, just carry on
693 unsigned char *data
= ptr
;
695 /* set the whole image to the background color */
696 if ( bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
) )
698 for (int i
= 0; i
< width
* height
; i
++)
707 int linesize
= ((width
* bpp
+ 31) / 32) * 4;
709 // flag indicating if we have any not fully transparent alpha values: this
710 // is used to account for the bitmaps which use 32bpp format (normally
711 // meaning that they have alpha channel) but have only zeroes in it so that
712 // without this hack they appear fully transparent -- and as this is
713 // unlikely intentional, we consider that they don't have alpha at all in
714 // this case (see #10915)
715 bool hasValidAlpha
= false;
717 for ( int row
= 0; row
< height
; row
++ )
719 int line
= isUpsideDown
? height
- 1 - row
: row
;
722 for ( int column
= 0; column
< width
; )
727 aByte
= stream
.GetC();
728 if ( !stream
.IsOk() )
733 for (int bit
= 0; bit
< 8 && column
< width
; bit
++)
735 int index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
736 ptr
[poffset
] = cmap
[index
].r
;
737 ptr
[poffset
+ 1] = cmap
[index
].g
;
738 ptr
[poffset
+ 2] = cmap
[index
].b
;
744 if ( comp
== BI_RLE4
)
748 aByte
= stream
.GetC();
749 if ( !stream
.IsOk() )
756 // end of scanline marker
760 else if ( aByte
== 1 )
762 // end of RLE data marker, stop decoding
766 else if ( aByte
== 2 )
768 // delta marker, move in image
769 aByte
= stream
.GetC();
770 if ( !stream
.IsOk() )
773 linepos
= column
* bpp
/ 4;
774 aByte
= stream
.GetC();
775 if ( !stream
.IsOk() )
777 row
+= aByte
; // upside down
781 int absolute
= aByte
;
784 for (int k
= 0; k
< absolute
; k
++)
789 aByte
= stream
.GetC();
790 if ( !stream
.IsOk() )
792 nibble
[0] = (wxUint8
)( (aByte
& 0xF0) >> 4 ) ;
793 nibble
[1] = (wxUint8
)( aByte
& 0x0F ) ;
795 ptr
[poffset
] = cmap
[nibble
[k%2
]].r
;
796 ptr
[poffset
+ 1] = cmap
[nibble
[k%2
]].g
;
797 ptr
[poffset
+ 2] = cmap
[nibble
[k%2
]].b
;
802 if ( readBytes
& 0x01 )
804 aByte
= stream
.GetC();
805 if ( !stream
.IsOk() )
813 nibble
[0] = (wxUint8
)( (aByte
& 0xF0) >> 4 ) ;
814 nibble
[1] = (wxUint8
)( aByte
& 0x0F ) ;
816 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
818 ptr
[poffset
] = cmap
[nibble
[l%2
]].r
;
819 ptr
[poffset
+ 1] = cmap
[nibble
[l%2
]].g
;
820 ptr
[poffset
+ 2] = cmap
[nibble
[l%2
]].b
;
829 for (int nibble
= 0; nibble
< 2 && column
< width
; nibble
++)
831 int index
= ((aByte
& (0xF0 >> (nibble
* 4))) >> (!nibble
* 4));
834 ptr
[poffset
] = cmap
[index
].r
;
835 ptr
[poffset
+ 1] = cmap
[index
].g
;
836 ptr
[poffset
+ 2] = cmap
[index
].b
;
843 if ( comp
== BI_RLE8
)
847 aByte
= stream
.GetC();
848 if ( !stream
.IsOk() )
855 // end of scanline marker
859 else if ( aByte
== 1 )
861 // end of RLE data marker, stop decoding
865 else if ( aByte
== 2 )
867 // delta marker, move in image
868 aByte
= stream
.GetC();
869 if ( !stream
.IsOk() )
872 linepos
= column
* bpp
/ 8;
873 aByte
= stream
.GetC();
874 if ( !stream
.IsOk() )
880 int absolute
= aByte
;
881 for (int k
= 0; k
< absolute
; k
++)
884 aByte
= stream
.GetC();
885 if ( !stream
.IsOk() )
887 ptr
[poffset
] = cmap
[aByte
].r
;
888 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
889 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
892 if ( absolute
& 0x01 )
894 aByte
= stream
.GetC();
895 if ( !stream
.IsOk() )
902 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
904 ptr
[poffset
] = cmap
[aByte
].r
;
905 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
906 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
914 ptr
[poffset
] = cmap
[aByte
].r
;
915 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
916 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
918 // linepos += size; seems to be wrong, RR
922 else if ( bpp
== 24 )
924 if ( !stream
.ReadAll(bbuf
, 3) )
927 ptr
[poffset
] = (unsigned char)bbuf
[2];
928 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
929 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
932 else if ( bpp
== 16 )
935 if ( !stream
.ReadAll(&aWord
, 2) )
937 aWord
= wxUINT16_SWAP_ON_BE(aWord
);
939 /* Use the masks and calculated amount of shift
940 to retrieve the color data out of the word. Then
941 shift it left by (8 - number of bits) such that
942 the image has the proper dynamic range */
943 temp
= (unsigned char)(((aWord
& rmask
) >> rshift
) << (8-rbits
));
945 temp
= (unsigned char)(((aWord
& gmask
) >> gshift
) << (8-gbits
));
946 ptr
[poffset
+ 1] = temp
;
947 temp
= (unsigned char)(((aWord
& bmask
) >> bshift
) << (8-bbits
));
948 ptr
[poffset
+ 2] = temp
;
954 if ( !stream
.ReadAll(&aDword
, 4) )
957 aDword
= wxINT32_SWAP_ON_BE(aDword
);
959 temp
= (unsigned char)((aDword
& rmask
) >> rshift
);
961 temp
= (unsigned char)((aDword
& gmask
) >> gshift
);
962 ptr
[poffset
+ 1] = temp
;
963 temp
= (unsigned char)((aDword
& bmask
) >> bshift
);
964 ptr
[poffset
+ 2] = temp
;
967 temp
= (unsigned char)((aDword
& amask
) >> ashift
);
968 alpha
[line
* width
+ column
] = temp
;
970 if ( temp
!= wxALPHA_TRANSPARENT
)
971 hasValidAlpha
= true;
976 while ( (linepos
< linesize
) && (comp
!= 1) && (comp
!= 2) )
979 if ( !stream
.ReadAll(&aByte
, 1) )
984 image
->SetMask(false);
986 // check if we had any valid alpha values in this bitmap
987 if ( alpha
&& !hasValidAlpha
)
989 // we didn't, so finally discard the alpha channel completely
993 const wxStreamError err
= stream
.GetLastError();
994 return err
== wxSTREAM_NO_ERROR
|| err
== wxSTREAM_EOF
;
997 bool wxBMPHandler::LoadDib(wxImage
*image
, wxInputStream
& stream
,
998 bool verbose
, bool IsBmp
)
1006 // read the header off the .BMP format file
1007 if ( !stream
.ReadAll(bbuf
, 2) ||
1008 !stream
.ReadAll(dbuf
, 16) )
1013 if ( !stream
.ReadAll(dbuf
, 4) )
1017 wxInt32 size
= wxINT32_SWAP_ON_BE(dbuf
[0]);
1019 wxFileOffset offset
= wxINT32_SWAP_ON_BE(dbuf
[2]);
1021 if ( !stream
.ReadAll(dbuf
, 4 * 2) )
1024 int width
= wxINT32_SWAP_ON_BE((int)dbuf
[0]);
1025 int height
= wxINT32_SWAP_ON_BE((int)dbuf
[1]);
1026 if ( !IsBmp
)height
= height
/ 2; // for icons divide by 2
1028 if ( width
> 32767 )
1032 wxLogError( _("DIB Header: Image width > 32767 pixels for file.") );
1036 if ( height
> 32767 )
1040 wxLogError( _("DIB Header: Image height > 32767 pixels for file.") );
1045 if ( !stream
.ReadAll(&aWord
, 2) )
1050 int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
1052 if ( !stream
.ReadAll(&aWord
, 2) )
1055 int bpp
= wxUINT16_SWAP_ON_BE((int)aWord
);
1056 if ( bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
!= 16 && bpp
!= 24 && bpp
!= 32 )
1060 wxLogError( _("DIB Header: Unknown bitdepth in file.") );
1065 if ( !stream
.ReadAll(dbuf
, 4 * 4) )
1068 int comp
= wxINT32_SWAP_ON_BE((int)dbuf
[0]);
1069 if ( comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&&
1070 comp
!= BI_BITFIELDS
)
1074 wxLogError( _("DIB Header: Unknown encoding in file.") );
1079 if ( !stream
.ReadAll(dbuf
, 4 * 2) )
1082 int ncolors
= wxINT32_SWAP_ON_BE( (int)dbuf
[0] );
1085 /* some more sanity checks */
1086 if (((comp
== BI_RLE4
) && (bpp
!= 4)) ||
1087 ((comp
== BI_RLE8
) && (bpp
!= 8)) ||
1088 ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
1092 wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") );
1097 //read DIB; this is the BMP image or the XOR part of an icon image
1098 if ( !DoLoadDib(image
, width
, height
, bpp
, ncolors
, comp
, offset
, stream
,
1099 verbose
, IsBmp
, true) )
1103 wxLogError( _("Error in reading image DIB.") );
1110 //read Icon mask which is monochrome
1111 //there is no palette, so we will create one
1113 if ( !DoLoadDib(&mask
, width
, height
, 1, 2, BI_RGB
, offset
, stream
,
1114 verbose
, IsBmp
, false) )
1118 wxLogError( _("ICO: Error in reading mask DIB.") );
1122 image
->SetMaskFromImage(mask
, 255, 255, 255);
1126 // the resolution in the bitmap header is in meters, convert to centimeters
1127 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONUNIT
, wxIMAGE_RESOLUTION_CM
);
1128 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONX
, dbuf
[2]/100);
1129 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONY
, dbuf
[3]/100);
1134 bool wxBMPHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1135 bool verbose
, int WXUNUSED(index
))
1137 // Read a single DIB fom the file:
1138 return LoadDib(image
, stream
, verbose
, true/*isBmp*/);
1141 bool wxBMPHandler::DoCanRead(wxInputStream
& stream
)
1143 unsigned char hdr
[2];
1145 if ( !stream
.ReadAll(hdr
, WXSIZEOF(hdr
)) ) // it's ok to modify the stream position here
1148 // do we have the BMP file signature?
1149 return hdr
[0] == 'B' && hdr
[1] == 'M';
1152 #endif // wxUSE_STREAMS
1156 //-----------------------------------------------------------------------------
1158 //-----------------------------------------------------------------------------
1160 IMPLEMENT_DYNAMIC_CLASS(wxICOHandler
, wxBMPHandler
)
1166 wxUint8 bWidth
; // Width of the image
1167 wxUint8 bHeight
; // Height of the image (times 2)
1168 wxUint8 bColorCount
; // Number of colors in image (0 if >=8bpp)
1169 wxUint8 bReserved
; // Reserved
1171 // these two are different in icons and cursors:
1173 wxUint16 wPlanes
; // Color Planes or XHotSpot
1174 wxUint16 wBitCount
; // Bits per pixel or YHotSpot
1176 wxUint32 dwBytesInRes
; // how many bytes in this resource?
1177 wxUint32 dwImageOffset
; // where in the file is this image
1182 wxUint16 idReserved
; // Reserved
1183 wxUint16 idType
; // resource type (1 for icons, 2 for cursors)
1184 wxUint16 idCount
; // how many images?
1188 bool wxICOHandler::SaveFile(wxImage
*image
,
1189 wxOutputStream
& stream
,
1193 //sanity check; icon must be less than 127 pixels high and 255 wide
1194 if ( image
->GetHeight () > 127 )
1198 wxLogError(_("ICO: Image too tall for an icon."));
1202 if ( image
->GetWidth () > 255 )
1206 wxLogError(_("ICO: Image too wide for an icon."));
1211 const int images
= 1; // only generate one image
1213 // VS: This is a hack of sort - since ICO and CUR files are almost
1214 // identical, we have all the meat in wxICOHandler and check for
1215 // the actual (handler) type when the code has to distinguish between
1217 int type
= (this->GetType() == wxBITMAP_TYPE_CUR
) ? 2 : 1;
1219 // write a header, (ICONDIR)
1220 // Calculate the header size
1221 wxUint32 offset
= 3 * sizeof(wxUint16
);
1224 IconDir
.idReserved
= 0;
1225 IconDir
.idType
= wxUINT16_SWAP_ON_BE((wxUint16
)type
);
1226 IconDir
.idCount
= wxUINT16_SWAP_ON_BE((wxUint16
)images
);
1227 if ( !stream
.WriteAll(&IconDir
.idReserved
, sizeof(IconDir
.idReserved
)) ||
1228 !stream
.WriteAll(&IconDir
.idType
, sizeof(IconDir
.idType
)) ||
1229 !stream
.WriteAll(&IconDir
.idCount
, sizeof(IconDir
.idCount
)) )
1233 wxLogError(_("ICO: Error writing the image file!"));
1238 // for each iamage write a description ICONDIRENTRY:
1239 ICONDIRENTRY icondirentry
;
1240 for (int img
= 0; img
< images
; img
++)
1244 if ( image
->HasMask() )
1246 // make another image with black/white:
1247 mask
= image
->ConvertToMono (image
->GetMaskRed(), image
->GetMaskGreen(), image
->GetMaskBlue() );
1249 // now we need to change the masked regions to black:
1250 unsigned char r
= image
->GetMaskRed();
1251 unsigned char g
= image
->GetMaskGreen();
1252 unsigned char b
= image
->GetMaskBlue();
1253 if ( (r
!= 0) || (g
!= 0) || (b
!= 0) )
1255 // Go round and apply black to the masked bits:
1257 for (i
= 0; i
< mask
.GetWidth(); i
++)
1259 for (j
= 0; j
< mask
.GetHeight(); j
++)
1261 if ((r
== mask
.GetRed(i
, j
)) &&
1262 (g
== mask
.GetGreen(i
, j
))&&
1263 (b
== mask
.GetBlue(i
, j
)) )
1264 image
->SetRGB(i
, j
, 0, 0, 0 );
1271 // just make a black mask all over:
1272 mask
= image
->Copy();
1274 for (i
= 0; i
< mask
.GetWidth(); i
++)
1275 for (j
= 0; j
< mask
.GetHeight(); j
++)
1276 mask
.SetRGB(i
, j
, 0, 0, 0 );
1278 // Set the formats for image and mask
1279 // (Windows never saves with more than 8 colors):
1280 image
->SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_8BPP
);
1282 // monochome bitmap:
1283 mask
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_1BPP_BW
);
1285 bool IsMask
= false;
1287 //calculate size and offset of image and mask
1288 wxCountingOutputStream cStream
;
1289 bool bResult
= SaveDib(image
, cStream
, verbose
, IsBmp
, IsMask
);
1294 wxLogError(_("ICO: Error writing the image file!"));
1300 bResult
= SaveDib(&mask
, cStream
, verbose
, IsBmp
, IsMask
);
1305 wxLogError(_("ICO: Error writing the image file!"));
1309 wxUint32 Size
= cStream
.GetSize();
1311 // wxCountingOutputStream::IsOk() always returns true for now and this
1312 // "if" provokes VC++ warnings in optimized build
1314 if ( !cStream
.IsOk() )
1318 wxLogError(_("ICO: Error writing the image file!"));
1324 offset
= offset
+ sizeof(ICONDIRENTRY
);
1326 icondirentry
.bWidth
= (wxUint8
)image
->GetWidth();
1327 icondirentry
.bHeight
= (wxUint8
)(2 * image
->GetHeight());
1328 icondirentry
.bColorCount
= 0;
1329 icondirentry
.bReserved
= 0;
1330 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE(1);
1331 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE(wxBMP_8BPP
);
1332 if ( type
== 2 /*CUR*/)
1334 int hx
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ?
1335 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) :
1336 image
->GetWidth() / 2;
1337 int hy
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ?
1338 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) :
1339 image
->GetHeight() / 2;
1341 // actually write the values of the hot spot here:
1342 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE((wxUint16
)hx
);
1343 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE((wxUint16
)hy
);
1345 icondirentry
.dwBytesInRes
= wxUINT32_SWAP_ON_BE(Size
);
1346 icondirentry
.dwImageOffset
= wxUINT32_SWAP_ON_BE(offset
);
1348 // increase size to allow for the data written:
1352 if ( !stream
.WriteAll(&icondirentry
.bWidth
, sizeof(icondirentry
.bWidth
)) ||
1353 !stream
.WriteAll(&icondirentry
.bHeight
, sizeof(icondirentry
.bHeight
)) ||
1354 !stream
.WriteAll(&icondirentry
.bColorCount
, sizeof(icondirentry
.bColorCount
)) ||
1355 !stream
.WriteAll(&icondirentry
.bReserved
, sizeof(icondirentry
.bReserved
)) ||
1356 !stream
.WriteAll(&icondirentry
.wPlanes
, sizeof(icondirentry
.wPlanes
)) ||
1357 !stream
.WriteAll(&icondirentry
.wBitCount
, sizeof(icondirentry
.wBitCount
)) ||
1358 !stream
.WriteAll(&icondirentry
.dwBytesInRes
, sizeof(icondirentry
.dwBytesInRes
)) ||
1359 !stream
.WriteAll(&icondirentry
.dwImageOffset
, sizeof(icondirentry
.dwImageOffset
)) )
1363 wxLogError(_("ICO: Error writing the image file!"));
1368 // actually save it:
1370 bResult
= SaveDib(image
, stream
, verbose
, IsBmp
, IsMask
);
1375 wxLogError(_("ICO: Error writing the image file!"));
1381 bResult
= SaveDib(&mask
, stream
, verbose
, IsBmp
, IsMask
);
1386 wxLogError(_("ICO: Error writing the image file!"));
1391 } // end of for loop
1396 bool wxICOHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1397 bool verbose
, int index
)
1399 if ( stream
.IsSeekable() && stream
.SeekI(0) == wxInvalidOffset
)
1404 return DoLoadFile(image
, stream
, verbose
, index
);
1407 bool wxICOHandler::DoLoadFile(wxImage
*image
, wxInputStream
& stream
,
1408 bool WXUNUSED(verbose
), int index
)
1410 bool bResult
wxDUMMY_INITIALIZE(false);
1415 if ( !stream
.ReadAll(&IconDir
, sizeof(IconDir
)) )
1418 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1420 // nType is 1 for Icons, 2 for Cursors:
1421 wxUint16 nType
= wxUINT16_SWAP_ON_BE(IconDir
.idType
);
1423 // loop round the icons and choose the best one:
1424 ICONDIRENTRY
*pIconDirEntry
= new ICONDIRENTRY
[nIcons
];
1425 ICONDIRENTRY
*pCurrentEntry
= pIconDirEntry
;
1428 int iSel
= wxNOT_FOUND
;
1430 // remember how many bytes we read from the stream:
1431 wxFileOffset alreadySeeked
= sizeof(IconDir
);
1433 for (unsigned int i
= 0; i
< nIcons
; i
++ )
1435 if ( !stream
.ReadAll(pCurrentEntry
, sizeof(ICONDIRENTRY
)) )
1438 alreadySeeked
+= stream
.LastRead();
1440 // bHeight and bColorCount are wxUint8
1441 if ( pCurrentEntry
->bWidth
>= wMax
)
1443 // see if we have more colors, ==0 indicates > 8bpp:
1444 if ( pCurrentEntry
->bColorCount
== 0 )
1445 pCurrentEntry
->bColorCount
= 255;
1446 if ( pCurrentEntry
->bColorCount
>= colmax
)
1449 wMax
= pCurrentEntry
->bWidth
;
1450 colmax
= pCurrentEntry
->bColorCount
;
1459 // VS: Note that we *have* to run the loop above even if index != -1, because
1460 // it reads ICONDIRENTRies.
1464 if ( iSel
== wxNOT_FOUND
|| iSel
< 0 || iSel
>= nIcons
)
1466 wxLogError(_("ICO: Invalid icon index."));
1471 // seek to selected icon:
1472 pCurrentEntry
= pIconDirEntry
+ iSel
;
1474 // NOTE: seeking a positive amount in wxFromCurrent mode allows us to
1475 // load even non-seekable streams (see wxInputStream::SeekI docs)!
1476 wxFileOffset offset
= wxUINT32_SWAP_ON_BE(pCurrentEntry
->dwImageOffset
) - alreadySeeked
;
1477 if (offset
!= 0 && stream
.SeekI(offset
, wxFromCurrent
) == wxInvalidOffset
)
1480 bResult
= LoadDib(image
, stream
, true, IsBmp
);
1481 bool bIsCursorType
= (this->GetType() == wxBITMAP_TYPE_CUR
) || (this->GetType() == wxBITMAP_TYPE_ANI
);
1482 if ( bResult
&& bIsCursorType
&& nType
== 2 )
1484 // it is a cursor, so let's set the hotspot:
1485 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wPlanes
));
1486 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wBitCount
));
1490 delete [] pIconDirEntry
;
1495 int wxICOHandler::DoGetImageCount(wxInputStream
& stream
)
1497 // It's ok to modify the stream position in this function.
1499 if ( stream
.IsSeekable() && stream
.SeekI(0) == wxInvalidOffset
)
1506 if ( !stream
.ReadAll(&IconDir
, sizeof(IconDir
)) )
1509 return (int)wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1512 bool wxICOHandler::DoCanRead(wxInputStream
& stream
)
1514 return CanReadICOOrCUR(&stream
, 1 /*for identifying an icon*/);
1518 #endif // wxUSE_STREAMS
1521 //-----------------------------------------------------------------------------
1523 //-----------------------------------------------------------------------------
1525 IMPLEMENT_DYNAMIC_CLASS(wxCURHandler
, wxICOHandler
)
1529 bool wxCURHandler::DoCanRead(wxInputStream
& stream
)
1531 return CanReadICOOrCUR(&stream
, 2 /*for identifying a cursor*/);
1534 #endif // wxUSE_STREAMS
1536 //-----------------------------------------------------------------------------
1538 //-----------------------------------------------------------------------------
1540 IMPLEMENT_DYNAMIC_CLASS(wxANIHandler
, wxCURHandler
)
1544 bool wxANIHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1545 bool WXUNUSED(verbose
), int index
)
1547 wxANIDecoder decoder
;
1548 if (!decoder
.Load(stream
))
1551 return decoder
.ConvertToImage(index
!= -1 ? (size_t)index
: 0, image
);
1554 bool wxANIHandler::DoCanRead(wxInputStream
& stream
)
1557 return decod
.CanRead(stream
);
1558 // it's ok to modify the stream position here
1561 int wxANIHandler::DoGetImageCount(wxInputStream
& stream
)
1563 wxANIDecoder decoder
;
1564 if (!decoder
.Load(stream
)) // it's ok to modify the stream position here
1567 return decoder
.GetFrameCount();
1570 static bool CanReadICOOrCUR(wxInputStream
*stream
, wxUint16 resourceType
)
1572 // It's ok to modify the stream position in this function.
1574 if ( stream
->IsSeekable() && stream
->SeekI(0) == wxInvalidOffset
)
1580 if ( !stream
->ReadAll(&iconDir
, sizeof(iconDir
)) )
1585 return !iconDir
.idReserved
// reserved, must be 0
1586 && wxUINT16_SWAP_ON_BE(iconDir
.idType
) == resourceType
// either 1 or 2
1587 && iconDir
.idCount
; // must contain at least one image
1590 #endif // wxUSE_STREAMS
1592 #endif // wxUSE_ICO_CUR
1594 #endif // wxUSE_IMAGE