1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/imagbmp.cpp
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 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
19 #include "wx/imagbmp.h"
23 #include "wx/msw/wrapwin.h"
27 #include "wx/bitmap.h"
28 #include "wx/palette.h"
33 #include "wx/filefn.h"
34 #include "wx/wfstream.h"
35 #include "wx/quantize.h"
36 #include "wx/scopeguard.h"
37 #include "wx/anidecod.h"
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
63 #define BI_BITFIELDS 3
66 #define poffset (line * width * 3 + column * 3)
68 bool wxBMPHandler::SaveFile(wxImage
*image
,
69 wxOutputStream
& stream
,
72 return SaveDib(image
, stream
, verbose
, true/*IsBmp*/, false/*IsMask*/);
75 bool wxBMPHandler::SaveDib(wxImage
*image
,
76 wxOutputStream
& stream
,
82 wxCHECK_MSG( image
, false, wxT("invalid pointer in wxBMPHandler::SaveFile") );
88 wxLogError(_("BMP: Couldn't save invalid image."));
93 // get the format of the BMP file to save, else use 24bpp
94 unsigned format
= wxBMP_24BPP
;
95 if ( image
->HasOption(wxIMAGE_OPTION_BMP_FORMAT
) )
96 format
= image
->GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT
);
98 wxUint16 bpp
; // # of bits per pixel
99 int palette_size
; // # of color map entries, ie. 2^bpp colors
101 // set the bpp and appropriate palette_size, and do additional checks
102 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_1BPP_BW
) )
107 else if ( format
== wxBMP_4BPP
)
112 else if ( (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_GREY
) ||
113 (format
== wxBMP_8BPP_RED
) || (format
== wxBMP_8BPP_PALETTE
) )
115 // need to set a wxPalette to use this, HOW TO CHECK IF VALID, SIZE?
116 if ((format
== wxBMP_8BPP_PALETTE
)
118 && !image
->HasPalette()
119 #endif // wxUSE_PALETTE
124 wxLogError(_("BMP: wxImage doesn't have own wxPalette."));
131 else // you get 24bpp
133 format
= wxBMP_24BPP
;
138 unsigned width
= image
->GetWidth();
139 unsigned row_padding
= (4 - int(width
*bpp
/8.0) % 4) % 4; // # bytes to pad to dword
140 unsigned row_width
= int(width
* bpp
/8.0) + row_padding
; // # of bytes per row
145 wxUint16 magic
; // format magic, always 'BM'
146 wxUint32 filesize
; // total file size, inc. headers
147 wxUint32 reserved
; // for future use
148 wxUint32 data_offset
; // image data offset in the file
151 wxUint32 bih_size
; // 2nd part's size
152 wxUint32 width
, height
; // bitmap's dimensions
153 wxUint16 planes
; // num of planes
154 wxUint16 bpp
; // bits per pixel
155 wxUint32 compression
; // compression method
156 wxUint32 size_of_bmp
; // size of the bitmap
157 wxUint32 h_res
, v_res
; // image resolution in pixels-per-meter
158 wxUint32 num_clrs
; // number of colors used
159 wxUint32 num_signif_clrs
;// number of significant colors
162 wxUint32 hdr_size
= 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/;
164 hdr
.magic
= wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/);
165 hdr
.filesize
= wxUINT32_SWAP_ON_BE( hdr_size
+ palette_size
*4 +
166 row_width
* image
->GetHeight() );
168 hdr
.data_offset
= wxUINT32_SWAP_ON_BE(hdr_size
+ palette_size
*4);
170 hdr
.bih_size
= wxUINT32_SWAP_ON_BE(hdr_size
- 14);
171 hdr
.width
= wxUINT32_SWAP_ON_BE(image
->GetWidth());
174 hdr
.height
= wxUINT32_SWAP_ON_BE(image
->GetHeight());
178 hdr
.height
= wxUINT32_SWAP_ON_BE(2 * image
->GetHeight());
180 hdr
.planes
= wxUINT16_SWAP_ON_BE(1); // always 1 plane
181 hdr
.bpp
= wxUINT16_SWAP_ON_BE(bpp
);
182 hdr
.compression
= 0; // RGB uncompressed
183 hdr
.size_of_bmp
= wxUINT32_SWAP_ON_BE(row_width
* image
->GetHeight());
185 // get the resolution from the image options or fall back to 72dpi standard
186 // for the BMP format if not specified
188 switch ( GetResolutionFromOptions(*image
, &hres
, &vres
) )
191 wxFAIL_MSG( wxT("unexpected image resolution units") );
194 case wxIMAGE_RESOLUTION_NONE
:
197 // fall through to convert it to correct units
199 case wxIMAGE_RESOLUTION_INCHES
:
200 // convert resolution in inches to resolution in centimeters
201 hres
= (int)(10*mm2inches
*hres
);
202 vres
= (int)(10*mm2inches
*vres
);
203 // fall through to convert it to resolution in meters
205 case wxIMAGE_RESOLUTION_CM
:
206 // convert resolution in centimeters to resolution in meters
212 hdr
.h_res
= wxUINT32_SWAP_ON_BE(hres
);
213 hdr
.v_res
= wxUINT32_SWAP_ON_BE(vres
);
214 hdr
.num_clrs
= wxUINT32_SWAP_ON_BE(palette_size
); // # colors in colormap
215 hdr
.num_signif_clrs
= 0; // all colors are significant
219 if (// VS: looks ugly but compilers tend to do ugly things with structs,
220 // like aligning hdr.filesize's ofset to dword :(
221 // VZ: we should add padding then...
222 !stream
.Write(&hdr
.magic
, 2) ||
223 !stream
.Write(&hdr
.filesize
, 4) ||
224 !stream
.Write(&hdr
.reserved
, 4) ||
225 !stream
.Write(&hdr
.data_offset
, 4)
230 wxLogError(_("BMP: Couldn't write the file (Bitmap) header."));
238 !stream
.Write(&hdr
.bih_size
, 4) ||
239 !stream
.Write(&hdr
.width
, 4) ||
240 !stream
.Write(&hdr
.height
, 4) ||
241 !stream
.Write(&hdr
.planes
, 2) ||
242 !stream
.Write(&hdr
.bpp
, 2) ||
243 !stream
.Write(&hdr
.compression
, 4) ||
244 !stream
.Write(&hdr
.size_of_bmp
, 4) ||
245 !stream
.Write(&hdr
.h_res
, 4) ||
246 !stream
.Write(&hdr
.v_res
, 4) ||
247 !stream
.Write(&hdr
.num_clrs
, 4) ||
248 !stream
.Write(&hdr
.num_signif_clrs
, 4)
253 wxLogError(_("BMP: Couldn't write the file (BitmapInfo) header."));
259 wxPalette
*palette
= NULL
; // entries for quantized images
260 wxUint8
*rgbquad
= NULL
; // for the RGBQUAD bytes for the colormap
261 wxImage
*q_image
= NULL
; // destination for quantized image
263 // if <24bpp use quantization to reduce colors for *some* of the formats
264 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_4BPP
) ||
265 (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_PALETTE
) )
267 // make a new palette and quantize the image
268 if (format
!= wxBMP_8BPP_PALETTE
)
270 q_image
= new wxImage();
272 // I get a delete error using Quantize when desired colors > 236
273 int quantize
= ((palette_size
> 236) ? 236 : palette_size
);
274 // fill the destination too, it gives much nicer 4bpp images
275 wxQuantize::Quantize( *image
, *q_image
, &palette
, quantize
, 0,
276 wxQUANTIZE_FILL_DESTINATION_IMAGE
);
281 palette
= new wxPalette(image
->GetPalette());
282 #endif // wxUSE_PALETTE
286 unsigned char r
, g
, b
;
287 rgbquad
= new wxUint8
[palette_size
*4];
289 for (i
= 0; i
< palette_size
; i
++)
292 if ( !palette
->GetRGB(i
, &r
, &g
, &b
) )
293 #endif // wxUSE_PALETTE
302 // make a 256 entry greyscale colormap or 2 entry black & white
303 else if ( (format
== wxBMP_8BPP_GREY
) || (format
== wxBMP_8BPP_RED
) ||
304 (format
== wxBMP_1BPP_BW
) )
306 rgbquad
= new wxUint8
[palette_size
*4];
308 for ( int i
= 0; i
< palette_size
; i
++ )
310 // if 1BPP_BW then the value should be either 0 or 255
311 wxUint8 c
= (wxUint8
)((i
> 0) && (format
== wxBMP_1BPP_BW
) ? 255 : i
);
320 // if the colormap was made, then it needs to be written
325 if ( !stream
.Write(rgbquad
, palette_size
*4) )
329 wxLogError(_("BMP: Couldn't write RGB color map."));
334 #endif // wxUSE_PALETTE
342 // pointer to the image data, use quantized if available
343 wxUint8
*data
= (wxUint8
*) image
->GetData();
344 if (q_image
) if (q_image
->Ok()) data
= (wxUint8
*) q_image
->GetData();
346 wxUint8
*buffer
= new wxUint8
[row_width
];
347 memset(buffer
, 0, row_width
);
351 for (y
= image
->GetHeight() -1; y
>= 0; y
--)
353 if ( format
== wxBMP_24BPP
) // 3 bytes per pixel red,green,blue
355 for ( x
= 0; x
< width
; x
++ )
357 pixel
= 3*(y
*width
+ x
);
359 buffer
[3*x
] = data
[pixel
+2];
360 buffer
[3*x
+ 1] = data
[pixel
+1];
361 buffer
[3*x
+ 2] = data
[pixel
];
364 else if ((format
== wxBMP_8BPP
) || // 1 byte per pixel in color
365 (format
== wxBMP_8BPP_PALETTE
))
367 for (x
= 0; x
< width
; x
++)
369 pixel
= 3*(y
*width
+ x
);
371 buffer
[x
] = (wxUint8
)palette
->GetPixel( data
[pixel
],
375 // FIXME: what should this be? use some std palette maybe?
377 #endif // wxUSE_PALETTE
380 else if ( format
== wxBMP_8BPP_GREY
) // 1 byte per pix, rgb ave to grey
382 for (x
= 0; x
< width
; x
++)
384 pixel
= 3*(y
*width
+ x
);
385 buffer
[x
] = (wxUint8
)(.299*data
[pixel
] +
390 else if ( format
== wxBMP_8BPP_RED
) // 1 byte per pixel, red as greys
392 for (x
= 0; x
< width
; x
++)
394 buffer
[x
] = (wxUint8
)data
[3*(y
*width
+ x
)];
397 else if ( format
== wxBMP_4BPP
) // 4 bpp in color
399 for (x
= 0; x
< width
; x
+=2)
401 pixel
= 3*(y
*width
+ x
);
403 // fill buffer, ignore if > width
405 buffer
[x
/2] = (wxUint8
)(
406 ((wxUint8
)palette
->GetPixel(data
[pixel
],
408 data
[pixel
+2]) << 4) |
411 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3],
415 // FIXME: what should this be? use some std palette maybe?
417 #endif // wxUSE_PALETTE
420 else if ( format
== wxBMP_1BPP
) // 1 bpp in "color"
422 for (x
= 0; x
< width
; x
+=8)
424 pixel
= 3*(y
*width
+ x
);
427 buffer
[x
/8] = (wxUint8
)(
428 ((wxUint8
)palette
->GetPixel(data
[pixel
], data
[pixel
+1], data
[pixel
+2]) << 7) |
429 (((x
+1) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3], data
[pixel
+4], data
[pixel
+5]) << 6)) |
430 (((x
+2) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+6], data
[pixel
+7], data
[pixel
+8]) << 5)) |
431 (((x
+3) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+9], data
[pixel
+10], data
[pixel
+11]) << 4)) |
432 (((x
+4) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+12], data
[pixel
+13], data
[pixel
+14]) << 3)) |
433 (((x
+5) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+15], data
[pixel
+16], data
[pixel
+17]) << 2)) |
434 (((x
+6) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+18], data
[pixel
+19], data
[pixel
+20]) << 1)) |
435 (((x
+7) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+21], data
[pixel
+22], data
[pixel
+23]) )) );
437 // FIXME: what should this be? use some std palette maybe?
439 #endif // wxUSE_PALETTE
442 else if ( format
== wxBMP_1BPP_BW
) // 1 bpp B&W colormap from red color ONLY
444 for (x
= 0; x
< width
; x
+=8)
446 pixel
= 3*(y
*width
+ x
);
448 buffer
[x
/8] = (wxUint8
)(
449 (((wxUint8
)(data
[pixel
] /128.)) << 7) |
450 (((x
+1) > width
) ? 0 : (((wxUint8
)(data
[pixel
+3] /128.)) << 6)) |
451 (((x
+2) > width
) ? 0 : (((wxUint8
)(data
[pixel
+6] /128.)) << 5)) |
452 (((x
+3) > width
) ? 0 : (((wxUint8
)(data
[pixel
+9] /128.)) << 4)) |
453 (((x
+4) > width
) ? 0 : (((wxUint8
)(data
[pixel
+12]/128.)) << 3)) |
454 (((x
+5) > width
) ? 0 : (((wxUint8
)(data
[pixel
+15]/128.)) << 2)) |
455 (((x
+6) > width
) ? 0 : (((wxUint8
)(data
[pixel
+18]/128.)) << 1)) |
456 (((x
+7) > width
) ? 0 : (((wxUint8
)(data
[pixel
+21]/128.)) )) );
460 if ( !stream
.Write(buffer
, row_width
) )
464 wxLogError(_("BMP: Couldn't write data."));
469 #endif // wxUSE_PALETTE
477 #endif // wxUSE_PALETTE
486 static void Free(BMPPalette
* pal
) { delete [] pal
; }
488 unsigned char r
, g
, b
;
491 bool wxBMPHandler::DoLoadDib(wxImage
* image
, int width
, int height
,
492 int bpp
, int ncolors
, int comp
,
493 wxFileOffset bmpOffset
, wxInputStream
& stream
,
494 bool verbose
, bool IsBmp
, bool hasPalette
)
496 wxInt32 aDword
, rmask
= 0, gmask
= 0, bmask
= 0, amask
= 0;
497 int rshift
= 0, gshift
= 0, bshift
= 0, ashift
= 0;
498 int rbits
= 0, gbits
= 0, bbits
= 0;
504 // allocate space for palette if needed:
509 cmap
= new BMPPalette
[ncolors
];
514 wxLogError(_("BMP: Couldn't allocate memory."));
524 wxON_BLOCK_EXIT1(&BMPPalette::Free
, cmap
);
526 // destroy existing here instead of:
528 image
->Create(width
, height
);
530 unsigned char *ptr
= image
->GetData();
536 wxLogError( _("BMP: Couldn't allocate memory.") );
541 unsigned char *alpha
;
544 // tell the image to allocate an alpha buffer
546 alpha
= image
->GetAlpha();
551 wxLogError(_("BMP: Couldn't allocate memory."));
561 // Reading the palette, if it exists:
562 if ( bpp
< 16 && ncolors
!= 0 )
564 unsigned char* r
= new unsigned char[ncolors
];
565 unsigned char* g
= new unsigned char[ncolors
];
566 unsigned char* b
= new unsigned char[ncolors
];
567 for (int j
= 0; j
< ncolors
; j
++)
571 stream
.Read(bbuf
, 4);
582 //used in reading .ico file mask
585 b
[j
] = cmap
[j
].b
= ( j
? 255 : 0 );
590 // Set the palette for the wxImage
591 image
->SetPalette(wxPalette(ncolors
, r
, g
, b
));
592 #endif // wxUSE_PALETTE
598 else if ( bpp
== 16 || bpp
== 32 )
600 if ( comp
== BI_BITFIELDS
)
603 stream
.Read(dbuf
, 4 * 3);
604 rmask
= wxINT32_SWAP_ON_BE(dbuf
[0]);
605 gmask
= wxINT32_SWAP_ON_BE(dbuf
[1]);
606 bmask
= wxINT32_SWAP_ON_BE(dbuf
[2]);
607 // find shift amount (Least significant bit of mask)
608 for (bit
= bpp
-1; bit
>=0; bit
--)
610 if (bmask
& (1 << bit
))
612 if (gmask
& (1 << bit
))
614 if (rmask
& (1 << bit
))
617 // Find number of bits in mask (MSB-LSB+1)
618 for (bit
= 0; bit
< bpp
; bit
++)
620 if (bmask
& (1 << bit
))
621 bbits
= bit
-bshift
+1;
622 if (gmask
& (1 << bit
))
623 gbits
= bit
-gshift
+1;
624 if (rmask
& (1 << bit
))
625 rbits
= bit
-rshift
+1;
628 else if ( bpp
== 16 )
640 else if ( bpp
== 32 )
658 * Reading the image data
662 // NOTE: seeking a positive amount in wxFromCurrent mode allows us to
663 // load even non-seekable streams (see wxInputStream::SeekI docs)!
664 const wxFileOffset pos
= stream
.TellI();
665 if (pos
!= wxInvalidOffset
&& bmpOffset
> pos
)
666 if (stream
.SeekI(bmpOffset
- pos
, wxFromCurrent
) == wxInvalidOffset
)
668 //else: icon, just carry on
671 unsigned char *data
= ptr
;
673 /* set the whole image to the background color */
674 if ( bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
) )
676 for (int i
= 0; i
< width
* height
; i
++)
685 int linesize
= ((width
* bpp
+ 31) / 32) * 4;
687 // flag indicating if we have any not fully transparent alpha values: this
688 // is used to account for the bitmaps which use 32bpp format (normally
689 // meaning that they have alpha channel) but have only zeroes in it so that
690 // without this hack they appear fully transparent -- and as this is
691 // unlikely intentional, we consider that they don't have alpha at all in
692 // this case (see #10915)
693 bool hasValidAlpha
= false;
695 /* BMPs are stored upside down */
696 for ( int line
= (height
- 1); line
>= 0; line
-- )
699 for ( int column
= 0; column
< width
; )
704 aByte
= stream
.GetC();
707 for (int bit
= 0; bit
< 8 && column
< width
; bit
++)
709 int index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
710 ptr
[poffset
] = cmap
[index
].r
;
711 ptr
[poffset
+ 1] = cmap
[index
].g
;
712 ptr
[poffset
+ 2] = cmap
[index
].b
;
718 if ( comp
== BI_RLE4
)
722 aByte
= stream
.GetC();
730 else if ( aByte
== 1 )
735 else if ( aByte
== 2 )
737 aByte
= stream
.GetC();
739 linepos
= column
* bpp
/ 4;
740 aByte
= stream
.GetC();
741 line
-= aByte
; // upside down
745 int absolute
= aByte
;
748 for (int k
= 0; k
< absolute
; k
++)
753 aByte
= stream
.GetC();
754 nibble
[0] = (wxUint8
)( (aByte
& 0xF0) >> 4 ) ;
755 nibble
[1] = (wxUint8
)( aByte
& 0x0F ) ;
757 ptr
[poffset
] = cmap
[nibble
[k%2
]].r
;
758 ptr
[poffset
+ 1] = cmap
[nibble
[k%2
]].g
;
759 ptr
[poffset
+ 2] = cmap
[nibble
[k%2
]].b
;
764 if ( readBytes
& 0x01 )
765 aByte
= stream
.GetC();
771 nibble
[0] = (wxUint8
)( (aByte
& 0xF0) >> 4 ) ;
772 nibble
[1] = (wxUint8
)( aByte
& 0x0F ) ;
774 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
776 ptr
[poffset
] = cmap
[nibble
[l%2
]].r
;
777 ptr
[poffset
+ 1] = cmap
[nibble
[l%2
]].g
;
778 ptr
[poffset
+ 2] = cmap
[nibble
[l%2
]].b
;
787 for (int nibble
= 0; nibble
< 2 && column
< width
; nibble
++)
789 int index
= ((aByte
& (0xF0 >> (nibble
* 4))) >> (!nibble
* 4));
792 ptr
[poffset
] = cmap
[index
].r
;
793 ptr
[poffset
+ 1] = cmap
[index
].g
;
794 ptr
[poffset
+ 2] = cmap
[index
].b
;
801 if ( comp
== BI_RLE8
)
805 aByte
= stream
.GetC();
810 /* column = width; */
812 else if ( aByte
== 1 )
817 else if ( aByte
== 2 )
819 aByte
= stream
.GetC();
821 linepos
= column
* bpp
/ 8;
822 aByte
= stream
.GetC();
827 int absolute
= aByte
;
828 for (int k
= 0; k
< absolute
; k
++)
831 aByte
= stream
.GetC();
832 ptr
[poffset
] = cmap
[aByte
].r
;
833 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
834 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
837 if ( absolute
& 0x01 )
838 aByte
= stream
.GetC();
843 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
845 ptr
[poffset
] = cmap
[aByte
].r
;
846 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
847 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
855 ptr
[poffset
] = cmap
[aByte
].r
;
856 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
857 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
859 // linepos += size; seems to be wrong, RR
863 else if ( bpp
== 24 )
865 stream
.Read(bbuf
, 3);
867 ptr
[poffset
] = (unsigned char)bbuf
[2];
868 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
869 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
872 else if ( bpp
== 16 )
875 stream
.Read(&aWord
, 2);
876 aWord
= wxUINT16_SWAP_ON_BE(aWord
);
878 /* Use the masks and calculated amount of shift
879 to retrieve the color data out of the word. Then
880 shift it left by (8 - number of bits) such that
881 the image has the proper dynamic range */
882 temp
= (unsigned char)(((aWord
& rmask
) >> rshift
) << (8-rbits
));
884 temp
= (unsigned char)(((aWord
& gmask
) >> gshift
) << (8-gbits
));
885 ptr
[poffset
+ 1] = temp
;
886 temp
= (unsigned char)(((aWord
& bmask
) >> bshift
) << (8-bbits
));
887 ptr
[poffset
+ 2] = temp
;
893 stream
.Read(&aDword
, 4);
894 aDword
= wxINT32_SWAP_ON_BE(aDword
);
896 temp
= (unsigned char)((aDword
& rmask
) >> rshift
);
898 temp
= (unsigned char)((aDword
& gmask
) >> gshift
);
899 ptr
[poffset
+ 1] = temp
;
900 temp
= (unsigned char)((aDword
& bmask
) >> bshift
);
901 ptr
[poffset
+ 2] = temp
;
904 temp
= (unsigned char)((aDword
& amask
) >> ashift
);
905 alpha
[line
* width
+ column
] = temp
;
907 if ( temp
!= wxALPHA_TRANSPARENT
)
908 hasValidAlpha
= true;
913 while ( (linepos
< linesize
) && (comp
!= 1) && (comp
!= 2) )
915 stream
.Read(&aByte
, 1);
922 image
->SetMask(false);
924 // check if we had any valid alpha values in this bitmap
925 if ( alpha
&& !hasValidAlpha
)
927 // we didn't, so finally discard the alpha channel completely
931 const wxStreamError err
= stream
.GetLastError();
932 return err
== wxSTREAM_NO_ERROR
|| err
== wxSTREAM_EOF
;
935 bool wxBMPHandler::LoadDib(wxImage
*image
, wxInputStream
& stream
,
936 bool verbose
, bool IsBmp
)
944 // read the header off the .BMP format file
945 stream
.Read(bbuf
, 2);
946 stream
.Read(dbuf
, 16);
950 stream
.Read(dbuf
, 4);
953 wxInt32 size
= wxINT32_SWAP_ON_BE(dbuf
[0]);
955 wxFileOffset offset
= wxINT32_SWAP_ON_BE(dbuf
[2]);
957 stream
.Read(dbuf
, 4 * 2);
958 int width
= wxINT32_SWAP_ON_BE((int)dbuf
[0]);
959 int height
= wxINT32_SWAP_ON_BE((int)dbuf
[1]);
960 if ( !IsBmp
)height
= height
/ 2; // for icons divide by 2
966 wxLogError( _("DIB Header: Image width > 32767 pixels for file.") );
970 if ( height
> 32767 )
974 wxLogError( _("DIB Header: Image height > 32767 pixels for file.") );
979 stream
.Read(&aWord
, 2);
982 int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
984 stream
.Read(&aWord
, 2);
985 int bpp
= wxUINT16_SWAP_ON_BE((int)aWord
);
986 if ( bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
!= 16 && bpp
!= 24 && bpp
!= 32 )
990 wxLogError( _("DIB Header: Unknown bitdepth in file.") );
995 stream
.Read(dbuf
, 4 * 4);
996 int comp
= wxINT32_SWAP_ON_BE((int)dbuf
[0]);
997 if ( comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&&
998 comp
!= BI_BITFIELDS
)
1002 wxLogError( _("DIB Header: Unknown encoding in file.") );
1007 stream
.Read(dbuf
, 4 * 2);
1009 int ncolors
= wxINT32_SWAP_ON_BE( (int)dbuf
[0] );
1012 /* some more sanity checks */
1013 if (((comp
== BI_RLE4
) && (bpp
!= 4)) ||
1014 ((comp
== BI_RLE8
) && (bpp
!= 8)) ||
1015 ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
1019 wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") );
1024 //read DIB; this is the BMP image or the XOR part of an icon image
1025 if ( !DoLoadDib(image
, width
, height
, bpp
, ncolors
, comp
, offset
, stream
,
1026 verbose
, IsBmp
, true) )
1030 wxLogError( _("Error in reading image DIB.") );
1037 //read Icon mask which is monochrome
1038 //there is no palette, so we will create one
1040 if ( !DoLoadDib(&mask
, width
, height
, 1, 2, BI_RGB
, offset
, stream
,
1041 verbose
, IsBmp
, false) )
1045 wxLogError( _("ICO: Error in reading mask DIB.") );
1049 image
->SetMaskFromImage(mask
, 255, 255, 255);
1053 // the resolution in the bitmap header is in meters, convert to centimeters
1054 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONUNIT
, wxIMAGE_RESOLUTION_CM
);
1055 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONX
, dbuf
[2]/100);
1056 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONY
, dbuf
[3]/100);
1061 bool wxBMPHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1062 bool verbose
, int WXUNUSED(index
))
1064 // Read a single DIB fom the file:
1065 return LoadDib(image
, stream
, verbose
, true/*isBmp*/);
1068 bool wxBMPHandler::DoCanRead(wxInputStream
& stream
)
1070 unsigned char hdr
[2];
1072 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) ) // it's ok to modify the stream position here
1075 // do we have the BMP file signature?
1076 return hdr
[0] == 'B' && hdr
[1] == 'M';
1079 #endif // wxUSE_STREAMS
1083 //-----------------------------------------------------------------------------
1085 //-----------------------------------------------------------------------------
1087 IMPLEMENT_DYNAMIC_CLASS(wxICOHandler
, wxBMPHandler
)
1093 wxUint8 bWidth
; // Width of the image
1094 wxUint8 bHeight
; // Height of the image (times 2)
1095 wxUint8 bColorCount
; // Number of colors in image (0 if >=8bpp)
1096 wxUint8 bReserved
; // Reserved
1098 // these two are different in icons and cursors:
1100 wxUint16 wPlanes
; // Color Planes or XHotSpot
1101 wxUint16 wBitCount
; // Bits per pixel or YHotSpot
1103 wxUint32 dwBytesInRes
; // how many bytes in this resource?
1104 wxUint32 dwImageOffset
; // where in the file is this image
1109 wxUint16 idReserved
; // Reserved
1110 wxUint16 idType
; // resource type (1 for icons, 2 for cursors)
1111 wxUint16 idCount
; // how many images?
1115 bool wxICOHandler::SaveFile(wxImage
*image
,
1116 wxOutputStream
& stream
,
1120 //sanity check; icon must be less than 127 pixels high and 255 wide
1121 if ( image
->GetHeight () > 127 )
1125 wxLogError(_("ICO: Image too tall for an icon."));
1129 if ( image
->GetWidth () > 255 )
1133 wxLogError(_("ICO: Image too wide for an icon."));
1138 const int images
= 1; // only generate one image
1140 // VS: This is a hack of sort - since ICO and CUR files are almost
1141 // identical, we have all the meat in wxICOHandler and check for
1142 // the actual (handler) type when the code has to distinguish between
1144 int type
= (this->GetType() == wxBITMAP_TYPE_CUR
) ? 2 : 1;
1146 // write a header, (ICONDIR)
1147 // Calculate the header size
1148 wxUint32 offset
= 3 * sizeof(wxUint16
);
1151 IconDir
.idReserved
= 0;
1152 IconDir
.idType
= wxUINT16_SWAP_ON_BE((wxUint16
)type
);
1153 IconDir
.idCount
= wxUINT16_SWAP_ON_BE((wxUint16
)images
);
1154 stream
.Write(&IconDir
.idReserved
, sizeof(IconDir
.idReserved
));
1155 stream
.Write(&IconDir
.idType
, sizeof(IconDir
.idType
));
1156 stream
.Write(&IconDir
.idCount
, sizeof(IconDir
.idCount
));
1157 if ( !stream
.IsOk() )
1161 wxLogError(_("ICO: Error writing the image file!"));
1166 // for each iamage write a description ICONDIRENTRY:
1167 ICONDIRENTRY icondirentry
;
1168 for (int img
= 0; img
< images
; img
++)
1172 if ( image
->HasMask() )
1174 // make another image with black/white:
1175 mask
= image
->ConvertToMono (image
->GetMaskRed(), image
->GetMaskGreen(), image
->GetMaskBlue() );
1177 // now we need to change the masked regions to black:
1178 unsigned char r
= image
->GetMaskRed();
1179 unsigned char g
= image
->GetMaskGreen();
1180 unsigned char b
= image
->GetMaskBlue();
1181 if ( (r
!= 0) || (g
!= 0) || (b
!= 0) )
1183 // Go round and apply black to the masked bits:
1185 for (i
= 0; i
< mask
.GetWidth(); i
++)
1187 for (j
= 0; j
< mask
.GetHeight(); j
++)
1189 if ((r
== mask
.GetRed(i
, j
)) &&
1190 (g
== mask
.GetGreen(i
, j
))&&
1191 (b
== mask
.GetBlue(i
, j
)) )
1192 image
->SetRGB(i
, j
, 0, 0, 0 );
1199 // just make a black mask all over:
1200 mask
= image
->Copy();
1202 for (i
= 0; i
< mask
.GetWidth(); i
++)
1203 for (j
= 0; j
< mask
.GetHeight(); j
++)
1204 mask
.SetRGB(i
, j
, 0, 0, 0 );
1206 // Set the formats for image and mask
1207 // (Windows never saves with more than 8 colors):
1208 image
->SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_8BPP
);
1210 // monochome bitmap:
1211 mask
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_1BPP_BW
);
1213 bool IsMask
= false;
1215 //calculate size and offset of image and mask
1216 wxCountingOutputStream cStream
;
1217 bool bResult
= SaveDib(image
, cStream
, verbose
, IsBmp
, IsMask
);
1222 wxLogError(_("ICO: Error writing the image file!"));
1228 bResult
= SaveDib(&mask
, cStream
, verbose
, IsBmp
, IsMask
);
1233 wxLogError(_("ICO: Error writing the image file!"));
1237 wxUint32 Size
= cStream
.GetSize();
1239 // wxCountingOutputStream::IsOk() always returns true for now and this
1240 // "if" provokes VC++ warnings in optimized build
1242 if ( !cStream
.Ok() )
1246 wxLogError(_("ICO: Error writing the image file!"));
1252 offset
= offset
+ sizeof(ICONDIRENTRY
);
1254 icondirentry
.bWidth
= (wxUint8
)image
->GetWidth();
1255 icondirentry
.bHeight
= (wxUint8
)(2 * image
->GetHeight());
1256 icondirentry
.bColorCount
= 0;
1257 icondirentry
.bReserved
= 0;
1258 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE(1);
1259 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE(wxBMP_8BPP
);
1260 if ( type
== 2 /*CUR*/)
1262 int hx
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ?
1263 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) :
1264 image
->GetWidth() / 2;
1265 int hy
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ?
1266 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) :
1267 image
->GetHeight() / 2;
1269 // actually write the values of the hot spot here:
1270 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE((wxUint16
)hx
);
1271 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE((wxUint16
)hy
);
1273 icondirentry
.dwBytesInRes
= wxUINT32_SWAP_ON_BE(Size
);
1274 icondirentry
.dwImageOffset
= wxUINT32_SWAP_ON_BE(offset
);
1276 // increase size to allow for the data written:
1280 stream
.Write(&icondirentry
.bWidth
, sizeof(icondirentry
.bWidth
));
1281 stream
.Write(&icondirentry
.bHeight
, sizeof(icondirentry
.bHeight
));
1282 stream
.Write(&icondirentry
.bColorCount
, sizeof(icondirentry
.bColorCount
));
1283 stream
.Write(&icondirentry
.bReserved
, sizeof(icondirentry
.bReserved
));
1284 stream
.Write(&icondirentry
.wPlanes
, sizeof(icondirentry
.wPlanes
));
1285 stream
.Write(&icondirentry
.wBitCount
, sizeof(icondirentry
.wBitCount
));
1286 stream
.Write(&icondirentry
.dwBytesInRes
, sizeof(icondirentry
.dwBytesInRes
));
1287 stream
.Write(&icondirentry
.dwImageOffset
, sizeof(icondirentry
.dwImageOffset
));
1288 if ( !stream
.IsOk() )
1292 wxLogError(_("ICO: Error writing the image file!"));
1297 // actually save it:
1299 bResult
= SaveDib(image
, stream
, verbose
, IsBmp
, IsMask
);
1304 wxLogError(_("ICO: Error writing the image file!"));
1310 bResult
= SaveDib(&mask
, stream
, verbose
, IsBmp
, IsMask
);
1315 wxLogError(_("ICO: Error writing the image file!"));
1320 } // end of for loop
1325 bool wxICOHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1326 bool verbose
, int index
)
1328 return DoLoadFile(image
, stream
, verbose
, index
);
1331 bool wxICOHandler::DoLoadFile(wxImage
*image
, wxInputStream
& stream
,
1332 bool WXUNUSED(verbose
), int index
)
1334 bool bResult
wxDUMMY_INITIALIZE(false);
1339 stream
.Read(&IconDir
, sizeof(IconDir
));
1340 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1342 // nType is 1 for Icons, 2 for Cursors:
1343 wxUint16 nType
= wxUINT16_SWAP_ON_BE(IconDir
.idType
);
1345 // loop round the icons and choose the best one:
1346 ICONDIRENTRY
*pIconDirEntry
= new ICONDIRENTRY
[nIcons
];
1347 ICONDIRENTRY
*pCurrentEntry
= pIconDirEntry
;
1350 int iSel
= wxNOT_FOUND
;
1352 // remember how many bytes we read from the stream:
1353 wxFileOffset alreadySeeked
= sizeof(IconDir
);
1355 for (unsigned int i
= 0; i
< nIcons
; i
++ )
1357 alreadySeeked
+= stream
.Read(pCurrentEntry
, sizeof(ICONDIRENTRY
)).LastRead();
1359 // bHeight and bColorCount are wxUint8
1360 if ( pCurrentEntry
->bWidth
>= wMax
)
1362 // see if we have more colors, ==0 indicates > 8bpp:
1363 if ( pCurrentEntry
->bColorCount
== 0 )
1364 pCurrentEntry
->bColorCount
= 255;
1365 if ( pCurrentEntry
->bColorCount
>= colmax
)
1368 wMax
= pCurrentEntry
->bWidth
;
1369 colmax
= pCurrentEntry
->bColorCount
;
1378 // VS: Note that we *have* to run the loop above even if index != -1, because
1379 // it reads ICONDIRENTRies.
1383 if ( iSel
== wxNOT_FOUND
|| iSel
< 0 || iSel
>= nIcons
)
1385 wxLogError(_("ICO: Invalid icon index."));
1390 // seek to selected icon:
1391 pCurrentEntry
= pIconDirEntry
+ iSel
;
1393 // NOTE: seeking a positive amount in wxFromCurrent mode allows us to
1394 // load even non-seekable streams (see wxInputStream::SeekI docs)!
1395 wxFileOffset offset
= wxUINT32_SWAP_ON_BE(pCurrentEntry
->dwImageOffset
) - alreadySeeked
;
1396 if (offset
!= 0 && stream
.SeekI(offset
, wxFromCurrent
) == wxInvalidOffset
)
1399 bResult
= LoadDib(image
, stream
, true, IsBmp
);
1400 bool bIsCursorType
= (this->GetType() == wxBITMAP_TYPE_CUR
) || (this->GetType() == wxBITMAP_TYPE_ANI
);
1401 if ( bResult
&& bIsCursorType
&& nType
== 2 )
1403 // it is a cursor, so let's set the hotspot:
1404 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wPlanes
));
1405 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wBitCount
));
1409 delete [] pIconDirEntry
;
1414 int wxICOHandler::DoGetImageCount(wxInputStream
& stream
)
1418 if (stream
.Read(&IconDir
, sizeof(IconDir
)).LastRead() != sizeof(IconDir
))
1419 // it's ok to modify the stream position here
1422 return (int)wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1425 bool wxICOHandler::DoCanRead(wxInputStream
& stream
)
1427 unsigned char hdr
[4];
1428 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) ) // it's ok to modify the stream position here
1431 // hdr[2] is one for an icon and two for a cursor
1432 return hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\1' && hdr
[3] == '\0';
1435 #endif // wxUSE_STREAMS
1438 //-----------------------------------------------------------------------------
1440 //-----------------------------------------------------------------------------
1442 IMPLEMENT_DYNAMIC_CLASS(wxCURHandler
, wxICOHandler
)
1446 bool wxCURHandler::DoCanRead(wxInputStream
& stream
)
1448 unsigned char hdr
[4];
1449 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) ) // it's ok to modify the stream position here
1452 // hdr[2] is one for an icon and two for a cursor
1453 return hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\2' && hdr
[3] == '\0';
1456 #endif // wxUSE_STREAMS
1458 //-----------------------------------------------------------------------------
1460 //-----------------------------------------------------------------------------
1462 IMPLEMENT_DYNAMIC_CLASS(wxANIHandler
, wxCURHandler
)
1466 bool wxANIHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1467 bool WXUNUSED(verbose
), int index
)
1469 wxANIDecoder decoder
;
1470 if (!decoder
.Load(stream
))
1473 return decoder
.ConvertToImage(index
!= -1 ? (size_t)index
: 0, image
);
1476 bool wxANIHandler::DoCanRead(wxInputStream
& stream
)
1479 return decod
.CanRead(stream
);
1480 // it's ok to modify the stream position here
1483 int wxANIHandler::DoGetImageCount(wxInputStream
& stream
)
1485 wxANIDecoder decoder
;
1486 if (!decoder
.Load(stream
)) // it's ok to modify the stream position here
1489 return decoder
.GetFrameCount();
1492 #endif // wxUSE_STREAMS
1494 #endif // wxUSE_ICO_CUR
1496 #endif // wxUSE_IMAGE