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"
32 #include "wx/filefn.h"
33 #include "wx/wfstream.h"
34 #include "wx/quantize.h"
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
66 #define BI_BITFIELDS 3
69 #define poffset (line * width * 3 + column * 3)
71 bool wxBMPHandler::SaveFile(wxImage
*image
,
72 wxOutputStream
& stream
,
75 return SaveDib(image
, stream
, verbose
, true/*IsBmp*/, false/*IsMask*/);
78 bool wxBMPHandler::SaveDib(wxImage
*image
,
79 wxOutputStream
& stream
,
85 wxCHECK_MSG( image
, false, _T("invalid pointer in wxBMPHandler::SaveFile") );
90 wxLogError(_("BMP: Couldn't save invalid image."));
94 // get the format of the BMP file to save, else use 24bpp
95 unsigned format
= wxBMP_24BPP
;
96 if ( image
->HasOption(wxIMAGE_OPTION_BMP_FORMAT
) )
97 format
= image
->GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT
);
99 wxUint16 bpp
; // # of bits per pixel
100 int palette_size
; // # of color map entries, ie. 2^bpp colors
102 // set the bpp and appropriate palette_size, and do additional checks
103 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_1BPP_BW
) )
108 else if ( format
== wxBMP_4BPP
)
113 else if ( (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_GREY
) ||
114 (format
== wxBMP_8BPP_RED
) || (format
== wxBMP_8BPP_PALETTE
) )
116 // need to set a wxPalette to use this, HOW TO CHECK IF VALID, SIZE?
117 if ((format
== wxBMP_8BPP_PALETTE
)
119 && !image
->HasPalette()
120 #endif // wxUSE_PALETTE
124 wxLogError(_("BMP: wxImage doesn't have own wxPalette."));
130 else // you get 24bpp
132 format
= wxBMP_24BPP
;
137 unsigned width
= image
->GetWidth();
138 unsigned row_padding
= (4 - int(width
*bpp
/8.0) % 4) % 4; // # bytes to pad to dword
139 unsigned row_width
= int(width
* bpp
/8.0) + row_padding
; // # of bytes per row
144 wxUint16 magic
; // format magic, always 'BM'
145 wxUint32 filesize
; // total file size, inc. headers
146 wxUint32 reserved
; // for future use
147 wxUint32 data_offset
; // image data offset in the file
150 wxUint32 bih_size
; // 2nd part's size
151 wxUint32 width
, height
; // bitmap's dimensions
152 wxUint16 planes
; // num of planes
153 wxUint16 bpp
; // bits per pixel
154 wxUint32 compression
; // compression method
155 wxUint32 size_of_bmp
; // size of the bitmap
156 wxUint32 h_res
, v_res
; // image resolution in dpi
157 wxUint32 num_clrs
; // number of colors used
158 wxUint32 num_signif_clrs
;// number of significant colors
161 wxUint32 hdr_size
= 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/;
163 hdr
.magic
= wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/);
164 hdr
.filesize
= wxUINT32_SWAP_ON_BE( hdr_size
+ palette_size
*4 +
165 row_width
* image
->GetHeight() );
167 hdr
.data_offset
= wxUINT32_SWAP_ON_BE(hdr_size
+ palette_size
*4);
169 hdr
.bih_size
= wxUINT32_SWAP_ON_BE(hdr_size
- 14);
170 hdr
.width
= wxUINT32_SWAP_ON_BE(image
->GetWidth());
173 hdr
.height
= wxUINT32_SWAP_ON_BE(image
->GetHeight());
177 hdr
.height
= wxUINT32_SWAP_ON_BE(2 * image
->GetHeight());
179 hdr
.planes
= wxUINT16_SWAP_ON_BE(1); // always 1 plane
180 hdr
.bpp
= wxUINT16_SWAP_ON_BE(bpp
);
181 hdr
.compression
= 0; // RGB uncompressed
182 hdr
.size_of_bmp
= wxUINT32_SWAP_ON_BE(row_width
* image
->GetHeight());
183 hdr
.h_res
= hdr
.v_res
= wxUINT32_SWAP_ON_BE(72); // 72dpi is standard
184 hdr
.num_clrs
= wxUINT32_SWAP_ON_BE(palette_size
); // # colors in colormap
185 hdr
.num_signif_clrs
= 0; // all colors are significant
189 if (// VS: looks ugly but compilers tend to do ugly things with structs,
190 // like aligning hdr.filesize's ofset to dword :(
191 // VZ: we should add padding then...
192 !stream
.Write(&hdr
.magic
, 2) ||
193 !stream
.Write(&hdr
.filesize
, 4) ||
194 !stream
.Write(&hdr
.reserved
, 4) ||
195 !stream
.Write(&hdr
.data_offset
, 4)
199 wxLogError(_("BMP: Couldn't write the file (Bitmap) header."));
206 !stream
.Write(&hdr
.bih_size
, 4) ||
207 !stream
.Write(&hdr
.width
, 4) ||
208 !stream
.Write(&hdr
.height
, 4) ||
209 !stream
.Write(&hdr
.planes
, 2) ||
210 !stream
.Write(&hdr
.bpp
, 2) ||
211 !stream
.Write(&hdr
.compression
, 4) ||
212 !stream
.Write(&hdr
.size_of_bmp
, 4) ||
213 !stream
.Write(&hdr
.h_res
, 4) ||
214 !stream
.Write(&hdr
.v_res
, 4) ||
215 !stream
.Write(&hdr
.num_clrs
, 4) ||
216 !stream
.Write(&hdr
.num_signif_clrs
, 4)
220 wxLogError(_("BMP: Couldn't write the file (BitmapInfo) header."));
225 wxPalette
*palette
= NULL
; // entries for quantized images
226 wxUint8
*rgbquad
= NULL
; // for the RGBQUAD bytes for the colormap
227 wxImage
*q_image
= NULL
; // destination for quantized image
229 // if <24bpp use quantization to reduce colors for *some* of the formats
230 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_4BPP
) ||
231 (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_PALETTE
) )
233 // make a new palette and quantize the image
234 if (format
!= wxBMP_8BPP_PALETTE
)
236 q_image
= new wxImage();
238 // I get a delete error using Quantize when desired colors > 236
239 int quantize
= ((palette_size
> 236) ? 236 : palette_size
);
240 // fill the destination too, it gives much nicer 4bpp images
241 wxQuantize::Quantize( *image
, *q_image
, &palette
, quantize
, 0,
242 wxQUANTIZE_FILL_DESTINATION_IMAGE
);
247 palette
= new wxPalette(image
->GetPalette());
248 #endif // wxUSE_PALETTE
252 unsigned char r
, g
, b
;
253 rgbquad
= new wxUint8
[palette_size
*4];
255 for (i
= 0; i
< palette_size
; i
++)
258 if ( !palette
->GetRGB(i
, &r
, &g
, &b
) )
259 #endif // wxUSE_PALETTE
268 // make a 256 entry greyscale colormap or 2 entry black & white
269 else if ( (format
== wxBMP_8BPP_GREY
) || (format
== wxBMP_8BPP_RED
) ||
270 (format
== wxBMP_1BPP_BW
) )
272 rgbquad
= new wxUint8
[palette_size
*4];
274 for ( int i
= 0; i
< palette_size
; i
++ )
276 // if 1BPP_BW then the value should be either 0 or 255
277 wxUint8 c
= (wxUint8
)((i
> 0) && (format
== wxBMP_1BPP_BW
) ? 255 : i
);
286 // if the colormap was made, then it needs to be written
291 if ( !stream
.Write(rgbquad
, palette_size
*4) )
294 wxLogError(_("BMP: Couldn't write RGB color map."));
298 #endif // wxUSE_PALETTE
306 // pointer to the image data, use quantized if available
307 wxUint8
*data
= (wxUint8
*) image
->GetData();
308 if (q_image
) if (q_image
->Ok()) data
= (wxUint8
*) q_image
->GetData();
310 wxUint8
*buffer
= new wxUint8
[row_width
];
311 memset(buffer
, 0, row_width
);
315 for (y
= image
->GetHeight() -1; y
>= 0; y
--)
317 if ( format
== wxBMP_24BPP
) // 3 bytes per pixel red,green,blue
319 for ( x
= 0; x
< width
; x
++ )
321 pixel
= 3*(y
*width
+ x
);
323 buffer
[3*x
] = data
[pixel
+2];
324 buffer
[3*x
+ 1] = data
[pixel
+1];
325 buffer
[3*x
+ 2] = data
[pixel
];
328 else if ((format
== wxBMP_8BPP
) || // 1 byte per pixel in color
329 (format
== wxBMP_8BPP_PALETTE
))
331 for (x
= 0; x
< width
; x
++)
333 pixel
= 3*(y
*width
+ x
);
335 buffer
[x
] = (wxUint8
)palette
->GetPixel( data
[pixel
],
339 // FIXME: what should this be? use some std palette maybe?
341 #endif // wxUSE_PALETTE
344 else if ( format
== wxBMP_8BPP_GREY
) // 1 byte per pix, rgb ave to grey
346 for (x
= 0; x
< width
; x
++)
348 pixel
= 3*(y
*width
+ x
);
349 buffer
[x
] = (wxUint8
)(.299*data
[pixel
] +
354 else if ( format
== wxBMP_8BPP_RED
) // 1 byte per pixel, red as greys
356 for (x
= 0; x
< width
; x
++)
358 buffer
[x
] = (wxUint8
)data
[3*(y
*width
+ x
)];
361 else if ( format
== wxBMP_4BPP
) // 4 bpp in color
363 for (x
= 0; x
< width
; x
+=2)
365 pixel
= 3*(y
*width
+ x
);
367 // fill buffer, ignore if > width
369 buffer
[x
/2] = (wxUint8
)(
370 ((wxUint8
)palette
->GetPixel(data
[pixel
],
372 data
[pixel
+2]) << 4) |
375 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3],
379 // FIXME: what should this be? use some std palette maybe?
381 #endif // wxUSE_PALETTE
384 else if ( format
== wxBMP_1BPP
) // 1 bpp in "color"
386 for (x
= 0; x
< width
; x
+=8)
388 pixel
= 3*(y
*width
+ x
);
391 buffer
[x
/8] = (wxUint8
)(
392 ((wxUint8
)palette
->GetPixel(data
[pixel
], data
[pixel
+1], data
[pixel
+2]) << 7) |
393 (((x
+1) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3], data
[pixel
+4], data
[pixel
+5]) << 6)) |
394 (((x
+2) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+6], data
[pixel
+7], data
[pixel
+8]) << 5)) |
395 (((x
+3) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+9], data
[pixel
+10], data
[pixel
+11]) << 4)) |
396 (((x
+4) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+12], data
[pixel
+13], data
[pixel
+14]) << 3)) |
397 (((x
+5) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+15], data
[pixel
+16], data
[pixel
+17]) << 2)) |
398 (((x
+6) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+18], data
[pixel
+19], data
[pixel
+20]) << 1)) |
399 (((x
+7) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+21], data
[pixel
+22], data
[pixel
+23]) )) );
401 // FIXME: what should this be? use some std palette maybe?
403 #endif // wxUSE_PALETTE
406 else if ( format
== wxBMP_1BPP_BW
) // 1 bpp B&W colormap from red color ONLY
408 for (x
= 0; x
< width
; x
+=8)
410 pixel
= 3*(y
*width
+ x
);
412 buffer
[x
/8] = (wxUint8
)(
413 (((wxUint8
)(data
[pixel
] /128.)) << 7) |
414 (((x
+1) > width
) ? 0 : (((wxUint8
)(data
[pixel
+3] /128.)) << 6)) |
415 (((x
+2) > width
) ? 0 : (((wxUint8
)(data
[pixel
+6] /128.)) << 5)) |
416 (((x
+3) > width
) ? 0 : (((wxUint8
)(data
[pixel
+9] /128.)) << 4)) |
417 (((x
+4) > width
) ? 0 : (((wxUint8
)(data
[pixel
+12]/128.)) << 3)) |
418 (((x
+5) > width
) ? 0 : (((wxUint8
)(data
[pixel
+15]/128.)) << 2)) |
419 (((x
+6) > width
) ? 0 : (((wxUint8
)(data
[pixel
+18]/128.)) << 1)) |
420 (((x
+7) > width
) ? 0 : (((wxUint8
)(data
[pixel
+21]/128.)) )) );
424 if ( !stream
.Write(buffer
, row_width
) )
427 wxLogError(_("BMP: Couldn't write data."));
431 #endif // wxUSE_PALETTE
439 #endif // wxUSE_PALETTE
448 unsigned char r
, g
, b
;
451 bool wxBMPHandler::DoLoadDib(wxImage
* image
, int width
, int height
,
452 int bpp
, int ncolors
, int comp
,
453 wxFileOffset bmpOffset
, wxInputStream
& stream
,
454 bool verbose
, bool IsBmp
, bool hasPalette
)
456 wxInt32 aDword
, rmask
= 0, gmask
= 0, bmask
= 0;
457 int rshift
= 0, gshift
= 0, bshift
= 0;
458 int rbits
= 0, gbits
= 0, bbits
= 0;
464 // allocate space for palette if needed:
469 cmap
= new _cmap
[ncolors
];
473 wxLogError(_("BMP: Couldn't allocate memory."));
480 // destroy existing here instead of:
482 image
->Create(width
, height
);
484 unsigned char *ptr
= image
->GetData();
489 wxLogError( _("BMP: Couldn't allocate memory.") );
495 // Reading the palette, if it exists:
496 if ( bpp
< 16 && ncolors
!= 0 )
498 unsigned char* r
= new unsigned char[ncolors
];
499 unsigned char* g
= new unsigned char[ncolors
];
500 unsigned char* b
= new unsigned char[ncolors
];
501 for (int j
= 0; j
< ncolors
; j
++)
505 stream
.Read(bbuf
, 4);
516 //used in reading .ico file mask
519 b
[j
] = cmap
[j
].b
= ( j
? 255 : 0 );
524 // Set the palette for the wxImage
525 image
->SetPalette(wxPalette(ncolors
, r
, g
, b
));
526 #endif // wxUSE_PALETTE
532 else if ( bpp
== 16 || bpp
== 32 )
534 if ( comp
== BI_BITFIELDS
)
537 stream
.Read(dbuf
, 4 * 3);
538 rmask
= wxINT32_SWAP_ON_BE(dbuf
[0]);
539 gmask
= wxINT32_SWAP_ON_BE(dbuf
[1]);
540 bmask
= wxINT32_SWAP_ON_BE(dbuf
[2]);
541 // find shift amount (Least significant bit of mask)
542 for (bit
= bpp
-1; bit
>=0; bit
--)
544 if (bmask
& (1 << bit
))
546 if (gmask
& (1 << bit
))
548 if (rmask
& (1 << bit
))
551 // Find number of bits in mask (MSB-LSB+1)
552 for (bit
= 0; bit
< bpp
; bit
++)
554 if (bmask
& (1 << bit
))
555 bbits
= bit
-bshift
+1;
556 if (gmask
& (1 << bit
))
557 gbits
= bit
-gshift
+1;
558 if (rmask
& (1 << bit
))
559 rbits
= bit
-rshift
+1;
562 else if ( bpp
== 16 )
574 else if ( bpp
== 32 )
589 * Reading the image data
592 stream
.SeekI(bmpOffset
); // else icon, just carry on
594 unsigned char *data
= ptr
;
596 /* set the whole image to the background color */
597 if ( bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
) )
599 for (int i
= 0; i
< width
* height
; i
++)
608 int linesize
= ((width
* bpp
+ 31) / 32) * 4;
610 /* BMPs are stored upside down */
611 for ( int line
= (height
- 1); line
>= 0; line
-- )
614 for ( int column
= 0; column
< width
; )
619 aByte
= stream
.GetC();
622 for (int bit
= 0; bit
< 8 && column
< width
; bit
++)
624 int index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
625 ptr
[poffset
] = cmap
[index
].r
;
626 ptr
[poffset
+ 1] = cmap
[index
].g
;
627 ptr
[poffset
+ 2] = cmap
[index
].b
;
633 if ( comp
== BI_RLE4
)
637 aByte
= stream
.GetC();
645 else if ( aByte
== 1 )
650 else if ( aByte
== 2 )
652 aByte
= stream
.GetC();
654 linepos
= column
* bpp
/ 4;
655 aByte
= stream
.GetC();
656 line
-= aByte
; // upside down
660 int absolute
= aByte
;
663 for (int k
= 0; k
< absolute
; k
++)
668 aByte
= stream
.GetC();
669 nibble
[0] = (wxUint8
)( (aByte
& 0xF0) >> 4 ) ;
670 nibble
[1] = (wxUint8
)( aByte
& 0x0F ) ;
672 ptr
[poffset
] = cmap
[nibble
[k%2
]].r
;
673 ptr
[poffset
+ 1] = cmap
[nibble
[k%2
]].g
;
674 ptr
[poffset
+ 2] = cmap
[nibble
[k%2
]].b
;
679 if ( readBytes
& 0x01 )
680 aByte
= stream
.GetC();
686 nibble
[0] = (wxUint8
)( (aByte
& 0xF0) >> 4 ) ;
687 nibble
[1] = (wxUint8
)( aByte
& 0x0F ) ;
689 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
691 ptr
[poffset
] = cmap
[nibble
[l%2
]].r
;
692 ptr
[poffset
+ 1] = cmap
[nibble
[l%2
]].g
;
693 ptr
[poffset
+ 2] = cmap
[nibble
[l%2
]].b
;
702 for (int nibble
= 0; nibble
< 2 && column
< width
; nibble
++)
704 int index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
707 ptr
[poffset
] = cmap
[index
].r
;
708 ptr
[poffset
+ 1] = cmap
[index
].g
;
709 ptr
[poffset
+ 2] = cmap
[index
].b
;
716 if ( comp
== BI_RLE8
)
720 aByte
= stream
.GetC();
725 /* column = width; */
727 else if ( aByte
== 1 )
732 else if ( aByte
== 2 )
734 aByte
= stream
.GetC();
736 linepos
= column
* bpp
/ 8;
737 aByte
= stream
.GetC();
742 int absolute
= aByte
;
743 for (int k
= 0; k
< absolute
; k
++)
746 aByte
= stream
.GetC();
747 ptr
[poffset
] = cmap
[aByte
].r
;
748 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
749 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
752 if ( absolute
& 0x01 )
753 aByte
= stream
.GetC();
758 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
760 ptr
[poffset
] = cmap
[aByte
].r
;
761 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
762 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
770 ptr
[poffset
] = cmap
[aByte
].r
;
771 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
772 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
774 // linepos += size; seems to be wrong, RR
778 else if ( bpp
== 24 )
780 stream
.Read(bbuf
, 3);
782 ptr
[poffset
] = (unsigned char)bbuf
[2];
783 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
784 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
787 else if ( bpp
== 16 )
790 stream
.Read(&aWord
, 2);
791 aWord
= wxUINT16_SWAP_ON_BE(aWord
);
793 /* use the masks and calculated amonut of shift
794 to retrieve the color data out of the word. Then
795 shift it left by (8 - number of bits) such that
796 the image has the proper dynamic range */
797 temp
= (unsigned char)((aWord
& rmask
) >> rshift
<< (8-rbits
));
799 temp
= (unsigned char)((aWord
& gmask
) >> gshift
<< (8-gbits
));
800 ptr
[poffset
+ 1] = temp
;
801 temp
= (unsigned char)((aWord
& bmask
) >> bshift
<< (8-bbits
));
802 ptr
[poffset
+ 2] = temp
;
808 stream
.Read(&aDword
, 4);
809 aDword
= wxINT32_SWAP_ON_BE(aDword
);
811 temp
= (unsigned char)((aDword
& rmask
) >> rshift
);
813 temp
= (unsigned char)((aDword
& gmask
) >> gshift
);
814 ptr
[poffset
+ 1] = temp
;
815 temp
= (unsigned char)((aDword
& bmask
) >> bshift
);
816 ptr
[poffset
+ 2] = temp
;
820 while ( (linepos
< linesize
) && (comp
!= 1) && (comp
!= 2) )
822 stream
.Read(&aByte
, 1);
831 image
->SetMask(false);
833 const wxStreamError err
= stream
.GetLastError();
834 return err
== wxSTREAM_NO_ERROR
|| err
== wxSTREAM_EOF
;
837 bool wxBMPHandler::LoadDib(wxImage
*image
, wxInputStream
& stream
,
838 bool verbose
, bool IsBmp
)
844 wxFileOffset offset
= 0; // keep gcc quiet
847 // read the header off the .BMP format file
849 offset
= stream
.TellI();
850 if (offset
== wxInvalidOffset
)
853 stream
.Read(bbuf
, 2);
854 stream
.Read(dbuf
, 16);
858 stream
.Read(dbuf
, 4);
861 wxInt32 size
= wxINT32_SWAP_ON_BE(dbuf
[0]);
863 offset
= offset
+ wxINT32_SWAP_ON_BE(dbuf
[2]);
865 stream
.Read(dbuf
, 4 * 2);
866 int width
= wxINT32_SWAP_ON_BE((int)dbuf
[0]);
867 int height
= wxINT32_SWAP_ON_BE((int)dbuf
[1]);
868 if ( !IsBmp
)height
= height
/ 2; // for icons divide by 2
873 wxLogError( _("DIB Header: Image width > 32767 pixels for file.") );
876 if ( height
> 32767 )
879 wxLogError( _("DIB Header: Image height > 32767 pixels for file.") );
883 stream
.Read(&aWord
, 2);
886 int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
888 stream
.Read(&aWord
, 2);
889 int bpp
= wxUINT16_SWAP_ON_BE((int)aWord
);
890 if ( bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
!= 16 && bpp
!= 24 && bpp
!= 32 )
893 wxLogError( _("DIB Header: Unknown bitdepth in file.") );
897 stream
.Read(dbuf
, 4 * 4);
898 int comp
= wxINT32_SWAP_ON_BE((int)dbuf
[0]);
899 if ( comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&&
900 comp
!= BI_BITFIELDS
)
903 wxLogError( _("DIB Header: Unknown encoding in file.") );
907 stream
.Read(dbuf
, 4 * 2);
908 int ncolors
= wxINT32_SWAP_ON_BE( (int)dbuf
[0] );
911 /* some more sanity checks */
912 if (((comp
== BI_RLE4
) && (bpp
!= 4)) ||
913 ((comp
== BI_RLE8
) && (bpp
!= 8)) ||
914 ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
917 wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") );
921 //read DIB; this is the BMP image or the XOR part of an icon image
922 if ( !DoLoadDib(image
, width
, height
, bpp
, ncolors
, comp
, offset
, stream
,
923 verbose
, IsBmp
, true) )
926 wxLogError( _("Error in reading image DIB.") );
932 //read Icon mask which is monochrome
933 //there is no palette, so we will create one
935 if ( !DoLoadDib(&mask
, width
, height
, 1, 2, BI_RGB
, offset
, stream
,
936 verbose
, IsBmp
, false) )
939 wxLogError( _("ICO: Error in reading mask DIB.") );
942 image
->SetMaskFromImage(mask
, 255, 255, 255);
949 bool wxBMPHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
950 bool verbose
, int WXUNUSED(index
))
952 // Read a single DIB fom the file:
953 return LoadDib(image
, stream
, verbose
, true/*isBmp*/);
956 bool wxBMPHandler::DoCanRead(wxInputStream
& stream
)
958 unsigned char hdr
[2];
960 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
963 // do we have the BMP file signature?
964 return hdr
[0] == 'B' && hdr
[1] == 'M';
967 #endif // wxUSE_STREAMS
971 //-----------------------------------------------------------------------------
973 //-----------------------------------------------------------------------------
975 IMPLEMENT_DYNAMIC_CLASS(wxICOHandler
, wxBMPHandler
)
981 wxUint8 bWidth
; // Width of the image
982 wxUint8 bHeight
; // Height of the image (times 2)
983 wxUint8 bColorCount
; // Number of colors in image (0 if >=8bpp)
984 wxUint8 bReserved
; // Reserved
986 // these two are different in icons and cursors:
988 wxUint16 wPlanes
; // Color Planes or XHotSpot
989 wxUint16 wBitCount
; // Bits per pixel or YHotSpot
991 wxUint32 dwBytesInRes
; // how many bytes in this resource?
992 wxUint32 dwImageOffset
; // where in the file is this image
997 wxUint16 idReserved
; // Reserved
998 wxUint16 idType
; // resource type (1 for icons, 2 for cursors)
999 wxUint16 idCount
; // how many images?
1003 bool wxICOHandler::SaveFile(wxImage
*image
,
1004 wxOutputStream
& stream
,
1008 //sanity check; icon must be less than 127 pixels high and 255 wide
1009 if ( image
->GetHeight () > 127 )
1012 wxLogError(_("ICO: Image too tall for an icon."));
1015 if ( image
->GetWidth () > 255 )
1018 wxLogError(_("ICO: Image too wide for an icon."));
1022 const int images
= 1; // only generate one image
1024 // VS: This is a hack of sort - since ICO and CUR files are almost
1025 // identical, we have all the meat in wxICOHandler and check for
1026 // the actual (handler) type when the code has to distinguish between
1028 int type
= (this->GetType() == wxBITMAP_TYPE_CUR
) ? 2 : 1;
1030 // write a header, (ICONDIR)
1031 // Calculate the header size
1032 wxUint32 offset
= 3 * sizeof(wxUint16
);
1035 IconDir
.idReserved
= 0;
1036 IconDir
.idType
= wxUINT16_SWAP_ON_BE((wxUint16
)type
);
1037 IconDir
.idCount
= wxUINT16_SWAP_ON_BE((wxUint16
)images
);
1038 stream
.Write(&IconDir
.idReserved
, sizeof(IconDir
.idReserved
));
1039 stream
.Write(&IconDir
.idType
, sizeof(IconDir
.idType
));
1040 stream
.Write(&IconDir
.idCount
, sizeof(IconDir
.idCount
));
1041 if ( !stream
.IsOk() )
1044 wxLogError(_("ICO: Error writing the image file!"));
1048 // for each iamage write a description ICONDIRENTRY:
1049 ICONDIRENTRY icondirentry
;
1050 for (int img
= 0; img
< images
; img
++)
1054 if ( image
->HasMask() )
1056 // make another image with black/white:
1057 mask
= image
->ConvertToMono (image
->GetMaskRed(), image
->GetMaskGreen(), image
->GetMaskBlue() );
1059 // now we need to change the masked regions to black:
1060 unsigned char r
= image
->GetMaskRed();
1061 unsigned char g
= image
->GetMaskGreen();
1062 unsigned char b
= image
->GetMaskBlue();
1063 if ( (r
!= 0) || (g
!= 0) || (b
!= 0) )
1065 // Go round and apply black to the masked bits:
1067 for (i
= 0; i
< mask
.GetWidth(); i
++)
1069 for (j
= 0; j
< mask
.GetHeight(); j
++)
1071 if ((r
== mask
.GetRed(i
, j
)) &&
1072 (g
== mask
.GetGreen(i
, j
))&&
1073 (b
== mask
.GetBlue(i
, j
)) )
1074 image
->SetRGB(i
, j
, 0, 0, 0 );
1081 // just make a black mask all over:
1082 mask
= image
->Copy();
1084 for (i
= 0; i
< mask
.GetWidth(); i
++)
1085 for (j
= 0; j
< mask
.GetHeight(); j
++)
1086 mask
.SetRGB(i
, j
, 0, 0, 0 );
1088 // Set the formats for image and mask
1089 // (Windows never saves with more than 8 colors):
1090 image
->SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_8BPP
);
1092 // monochome bitmap:
1093 mask
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_1BPP_BW
);
1095 bool IsMask
= false;
1097 //calculate size and offset of image and mask
1098 wxCountingOutputStream cStream
;
1099 bool bResult
= SaveDib(image
, cStream
, verbose
, IsBmp
, IsMask
);
1103 wxLogError(_("ICO: Error writing the image file!"));
1108 bResult
= SaveDib(&mask
, cStream
, verbose
, IsBmp
, IsMask
);
1112 wxLogError(_("ICO: Error writing the image file!"));
1115 wxUint32 Size
= cStream
.GetSize();
1117 // wxCountingOutputStream::IsOk() always returns true for now and this
1118 // "if" provokes VC++ warnings in optimized build
1120 if ( !cStream
.Ok() )
1123 wxLogError(_("ICO: Error writing the image file!"));
1128 offset
= offset
+ sizeof(ICONDIRENTRY
);
1130 icondirentry
.bWidth
= (wxUint8
)image
->GetWidth();
1131 icondirentry
.bHeight
= (wxUint8
)(2 * image
->GetHeight());
1132 icondirentry
.bColorCount
= 0;
1133 icondirentry
.bReserved
= 0;
1134 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE(1);
1135 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE(wxBMP_8BPP
);
1136 if ( type
== 2 /*CUR*/)
1138 int hx
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ?
1139 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) :
1140 image
->GetWidth() / 2;
1141 int hy
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ?
1142 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) :
1143 image
->GetHeight() / 2;
1145 // actually write the values of the hot spot here:
1146 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE((wxUint16
)hx
);
1147 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE((wxUint16
)hy
);
1149 icondirentry
.dwBytesInRes
= wxUINT32_SWAP_ON_BE(Size
);
1150 icondirentry
.dwImageOffset
= wxUINT32_SWAP_ON_BE(offset
);
1152 // increase size to allow for the data written:
1156 stream
.Write(&icondirentry
.bWidth
, sizeof(icondirentry
.bWidth
));
1157 stream
.Write(&icondirentry
.bHeight
, sizeof(icondirentry
.bHeight
));
1158 stream
.Write(&icondirentry
.bColorCount
, sizeof(icondirentry
.bColorCount
));
1159 stream
.Write(&icondirentry
.bReserved
, sizeof(icondirentry
.bReserved
));
1160 stream
.Write(&icondirentry
.wPlanes
, sizeof(icondirentry
.wPlanes
));
1161 stream
.Write(&icondirentry
.wBitCount
, sizeof(icondirentry
.wBitCount
));
1162 stream
.Write(&icondirentry
.dwBytesInRes
, sizeof(icondirentry
.dwBytesInRes
));
1163 stream
.Write(&icondirentry
.dwImageOffset
, sizeof(icondirentry
.dwImageOffset
));
1164 if ( !stream
.IsOk() )
1167 wxLogError(_("ICO: Error writing the image file!"));
1171 // actually save it:
1173 bResult
= SaveDib(image
, stream
, verbose
, IsBmp
, IsMask
);
1177 wxLogError(_("ICO: Error writing the image file!"));
1182 bResult
= SaveDib(&mask
, stream
, verbose
, IsBmp
, IsMask
);
1186 wxLogError(_("ICO: Error writing the image file!"));
1190 } // end of for loop
1195 bool wxICOHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1196 bool verbose
, int index
)
1199 return DoLoadFile(image
, stream
, verbose
, index
);
1202 bool wxICOHandler::DoLoadFile(wxImage
*image
, wxInputStream
& stream
,
1203 bool WXUNUSED(verbose
), int index
)
1205 bool bResult
wxDUMMY_INITIALIZE(false);
1210 wxFileOffset iPos
= stream
.TellI();
1211 stream
.Read(&IconDir
, sizeof(IconDir
));
1212 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1213 // nType is 1 for Icons, 2 for Cursors:
1214 wxUint16 nType
= wxUINT16_SWAP_ON_BE(IconDir
.idType
);
1216 // loop round the icons and choose the best one:
1217 ICONDIRENTRY
*pIconDirEntry
= new ICONDIRENTRY
[nIcons
];
1218 ICONDIRENTRY
*pCurrentEntry
= pIconDirEntry
;
1221 int iSel
= wxNOT_FOUND
;
1223 for (int i
= 0; i
< nIcons
; i
++ )
1225 stream
.Read(pCurrentEntry
, sizeof(ICONDIRENTRY
));
1226 // bHeight and bColorCount are wxUint8
1227 if ( pCurrentEntry
->bWidth
>= wMax
)
1229 // see if we have more colors, ==0 indicates > 8bpp:
1230 if ( pCurrentEntry
->bColorCount
== 0 )
1231 pCurrentEntry
->bColorCount
= 255;
1232 if ( pCurrentEntry
->bColorCount
>= colmax
)
1235 wMax
= pCurrentEntry
->bWidth
;
1236 colmax
= pCurrentEntry
->bColorCount
;
1244 // VS: Note that we *have* to run the loop above even if index != -1, because
1245 // it reads ICONDIRENTRies.
1249 if ( iSel
== wxNOT_FOUND
|| iSel
< 0 || iSel
>= nIcons
)
1251 wxLogError(_("ICO: Invalid icon index."));
1256 // seek to selected icon:
1257 pCurrentEntry
= pIconDirEntry
+ iSel
;
1258 stream
.SeekI(iPos
+ wxUINT32_SWAP_ON_BE(pCurrentEntry
->dwImageOffset
), wxFromStart
);
1259 bResult
= LoadDib(image
, stream
, true, IsBmp
);
1260 bool bIsCursorType
= (this->GetType() == wxBITMAP_TYPE_CUR
) || (this->GetType() == wxBITMAP_TYPE_ANI
);
1261 if ( bResult
&& bIsCursorType
&& nType
== 2 )
1263 // it is a cursor, so let's set the hotspot:
1264 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wPlanes
));
1265 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wBitCount
));
1268 delete[] pIconDirEntry
;
1272 int wxICOHandler::GetImageCount(wxInputStream
& stream
)
1275 wxFileOffset iPos
= stream
.TellI();
1277 stream
.Read(&IconDir
, sizeof(IconDir
));
1278 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1283 bool wxICOHandler::DoCanRead(wxInputStream
& stream
)
1286 unsigned char hdr
[4];
1287 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
1290 // hdr[2] is one for an icon and two for a cursor
1291 return hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\1' && hdr
[3] == '\0';
1294 #endif // wxUSE_STREAMS
1297 //-----------------------------------------------------------------------------
1299 //-----------------------------------------------------------------------------
1301 IMPLEMENT_DYNAMIC_CLASS(wxCURHandler
, wxICOHandler
)
1305 bool wxCURHandler::DoCanRead(wxInputStream
& stream
)
1308 unsigned char hdr
[4];
1309 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
1312 // hdr[2] is one for an icon and two for a cursor
1313 return hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\2' && hdr
[3] == '\0';
1316 #endif // wxUSE_STREAMS
1318 //-----------------------------------------------------------------------------
1320 //-----------------------------------------------------------------------------
1322 IMPLEMENT_DYNAMIC_CLASS(wxANIHandler
, wxCURHandler
)
1326 bool wxANIHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1327 bool verbose
, int index
)
1333 memcpy( &riff32
, "RIFF", 4 );
1335 memcpy( &list32
, "LIST", 4 );
1337 memcpy( &ico32
, "icon", 4 );
1341 stream
.Read(&FCC1
, 4);
1342 if ( FCC1
!= riff32
)
1345 // we have a riff file:
1346 while (stream
.IsOk())
1348 // we always have a data size
1349 stream
.Read(&datalen
, 4);
1350 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
1351 //data should be padded to make even number of bytes
1352 if (datalen
% 2 == 1) datalen
++ ;
1353 //now either data or a FCC
1354 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
1356 stream
.Read(&FCC2
, 4);
1360 if (FCC1
== ico32
&& iIcon
>= index
)
1362 return DoLoadFile(image
, stream
, verbose
, -1);
1366 stream
.SeekI(stream
.TellI() + datalen
);
1367 if ( FCC1
== ico32
)
1372 // try to read next data chunk:
1373 stream
.Read(&FCC1
, 4);
1378 bool wxANIHandler::DoCanRead(wxInputStream
& stream
)
1384 memcpy( &riff32
, "RIFF", 4 );
1386 memcpy( &list32
, "LIST", 4 );
1388 memcpy( &ico32
, "icon", 4 );
1390 memcpy( &anih32
, "anih", 4 );
1393 if ( !stream
.Read(&FCC1
, 4) )
1396 if ( FCC1
!= riff32
)
1399 // we have a riff file:
1400 while ( stream
.IsOk() )
1402 if ( FCC1
== anih32
)
1404 // we always have a data size:
1405 stream
.Read(&datalen
, 4);
1406 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
1407 //data should be padded to make even number of bytes
1408 if (datalen
% 2 == 1) datalen
++ ;
1409 // now either data or a FCC:
1410 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
1412 stream
.Read(&FCC2
, 4);
1416 stream
.SeekI(stream
.TellI() + datalen
);
1419 // try to read next data chunk:
1420 if ( !stream
.Read(&FCC1
, 4) )
1422 // reading failed -- either EOF or IO error, bail out anyhow
1430 int wxANIHandler::GetImageCount(wxInputStream
& stream
)
1436 memcpy( &riff32
, "RIFF", 4 );
1438 memcpy( &list32
, "LIST", 4 );
1440 memcpy( &ico32
, "icon", 4 );
1442 memcpy( &anih32
, "anih", 4 );
1445 stream
.Read(&FCC1
, 4);
1446 if ( FCC1
!= riff32
)
1449 // we have a riff file:
1450 while ( stream
.IsOk() )
1452 // we always have a data size:
1453 stream
.Read(&datalen
, 4);
1454 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
1455 //data should be padded to make even number of bytes
1456 if (datalen
% 2 == 1) datalen
++ ;
1457 // now either data or a FCC:
1458 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
1460 stream
.Read(&FCC2
, 4);
1464 if ( FCC1
== anih32
)
1466 wxUint32
*pData
= new wxUint32
[datalen
/4];
1467 stream
.Read(pData
, datalen
);
1468 int nIcons
= wxINT32_SWAP_ON_BE(*(pData
+ 1));
1473 stream
.SeekI(stream
.TellI() + datalen
);
1476 // try to read next data chunk:
1477 stream
.Read(&FCC1
, 4);
1483 #endif // wxUSE_STREAMS
1485 #endif // wxUSE_ICO_CUR
1487 #endif // wxUSE_IMAGE