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 // ----------------------------------------------------------------------------
48 static bool CanReadICOOrCUR(wxInputStream
*stream
, wxUint16 resourceType
);
50 #endif // wxUSE_ICO_CUR
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
73 #define BI_BITFIELDS 3
76 #define poffset (line * width * 3 + column * 3)
78 bool wxBMPHandler::SaveFile(wxImage
*image
,
79 wxOutputStream
& stream
,
82 return SaveDib(image
, stream
, verbose
, true/*IsBmp*/, false/*IsMask*/);
85 bool wxBMPHandler::SaveDib(wxImage
*image
,
86 wxOutputStream
& stream
,
92 wxCHECK_MSG( image
, false, wxT("invalid pointer in wxBMPHandler::SaveFile") );
98 wxLogError(_("BMP: Couldn't save invalid image."));
103 // get the format of the BMP file to save, else use 24bpp
104 unsigned format
= wxBMP_24BPP
;
105 if ( image
->HasOption(wxIMAGE_OPTION_BMP_FORMAT
) )
106 format
= image
->GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT
);
108 wxUint16 bpp
; // # of bits per pixel
109 int palette_size
; // # of color map entries, ie. 2^bpp colors
111 // set the bpp and appropriate palette_size, and do additional checks
112 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_1BPP_BW
) )
117 else if ( format
== wxBMP_4BPP
)
122 else if ( (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_GREY
) ||
123 (format
== wxBMP_8BPP_RED
) || (format
== wxBMP_8BPP_PALETTE
) )
125 // need to set a wxPalette to use this, HOW TO CHECK IF VALID, SIZE?
126 if ((format
== wxBMP_8BPP_PALETTE
)
128 && !image
->HasPalette()
129 #endif // wxUSE_PALETTE
134 wxLogError(_("BMP: wxImage doesn't have own wxPalette."));
141 else // you get 24bpp
143 format
= wxBMP_24BPP
;
148 unsigned width
= image
->GetWidth();
149 unsigned row_padding
= (4 - ((width
* bpp
+ 7) / 8) % 4) % 4; // # bytes to pad to dword
150 unsigned row_width
= (width
* bpp
+ 7) / 8 + row_padding
; // # of bytes per row
155 wxUint16 magic
; // format magic, always 'BM'
156 wxUint32 filesize
; // total file size, inc. headers
157 wxUint32 reserved
; // for future use
158 wxUint32 data_offset
; // image data offset in the file
161 wxUint32 bih_size
; // 2nd part's size
162 wxUint32 width
, height
; // bitmap's dimensions
163 wxUint16 planes
; // num of planes
164 wxUint16 bpp
; // bits per pixel
165 wxUint32 compression
; // compression method
166 wxUint32 size_of_bmp
; // size of the bitmap
167 wxUint32 h_res
, v_res
; // image resolution in pixels-per-meter
168 wxUint32 num_clrs
; // number of colors used
169 wxUint32 num_signif_clrs
;// number of significant colors
172 wxUint32 hdr_size
= 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/;
174 hdr
.magic
= wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/);
175 hdr
.filesize
= wxUINT32_SWAP_ON_BE( hdr_size
+ palette_size
*4 +
176 row_width
* image
->GetHeight() );
178 hdr
.data_offset
= wxUINT32_SWAP_ON_BE(hdr_size
+ palette_size
*4);
180 hdr
.bih_size
= wxUINT32_SWAP_ON_BE(hdr_size
- 14);
181 hdr
.width
= wxUINT32_SWAP_ON_BE(image
->GetWidth());
184 hdr
.height
= wxUINT32_SWAP_ON_BE(image
->GetHeight());
188 hdr
.height
= wxUINT32_SWAP_ON_BE(2 * image
->GetHeight());
190 hdr
.planes
= wxUINT16_SWAP_ON_BE(1); // always 1 plane
191 hdr
.bpp
= wxUINT16_SWAP_ON_BE(bpp
);
192 hdr
.compression
= 0; // RGB uncompressed
193 hdr
.size_of_bmp
= wxUINT32_SWAP_ON_BE(row_width
* image
->GetHeight());
195 // get the resolution from the image options or fall back to 72dpi standard
196 // for the BMP format if not specified
198 switch ( GetResolutionFromOptions(*image
, &hres
, &vres
) )
201 wxFAIL_MSG( wxT("unexpected image resolution units") );
204 case wxIMAGE_RESOLUTION_NONE
:
207 // fall through to convert it to correct units
209 case wxIMAGE_RESOLUTION_INCHES
:
210 // convert resolution in inches to resolution in centimeters
211 hres
= (int)(10*mm2inches
*hres
);
212 vres
= (int)(10*mm2inches
*vres
);
213 // fall through to convert it to resolution in meters
215 case wxIMAGE_RESOLUTION_CM
:
216 // convert resolution in centimeters to resolution in meters
222 hdr
.h_res
= wxUINT32_SWAP_ON_BE(hres
);
223 hdr
.v_res
= wxUINT32_SWAP_ON_BE(vres
);
224 hdr
.num_clrs
= wxUINT32_SWAP_ON_BE(palette_size
); // # colors in colormap
225 hdr
.num_signif_clrs
= 0; // all colors are significant
229 if (// VS: looks ugly but compilers tend to do ugly things with structs,
230 // like aligning hdr.filesize's ofset to dword :(
231 // VZ: we should add padding then...
232 !stream
.WriteAll(&hdr
.magic
, 2) ||
233 !stream
.WriteAll(&hdr
.filesize
, 4) ||
234 !stream
.WriteAll(&hdr
.reserved
, 4) ||
235 !stream
.WriteAll(&hdr
.data_offset
, 4)
240 wxLogError(_("BMP: Couldn't write the file (Bitmap) header."));
248 !stream
.WriteAll(&hdr
.bih_size
, 4) ||
249 !stream
.WriteAll(&hdr
.width
, 4) ||
250 !stream
.WriteAll(&hdr
.height
, 4) ||
251 !stream
.WriteAll(&hdr
.planes
, 2) ||
252 !stream
.WriteAll(&hdr
.bpp
, 2) ||
253 !stream
.WriteAll(&hdr
.compression
, 4) ||
254 !stream
.WriteAll(&hdr
.size_of_bmp
, 4) ||
255 !stream
.WriteAll(&hdr
.h_res
, 4) ||
256 !stream
.WriteAll(&hdr
.v_res
, 4) ||
257 !stream
.WriteAll(&hdr
.num_clrs
, 4) ||
258 !stream
.WriteAll(&hdr
.num_signif_clrs
, 4)
263 wxLogError(_("BMP: Couldn't write the file (BitmapInfo) header."));
269 wxPalette
*palette
= NULL
; // entries for quantized images
270 wxUint8
*rgbquad
= NULL
; // for the RGBQUAD bytes for the colormap
271 wxImage
*q_image
= NULL
; // destination for quantized image
273 // if <24bpp use quantization to reduce colors for *some* of the formats
274 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_4BPP
) ||
275 (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_PALETTE
) )
277 // make a new palette and quantize the image
278 if (format
!= wxBMP_8BPP_PALETTE
)
280 q_image
= new wxImage();
282 // I get a delete error using Quantize when desired colors > 236
283 int quantize
= ((palette_size
> 236) ? 236 : palette_size
);
284 // fill the destination too, it gives much nicer 4bpp images
285 wxQuantize::Quantize( *image
, *q_image
, &palette
, quantize
, 0,
286 wxQUANTIZE_FILL_DESTINATION_IMAGE
);
291 palette
= new wxPalette(image
->GetPalette());
292 #endif // wxUSE_PALETTE
296 unsigned char r
, g
, b
;
297 rgbquad
= new wxUint8
[palette_size
*4];
299 for (i
= 0; i
< palette_size
; i
++)
302 if ( !palette
->GetRGB(i
, &r
, &g
, &b
) )
303 #endif // wxUSE_PALETTE
312 // make a 256 entry greyscale colormap or 2 entry black & white
313 else if ( (format
== wxBMP_8BPP_GREY
) || (format
== wxBMP_8BPP_RED
) ||
314 (format
== wxBMP_1BPP_BW
) )
316 rgbquad
= new wxUint8
[palette_size
*4];
318 for ( int i
= 0; i
< palette_size
; i
++ )
320 // if 1BPP_BW then the value should be either 0 or 255
321 wxUint8 c
= (wxUint8
)((i
> 0) && (format
== wxBMP_1BPP_BW
) ? 255 : i
);
330 // if the colormap was made, then it needs to be written
335 if ( !stream
.WriteAll(rgbquad
, palette_size
*4) )
339 wxLogError(_("BMP: Couldn't write RGB color map."));
344 #endif // wxUSE_PALETTE
352 // pointer to the image data, use quantized if available
353 wxUint8
*data
= (wxUint8
*) image
->GetData();
354 if (q_image
) if (q_image
->IsOk()) data
= (wxUint8
*) q_image
->GetData();
356 wxUint8
*buffer
= new wxUint8
[row_width
];
357 memset(buffer
, 0, row_width
);
361 for (y
= image
->GetHeight() -1; y
>= 0; y
--)
363 if ( format
== wxBMP_24BPP
) // 3 bytes per pixel red,green,blue
365 for ( x
= 0; x
< width
; x
++ )
367 pixel
= 3*(y
*width
+ x
);
369 buffer
[3*x
] = data
[pixel
+2];
370 buffer
[3*x
+ 1] = data
[pixel
+1];
371 buffer
[3*x
+ 2] = data
[pixel
];
374 else if ((format
== wxBMP_8BPP
) || // 1 byte per pixel in color
375 (format
== wxBMP_8BPP_PALETTE
))
377 for (x
= 0; x
< width
; x
++)
379 pixel
= 3*(y
*width
+ x
);
381 buffer
[x
] = (wxUint8
)palette
->GetPixel( data
[pixel
],
385 // FIXME: what should this be? use some std palette maybe?
387 #endif // wxUSE_PALETTE
390 else if ( format
== wxBMP_8BPP_GREY
) // 1 byte per pix, rgb ave to grey
392 for (x
= 0; x
< width
; x
++)
394 pixel
= 3*(y
*width
+ x
);
395 buffer
[x
] = (wxUint8
)(.299*data
[pixel
] +
400 else if ( format
== wxBMP_8BPP_RED
) // 1 byte per pixel, red as greys
402 for (x
= 0; x
< width
; x
++)
404 buffer
[x
] = (wxUint8
)data
[3*(y
*width
+ x
)];
407 else if ( format
== wxBMP_4BPP
) // 4 bpp in color
409 for (x
= 0; x
< width
; x
+=2)
411 pixel
= 3*(y
*width
+ x
);
413 // fill buffer, ignore if > width
415 buffer
[x
/2] = (wxUint8
)(
416 ((wxUint8
)palette
->GetPixel(data
[pixel
],
418 data
[pixel
+2]) << 4) |
421 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3],
425 // FIXME: what should this be? use some std palette maybe?
427 #endif // wxUSE_PALETTE
430 else if ( format
== wxBMP_1BPP
) // 1 bpp in "color"
432 for (x
= 0; x
< width
; x
+=8)
434 pixel
= 3*(y
*width
+ x
);
437 buffer
[x
/8] = (wxUint8
)(
438 ((wxUint8
)palette
->GetPixel(data
[pixel
], data
[pixel
+1], data
[pixel
+2]) << 7) |
439 (((x
+1) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3], data
[pixel
+4], data
[pixel
+5]) << 6)) |
440 (((x
+2) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+6], data
[pixel
+7], data
[pixel
+8]) << 5)) |
441 (((x
+3) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+9], data
[pixel
+10], data
[pixel
+11]) << 4)) |
442 (((x
+4) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+12], data
[pixel
+13], data
[pixel
+14]) << 3)) |
443 (((x
+5) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+15], data
[pixel
+16], data
[pixel
+17]) << 2)) |
444 (((x
+6) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+18], data
[pixel
+19], data
[pixel
+20]) << 1)) |
445 (((x
+7) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+21], data
[pixel
+22], data
[pixel
+23]) )) );
447 // FIXME: what should this be? use some std palette maybe?
449 #endif // wxUSE_PALETTE
452 else if ( format
== wxBMP_1BPP_BW
) // 1 bpp B&W colormap from red color ONLY
454 for (x
= 0; x
< width
; x
+=8)
456 pixel
= 3*(y
*width
+ x
);
458 buffer
[x
/8] = (wxUint8
)(
459 (((wxUint8
)(data
[pixel
] /128.)) << 7) |
460 (((x
+1) > width
) ? 0 : (((wxUint8
)(data
[pixel
+3] /128.)) << 6)) |
461 (((x
+2) > width
) ? 0 : (((wxUint8
)(data
[pixel
+6] /128.)) << 5)) |
462 (((x
+3) > width
) ? 0 : (((wxUint8
)(data
[pixel
+9] /128.)) << 4)) |
463 (((x
+4) > width
) ? 0 : (((wxUint8
)(data
[pixel
+12]/128.)) << 3)) |
464 (((x
+5) > width
) ? 0 : (((wxUint8
)(data
[pixel
+15]/128.)) << 2)) |
465 (((x
+6) > width
) ? 0 : (((wxUint8
)(data
[pixel
+18]/128.)) << 1)) |
466 (((x
+7) > width
) ? 0 : (((wxUint8
)(data
[pixel
+21]/128.)) )) );
470 if ( !stream
.WriteAll(buffer
, row_width
) )
474 wxLogError(_("BMP: Couldn't write data."));
479 #endif // wxUSE_PALETTE
487 #endif // wxUSE_PALETTE
496 static void Free(BMPPalette
* pal
) { delete [] pal
; }
498 unsigned char r
, g
, b
;
501 bool wxBMPHandler::DoLoadDib(wxImage
* image
, int width
, int height
,
502 int bpp
, int ncolors
, int comp
,
503 wxFileOffset bmpOffset
, wxInputStream
& stream
,
504 bool verbose
, bool IsBmp
, bool hasPalette
)
506 wxInt32 aDword
, rmask
= 0, gmask
= 0, bmask
= 0, amask
= 0;
507 int rshift
= 0, gshift
= 0, bshift
= 0, ashift
= 0;
508 int rbits
= 0, gbits
= 0, bbits
= 0;
514 // allocate space for palette if needed:
519 cmap
= new BMPPalette
[ncolors
];
524 wxLogError(_("BMP: Couldn't allocate memory."));
534 wxON_BLOCK_EXIT1(&BMPPalette::Free
, cmap
);
536 bool isUpsideDown
= true;
540 isUpsideDown
= false;
544 // destroy existing here instead of:
546 image
->Create(width
, height
);
548 unsigned char *ptr
= image
->GetData();
554 wxLogError( _("BMP: Couldn't allocate memory.") );
559 unsigned char *alpha
;
562 // tell the image to allocate an alpha buffer
564 alpha
= image
->GetAlpha();
569 wxLogError(_("BMP: Couldn't allocate memory."));
579 // Reading the palette, if it exists:
580 if ( bpp
< 16 && ncolors
!= 0 )
582 unsigned char* r
= new unsigned char[ncolors
];
583 unsigned char* g
= new unsigned char[ncolors
];
584 unsigned char* b
= new unsigned char[ncolors
];
585 for (int j
= 0; j
< ncolors
; j
++)
589 if ( !stream
.ReadAll(bbuf
, 4) )
602 //used in reading .ico file mask
605 b
[j
] = cmap
[j
].b
= ( j
? 255 : 0 );
610 // Set the palette for the wxImage
611 image
->SetPalette(wxPalette(ncolors
, r
, g
, b
));
612 #endif // wxUSE_PALETTE
618 else if ( bpp
== 16 || bpp
== 32 )
620 if ( comp
== BI_BITFIELDS
)
623 if ( !stream
.ReadAll(dbuf
, 4 * 3) )
626 rmask
= wxINT32_SWAP_ON_BE(dbuf
[0]);
627 gmask
= wxINT32_SWAP_ON_BE(dbuf
[1]);
628 bmask
= wxINT32_SWAP_ON_BE(dbuf
[2]);
629 // find shift amount (Least significant bit of mask)
630 for (bit
= bpp
-1; bit
>=0; bit
--)
632 if (bmask
& (1 << bit
))
634 if (gmask
& (1 << bit
))
636 if (rmask
& (1 << bit
))
639 // Find number of bits in mask (MSB-LSB+1)
640 for (bit
= 0; bit
< bpp
; bit
++)
642 if (bmask
& (1 << bit
))
643 bbits
= bit
-bshift
+1;
644 if (gmask
& (1 << bit
))
645 gbits
= bit
-gshift
+1;
646 if (rmask
& (1 << bit
))
647 rbits
= bit
-rshift
+1;
650 else if ( bpp
== 16 )
662 else if ( bpp
== 32 )
680 * Reading the image data
684 // NOTE: seeking a positive amount in wxFromCurrent mode allows us to
685 // load even non-seekable streams (see wxInputStream::SeekI docs)!
686 const wxFileOffset pos
= stream
.TellI();
687 if ( pos
== wxInvalidOffset
||
689 stream
.SeekI(bmpOffset
- pos
, wxFromCurrent
) == wxInvalidOffset
) )
691 //else: icon, just carry on
694 unsigned char *data
= ptr
;
696 /* set the whole image to the background color */
697 if ( bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
) )
699 for (int i
= 0; i
< width
* height
; i
++)
708 int linesize
= ((width
* bpp
+ 31) / 32) * 4;
710 // flag indicating if we have any not fully transparent alpha values: this
711 // is used to account for the bitmaps which use 32bpp format (normally
712 // meaning that they have alpha channel) but have only zeroes in it so that
713 // without this hack they appear fully transparent -- and as this is
714 // unlikely intentional, we consider that they don't have alpha at all in
715 // this case (see #10915)
716 bool hasValidAlpha
= false;
718 for ( int row
= 0; row
< height
; row
++ )
720 int line
= isUpsideDown
? height
- 1 - row
: row
;
723 for ( int column
= 0; column
< width
; )
728 aByte
= stream
.GetC();
729 if ( !stream
.IsOk() )
734 for (int bit
= 0; bit
< 8 && column
< width
; bit
++)
736 int index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
737 ptr
[poffset
] = cmap
[index
].r
;
738 ptr
[poffset
+ 1] = cmap
[index
].g
;
739 ptr
[poffset
+ 2] = cmap
[index
].b
;
745 if ( comp
== BI_RLE4
)
749 aByte
= stream
.GetC();
750 if ( !stream
.IsOk() )
757 // end of scanline marker
761 else if ( aByte
== 1 )
763 // end of RLE data marker, stop decoding
767 else if ( aByte
== 2 )
769 // delta marker, move in image
770 aByte
= stream
.GetC();
771 if ( !stream
.IsOk() )
774 linepos
= column
* bpp
/ 4;
775 aByte
= stream
.GetC();
776 if ( !stream
.IsOk() )
778 row
+= aByte
; // upside down
782 int absolute
= aByte
;
785 for (int k
= 0; k
< absolute
; k
++)
790 aByte
= stream
.GetC();
791 if ( !stream
.IsOk() )
793 nibble
[0] = (wxUint8
)( (aByte
& 0xF0) >> 4 ) ;
794 nibble
[1] = (wxUint8
)( aByte
& 0x0F ) ;
796 ptr
[poffset
] = cmap
[nibble
[k%2
]].r
;
797 ptr
[poffset
+ 1] = cmap
[nibble
[k%2
]].g
;
798 ptr
[poffset
+ 2] = cmap
[nibble
[k%2
]].b
;
803 if ( readBytes
& 0x01 )
805 aByte
= stream
.GetC();
806 if ( !stream
.IsOk() )
814 nibble
[0] = (wxUint8
)( (aByte
& 0xF0) >> 4 ) ;
815 nibble
[1] = (wxUint8
)( aByte
& 0x0F ) ;
817 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
819 ptr
[poffset
] = cmap
[nibble
[l%2
]].r
;
820 ptr
[poffset
+ 1] = cmap
[nibble
[l%2
]].g
;
821 ptr
[poffset
+ 2] = cmap
[nibble
[l%2
]].b
;
830 for (int nibble
= 0; nibble
< 2 && column
< width
; nibble
++)
832 int index
= ((aByte
& (0xF0 >> (nibble
* 4))) >> (!nibble
* 4));
835 ptr
[poffset
] = cmap
[index
].r
;
836 ptr
[poffset
+ 1] = cmap
[index
].g
;
837 ptr
[poffset
+ 2] = cmap
[index
].b
;
844 if ( comp
== BI_RLE8
)
848 aByte
= stream
.GetC();
849 if ( !stream
.IsOk() )
856 // end of scanline marker
860 else if ( aByte
== 1 )
862 // end of RLE data marker, stop decoding
866 else if ( aByte
== 2 )
868 // delta marker, move in image
869 aByte
= stream
.GetC();
870 if ( !stream
.IsOk() )
873 linepos
= column
* bpp
/ 8;
874 aByte
= stream
.GetC();
875 if ( !stream
.IsOk() )
881 int absolute
= aByte
;
882 for (int k
= 0; k
< absolute
; k
++)
885 aByte
= stream
.GetC();
886 if ( !stream
.IsOk() )
888 ptr
[poffset
] = cmap
[aByte
].r
;
889 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
890 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
893 if ( absolute
& 0x01 )
895 aByte
= stream
.GetC();
896 if ( !stream
.IsOk() )
903 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
905 ptr
[poffset
] = cmap
[aByte
].r
;
906 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
907 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
915 ptr
[poffset
] = cmap
[aByte
].r
;
916 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
917 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
919 // linepos += size; seems to be wrong, RR
923 else if ( bpp
== 24 )
925 if ( !stream
.ReadAll(bbuf
, 3) )
928 ptr
[poffset
] = (unsigned char)bbuf
[2];
929 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
930 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
933 else if ( bpp
== 16 )
936 if ( !stream
.ReadAll(&aWord
, 2) )
938 aWord
= wxUINT16_SWAP_ON_BE(aWord
);
940 /* Use the masks and calculated amount of shift
941 to retrieve the color data out of the word. Then
942 shift it left by (8 - number of bits) such that
943 the image has the proper dynamic range */
944 temp
= (unsigned char)(((aWord
& rmask
) >> rshift
) << (8-rbits
));
946 temp
= (unsigned char)(((aWord
& gmask
) >> gshift
) << (8-gbits
));
947 ptr
[poffset
+ 1] = temp
;
948 temp
= (unsigned char)(((aWord
& bmask
) >> bshift
) << (8-bbits
));
949 ptr
[poffset
+ 2] = temp
;
955 if ( !stream
.ReadAll(&aDword
, 4) )
958 aDword
= wxINT32_SWAP_ON_BE(aDword
);
960 temp
= (unsigned char)((aDword
& rmask
) >> rshift
);
962 temp
= (unsigned char)((aDword
& gmask
) >> gshift
);
963 ptr
[poffset
+ 1] = temp
;
964 temp
= (unsigned char)((aDword
& bmask
) >> bshift
);
965 ptr
[poffset
+ 2] = temp
;
968 temp
= (unsigned char)((aDword
& amask
) >> ashift
);
969 alpha
[line
* width
+ column
] = temp
;
971 if ( temp
!= wxALPHA_TRANSPARENT
)
972 hasValidAlpha
= true;
977 while ( (linepos
< linesize
) && (comp
!= 1) && (comp
!= 2) )
980 if ( !stream
.ReadAll(&aByte
, 1) )
985 image
->SetMask(false);
987 // check if we had any valid alpha values in this bitmap
988 if ( alpha
&& !hasValidAlpha
)
990 // we didn't, so finally discard the alpha channel completely
994 const wxStreamError err
= stream
.GetLastError();
995 return err
== wxSTREAM_NO_ERROR
|| err
== wxSTREAM_EOF
;
998 bool wxBMPHandler::LoadDib(wxImage
*image
, wxInputStream
& stream
,
999 bool verbose
, bool IsBmp
)
1007 // read the header off the .BMP format file
1008 if ( !stream
.ReadAll(bbuf
, 2) ||
1009 !stream
.ReadAll(dbuf
, 16) )
1014 if ( !stream
.ReadAll(dbuf
, 4) )
1018 wxInt32 size
= wxINT32_SWAP_ON_BE(dbuf
[0]);
1020 wxFileOffset offset
= wxINT32_SWAP_ON_BE(dbuf
[2]);
1022 if ( !stream
.ReadAll(dbuf
, 4 * 2) )
1025 int width
= wxINT32_SWAP_ON_BE((int)dbuf
[0]);
1026 int height
= wxINT32_SWAP_ON_BE((int)dbuf
[1]);
1027 if ( !IsBmp
)height
= height
/ 2; // for icons divide by 2
1029 if ( width
> 32767 )
1033 wxLogError( _("DIB Header: Image width > 32767 pixels for file.") );
1037 if ( height
> 32767 )
1041 wxLogError( _("DIB Header: Image height > 32767 pixels for file.") );
1046 if ( !stream
.ReadAll(&aWord
, 2) )
1051 int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
1053 if ( !stream
.ReadAll(&aWord
, 2) )
1056 int bpp
= wxUINT16_SWAP_ON_BE((int)aWord
);
1057 if ( bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
!= 16 && bpp
!= 24 && bpp
!= 32 )
1061 wxLogError( _("DIB Header: Unknown bitdepth in file.") );
1066 if ( !stream
.ReadAll(dbuf
, 4 * 4) )
1069 int comp
= wxINT32_SWAP_ON_BE((int)dbuf
[0]);
1070 if ( comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&&
1071 comp
!= BI_BITFIELDS
)
1075 wxLogError( _("DIB Header: Unknown encoding in file.") );
1080 if ( !stream
.ReadAll(dbuf
, 4 * 2) )
1083 int ncolors
= wxINT32_SWAP_ON_BE( (int)dbuf
[0] );
1086 /* some more sanity checks */
1087 if (((comp
== BI_RLE4
) && (bpp
!= 4)) ||
1088 ((comp
== BI_RLE8
) && (bpp
!= 8)) ||
1089 ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
1093 wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") );
1098 //read DIB; this is the BMP image or the XOR part of an icon image
1099 if ( !DoLoadDib(image
, width
, height
, bpp
, ncolors
, comp
, offset
, stream
,
1100 verbose
, IsBmp
, true) )
1104 wxLogError( _("Error in reading image DIB.") );
1111 //read Icon mask which is monochrome
1112 //there is no palette, so we will create one
1114 if ( !DoLoadDib(&mask
, width
, height
, 1, 2, BI_RGB
, offset
, stream
,
1115 verbose
, IsBmp
, false) )
1119 wxLogError( _("ICO: Error in reading mask DIB.") );
1123 image
->SetMaskFromImage(mask
, 255, 255, 255);
1127 // the resolution in the bitmap header is in meters, convert to centimeters
1128 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONUNIT
, wxIMAGE_RESOLUTION_CM
);
1129 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONX
, dbuf
[2]/100);
1130 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONY
, dbuf
[3]/100);
1135 bool wxBMPHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1136 bool verbose
, int WXUNUSED(index
))
1138 // Read a single DIB fom the file:
1139 return LoadDib(image
, stream
, verbose
, true/*isBmp*/);
1142 bool wxBMPHandler::DoCanRead(wxInputStream
& stream
)
1144 unsigned char hdr
[2];
1146 if ( !stream
.ReadAll(hdr
, WXSIZEOF(hdr
)) ) // it's ok to modify the stream position here
1149 // do we have the BMP file signature?
1150 return hdr
[0] == 'B' && hdr
[1] == 'M';
1153 #endif // wxUSE_STREAMS
1157 //-----------------------------------------------------------------------------
1159 //-----------------------------------------------------------------------------
1161 IMPLEMENT_DYNAMIC_CLASS(wxICOHandler
, wxBMPHandler
)
1167 wxUint8 bWidth
; // Width of the image
1168 wxUint8 bHeight
; // Height of the image (times 2)
1169 wxUint8 bColorCount
; // Number of colors in image (0 if >=8bpp)
1170 wxUint8 bReserved
; // Reserved
1172 // these two are different in icons and cursors:
1174 wxUint16 wPlanes
; // Color Planes or XHotSpot
1175 wxUint16 wBitCount
; // Bits per pixel or YHotSpot
1177 wxUint32 dwBytesInRes
; // how many bytes in this resource?
1178 wxUint32 dwImageOffset
; // where in the file is this image
1183 wxUint16 idReserved
; // Reserved
1184 wxUint16 idType
; // resource type (1 for icons, 2 for cursors)
1185 wxUint16 idCount
; // how many images?
1189 bool wxICOHandler::SaveFile(wxImage
*image
,
1190 wxOutputStream
& stream
,
1194 //sanity check; icon must be less than 127 pixels high and 255 wide
1195 if ( image
->GetHeight () > 127 )
1199 wxLogError(_("ICO: Image too tall for an icon."));
1203 if ( image
->GetWidth () > 255 )
1207 wxLogError(_("ICO: Image too wide for an icon."));
1212 const int images
= 1; // only generate one image
1214 // VS: This is a hack of sort - since ICO and CUR files are almost
1215 // identical, we have all the meat in wxICOHandler and check for
1216 // the actual (handler) type when the code has to distinguish between
1218 int type
= (this->GetType() == wxBITMAP_TYPE_CUR
) ? 2 : 1;
1220 // write a header, (ICONDIR)
1221 // Calculate the header size
1222 wxUint32 offset
= 3 * sizeof(wxUint16
);
1225 IconDir
.idReserved
= 0;
1226 IconDir
.idType
= wxUINT16_SWAP_ON_BE((wxUint16
)type
);
1227 IconDir
.idCount
= wxUINT16_SWAP_ON_BE((wxUint16
)images
);
1228 if ( !stream
.WriteAll(&IconDir
.idReserved
, sizeof(IconDir
.idReserved
)) ||
1229 !stream
.WriteAll(&IconDir
.idType
, sizeof(IconDir
.idType
)) ||
1230 !stream
.WriteAll(&IconDir
.idCount
, sizeof(IconDir
.idCount
)) )
1234 wxLogError(_("ICO: Error writing the image file!"));
1239 // for each iamage write a description ICONDIRENTRY:
1240 ICONDIRENTRY icondirentry
;
1241 for (int img
= 0; img
< images
; img
++)
1245 if ( image
->HasMask() )
1247 // make another image with black/white:
1248 mask
= image
->ConvertToMono (image
->GetMaskRed(), image
->GetMaskGreen(), image
->GetMaskBlue() );
1250 // now we need to change the masked regions to black:
1251 unsigned char r
= image
->GetMaskRed();
1252 unsigned char g
= image
->GetMaskGreen();
1253 unsigned char b
= image
->GetMaskBlue();
1254 if ( (r
!= 0) || (g
!= 0) || (b
!= 0) )
1256 // Go round and apply black to the masked bits:
1258 for (i
= 0; i
< mask
.GetWidth(); i
++)
1260 for (j
= 0; j
< mask
.GetHeight(); j
++)
1262 if ((r
== mask
.GetRed(i
, j
)) &&
1263 (g
== mask
.GetGreen(i
, j
))&&
1264 (b
== mask
.GetBlue(i
, j
)) )
1265 image
->SetRGB(i
, j
, 0, 0, 0 );
1272 // just make a black mask all over:
1273 mask
= image
->Copy();
1275 for (i
= 0; i
< mask
.GetWidth(); i
++)
1276 for (j
= 0; j
< mask
.GetHeight(); j
++)
1277 mask
.SetRGB(i
, j
, 0, 0, 0 );
1279 // Set the formats for image and mask
1280 // (Windows never saves with more than 8 colors):
1281 image
->SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_8BPP
);
1283 // monochome bitmap:
1284 mask
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_1BPP_BW
);
1286 bool IsMask
= false;
1288 //calculate size and offset of image and mask
1289 wxCountingOutputStream cStream
;
1290 bool bResult
= SaveDib(image
, cStream
, verbose
, IsBmp
, IsMask
);
1295 wxLogError(_("ICO: Error writing the image file!"));
1301 bResult
= SaveDib(&mask
, cStream
, verbose
, IsBmp
, IsMask
);
1306 wxLogError(_("ICO: Error writing the image file!"));
1310 wxUint32 Size
= cStream
.GetSize();
1312 // wxCountingOutputStream::IsOk() always returns true for now and this
1313 // "if" provokes VC++ warnings in optimized build
1315 if ( !cStream
.IsOk() )
1319 wxLogError(_("ICO: Error writing the image file!"));
1325 offset
= offset
+ sizeof(ICONDIRENTRY
);
1327 icondirentry
.bWidth
= (wxUint8
)image
->GetWidth();
1328 icondirentry
.bHeight
= (wxUint8
)(2 * image
->GetHeight());
1329 icondirentry
.bColorCount
= 0;
1330 icondirentry
.bReserved
= 0;
1331 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE(1);
1332 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE(wxBMP_8BPP
);
1333 if ( type
== 2 /*CUR*/)
1335 int hx
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ?
1336 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) :
1337 image
->GetWidth() / 2;
1338 int hy
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ?
1339 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) :
1340 image
->GetHeight() / 2;
1342 // actually write the values of the hot spot here:
1343 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE((wxUint16
)hx
);
1344 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE((wxUint16
)hy
);
1346 icondirentry
.dwBytesInRes
= wxUINT32_SWAP_ON_BE(Size
);
1347 icondirentry
.dwImageOffset
= wxUINT32_SWAP_ON_BE(offset
);
1349 // increase size to allow for the data written:
1353 if ( !stream
.WriteAll(&icondirentry
.bWidth
, sizeof(icondirentry
.bWidth
)) ||
1354 !stream
.WriteAll(&icondirentry
.bHeight
, sizeof(icondirentry
.bHeight
)) ||
1355 !stream
.WriteAll(&icondirentry
.bColorCount
, sizeof(icondirentry
.bColorCount
)) ||
1356 !stream
.WriteAll(&icondirentry
.bReserved
, sizeof(icondirentry
.bReserved
)) ||
1357 !stream
.WriteAll(&icondirentry
.wPlanes
, sizeof(icondirentry
.wPlanes
)) ||
1358 !stream
.WriteAll(&icondirentry
.wBitCount
, sizeof(icondirentry
.wBitCount
)) ||
1359 !stream
.WriteAll(&icondirentry
.dwBytesInRes
, sizeof(icondirentry
.dwBytesInRes
)) ||
1360 !stream
.WriteAll(&icondirentry
.dwImageOffset
, sizeof(icondirentry
.dwImageOffset
)) )
1364 wxLogError(_("ICO: Error writing the image file!"));
1369 // actually save it:
1371 bResult
= SaveDib(image
, stream
, verbose
, IsBmp
, IsMask
);
1376 wxLogError(_("ICO: Error writing the image file!"));
1382 bResult
= SaveDib(&mask
, stream
, verbose
, IsBmp
, IsMask
);
1387 wxLogError(_("ICO: Error writing the image file!"));
1392 } // end of for loop
1397 bool wxICOHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1398 bool verbose
, int index
)
1400 if ( stream
.IsSeekable() && stream
.SeekI(0) == wxInvalidOffset
)
1405 return DoLoadFile(image
, stream
, verbose
, index
);
1408 bool wxICOHandler::DoLoadFile(wxImage
*image
, wxInputStream
& stream
,
1409 bool WXUNUSED(verbose
), int index
)
1411 bool bResult
wxDUMMY_INITIALIZE(false);
1416 if ( !stream
.ReadAll(&IconDir
, sizeof(IconDir
)) )
1419 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1421 // nType is 1 for Icons, 2 for Cursors:
1422 wxUint16 nType
= wxUINT16_SWAP_ON_BE(IconDir
.idType
);
1424 // loop round the icons and choose the best one:
1425 ICONDIRENTRY
*pIconDirEntry
= new ICONDIRENTRY
[nIcons
];
1426 ICONDIRENTRY
*pCurrentEntry
= pIconDirEntry
;
1429 int iSel
= wxNOT_FOUND
;
1431 // remember how many bytes we read from the stream:
1432 wxFileOffset alreadySeeked
= sizeof(IconDir
);
1434 for (unsigned int i
= 0; i
< nIcons
; i
++ )
1436 if ( !stream
.ReadAll(pCurrentEntry
, sizeof(ICONDIRENTRY
)) )
1439 alreadySeeked
+= stream
.LastRead();
1441 // bHeight and bColorCount are wxUint8
1442 if ( pCurrentEntry
->bWidth
>= wMax
)
1444 // see if we have more colors, ==0 indicates > 8bpp:
1445 if ( pCurrentEntry
->bColorCount
== 0 )
1446 pCurrentEntry
->bColorCount
= 255;
1447 if ( pCurrentEntry
->bColorCount
>= colmax
)
1450 wMax
= pCurrentEntry
->bWidth
;
1451 colmax
= pCurrentEntry
->bColorCount
;
1460 // VS: Note that we *have* to run the loop above even if index != -1, because
1461 // it reads ICONDIRENTRies.
1465 if ( iSel
== wxNOT_FOUND
|| iSel
< 0 || iSel
>= nIcons
)
1467 wxLogError(_("ICO: Invalid icon index."));
1472 // seek to selected icon:
1473 pCurrentEntry
= pIconDirEntry
+ iSel
;
1475 // NOTE: seeking a positive amount in wxFromCurrent mode allows us to
1476 // load even non-seekable streams (see wxInputStream::SeekI docs)!
1477 wxFileOffset offset
= wxUINT32_SWAP_ON_BE(pCurrentEntry
->dwImageOffset
) - alreadySeeked
;
1478 if (offset
!= 0 && stream
.SeekI(offset
, wxFromCurrent
) == wxInvalidOffset
)
1481 bResult
= LoadDib(image
, stream
, true, IsBmp
);
1482 bool bIsCursorType
= (this->GetType() == wxBITMAP_TYPE_CUR
) || (this->GetType() == wxBITMAP_TYPE_ANI
);
1483 if ( bResult
&& bIsCursorType
&& nType
== 2 )
1485 // it is a cursor, so let's set the hotspot:
1486 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wPlanes
));
1487 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wBitCount
));
1491 delete [] pIconDirEntry
;
1496 int wxICOHandler::DoGetImageCount(wxInputStream
& stream
)
1498 // It's ok to modify the stream position in this function.
1500 if ( stream
.IsSeekable() && stream
.SeekI(0) == wxInvalidOffset
)
1507 if ( !stream
.ReadAll(&IconDir
, sizeof(IconDir
)) )
1510 return (int)wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1513 bool wxICOHandler::DoCanRead(wxInputStream
& stream
)
1515 return CanReadICOOrCUR(&stream
, 1 /*for identifying an icon*/);
1519 #endif // wxUSE_STREAMS
1522 //-----------------------------------------------------------------------------
1524 //-----------------------------------------------------------------------------
1526 IMPLEMENT_DYNAMIC_CLASS(wxCURHandler
, wxICOHandler
)
1530 bool wxCURHandler::DoCanRead(wxInputStream
& stream
)
1532 return CanReadICOOrCUR(&stream
, 2 /*for identifying a cursor*/);
1535 #endif // wxUSE_STREAMS
1537 //-----------------------------------------------------------------------------
1539 //-----------------------------------------------------------------------------
1541 IMPLEMENT_DYNAMIC_CLASS(wxANIHandler
, wxCURHandler
)
1545 bool wxANIHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1546 bool WXUNUSED(verbose
), int index
)
1548 wxANIDecoder decoder
;
1549 if (!decoder
.Load(stream
))
1552 return decoder
.ConvertToImage(index
!= -1 ? (size_t)index
: 0, image
);
1555 bool wxANIHandler::DoCanRead(wxInputStream
& stream
)
1558 return decod
.CanRead(stream
);
1559 // it's ok to modify the stream position here
1562 int wxANIHandler::DoGetImageCount(wxInputStream
& stream
)
1564 wxANIDecoder decoder
;
1565 if (!decoder
.Load(stream
)) // it's ok to modify the stream position here
1568 return decoder
.GetFrameCount();
1571 static bool CanReadICOOrCUR(wxInputStream
*stream
, wxUint16 resourceType
)
1573 // It's ok to modify the stream position in this function.
1575 if ( stream
->IsSeekable() && stream
->SeekI(0) == wxInvalidOffset
)
1581 if ( !stream
->ReadAll(&iconDir
, sizeof(iconDir
)) )
1586 return !iconDir
.idReserved
// reserved, must be 0
1587 && wxUINT16_SWAP_ON_BE(iconDir
.idType
) == resourceType
// either 1 or 2
1588 && iconDir
.idCount
; // must contain at least one image
1591 #endif // wxUSE_STREAMS
1593 #endif // wxUSE_ICO_CUR
1595 #endif // wxUSE_IMAGE