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"
21 #include "wx/msw/wrapwin.h"
25 #include "wx/bitmap.h"
26 #include "wx/module.h"
29 #include "wx/imagbmp.h"
30 #include "wx/filefn.h"
31 #include "wx/wfstream.h"
33 #include "wx/quantize.h"
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
65 #define BI_BITFIELDS 3
68 #define poffset (line * width * 3 + column * 3)
70 bool wxBMPHandler::SaveFile(wxImage
*image
,
71 wxOutputStream
& stream
,
74 return SaveDib(image
, stream
, verbose
, true/*IsBmp*/, false/*IsMask*/);
77 bool wxBMPHandler::SaveDib(wxImage
*image
,
78 wxOutputStream
& stream
,
84 wxCHECK_MSG( image
, false, _T("invalid pointer in wxBMPHandler::SaveFile") );
89 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
123 wxLogError(_("BMP: wxImage doesn't have own wxPalette."));
129 else // you get 24bpp
131 format
= wxBMP_24BPP
;
136 unsigned width
= image
->GetWidth();
137 unsigned row_padding
= (4 - int(width
*bpp
/8.0) % 4) % 4; // # bytes to pad to dword
138 unsigned row_width
= int(width
* bpp
/8.0) + row_padding
; // # of bytes per row
143 wxUint16 magic
; // format magic, always 'BM'
144 wxUint32 filesize
; // total file size, inc. headers
145 wxUint32 reserved
; // for future use
146 wxUint32 data_offset
; // image data offset in the file
149 wxUint32 bih_size
; // 2nd part's size
150 wxUint32 width
, height
; // bitmap's dimensions
151 wxUint16 planes
; // num of planes
152 wxUint16 bpp
; // bits per pixel
153 wxUint32 compression
; // compression method
154 wxUint32 size_of_bmp
; // size of the bitmap
155 wxUint32 h_res
, v_res
; // image resolution in dpi
156 wxUint32 num_clrs
; // number of colors used
157 wxUint32 num_signif_clrs
;// number of significant colors
160 wxUint32 hdr_size
= 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/;
162 hdr
.magic
= wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/);
163 hdr
.filesize
= wxUINT32_SWAP_ON_BE( hdr_size
+ palette_size
*4 +
164 row_width
* image
->GetHeight() );
166 hdr
.data_offset
= wxUINT32_SWAP_ON_BE(hdr_size
+ palette_size
*4);
168 hdr
.bih_size
= wxUINT32_SWAP_ON_BE(hdr_size
- 14);
169 hdr
.width
= wxUINT32_SWAP_ON_BE(image
->GetWidth());
172 hdr
.height
= wxUINT32_SWAP_ON_BE(image
->GetHeight());
176 hdr
.height
= wxUINT32_SWAP_ON_BE(2 * image
->GetHeight());
178 hdr
.planes
= wxUINT16_SWAP_ON_BE(1); // always 1 plane
179 hdr
.bpp
= wxUINT16_SWAP_ON_BE(bpp
);
180 hdr
.compression
= 0; // RGB uncompressed
181 hdr
.size_of_bmp
= wxUINT32_SWAP_ON_BE(row_width
* image
->GetHeight());
182 hdr
.h_res
= hdr
.v_res
= wxUINT32_SWAP_ON_BE(72); // 72dpi is standard
183 hdr
.num_clrs
= wxUINT32_SWAP_ON_BE(palette_size
); // # colors in colormap
184 hdr
.num_signif_clrs
= 0; // all colors are significant
188 if (// VS: looks ugly but compilers tend to do ugly things with structs,
189 // like aligning hdr.filesize's ofset to dword :(
190 // VZ: we should add padding then...
191 !stream
.Write(&hdr
.magic
, 2) ||
192 !stream
.Write(&hdr
.filesize
, 4) ||
193 !stream
.Write(&hdr
.reserved
, 4) ||
194 !stream
.Write(&hdr
.data_offset
, 4)
198 wxLogError(_("BMP: Couldn't write the file (Bitmap) header."));
205 !stream
.Write(&hdr
.bih_size
, 4) ||
206 !stream
.Write(&hdr
.width
, 4) ||
207 !stream
.Write(&hdr
.height
, 4) ||
208 !stream
.Write(&hdr
.planes
, 2) ||
209 !stream
.Write(&hdr
.bpp
, 2) ||
210 !stream
.Write(&hdr
.compression
, 4) ||
211 !stream
.Write(&hdr
.size_of_bmp
, 4) ||
212 !stream
.Write(&hdr
.h_res
, 4) ||
213 !stream
.Write(&hdr
.v_res
, 4) ||
214 !stream
.Write(&hdr
.num_clrs
, 4) ||
215 !stream
.Write(&hdr
.num_signif_clrs
, 4)
219 wxLogError(_("BMP: Couldn't write the file (BitmapInfo) header."));
224 wxPalette
*palette
= NULL
; // entries for quantized images
225 wxUint8
*rgbquad
= NULL
; // for the RGBQUAD bytes for the colormap
226 wxImage
*q_image
= NULL
; // destination for quantized image
228 // if <24bpp use quantization to reduce colors for *some* of the formats
229 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_4BPP
) ||
230 (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_PALETTE
) )
232 // make a new palette and quantize the image
233 if (format
!= wxBMP_8BPP_PALETTE
)
235 q_image
= new wxImage();
237 // I get a delete error using Quantize when desired colors > 236
238 int quantize
= ((palette_size
> 236) ? 236 : palette_size
);
239 // fill the destination too, it gives much nicer 4bpp images
240 wxQuantize::Quantize( *image
, *q_image
, &palette
, quantize
, 0,
241 wxQUANTIZE_FILL_DESTINATION_IMAGE
);
246 palette
= new wxPalette(image
->GetPalette());
247 #endif // wxUSE_PALETTE
251 unsigned char r
, g
, b
;
252 rgbquad
= new wxUint8
[palette_size
*4];
254 for (i
= 0; i
< palette_size
; i
++)
257 if ( !palette
->GetRGB(i
, &r
, &g
, &b
) )
258 #endif // wxUSE_PALETTE
267 // make a 256 entry greyscale colormap or 2 entry black & white
268 else if ( (format
== wxBMP_8BPP_GREY
) || (format
== wxBMP_8BPP_RED
) ||
269 (format
== wxBMP_1BPP_BW
) )
271 rgbquad
= new wxUint8
[palette_size
*4];
273 for ( int i
= 0; i
< palette_size
; i
++ )
275 // if 1BPP_BW then the value should be either 0 or 255
276 wxUint8 c
= (wxUint8
)((i
> 0) && (format
== wxBMP_1BPP_BW
) ? 255 : i
);
285 // if the colormap was made, then it needs to be written
290 if ( !stream
.Write(rgbquad
, palette_size
*4) )
293 wxLogError(_("BMP: Couldn't write RGB color map."));
297 #endif // wxUSE_PALETTE
305 // pointer to the image data, use quantized if available
306 wxUint8
*data
= (wxUint8
*) image
->GetData();
307 if (q_image
) if (q_image
->Ok()) data
= (wxUint8
*) q_image
->GetData();
309 wxUint8
*buffer
= new wxUint8
[row_width
];
310 memset(buffer
, 0, row_width
);
314 for (y
= image
->GetHeight() -1; y
>= 0; y
--)
316 if ( format
== wxBMP_24BPP
) // 3 bytes per pixel red,green,blue
318 for ( x
= 0; x
< width
; x
++ )
320 pixel
= 3*(y
*width
+ x
);
322 buffer
[3*x
] = data
[pixel
+2];
323 buffer
[3*x
+ 1] = data
[pixel
+1];
324 buffer
[3*x
+ 2] = data
[pixel
];
327 else if ((format
== wxBMP_8BPP
) || // 1 byte per pixel in color
328 (format
== wxBMP_8BPP_PALETTE
))
330 for (x
= 0; x
< width
; x
++)
332 pixel
= 3*(y
*width
+ x
);
334 buffer
[x
] = (wxUint8
)palette
->GetPixel( data
[pixel
],
338 // FIXME: what should this be? use some std palette maybe?
340 #endif // wxUSE_PALETTE
343 else if ( format
== wxBMP_8BPP_GREY
) // 1 byte per pix, rgb ave to grey
345 for (x
= 0; x
< width
; x
++)
347 pixel
= 3*(y
*width
+ x
);
348 buffer
[x
] = (wxUint8
)(.299*data
[pixel
] +
353 else if ( format
== wxBMP_8BPP_RED
) // 1 byte per pixel, red as greys
355 for (x
= 0; x
< width
; x
++)
357 buffer
[x
] = (wxUint8
)data
[3*(y
*width
+ x
)];
360 else if ( format
== wxBMP_4BPP
) // 4 bpp in color
362 for (x
= 0; x
< width
; x
+=2)
364 pixel
= 3*(y
*width
+ x
);
366 // fill buffer, ignore if > width
368 buffer
[x
/2] = (wxUint8
)(
369 ((wxUint8
)palette
->GetPixel(data
[pixel
],
371 data
[pixel
+2]) << 4) |
374 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3],
378 // FIXME: what should this be? use some std palette maybe?
380 #endif // wxUSE_PALETTE
383 else if ( format
== wxBMP_1BPP
) // 1 bpp in "color"
385 for (x
= 0; x
< width
; x
+=8)
387 pixel
= 3*(y
*width
+ x
);
390 buffer
[x
/8] = (wxUint8
)(
391 ((wxUint8
)palette
->GetPixel(data
[pixel
], data
[pixel
+1], data
[pixel
+2]) << 7) |
392 (((x
+1) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3], data
[pixel
+4], data
[pixel
+5]) << 6)) |
393 (((x
+2) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+6], data
[pixel
+7], data
[pixel
+8]) << 5)) |
394 (((x
+3) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+9], data
[pixel
+10], data
[pixel
+11]) << 4)) |
395 (((x
+4) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+12], data
[pixel
+13], data
[pixel
+14]) << 3)) |
396 (((x
+5) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+15], data
[pixel
+16], data
[pixel
+17]) << 2)) |
397 (((x
+6) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+18], data
[pixel
+19], data
[pixel
+20]) << 1)) |
398 (((x
+7) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+21], data
[pixel
+22], data
[pixel
+23]) )) );
400 // FIXME: what should this be? use some std palette maybe?
402 #endif // wxUSE_PALETTE
405 else if ( format
== wxBMP_1BPP_BW
) // 1 bpp B&W colormap from red color ONLY
407 for (x
= 0; x
< width
; x
+=8)
409 pixel
= 3*(y
*width
+ x
);
411 buffer
[x
/8] = (wxUint8
)(
412 (((wxUint8
)(data
[pixel
] /128.)) << 7) |
413 (((x
+1) > width
) ? 0 : (((wxUint8
)(data
[pixel
+3] /128.)) << 6)) |
414 (((x
+2) > width
) ? 0 : (((wxUint8
)(data
[pixel
+6] /128.)) << 5)) |
415 (((x
+3) > width
) ? 0 : (((wxUint8
)(data
[pixel
+9] /128.)) << 4)) |
416 (((x
+4) > width
) ? 0 : (((wxUint8
)(data
[pixel
+12]/128.)) << 3)) |
417 (((x
+5) > width
) ? 0 : (((wxUint8
)(data
[pixel
+15]/128.)) << 2)) |
418 (((x
+6) > width
) ? 0 : (((wxUint8
)(data
[pixel
+18]/128.)) << 1)) |
419 (((x
+7) > width
) ? 0 : (((wxUint8
)(data
[pixel
+21]/128.)) )) );
423 if ( !stream
.Write(buffer
, row_width
) )
426 wxLogError(_("BMP: Couldn't write data."));
430 #endif // wxUSE_PALETTE
438 #endif // wxUSE_PALETTE
447 unsigned char r
, g
, b
;
450 bool wxBMPHandler::DoLoadDib(wxImage
* image
, int width
, int height
,
451 int bpp
, int ncolors
, int comp
,
452 wxFileOffset bmpOffset
, wxInputStream
& stream
,
453 bool verbose
, bool IsBmp
, bool hasPalette
)
455 wxInt32 aDword
, rmask
= 0, gmask
= 0, bmask
= 0;
456 int rshift
= 0, gshift
= 0, bshift
= 0;
457 int rbits
= 0, gbits
= 0, bbits
= 0;
463 // allocate space for palette if needed:
468 cmap
= new _cmap
[ncolors
];
472 wxLogError(_("BMP: Couldn't allocate memory."));
479 // destroy existing here instead of:
481 image
->Create(width
, height
);
483 unsigned char *ptr
= image
->GetData();
488 wxLogError( _("BMP: Couldn't allocate memory.") );
494 // Reading the palette, if it exists:
495 if ( bpp
< 16 && ncolors
!= 0 )
497 unsigned char* r
= new unsigned char[ncolors
];
498 unsigned char* g
= new unsigned char[ncolors
];
499 unsigned char* b
= new unsigned char[ncolors
];
500 for (int j
= 0; j
< ncolors
; j
++)
504 stream
.Read(bbuf
, 4);
515 //used in reading .ico file mask
518 b
[j
] = cmap
[j
].b
= ( j
? 255 : 0 );
523 // Set the palette for the wxImage
524 image
->SetPalette(wxPalette(ncolors
, r
, g
, b
));
525 #endif // wxUSE_PALETTE
531 else if ( bpp
== 16 || bpp
== 32 )
533 if ( comp
== BI_BITFIELDS
)
536 stream
.Read(dbuf
, 4 * 3);
537 rmask
= wxINT32_SWAP_ON_BE(dbuf
[0]);
538 gmask
= wxINT32_SWAP_ON_BE(dbuf
[1]);
539 bmask
= wxINT32_SWAP_ON_BE(dbuf
[2]);
540 // find shift amount (Least significant bit of mask)
541 for (bit
= bpp
-1; bit
>=0; bit
--)
543 if (bmask
& (1 << bit
))
545 if (gmask
& (1 << bit
))
547 if (rmask
& (1 << bit
))
550 // Find number of bits in mask (MSB-LSB+1)
551 for (bit
= 0; bit
< bpp
; bit
++)
553 if (bmask
& (1 << bit
))
554 bbits
= bit
-bshift
+1;
555 if (gmask
& (1 << bit
))
556 gbits
= bit
-gshift
+1;
557 if (rmask
& (1 << bit
))
558 rbits
= bit
-rshift
+1;
561 else if ( bpp
== 16 )
573 else if ( bpp
== 32 )
588 * Reading the image data
591 stream
.SeekI(bmpOffset
); // else icon, just carry on
593 unsigned char *data
= ptr
;
595 /* set the whole image to the background color */
596 if ( bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
) )
598 for (int i
= 0; i
< width
* height
; i
++)
607 int linesize
= ((width
* bpp
+ 31) / 32) * 4;
609 /* BMPs are stored upside down */
610 for ( int line
= (height
- 1); line
>= 0; line
-- )
613 for ( int column
= 0; column
< width
; )
618 aByte
= stream
.GetC();
621 for (int bit
= 0; bit
< 8 && column
< width
; bit
++)
623 int index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
624 ptr
[poffset
] = cmap
[index
].r
;
625 ptr
[poffset
+ 1] = cmap
[index
].g
;
626 ptr
[poffset
+ 2] = cmap
[index
].b
;
632 if ( comp
== BI_RLE4
)
636 aByte
= stream
.GetC();
644 else if ( aByte
== 1 )
649 else if ( aByte
== 2 )
651 aByte
= stream
.GetC();
653 linepos
= column
* bpp
/ 4;
654 aByte
= stream
.GetC();
655 line
-= aByte
; // upside down
659 int absolute
= aByte
;
662 for (int k
= 0; k
< absolute
; k
++)
667 aByte
= stream
.GetC();
668 nibble
[0] = (wxUint8
)( (aByte
& 0xF0) >> 4 ) ;
669 nibble
[1] = (wxUint8
)( aByte
& 0x0F ) ;
671 ptr
[poffset
] = cmap
[nibble
[k%2
]].r
;
672 ptr
[poffset
+ 1] = cmap
[nibble
[k%2
]].g
;
673 ptr
[poffset
+ 2] = cmap
[nibble
[k%2
]].b
;
678 if ( readBytes
& 0x01 )
679 aByte
= stream
.GetC();
685 nibble
[0] = (wxUint8
)( (aByte
& 0xF0) >> 4 ) ;
686 nibble
[1] = (wxUint8
)( aByte
& 0x0F ) ;
688 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
690 ptr
[poffset
] = cmap
[nibble
[l%2
]].r
;
691 ptr
[poffset
+ 1] = cmap
[nibble
[l%2
]].g
;
692 ptr
[poffset
+ 2] = cmap
[nibble
[l%2
]].b
;
701 for (int nibble
= 0; nibble
< 2 && column
< width
; nibble
++)
703 int index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
706 ptr
[poffset
] = cmap
[index
].r
;
707 ptr
[poffset
+ 1] = cmap
[index
].g
;
708 ptr
[poffset
+ 2] = cmap
[index
].b
;
715 if ( comp
== BI_RLE8
)
719 aByte
= stream
.GetC();
724 /* column = width; */
726 else if ( aByte
== 1 )
731 else if ( aByte
== 2 )
733 aByte
= stream
.GetC();
735 linepos
= column
* bpp
/ 8;
736 aByte
= stream
.GetC();
741 int absolute
= aByte
;
742 for (int k
= 0; k
< absolute
; k
++)
745 aByte
= stream
.GetC();
746 ptr
[poffset
] = cmap
[aByte
].r
;
747 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
748 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
751 if ( absolute
& 0x01 )
752 aByte
= stream
.GetC();
757 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
759 ptr
[poffset
] = cmap
[aByte
].r
;
760 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
761 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
769 ptr
[poffset
] = cmap
[aByte
].r
;
770 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
771 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
773 // linepos += size; seems to be wrong, RR
777 else if ( bpp
== 24 )
779 stream
.Read(bbuf
, 3);
781 ptr
[poffset
] = (unsigned char)bbuf
[2];
782 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
783 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
786 else if ( bpp
== 16 )
789 stream
.Read(&aWord
, 2);
790 aWord
= wxUINT16_SWAP_ON_BE(aWord
);
792 /* use the masks and calculated amonut of shift
793 to retrieve the color data out of the word. Then
794 shift it left by (8 - number of bits) such that
795 the image has the proper dynamic range */
796 temp
= (unsigned char)((aWord
& rmask
) >> rshift
<< (8-rbits
));
798 temp
= (unsigned char)((aWord
& gmask
) >> gshift
<< (8-gbits
));
799 ptr
[poffset
+ 1] = temp
;
800 temp
= (unsigned char)((aWord
& bmask
) >> bshift
<< (8-bbits
));
801 ptr
[poffset
+ 2] = temp
;
807 stream
.Read(&aDword
, 4);
808 aDword
= wxINT32_SWAP_ON_BE(aDword
);
810 temp
= (unsigned char)((aDword
& rmask
) >> rshift
);
812 temp
= (unsigned char)((aDword
& gmask
) >> gshift
);
813 ptr
[poffset
+ 1] = temp
;
814 temp
= (unsigned char)((aDword
& bmask
) >> bshift
);
815 ptr
[poffset
+ 2] = temp
;
819 while ( (linepos
< linesize
) && (comp
!= 1) && (comp
!= 2) )
821 stream
.Read(&aByte
, 1);
830 image
->SetMask(false);
832 const wxStreamError err
= stream
.GetLastError();
833 return err
== wxSTREAM_NO_ERROR
|| err
== wxSTREAM_EOF
;
836 bool wxBMPHandler::LoadDib(wxImage
*image
, wxInputStream
& stream
,
837 bool verbose
, bool IsBmp
)
843 wxFileOffset offset
= 0; // keep gcc quiet
846 // read the header off the .BMP format file
848 offset
= stream
.TellI();
849 if (offset
== wxInvalidOffset
)
852 stream
.Read(bbuf
, 2);
853 stream
.Read(dbuf
, 16);
857 stream
.Read(dbuf
, 4);
860 wxInt32 size
= wxINT32_SWAP_ON_BE(dbuf
[0]);
862 offset
= offset
+ wxINT32_SWAP_ON_BE(dbuf
[2]);
864 stream
.Read(dbuf
, 4 * 2);
865 int width
= wxINT32_SWAP_ON_BE((int)dbuf
[0]);
866 int height
= wxINT32_SWAP_ON_BE((int)dbuf
[1]);
867 if ( !IsBmp
)height
= height
/ 2; // for icons divide by 2
872 wxLogError( _("DIB Header: Image width > 32767 pixels for file.") );
875 if ( height
> 32767 )
878 wxLogError( _("DIB Header: Image height > 32767 pixels for file.") );
882 stream
.Read(&aWord
, 2);
885 int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
887 stream
.Read(&aWord
, 2);
888 int bpp
= wxUINT16_SWAP_ON_BE((int)aWord
);
889 if ( bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
!= 16 && bpp
!= 24 && bpp
!= 32 )
892 wxLogError( _("DIB Header: Unknown bitdepth in file.") );
896 stream
.Read(dbuf
, 4 * 4);
897 int comp
= wxINT32_SWAP_ON_BE((int)dbuf
[0]);
898 if ( comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&&
899 comp
!= BI_BITFIELDS
)
902 wxLogError( _("DIB Header: Unknown encoding in file.") );
906 stream
.Read(dbuf
, 4 * 2);
907 int ncolors
= wxINT32_SWAP_ON_BE( (int)dbuf
[0] );
910 /* some more sanity checks */
911 if (((comp
== BI_RLE4
) && (bpp
!= 4)) ||
912 ((comp
== BI_RLE8
) && (bpp
!= 8)) ||
913 ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
916 wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") );
920 //read DIB; this is the BMP image or the XOR part of an icon image
921 if ( !DoLoadDib(image
, width
, height
, bpp
, ncolors
, comp
, offset
, stream
,
922 verbose
, IsBmp
, true) )
925 wxLogError( _("Error in reading image DIB.") );
931 //read Icon mask which is monochrome
932 //there is no palette, so we will create one
934 if ( !DoLoadDib(&mask
, width
, height
, 1, 2, BI_RGB
, offset
, stream
,
935 verbose
, IsBmp
, false) )
938 wxLogError( _("ICO: Error in reading mask DIB.") );
941 image
->SetMaskFromImage(mask
, 255, 255, 255);
948 bool wxBMPHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
949 bool verbose
, int WXUNUSED(index
))
951 // Read a single DIB fom the file:
952 return LoadDib(image
, stream
, verbose
, true/*isBmp*/);
955 bool wxBMPHandler::DoCanRead(wxInputStream
& stream
)
957 unsigned char hdr
[2];
959 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
962 // do we have the BMP file signature?
963 return hdr
[0] == 'B' && hdr
[1] == 'M';
966 #endif // wxUSE_STREAMS
970 //-----------------------------------------------------------------------------
972 //-----------------------------------------------------------------------------
974 IMPLEMENT_DYNAMIC_CLASS(wxICOHandler
, wxBMPHandler
)
980 wxUint8 bWidth
; // Width of the image
981 wxUint8 bHeight
; // Height of the image (times 2)
982 wxUint8 bColorCount
; // Number of colors in image (0 if >=8bpp)
983 wxUint8 bReserved
; // Reserved
985 // these two are different in icons and cursors:
987 wxUint16 wPlanes
; // Color Planes or XHotSpot
988 wxUint16 wBitCount
; // Bits per pixel or YHotSpot
990 wxUint32 dwBytesInRes
; // how many bytes in this resource?
991 wxUint32 dwImageOffset
; // where in the file is this image
996 wxUint16 idReserved
; // Reserved
997 wxUint16 idType
; // resource type (1 for icons, 2 for cursors)
998 wxUint16 idCount
; // how many images?
1002 bool wxICOHandler::SaveFile(wxImage
*image
,
1003 wxOutputStream
& stream
,
1007 //sanity check; icon must be less than 127 pixels high and 255 wide
1008 if ( image
->GetHeight () > 127 )
1011 wxLogError(_("ICO: Image too tall for an icon."));
1014 if ( image
->GetWidth () > 255 )
1017 wxLogError(_("ICO: Image too wide for an icon."));
1021 const int images
= 1; // only generate one image
1023 // VS: This is a hack of sort - since ICO and CUR files are almost
1024 // identical, we have all the meat in wxICOHandler and check for
1025 // the actual (handler) type when the code has to distinguish between
1027 int type
= (this->GetType() == wxBITMAP_TYPE_CUR
) ? 2 : 1;
1029 // write a header, (ICONDIR)
1030 // Calculate the header size
1031 wxUint32 offset
= 3 * sizeof(wxUint16
);
1034 IconDir
.idReserved
= 0;
1035 IconDir
.idType
= wxUINT16_SWAP_ON_BE((wxUint16
)type
);
1036 IconDir
.idCount
= wxUINT16_SWAP_ON_BE((wxUint16
)images
);
1037 stream
.Write(&IconDir
.idReserved
, sizeof(IconDir
.idReserved
));
1038 stream
.Write(&IconDir
.idType
, sizeof(IconDir
.idType
));
1039 stream
.Write(&IconDir
.idCount
, sizeof(IconDir
.idCount
));
1040 if ( !stream
.IsOk() )
1043 wxLogError(_("ICO: Error writing the image file!"));
1047 // for each iamage write a description ICONDIRENTRY:
1048 ICONDIRENTRY icondirentry
;
1049 for (int img
= 0; img
< images
; img
++)
1053 if ( image
->HasMask() )
1055 // make another image with black/white:
1056 mask
= image
->ConvertToMono (image
->GetMaskRed(), image
->GetMaskGreen(), image
->GetMaskBlue() );
1058 // now we need to change the masked regions to black:
1059 unsigned char r
= image
->GetMaskRed();
1060 unsigned char g
= image
->GetMaskGreen();
1061 unsigned char b
= image
->GetMaskBlue();
1062 if ( (r
!= 0) || (g
!= 0) || (b
!= 0) )
1064 // Go round and apply black to the masked bits:
1066 for (i
= 0; i
< mask
.GetWidth(); i
++)
1068 for (j
= 0; j
< mask
.GetHeight(); j
++)
1070 if ((r
== mask
.GetRed(i
, j
)) &&
1071 (g
== mask
.GetGreen(i
, j
))&&
1072 (b
== mask
.GetBlue(i
, j
)) )
1073 image
->SetRGB(i
, j
, 0, 0, 0 );
1080 // just make a black mask all over:
1081 mask
= image
->Copy();
1083 for (i
= 0; i
< mask
.GetWidth(); i
++)
1084 for (j
= 0; j
< mask
.GetHeight(); j
++)
1085 mask
.SetRGB(i
, j
, 0, 0, 0 );
1087 // Set the formats for image and mask
1088 // (Windows never saves with more than 8 colors):
1089 image
->SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_8BPP
);
1091 // monochome bitmap:
1092 mask
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_1BPP_BW
);
1094 bool IsMask
= false;
1096 //calculate size and offset of image and mask
1097 wxCountingOutputStream cStream
;
1098 bool bResult
= SaveDib(image
, cStream
, verbose
, IsBmp
, IsMask
);
1102 wxLogError(_("ICO: Error writing the image file!"));
1107 bResult
= SaveDib(&mask
, cStream
, verbose
, IsBmp
, IsMask
);
1111 wxLogError(_("ICO: Error writing the image file!"));
1114 wxUint32 Size
= cStream
.GetSize();
1116 // wxCountingOutputStream::Ok() always returns true for now and this
1117 // "if" provokes VC++ warnings in optimized build
1119 if ( !cStream
.Ok() )
1122 wxLogError(_("ICO: Error writing the image file!"));
1127 offset
= offset
+ sizeof(ICONDIRENTRY
);
1129 icondirentry
.bWidth
= (wxUint8
)image
->GetWidth();
1130 icondirentry
.bHeight
= (wxUint8
)(2 * image
->GetHeight());
1131 icondirentry
.bColorCount
= 0;
1132 icondirentry
.bReserved
= 0;
1133 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE(1);
1134 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE(wxBMP_8BPP
);
1135 if ( type
== 2 /*CUR*/)
1137 int hx
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ?
1138 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) :
1139 image
->GetWidth() / 2;
1140 int hy
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ?
1141 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) :
1142 image
->GetHeight() / 2;
1144 // actually write the values of the hot spot here:
1145 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE((wxUint16
)hx
);
1146 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE((wxUint16
)hy
);
1148 icondirentry
.dwBytesInRes
= wxUINT32_SWAP_ON_BE(Size
);
1149 icondirentry
.dwImageOffset
= wxUINT32_SWAP_ON_BE(offset
);
1151 // increase size to allow for the data written:
1155 stream
.Write(&icondirentry
.bWidth
, sizeof(icondirentry
.bWidth
));
1156 stream
.Write(&icondirentry
.bHeight
, sizeof(icondirentry
.bHeight
));
1157 stream
.Write(&icondirentry
.bColorCount
, sizeof(icondirentry
.bColorCount
));
1158 stream
.Write(&icondirentry
.bReserved
, sizeof(icondirentry
.bReserved
));
1159 stream
.Write(&icondirentry
.wPlanes
, sizeof(icondirentry
.wPlanes
));
1160 stream
.Write(&icondirentry
.wBitCount
, sizeof(icondirentry
.wBitCount
));
1161 stream
.Write(&icondirentry
.dwBytesInRes
, sizeof(icondirentry
.dwBytesInRes
));
1162 stream
.Write(&icondirentry
.dwImageOffset
, sizeof(icondirentry
.dwImageOffset
));
1163 if ( !stream
.IsOk() )
1166 wxLogError(_("ICO: Error writing the image file!"));
1170 // actually save it:
1172 bResult
= SaveDib(image
, stream
, verbose
, IsBmp
, IsMask
);
1176 wxLogError(_("ICO: Error writing the image file!"));
1181 bResult
= SaveDib(&mask
, stream
, verbose
, IsBmp
, IsMask
);
1185 wxLogError(_("ICO: Error writing the image file!"));
1189 } // end of for loop
1194 bool wxICOHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1195 bool verbose
, int index
)
1198 return DoLoadFile(image
, stream
, verbose
, index
);
1201 bool wxICOHandler::DoLoadFile(wxImage
*image
, wxInputStream
& stream
,
1202 bool WXUNUSED(verbose
), int index
)
1204 bool bResult
wxDUMMY_INITIALIZE(false);
1209 wxFileOffset iPos
= stream
.TellI();
1210 stream
.Read(&IconDir
, sizeof(IconDir
));
1211 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1212 // nType is 1 for Icons, 2 for Cursors:
1213 wxUint16 nType
= wxUINT16_SWAP_ON_BE(IconDir
.idType
);
1215 // loop round the icons and choose the best one:
1216 ICONDIRENTRY
*pIconDirEntry
= new ICONDIRENTRY
[nIcons
];
1217 ICONDIRENTRY
*pCurrentEntry
= pIconDirEntry
;
1220 int iSel
= wxNOT_FOUND
;
1222 for (int i
= 0; i
< nIcons
; i
++ )
1224 stream
.Read(pCurrentEntry
, sizeof(ICONDIRENTRY
));
1225 // bHeight and bColorCount are wxUint8
1226 if ( pCurrentEntry
->bWidth
>= wMax
)
1228 // see if we have more colors, ==0 indicates > 8bpp:
1229 if ( pCurrentEntry
->bColorCount
== 0 )
1230 pCurrentEntry
->bColorCount
= 255;
1231 if ( pCurrentEntry
->bColorCount
>= colmax
)
1234 wMax
= pCurrentEntry
->bWidth
;
1235 colmax
= pCurrentEntry
->bColorCount
;
1243 // VS: Note that we *have* to run the loop above even if index != -1, because
1244 // it reads ICONDIRENTRies.
1248 if ( iSel
== wxNOT_FOUND
|| iSel
< 0 || iSel
>= nIcons
)
1250 wxLogError(_("ICO: Invalid icon index."));
1255 // seek to selected icon:
1256 pCurrentEntry
= pIconDirEntry
+ iSel
;
1257 stream
.SeekI(iPos
+ wxUINT32_SWAP_ON_BE(pCurrentEntry
->dwImageOffset
), wxFromStart
);
1258 bResult
= LoadDib(image
, stream
, true, IsBmp
);
1259 bool bIsCursorType
= (this->GetType() == wxBITMAP_TYPE_CUR
) || (this->GetType() == wxBITMAP_TYPE_ANI
);
1260 if ( bResult
&& bIsCursorType
&& nType
== 2 )
1262 // it is a cursor, so let's set the hotspot:
1263 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wPlanes
));
1264 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wBitCount
));
1267 delete[] pIconDirEntry
;
1271 int wxICOHandler::GetImageCount(wxInputStream
& stream
)
1274 wxFileOffset iPos
= stream
.TellI();
1276 stream
.Read(&IconDir
, sizeof(IconDir
));
1277 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1282 bool wxICOHandler::DoCanRead(wxInputStream
& stream
)
1285 unsigned char hdr
[4];
1286 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
1289 // hdr[2] is one for an icon and two for a cursor
1290 return hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\1' && hdr
[3] == '\0';
1293 #endif // wxUSE_STREAMS
1296 //-----------------------------------------------------------------------------
1298 //-----------------------------------------------------------------------------
1300 IMPLEMENT_DYNAMIC_CLASS(wxCURHandler
, wxICOHandler
)
1304 bool wxCURHandler::DoCanRead(wxInputStream
& stream
)
1307 unsigned char hdr
[4];
1308 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
1311 // hdr[2] is one for an icon and two for a cursor
1312 return hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\2' && hdr
[3] == '\0';
1315 #endif // wxUSE_STREAMS
1317 //-----------------------------------------------------------------------------
1319 //-----------------------------------------------------------------------------
1321 IMPLEMENT_DYNAMIC_CLASS(wxANIHandler
, wxCURHandler
)
1325 bool wxANIHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1326 bool verbose
, int index
)
1332 memcpy( &riff32
, "RIFF", 4 );
1334 memcpy( &list32
, "LIST", 4 );
1336 memcpy( &ico32
, "icon", 4 );
1340 stream
.Read(&FCC1
, 4);
1341 if ( FCC1
!= riff32
)
1344 // we have a riff file:
1345 while (stream
.IsOk())
1347 // we always have a data size
1348 stream
.Read(&datalen
, 4);
1349 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
1350 //data should be padded to make even number of bytes
1351 if (datalen
% 2 == 1) datalen
++ ;
1352 //now either data or a FCC
1353 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
1355 stream
.Read(&FCC2
, 4);
1359 if (FCC1
== ico32
&& iIcon
>= index
)
1361 return DoLoadFile(image
, stream
, verbose
, -1);
1365 stream
.SeekI(stream
.TellI() + datalen
);
1366 if ( FCC1
== ico32
)
1371 // try to read next data chunk:
1372 stream
.Read(&FCC1
, 4);
1377 bool wxANIHandler::DoCanRead(wxInputStream
& stream
)
1383 memcpy( &riff32
, "RIFF", 4 );
1385 memcpy( &list32
, "LIST", 4 );
1387 memcpy( &ico32
, "icon", 4 );
1389 memcpy( &anih32
, "anih", 4 );
1392 if ( !stream
.Read(&FCC1
, 4) )
1395 if ( FCC1
!= riff32
)
1398 // we have a riff file:
1399 while ( stream
.IsOk() )
1401 if ( FCC1
== anih32
)
1403 // we always have a data size:
1404 stream
.Read(&datalen
, 4);
1405 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
1406 //data should be padded to make even number of bytes
1407 if (datalen
% 2 == 1) datalen
++ ;
1408 // now either data or a FCC:
1409 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
1411 stream
.Read(&FCC2
, 4);
1415 stream
.SeekI(stream
.TellI() + datalen
);
1418 // try to read next data chunk:
1419 if ( !stream
.Read(&FCC1
, 4) )
1421 // reading failed -- either EOF or IO error, bail out anyhow
1429 int wxANIHandler::GetImageCount(wxInputStream
& stream
)
1435 memcpy( &riff32
, "RIFF", 4 );
1437 memcpy( &list32
, "LIST", 4 );
1439 memcpy( &ico32
, "icon", 4 );
1441 memcpy( &anih32
, "anih", 4 );
1444 stream
.Read(&FCC1
, 4);
1445 if ( FCC1
!= riff32
)
1448 // we have a riff file:
1449 while ( stream
.IsOk() )
1451 // we always have a data size:
1452 stream
.Read(&datalen
, 4);
1453 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
1454 //data should be padded to make even number of bytes
1455 if (datalen
% 2 == 1) datalen
++ ;
1456 // now either data or a FCC:
1457 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
1459 stream
.Read(&FCC2
, 4);
1463 if ( FCC1
== anih32
)
1465 wxUint32
*pData
= new wxUint32
[datalen
/4];
1466 stream
.Read(pData
, datalen
);
1467 int nIcons
= wxINT32_SWAP_ON_BE(*(pData
+ 1));
1472 stream
.SeekI(stream
.TellI() + datalen
);
1475 // try to read next data chunk:
1476 stream
.Read(&FCC1
, 4);
1482 #endif // wxUSE_STREAMS
1484 #endif // wxUSE_ICO_CUR
1486 #endif // wxUSE_IMAGE