1 /////////////////////////////////////////////////////////////////////////////
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "imagbmp.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
25 #include "wx/imagbmp.h"
26 #include "wx/bitmap.h"
30 #include "wx/filefn.h"
31 #include "wx/wfstream.h"
33 #include "wx/module.h"
34 #include "wx/quantize.h"
46 #include "wx/msw/wrapwin.h"
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
70 #define BI_BITFIELDS 3
73 #define poffset (line * width * 3 + column * 3)
75 bool wxBMPHandler::SaveFile(wxImage
*image
,
76 wxOutputStream
& stream
,
79 return SaveDib(image
, stream
, verbose
, true/*IsBmp*/, false/*IsMask*/);
82 bool wxBMPHandler::SaveDib(wxImage
*image
,
83 wxOutputStream
& stream
,
89 wxCHECK_MSG( image
, false, _T("invalid pointer in wxBMPHandler::SaveFile") );
94 wxLogError(_("BMP: Couldn't save invalid image."));
98 // get the format of the BMP file to save, else use 24bpp
99 unsigned format
= wxBMP_24BPP
;
100 if ( image
->HasOption(wxIMAGE_OPTION_BMP_FORMAT
) )
101 format
= image
->GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT
);
103 wxUint16 bpp
; // # of bits per pixel
104 int palette_size
; // # of color map entries, ie. 2^bpp colors
106 // set the bpp and appropriate palette_size, and do additional checks
107 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_1BPP_BW
) )
112 else if ( format
== wxBMP_4BPP
)
117 else if ( (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_GREY
) ||
118 (format
== wxBMP_8BPP_RED
) || (format
== wxBMP_8BPP_PALETTE
) )
120 // need to set a wxPalette to use this, HOW TO CHECK IF VALID, SIZE?
121 if ((format
== wxBMP_8BPP_PALETTE
)
123 && !image
->HasPalette()
124 #endif // wxUSE_PALETTE
128 wxLogError(_("BMP: wxImage doesn't have own wxPalette."));
134 else // you get 24bpp
136 format
= wxBMP_24BPP
;
141 unsigned width
= image
->GetWidth();
142 unsigned row_padding
= (4 - int(width
*bpp
/8.0) % 4) % 4; // # bytes to pad to dword
143 unsigned row_width
= int(width
* bpp
/8.0) + row_padding
; // # of bytes per row
148 wxUint16 magic
; // format magic, always 'BM'
149 wxUint32 filesize
; // total file size, inc. headers
150 wxUint32 reserved
; // for future use
151 wxUint32 data_offset
; // image data offset in the file
154 wxUint32 bih_size
; // 2nd part's size
155 wxUint32 width
, height
; // bitmap's dimensions
156 wxUint16 planes
; // num of planes
157 wxUint16 bpp
; // bits per pixel
158 wxUint32 compression
; // compression method
159 wxUint32 size_of_bmp
; // size of the bitmap
160 wxUint32 h_res
, v_res
; // image resolution in dpi
161 wxUint32 num_clrs
; // number of colors used
162 wxUint32 num_signif_clrs
;// number of significant colors
165 wxUint32 hdr_size
= 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/;
167 hdr
.magic
= wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/);
168 hdr
.filesize
= wxUINT32_SWAP_ON_BE( hdr_size
+ palette_size
*4 +
169 row_width
* image
->GetHeight() );
171 hdr
.data_offset
= wxUINT32_SWAP_ON_BE(hdr_size
+ palette_size
*4);
173 hdr
.bih_size
= wxUINT32_SWAP_ON_BE(hdr_size
- 14);
174 hdr
.width
= wxUINT32_SWAP_ON_BE(image
->GetWidth());
177 hdr
.height
= wxUINT32_SWAP_ON_BE(image
->GetHeight());
181 hdr
.height
= wxUINT32_SWAP_ON_BE(2 * image
->GetHeight());
183 hdr
.planes
= wxUINT16_SWAP_ON_BE(1); // always 1 plane
184 hdr
.bpp
= wxUINT16_SWAP_ON_BE(bpp
);
185 hdr
.compression
= 0; // RGB uncompressed
186 hdr
.size_of_bmp
= wxUINT32_SWAP_ON_BE(row_width
* image
->GetHeight());
187 hdr
.h_res
= hdr
.v_res
= wxUINT32_SWAP_ON_BE(72); // 72dpi is standard
188 hdr
.num_clrs
= wxUINT32_SWAP_ON_BE(palette_size
); // # colors in colormap
189 hdr
.num_signif_clrs
= 0; // all colors are significant
193 if (// VS: looks ugly but compilers tend to do ugly things with structs,
194 // like aligning hdr.filesize's ofset to dword :(
195 // VZ: we should add padding then...
196 !stream
.Write(&hdr
.magic
, 2) ||
197 !stream
.Write(&hdr
.filesize
, 4) ||
198 !stream
.Write(&hdr
.reserved
, 4) ||
199 !stream
.Write(&hdr
.data_offset
, 4)
203 wxLogError(_("BMP: Couldn't write the file (Bitmap) header."));
210 !stream
.Write(&hdr
.bih_size
, 4) ||
211 !stream
.Write(&hdr
.width
, 4) ||
212 !stream
.Write(&hdr
.height
, 4) ||
213 !stream
.Write(&hdr
.planes
, 2) ||
214 !stream
.Write(&hdr
.bpp
, 2) ||
215 !stream
.Write(&hdr
.compression
, 4) ||
216 !stream
.Write(&hdr
.size_of_bmp
, 4) ||
217 !stream
.Write(&hdr
.h_res
, 4) ||
218 !stream
.Write(&hdr
.v_res
, 4) ||
219 !stream
.Write(&hdr
.num_clrs
, 4) ||
220 !stream
.Write(&hdr
.num_signif_clrs
, 4)
224 wxLogError(_("BMP: Couldn't write the file (BitmapInfo) header."));
229 wxPalette
*palette
= NULL
; // entries for quantized images
230 wxUint8
*rgbquad
= NULL
; // for the RGBQUAD bytes for the colormap
231 wxImage
*q_image
= NULL
; // destination for quantized image
233 // if <24bpp use quantization to reduce colors for *some* of the formats
234 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_4BPP
) ||
235 (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_PALETTE
) )
237 // make a new palette and quantize the image
238 if (format
!= wxBMP_8BPP_PALETTE
)
240 q_image
= new wxImage();
242 // I get a delete error using Quantize when desired colors > 236
243 int quantize
= ((palette_size
> 236) ? 236 : palette_size
);
244 // fill the destination too, it gives much nicer 4bpp images
245 wxQuantize::Quantize( *image
, *q_image
, &palette
, quantize
, 0,
246 wxQUANTIZE_FILL_DESTINATION_IMAGE
);
251 palette
= new wxPalette(image
->GetPalette());
252 #endif // wxUSE_PALETTE
256 unsigned char r
, g
, b
;
257 rgbquad
= new wxUint8
[palette_size
*4];
259 for (i
= 0; i
< palette_size
; i
++)
262 if ( !palette
->GetRGB(i
, &r
, &g
, &b
) )
263 #endif // wxUSE_PALETTE
272 // make a 256 entry greyscale colormap or 2 entry black & white
273 else if ( (format
== wxBMP_8BPP_GREY
) || (format
== wxBMP_8BPP_RED
) ||
274 (format
== wxBMP_1BPP_BW
) )
277 rgbquad
= new wxUint8
[palette_size
*4];
279 for (i
= 0; i
< palette_size
; i
++)
281 // if 1BPP_BW then just 0 and 255 then exit
282 if (( i
> 0) && (format
== wxBMP_1BPP_BW
)) i
= 255;
283 rgbquad
[i
*4] = rgbquad
[i
*4+1] = rgbquad
[i
*4+2] = (wxUint8
)i
;
288 // if the colormap was made, then it needs to be written
293 if ( !stream
.Write(rgbquad
, palette_size
*4) )
296 wxLogError(_("BMP: Couldn't write RGB color map."));
300 #endif // wxUSE_PALETTE
308 // pointer to the image data, use quantized if available
309 wxUint8
*data
= (wxUint8
*) image
->GetData();
310 if (q_image
) if (q_image
->Ok()) data
= (wxUint8
*) q_image
->GetData();
312 wxUint8
*buffer
= new wxUint8
[row_width
];
313 memset(buffer
, 0, row_width
);
317 for (y
= image
->GetHeight() -1; y
>= 0; y
--)
319 if ( format
== wxBMP_24BPP
) // 3 bytes per pixel red,green,blue
321 for ( x
= 0; x
< width
; x
++ )
323 pixel
= 3*(y
*width
+ x
);
325 buffer
[3*x
] = data
[pixel
+2];
326 buffer
[3*x
+ 1] = data
[pixel
+1];
327 buffer
[3*x
+ 2] = data
[pixel
];
330 else if ((format
== wxBMP_8BPP
) || // 1 byte per pixel in color
331 (format
== wxBMP_8BPP_PALETTE
))
333 for (x
= 0; x
< width
; x
++)
335 pixel
= 3*(y
*width
+ x
);
337 buffer
[x
] = (wxUint8
)palette
->GetPixel( data
[pixel
],
341 // FIXME: what should this be? use some std palette maybe?
343 #endif // wxUSE_PALETTE
346 else if ( format
== wxBMP_8BPP_GREY
) // 1 byte per pix, rgb ave to grey
348 for (x
= 0; x
< width
; x
++)
350 pixel
= 3*(y
*width
+ x
);
351 buffer
[x
] = (wxUint8
)(.299*data
[pixel
] +
356 else if ( format
== wxBMP_8BPP_RED
) // 1 byte per pixel, red as greys
358 for (x
= 0; x
< width
; x
++)
360 buffer
[x
] = (wxUint8
)data
[3*(y
*width
+ x
)];
363 else if ( format
== wxBMP_4BPP
) // 4 bpp in color
365 for (x
= 0; x
< width
; x
+=2)
367 pixel
= 3*(y
*width
+ x
);
369 // fill buffer, ignore if > width
371 buffer
[x
/2] = (wxUint8
)(
372 ((wxUint8
)palette
->GetPixel(data
[pixel
],
374 data
[pixel
+2]) << 4) |
377 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3],
381 // FIXME: what should this be? use some std palette maybe?
383 #endif // wxUSE_PALETTE
386 else if ( format
== wxBMP_1BPP
) // 1 bpp in "color"
388 for (x
= 0; x
< width
; x
+=8)
390 pixel
= 3*(y
*width
+ x
);
393 buffer
[x
/8] = (wxUint8
)(
394 ((wxUint8
)palette
->GetPixel(data
[pixel
], data
[pixel
+1], data
[pixel
+2]) << 7) |
395 (((x
+1) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3], data
[pixel
+4], data
[pixel
+5]) << 6)) |
396 (((x
+2) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+6], data
[pixel
+7], data
[pixel
+8]) << 5)) |
397 (((x
+3) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+9], data
[pixel
+10], data
[pixel
+11]) << 4)) |
398 (((x
+4) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+12], data
[pixel
+13], data
[pixel
+14]) << 3)) |
399 (((x
+5) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+15], data
[pixel
+16], data
[pixel
+17]) << 2)) |
400 (((x
+6) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+18], data
[pixel
+19], data
[pixel
+20]) << 1)) |
401 (((x
+7) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+21], data
[pixel
+22], data
[pixel
+23]) )) );
403 // FIXME: what should this be? use some std palette maybe?
405 #endif // wxUSE_PALETTE
408 else if ( format
== wxBMP_1BPP_BW
) // 1 bpp B&W colormap from red color ONLY
410 for (x
= 0; x
< width
; x
+=8)
412 pixel
= 3*(y
*width
+ x
);
414 buffer
[x
/8] = (wxUint8
)(
415 (((wxUint8
)(data
[pixel
] /128.)) << 7) |
416 (((x
+1) > width
) ? 0 : (((wxUint8
)(data
[pixel
+3] /128.)) << 6)) |
417 (((x
+2) > width
) ? 0 : (((wxUint8
)(data
[pixel
+6] /128.)) << 5)) |
418 (((x
+3) > width
) ? 0 : (((wxUint8
)(data
[pixel
+9] /128.)) << 4)) |
419 (((x
+4) > width
) ? 0 : (((wxUint8
)(data
[pixel
+12]/128.)) << 3)) |
420 (((x
+5) > width
) ? 0 : (((wxUint8
)(data
[pixel
+15]/128.)) << 2)) |
421 (((x
+6) > width
) ? 0 : (((wxUint8
)(data
[pixel
+18]/128.)) << 1)) |
422 (((x
+7) > width
) ? 0 : (((wxUint8
)(data
[pixel
+21]/128.)) )) );
426 if ( !stream
.Write(buffer
, row_width
) )
429 wxLogError(_("BMP: Couldn't write data."));
433 #endif // wxUSE_PALETTE
441 #endif // wxUSE_PALETTE
450 unsigned char r
, g
, b
;
453 bool wxBMPHandler::DoLoadDib(wxImage
* image
, int width
, int height
,
454 int bpp
, int ncolors
, int comp
,
455 wxFileOffset bmpOffset
, wxInputStream
& stream
,
456 bool verbose
, bool IsBmp
, bool hasPalette
)
458 wxInt32 aDword
, rmask
= 0, gmask
= 0, bmask
= 0;
459 int rshift
= 0, gshift
= 0, bshift
= 0;
460 int rbits
= 0, gbits
= 0, bbits
= 0;
466 // allocate space for palette if needed:
471 cmap
= new _cmap
[ncolors
];
475 wxLogError(_("BMP: Couldn't allocate memory."));
482 // destroy existing here instead of:
484 image
->Create(width
, height
);
486 unsigned char *ptr
= image
->GetData();
491 wxLogError( _("BMP: Couldn't allocate memory.") );
497 // Reading the palette, if it exists:
498 if ( bpp
< 16 && ncolors
!= 0 )
500 unsigned char* r
= new unsigned char[ncolors
];
501 unsigned char* g
= new unsigned char[ncolors
];
502 unsigned char* b
= new unsigned char[ncolors
];
503 for (int j
= 0; j
< ncolors
; j
++)
507 stream
.Read(bbuf
, 4);
518 //used in reading .ico file mask
521 b
[j
] = cmap
[j
].b
= ( j
? 255 : 0 );
526 // Set the palette for the wxImage
527 image
->SetPalette(wxPalette(ncolors
, r
, g
, b
));
528 #endif // wxUSE_PALETTE
534 else if ( bpp
== 16 || bpp
== 32 )
536 if ( comp
== BI_BITFIELDS
)
539 stream
.Read(dbuf
, 4 * 3);
540 rmask
= wxINT32_SWAP_ON_BE(dbuf
[0]);
541 gmask
= wxINT32_SWAP_ON_BE(dbuf
[1]);
542 bmask
= wxINT32_SWAP_ON_BE(dbuf
[2]);
543 // find shift amount (Least significant bit of mask)
544 for (bit
= bpp
-1; bit
>=0; bit
--)
546 if (bmask
& (1 << bit
))
548 if (gmask
& (1 << bit
))
550 if (rmask
& (1 << bit
))
553 // Find number of bits in mask (MSB-LSB+1)
554 for (bit
= 0; bit
< bpp
; bit
++)
556 if (bmask
& (1 << bit
))
557 bbits
= bit
-bshift
+1;
558 if (gmask
& (1 << bit
))
559 gbits
= bit
-gshift
+1;
560 if (rmask
& (1 << bit
))
561 rbits
= bit
-rshift
+1;
564 else if ( bpp
== 16 )
576 else if ( bpp
== 32 )
591 * Reading the image data
594 stream
.SeekI(bmpOffset
); // else icon, just carry on
596 unsigned char *data
= ptr
;
598 /* set the whole image to the background color */
599 if ( bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
) )
601 for (int i
= 0; i
< width
* height
; i
++)
610 int linesize
= ((width
* bpp
+ 31) / 32) * 4;
612 /* BMPs are stored upside down */
613 for ( int line
= (height
- 1); line
>= 0; line
-- )
616 for ( int column
= 0; column
< width
; )
621 aByte
= stream
.GetC();
624 for (int bit
= 0; bit
< 8 && column
< width
; bit
++)
626 int index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
627 ptr
[poffset
] = cmap
[index
].r
;
628 ptr
[poffset
+ 1] = cmap
[index
].g
;
629 ptr
[poffset
+ 2] = cmap
[index
].b
;
635 if ( comp
== BI_RLE4
)
639 aByte
= stream
.GetC();
647 else if ( aByte
== 1 )
652 else if ( aByte
== 2 )
654 aByte
= stream
.GetC();
656 linepos
= column
* bpp
/ 4;
657 aByte
= stream
.GetC();
658 line
-= aByte
; // upside down
662 int absolute
= aByte
;
665 for (int k
= 0; k
< absolute
; k
++)
670 aByte
= stream
.GetC();
671 nibble
[0] = (wxUint8
)( (aByte
& 0xF0) >> 4 ) ;
672 nibble
[1] = (wxUint8
)( aByte
& 0x0F ) ;
674 ptr
[poffset
] = cmap
[nibble
[k%2
]].r
;
675 ptr
[poffset
+ 1] = cmap
[nibble
[k%2
]].g
;
676 ptr
[poffset
+ 2] = cmap
[nibble
[k%2
]].b
;
681 if ( readBytes
& 0x01 )
682 aByte
= stream
.GetC();
688 nibble
[0] = (wxUint8
)( (aByte
& 0xF0) >> 4 ) ;
689 nibble
[1] = (wxUint8
)( aByte
& 0x0F ) ;
691 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
693 ptr
[poffset
] = cmap
[nibble
[l%2
]].r
;
694 ptr
[poffset
+ 1] = cmap
[nibble
[l%2
]].g
;
695 ptr
[poffset
+ 2] = cmap
[nibble
[l%2
]].b
;
704 for (int nibble
= 0; nibble
< 2 && column
< width
; nibble
++)
706 int index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
709 ptr
[poffset
] = cmap
[index
].r
;
710 ptr
[poffset
+ 1] = cmap
[index
].g
;
711 ptr
[poffset
+ 2] = cmap
[index
].b
;
718 if ( comp
== BI_RLE8
)
722 aByte
= stream
.GetC();
727 /* column = width; */
729 else if ( aByte
== 1 )
734 else if ( aByte
== 2 )
736 aByte
= stream
.GetC();
738 linepos
= column
* bpp
/ 8;
739 aByte
= stream
.GetC();
744 int absolute
= aByte
;
745 for (int k
= 0; k
< absolute
; k
++)
748 aByte
= stream
.GetC();
749 ptr
[poffset
] = cmap
[aByte
].r
;
750 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
751 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
754 if ( absolute
& 0x01 )
755 aByte
= stream
.GetC();
760 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
762 ptr
[poffset
] = cmap
[aByte
].r
;
763 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
764 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
772 ptr
[poffset
] = cmap
[aByte
].r
;
773 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
774 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
776 // linepos += size; seems to be wrong, RR
780 else if ( bpp
== 24 )
782 stream
.Read(bbuf
, 3);
784 ptr
[poffset
] = (unsigned char)bbuf
[2];
785 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
786 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
789 else if ( bpp
== 16 )
792 stream
.Read(&aWord
, 2);
793 aWord
= wxUINT16_SWAP_ON_BE(aWord
);
795 /* use the masks and calculated amonut of shift
796 to retrieve the color data out of the word. Then
797 shift it left by (8 - number of bits) such that
798 the image has the proper dynamic range */
799 temp
= (unsigned char)((aWord
& rmask
) >> rshift
<< (8-rbits
));
801 temp
= (unsigned char)((aWord
& gmask
) >> gshift
<< (8-gbits
));
802 ptr
[poffset
+ 1] = temp
;
803 temp
= (unsigned char)((aWord
& bmask
) >> bshift
<< (8-bbits
));
804 ptr
[poffset
+ 2] = temp
;
810 stream
.Read(&aDword
, 4);
811 aDword
= wxINT32_SWAP_ON_BE(aDword
);
813 temp
= (unsigned char)((aDword
& rmask
) >> rshift
);
815 temp
= (unsigned char)((aDword
& gmask
) >> gshift
);
816 ptr
[poffset
+ 1] = temp
;
817 temp
= (unsigned char)((aDword
& bmask
) >> bshift
);
818 ptr
[poffset
+ 2] = temp
;
822 while ( (linepos
< linesize
) && (comp
!= 1) && (comp
!= 2) )
824 stream
.Read(&aByte
, 1);
833 image
->SetMask(false);
835 const wxStreamError err
= stream
.GetLastError();
836 return err
== wxSTREAM_NO_ERROR
|| err
== wxSTREAM_EOF
;
839 bool wxBMPHandler::LoadDib(wxImage
*image
, wxInputStream
& stream
,
840 bool verbose
, bool IsBmp
)
846 wxFileOffset offset
= 0; // keep gcc quiet
849 // read the header off the .BMP format file
851 offset
= stream
.TellI();
852 if (offset
== wxInvalidOffset
)
855 stream
.Read(bbuf
, 2);
856 stream
.Read(dbuf
, 16);
860 stream
.Read(dbuf
, 4);
863 wxInt32 size
= wxINT32_SWAP_ON_BE(dbuf
[0]);
865 offset
= offset
+ wxINT32_SWAP_ON_BE(dbuf
[2]);
867 stream
.Read(dbuf
, 4 * 2);
868 int width
= (int)wxINT32_SWAP_ON_BE(dbuf
[0]);
869 int height
= (int)wxINT32_SWAP_ON_BE(dbuf
[1]);
870 if ( !IsBmp
)height
= height
/ 2; // for icons divide by 2
875 wxLogError( _("DIB Header: Image width > 32767 pixels for file.") );
878 if ( height
> 32767 )
881 wxLogError( _("DIB Header: Image height > 32767 pixels for file.") );
885 stream
.Read(&aWord
, 2);
888 int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
890 stream
.Read(&aWord
, 2);
891 int bpp
= (int)wxUINT16_SWAP_ON_BE(aWord
);
892 if ( bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
!= 16 && bpp
!= 24 && bpp
!= 32 )
895 wxLogError( _("DIB Header: Unknown bitdepth in file.") );
899 stream
.Read(dbuf
, 4 * 4);
900 int comp
= (int)wxINT32_SWAP_ON_BE(dbuf
[0]);
901 if ( comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&&
902 comp
!= BI_BITFIELDS
)
905 wxLogError( _("DIB Header: Unknown encoding in file.") );
909 stream
.Read(dbuf
, 4 * 2);
910 int ncolors
= (int)wxINT32_SWAP_ON_BE( dbuf
[0] );
913 /* some more sanity checks */
914 if (((comp
== BI_RLE4
) && (bpp
!= 4)) ||
915 ((comp
== BI_RLE8
) && (bpp
!= 8)) ||
916 ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
919 wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") );
923 //read DIB; this is the BMP image or the XOR part of an icon image
924 if ( !DoLoadDib(image
, width
, height
, bpp
, ncolors
, comp
, offset
, stream
,
925 verbose
, IsBmp
, true) )
928 wxLogError( _("Error in reading image DIB .") );
934 //read Icon mask which is monochrome
935 //there is no palette, so we will create one
937 if ( !DoLoadDib(&mask
, width
, height
, 1, 2, BI_RGB
, offset
, stream
,
938 verbose
, IsBmp
, false) )
941 wxLogError( _("ICO: Error in reading mask DIB.") );
944 image
->SetMaskFromImage(mask
, 255, 255, 255);
951 bool wxBMPHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
952 bool verbose
, int WXUNUSED(index
))
954 // Read a single DIB fom the file:
955 return LoadDib(image
, stream
, verbose
, true/*isBmp*/);
958 bool wxBMPHandler::DoCanRead(wxInputStream
& stream
)
960 unsigned char hdr
[2];
962 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
965 // do we have the BMP file signature?
966 return hdr
[0] == 'B' && hdr
[1] == 'M';
969 #endif // wxUSE_STREAMS
973 //-----------------------------------------------------------------------------
975 //-----------------------------------------------------------------------------
977 IMPLEMENT_DYNAMIC_CLASS(wxICOHandler
, wxBMPHandler
)
983 wxUint8 bWidth
; // Width of the image
984 wxUint8 bHeight
; // Height of the image (times 2)
985 wxUint8 bColorCount
; // Number of colors in image (0 if >=8bpp)
986 wxUint8 bReserved
; // Reserved
988 // these two are different in icons and cursors:
990 wxUint16 wPlanes
; // Color Planes or XHotSpot
991 wxUint16 wBitCount
; // Bits per pixel or YHotSpot
993 wxUint32 dwBytesInRes
; // how many bytes in this resource?
994 wxUint32 dwImageOffset
; // where in the file is this image
999 wxUint16 idReserved
; // Reserved
1000 wxUint16 idType
; // resource type (1 for icons, 2 for cursors)
1001 wxUint16 idCount
; // how many images?
1005 bool wxICOHandler::SaveFile(wxImage
*image
,
1006 wxOutputStream
& stream
,
1010 //sanity check; icon must be less than 127 pixels high and 255 wide
1011 if ( image
->GetHeight () > 127 )
1014 wxLogError(_("ICO: Image too tall for an icon."));
1017 if ( image
->GetWidth () > 255 )
1020 wxLogError(_("ICO: Image too wide for an icon."));
1024 int images
= 1; // only generate one image
1026 // VS: This is a hack of sort - since ICO and CUR files are almost
1027 // identical, we have all the meat in wxICOHandler and check for
1028 // the actual (handler) type when the code has to distinguish between
1030 int type
= (this->GetType() == wxBITMAP_TYPE_CUR
) ? 2 : 1;
1032 // write a header, (ICONDIR)
1033 // Calculate the header size
1034 wxUint32 offset
= 3 * sizeof(wxUint16
);
1037 IconDir
.idReserved
= 0;
1038 IconDir
.idType
= (WORD
)wxUINT16_SWAP_ON_BE(type
);
1039 IconDir
.idCount
= (WORD
)wxUINT16_SWAP_ON_BE(images
);
1040 stream
.Write(&IconDir
.idReserved
, sizeof(IconDir
.idReserved
));
1041 stream
.Write(&IconDir
.idType
, sizeof(IconDir
.idType
));
1042 stream
.Write(&IconDir
.idCount
, sizeof(IconDir
.idCount
));
1043 if ( !stream
.IsOk() )
1046 wxLogError(_("ICO: Error writing the image file!"));
1050 // for each iamage write a description ICONDIRENTRY:
1051 ICONDIRENTRY icondirentry
;
1052 for (int i
= 0; i
< images
; i
++)
1056 if ( image
->HasMask() )
1058 // make another image with black/white:
1059 mask
= image
->ConvertToMono (image
->GetMaskRed(), image
->GetMaskGreen(), image
->GetMaskBlue() );
1061 // now we need to change the masked regions to black:
1062 unsigned char r
= image
->GetMaskRed();
1063 unsigned char g
= image
->GetMaskGreen();
1064 unsigned char b
= image
->GetMaskBlue();
1065 if ( (r
!= 0) || (g
!= 0) || (b
!= 0) )
1067 // Go round and apply black to the masked bits:
1069 for (i
= 0; i
< mask
.GetWidth(); i
++)
1071 for (j
= 0; j
< mask
.GetHeight(); j
++)
1073 if ((r
== mask
.GetRed(i
, j
)) &&
1074 (g
== mask
.GetGreen(i
, j
))&&
1075 (b
== mask
.GetBlue(i
, j
)) )
1076 image
->SetRGB(i
, j
, 0, 0, 0 );
1083 // just make a black mask all over:
1084 mask
= image
->Copy();
1086 for (i
= 0; i
< mask
.GetWidth(); i
++)
1087 for (j
= 0; j
< mask
.GetHeight(); j
++)
1088 mask
.SetRGB(i
, j
, 0, 0, 0 );
1090 // Set the formats for image and mask
1091 // (Windows never saves with more than 8 colors):
1092 image
->SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_8BPP
);
1094 // monochome bitmap:
1095 mask
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_1BPP_BW
);
1097 bool IsMask
= false;
1099 //calculate size and offset of image and mask
1100 wxCountingOutputStream cStream
;
1101 bool bResult
= SaveDib(image
, cStream
, verbose
, IsBmp
, IsMask
);
1105 wxLogError(_("ICO: Error writing the image file!"));
1110 bResult
= SaveDib(&mask
, cStream
, verbose
, IsBmp
, IsMask
);
1114 wxLogError(_("ICO: Error writing the image file!"));
1117 wxUint32 Size
= cStream
.GetSize();
1119 // wxCountingOutputStream::Ok() always returns true for now and this
1120 // "if" provokes VC++ warnings in optimized build
1122 if ( !cStream
.Ok() )
1125 wxLogError(_("ICO: Error writing the image file!"));
1130 offset
= offset
+ sizeof(ICONDIRENTRY
);
1132 icondirentry
.bWidth
= (BYTE
)image
->GetWidth();
1133 icondirentry
.bHeight
= (BYTE
)(2 * image
->GetHeight());
1134 icondirentry
.bColorCount
= 0;
1135 icondirentry
.bReserved
= 0;
1136 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE(1);
1137 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE(wxBMP_8BPP
);
1138 if ( type
== 2 /*CUR*/)
1140 int hx
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ?
1141 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) :
1142 image
->GetWidth() / 2;
1143 int hy
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ?
1144 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) :
1145 image
->GetHeight() / 2;
1147 // actually write the values of the hot spot here:
1148 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE((wxUint16
)hx
);
1149 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE((wxUint16
)hy
);
1151 icondirentry
.dwBytesInRes
= wxUINT32_SWAP_ON_BE(Size
);
1152 icondirentry
.dwImageOffset
= wxUINT32_SWAP_ON_BE(offset
);
1154 // increase size to allow for the data written:
1158 stream
.Write(&icondirentry
.bWidth
, sizeof(icondirentry
.bWidth
));
1159 stream
.Write(&icondirentry
.bHeight
, sizeof(icondirentry
.bHeight
));
1160 stream
.Write(&icondirentry
.bColorCount
, sizeof(icondirentry
.bColorCount
));
1161 stream
.Write(&icondirentry
.bReserved
, sizeof(icondirentry
.bReserved
));
1162 stream
.Write(&icondirentry
.wPlanes
, sizeof(icondirentry
.wPlanes
));
1163 stream
.Write(&icondirentry
.wBitCount
, sizeof(icondirentry
.wBitCount
));
1164 stream
.Write(&icondirentry
.dwBytesInRes
, sizeof(icondirentry
.dwBytesInRes
));
1165 stream
.Write(&icondirentry
.dwImageOffset
, sizeof(icondirentry
.dwImageOffset
));
1166 if ( !stream
.IsOk() )
1169 wxLogError(_("ICO: Error writing the image file!"));
1173 // actually save it:
1175 bResult
= SaveDib(image
, stream
, verbose
, IsBmp
, IsMask
);
1179 wxLogError(_("ICO: Error writing the image file!"));
1184 bResult
= SaveDib(&mask
, stream
, verbose
, IsBmp
, IsMask
);
1188 wxLogError(_("ICO: Error writing the image file!"));
1192 } // end of for loop
1197 bool wxICOHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1198 bool verbose
, int index
)
1201 return DoLoadFile(image
, stream
, verbose
, index
);
1204 bool wxICOHandler::DoLoadFile(wxImage
*image
, wxInputStream
& stream
,
1205 bool WXUNUSED(verbose
), int index
)
1207 bool bResult
wxDUMMY_INITIALIZE(false);
1212 wxFileOffset iPos
= stream
.TellI();
1213 stream
.Read(&IconDir
, sizeof(IconDir
));
1214 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1215 // nType is 1 for Icons, 2 for Cursors:
1216 wxUint16 nType
= wxUINT16_SWAP_ON_BE(IconDir
.idType
);
1218 // loop round the icons and choose the best one:
1219 ICONDIRENTRY
*pIconDirEntry
= new ICONDIRENTRY
[nIcons
];
1220 ICONDIRENTRY
*pCurrentEntry
= pIconDirEntry
;
1223 int iSel
= wxNOT_FOUND
;
1225 for (int i
= 0; i
< nIcons
; i
++ )
1227 stream
.Read(pCurrentEntry
, sizeof(ICONDIRENTRY
));
1228 // bHeight and bColorCount are wxUint8
1229 if ( pCurrentEntry
->bWidth
>= wMax
)
1231 // see if we have more colors, ==0 indicates > 8bpp:
1232 if ( pCurrentEntry
->bColorCount
== 0 )
1233 pCurrentEntry
->bColorCount
= 255;
1234 if ( pCurrentEntry
->bColorCount
>= colmax
)
1237 wMax
= pCurrentEntry
->bWidth
;
1238 colmax
= pCurrentEntry
->bColorCount
;
1246 // VS: Note that we *have* to run the loop above even if index != -1, because
1247 // it reads ICONDIRENTRies.
1251 if ( iSel
== wxNOT_FOUND
|| iSel
< 0 || iSel
>= nIcons
)
1253 wxLogError(_("ICO: Invalid icon index."));
1258 // seek to selected icon:
1259 pCurrentEntry
= pIconDirEntry
+ iSel
;
1260 stream
.SeekI(iPos
+ wxUINT32_SWAP_ON_BE(pCurrentEntry
->dwImageOffset
), wxFromStart
);
1261 bResult
= LoadDib(image
, stream
, true, IsBmp
);
1262 bool bIsCursorType
= (this->GetType() == wxBITMAP_TYPE_CUR
) || (this->GetType() == wxBITMAP_TYPE_ANI
);
1263 if ( bResult
&& bIsCursorType
&& nType
== 2 )
1265 // it is a cursor, so let's set the hotspot:
1266 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wPlanes
));
1267 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wBitCount
));
1270 delete[] pIconDirEntry
;
1274 int wxICOHandler::GetImageCount(wxInputStream
& stream
)
1277 wxFileOffset iPos
= stream
.TellI();
1279 stream
.Read(&IconDir
, sizeof(IconDir
));
1280 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1285 bool wxICOHandler::DoCanRead(wxInputStream
& stream
)
1288 unsigned char hdr
[4];
1289 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
1292 // hdr[2] is one for an icon and two for a cursor
1293 return hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\1' && hdr
[3] == '\0';
1296 #endif // wxUSE_STREAMS
1299 //-----------------------------------------------------------------------------
1301 //-----------------------------------------------------------------------------
1303 IMPLEMENT_DYNAMIC_CLASS(wxCURHandler
, wxICOHandler
)
1307 bool wxCURHandler::DoCanRead(wxInputStream
& stream
)
1310 unsigned char hdr
[4];
1311 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
1314 // hdr[2] is one for an icon and two for a cursor
1315 return hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\2' && hdr
[3] == '\0';
1318 #endif // wxUSE_STREAMS
1320 //-----------------------------------------------------------------------------
1322 //-----------------------------------------------------------------------------
1324 IMPLEMENT_DYNAMIC_CLASS(wxANIHandler
, wxCURHandler
)
1328 bool wxANIHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1329 bool verbose
, int index
)
1335 memcpy( &riff32
, "RIFF", 4 );
1337 memcpy( &list32
, "LIST", 4 );
1339 memcpy( &ico32
, "icon", 4 );
1343 stream
.Read(&FCC1
, 4);
1344 if ( FCC1
!= riff32
)
1347 // we have a riff file:
1348 while (stream
.IsOk())
1350 // we always have a data size
1351 stream
.Read(&datalen
, 4);
1352 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
1353 //data should be padded to make even number of bytes
1354 if (datalen
% 2 == 1) datalen
++ ;
1355 //now either data or a FCC
1356 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
1358 stream
.Read(&FCC2
, 4);
1362 if (FCC1
== ico32
&& iIcon
>= index
)
1364 return DoLoadFile(image
, stream
, verbose
, -1);
1368 stream
.SeekI(stream
.TellI() + datalen
);
1369 if ( FCC1
== ico32
)
1374 // try to read next data chunk:
1375 stream
.Read(&FCC1
, 4);
1380 bool wxANIHandler::DoCanRead(wxInputStream
& stream
)
1386 memcpy( &riff32
, "RIFF", 4 );
1388 memcpy( &list32
, "LIST", 4 );
1390 memcpy( &ico32
, "icon", 4 );
1392 memcpy( &anih32
, "anih", 4 );
1395 if ( !stream
.Read(&FCC1
, 4) )
1398 if ( FCC1
!= riff32
)
1401 // we have a riff file:
1402 while ( stream
.IsOk() )
1404 if ( FCC1
== anih32
)
1406 // we always have a data size:
1407 stream
.Read(&datalen
, 4);
1408 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
1409 //data should be padded to make even number of bytes
1410 if (datalen
% 2 == 1) datalen
++ ;
1411 // now either data or a FCC:
1412 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
1414 stream
.Read(&FCC2
, 4);
1418 stream
.SeekI(stream
.TellI() + datalen
);
1421 // try to read next data chunk:
1422 if ( !stream
.Read(&FCC1
, 4) )
1424 // reading failed -- either EOF or IO error, bail out anyhow
1432 int wxANIHandler::GetImageCount(wxInputStream
& stream
)
1438 memcpy( &riff32
, "RIFF", 4 );
1440 memcpy( &list32
, "LIST", 4 );
1442 memcpy( &ico32
, "icon", 4 );
1444 memcpy( &anih32
, "anih", 4 );
1447 stream
.Read(&FCC1
, 4);
1448 if ( FCC1
!= riff32
)
1451 // we have a riff file:
1452 while ( stream
.IsOk() )
1454 // we always have a data size:
1455 stream
.Read(&datalen
, 4);
1456 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
1457 //data should be padded to make even number of bytes
1458 if (datalen
% 2 == 1) datalen
++ ;
1459 // now either data or a FCC:
1460 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
1462 stream
.Read(&FCC2
, 4);
1466 if ( FCC1
== anih32
)
1468 wxUint32
*pData
= new wxUint32
[datalen
/4];
1469 stream
.Read(pData
, datalen
);
1470 int nIcons
= wxINT32_SWAP_ON_BE(*(pData
+ 1));
1475 stream
.SeekI(stream
.TellI() + datalen
);
1478 // try to read next data chunk:
1479 stream
.Read(&FCC1
, 4);
1485 #endif // wxUSE_STREAMS
1487 #endif // wxUSE_ICO_CUR
1489 #endif // wxUSE_IMAGE