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;
290 // if the colormap was made, then it needs to be written
295 if ( !stream
.Write(rgbquad
, palette_size
*4) )
298 wxLogError(_("BMP: Couldn't write RGB color map."));
302 #endif // wxUSE_PALETTE
310 // pointer to the image data, use quantized if available
311 wxUint8
*data
= (wxUint8
*) image
->GetData();
312 if (q_image
) if (q_image
->Ok()) data
= (wxUint8
*) q_image
->GetData();
314 wxUint8
*buffer
= new wxUint8
[row_width
];
315 memset(buffer
, 0, row_width
);
319 for (y
= image
->GetHeight() -1; y
>= 0; y
--)
321 if ( format
== wxBMP_24BPP
) // 3 bytes per pixel red,green,blue
323 for ( x
= 0; x
< width
; x
++ )
325 pixel
= 3*(y
*width
+ x
);
327 buffer
[3*x
] = data
[pixel
+2];
328 buffer
[3*x
+ 1] = data
[pixel
+1];
329 buffer
[3*x
+ 2] = data
[pixel
];
332 else if ((format
== wxBMP_8BPP
) || // 1 byte per pixel in color
333 (format
== wxBMP_8BPP_PALETTE
))
335 for (x
= 0; x
< width
; x
++)
337 pixel
= 3*(y
*width
+ x
);
339 buffer
[x
] = palette
->GetPixel( data
[pixel
],
343 // FIXME: what should this be? use some std palette maybe?
345 #endif // wxUSE_PALETTE
348 else if ( format
== wxBMP_8BPP_GREY
) // 1 byte per pix, rgb ave to grey
350 for (x
= 0; x
< width
; x
++)
352 pixel
= 3*(y
*width
+ x
);
353 buffer
[x
] = (wxUint8
)(.299*data
[pixel
] +
358 else if ( format
== wxBMP_8BPP_RED
) // 1 byte per pixel, red as greys
360 for (x
= 0; x
< width
; x
++)
362 buffer
[x
] = (wxUint8
)data
[3*(y
*width
+ x
)];
365 else if ( format
== wxBMP_4BPP
) // 4 bpp in color
367 for (x
= 0; x
< width
; x
+=2)
369 pixel
= 3*(y
*width
+ x
);
371 // fill buffer, ignore if > width
374 ((wxUint8
)palette
->GetPixel(data
[pixel
],
376 data
[pixel
+2]) << 4) |
379 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3],
383 // FIXME: what should this be? use some std palette maybe?
385 #endif // wxUSE_PALETTE
388 else if ( format
== wxBMP_1BPP
) // 1 bpp in "color"
390 for (x
= 0; x
< width
; x
+=8)
392 pixel
= 3*(y
*width
+ x
);
395 buffer
[x
/8] = ((wxUint8
)palette
->GetPixel(data
[pixel
], data
[pixel
+1], data
[pixel
+2]) << 7) |
396 (((x
+1) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3], data
[pixel
+4], data
[pixel
+5]) << 6)) |
397 (((x
+2) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+6], data
[pixel
+7], data
[pixel
+8]) << 5)) |
398 (((x
+3) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+9], data
[pixel
+10], data
[pixel
+11]) << 4)) |
399 (((x
+4) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+12], data
[pixel
+13], data
[pixel
+14]) << 3)) |
400 (((x
+5) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+15], data
[pixel
+16], data
[pixel
+17]) << 2)) |
401 (((x
+6) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+18], data
[pixel
+19], data
[pixel
+20]) << 1)) |
402 (((x
+7) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+21], data
[pixel
+22], data
[pixel
+23]) ));
404 // FIXME: what should this be? use some std palette maybe?
406 #endif // wxUSE_PALETTE
409 else if ( format
== wxBMP_1BPP_BW
) // 1 bpp B&W colormap from red color ONLY
411 for (x
= 0; x
< width
; x
+=8)
413 pixel
= 3*(y
*width
+ x
);
416 (((wxUint8
)(data
[pixel
] /128.)) << 7) |
417 (((x
+1) > width
) ? 0 : (((wxUint8
)(data
[pixel
+3] /128.)) << 6)) |
418 (((x
+2) > width
) ? 0 : (((wxUint8
)(data
[pixel
+6] /128.)) << 5)) |
419 (((x
+3) > width
) ? 0 : (((wxUint8
)(data
[pixel
+9] /128.)) << 4)) |
420 (((x
+4) > width
) ? 0 : (((wxUint8
)(data
[pixel
+12]/128.)) << 3)) |
421 (((x
+5) > width
) ? 0 : (((wxUint8
)(data
[pixel
+15]/128.)) << 2)) |
422 (((x
+6) > width
) ? 0 : (((wxUint8
)(data
[pixel
+18]/128.)) << 1)) |
423 (((x
+7) > width
) ? 0 : (((wxUint8
)(data
[pixel
+21]/128.)) ));
427 if ( !stream
.Write(buffer
, row_width
) )
430 wxLogError(_("BMP: Couldn't write data."));
434 #endif // wxUSE_PALETTE
442 #endif // wxUSE_PALETTE
451 unsigned char r
, g
, b
;
454 bool wxBMPHandler::DoLoadDib(wxImage
* image
, int width
, int height
,
455 int bpp
, int ncolors
, int comp
,
456 off_t bmpOffset
, wxInputStream
& stream
,
457 bool verbose
, bool IsBmp
, bool hasPalette
)
459 wxInt32 aDword
, rmask
= 0, gmask
= 0, bmask
= 0;
460 int rshift
= 0, gshift
= 0, bshift
= 0;
461 int rbits
= 0, gbits
= 0, bbits
= 0;
467 // allocate space for palette if needed:
472 cmap
= new _cmap
[ncolors
];
476 wxLogError(_("BMP: Couldn't allocate memory."));
483 // destroy existing here instead of:
485 image
->Create(width
, height
);
487 unsigned char *ptr
= image
->GetData();
492 wxLogError( _("BMP: Couldn't allocate memory.") );
498 // Reading the palette, if it exists:
499 if ( bpp
< 16 && ncolors
!= 0 )
501 unsigned char* r
= new unsigned char[ncolors
];
502 unsigned char* g
= new unsigned char[ncolors
];
503 unsigned char* b
= new unsigned char[ncolors
];
504 for (int j
= 0; j
< ncolors
; j
++)
508 stream
.Read(bbuf
, 4);
519 //used in reading .ico file mask
520 r
[j
] = cmap
[j
].r
= j
* 255;
521 g
[j
] = cmap
[j
].g
= j
* 255;
522 b
[j
] = cmap
[j
].b
= j
* 255;
527 // Set the palette for the wxImage
528 image
->SetPalette(wxPalette(ncolors
, r
, g
, b
));
529 #endif // wxUSE_PALETTE
535 else if ( bpp
== 16 || bpp
== 32 )
537 if ( comp
== BI_BITFIELDS
)
540 stream
.Read(dbuf
, 4 * 3);
541 rmask
= wxINT32_SWAP_ON_BE(dbuf
[0]);
542 gmask
= wxINT32_SWAP_ON_BE(dbuf
[1]);
543 bmask
= wxINT32_SWAP_ON_BE(dbuf
[2]);
544 // find shift amount (Least significant bit of mask)
545 for (bit
= bpp
-1; bit
>=0; bit
--)
547 if (bmask
& (1 << bit
))
549 if (gmask
& (1 << bit
))
551 if (rmask
& (1 << bit
))
554 // Find number of bits in mask (MSB-LSB+1)
555 for (bit
= 0; bit
< bpp
; bit
++)
557 if (bmask
& (1 << bit
))
558 bbits
= bit
-bshift
+1;
559 if (gmask
& (1 << bit
))
560 gbits
= bit
-gshift
+1;
561 if (rmask
& (1 << bit
))
562 rbits
= bit
-rshift
+1;
565 else if ( bpp
== 16 )
577 else if ( bpp
== 32 )
592 * Reading the image data
595 stream
.SeekI(bmpOffset
); // else icon, just carry on
597 unsigned char *data
= ptr
;
599 /* set the whole image to the background color */
600 if ( bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
) )
602 for (int i
= 0; i
< width
* height
; i
++)
611 int linesize
= ((width
* bpp
+ 31) / 32) * 4;
613 /* BMPs are stored upside down */
614 for ( int line
= (height
- 1); line
>= 0; line
-- )
617 for ( int column
= 0; column
< width
; )
622 aByte
= stream
.GetC();
625 for (int bit
= 0; bit
< 8 && column
< width
; bit
++)
627 int index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
628 ptr
[poffset
] = cmap
[index
].r
;
629 ptr
[poffset
+ 1] = cmap
[index
].g
;
630 ptr
[poffset
+ 2] = cmap
[index
].b
;
636 if ( comp
== BI_RLE4
)
640 aByte
= stream
.GetC();
648 else if ( aByte
== 1 )
653 else if ( aByte
== 2 )
655 aByte
= stream
.GetC();
657 linepos
= column
* bpp
/ 4;
658 aByte
= stream
.GetC();
659 line
-= aByte
; // upside down
663 int absolute
= aByte
;
666 for (int k
= 0; k
< absolute
; k
++)
671 aByte
= stream
.GetC();
672 nibble
[0] = ( (aByte
& 0xF0) >> 4 ) ;
673 nibble
[1] = ( aByte
& 0x0F ) ;
675 ptr
[poffset
] = cmap
[nibble
[k%2
]].r
;
676 ptr
[poffset
+ 1] = cmap
[nibble
[k%2
]].g
;
677 ptr
[poffset
+ 2] = cmap
[nibble
[k%2
]].b
;
682 if ( readBytes
& 0x01 )
683 aByte
= stream
.GetC();
689 nibble
[0] = ( (aByte
& 0xF0) >> 4 ) ;
690 nibble
[1] = ( aByte
& 0x0F ) ;
692 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
694 ptr
[poffset
] = cmap
[nibble
[l%2
]].r
;
695 ptr
[poffset
+ 1] = cmap
[nibble
[l%2
]].g
;
696 ptr
[poffset
+ 2] = cmap
[nibble
[l%2
]].b
;
705 for (int nibble
= 0; nibble
< 2 && column
< width
; nibble
++)
707 int index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
710 ptr
[poffset
] = cmap
[index
].r
;
711 ptr
[poffset
+ 1] = cmap
[index
].g
;
712 ptr
[poffset
+ 2] = cmap
[index
].b
;
719 if ( comp
== BI_RLE8
)
723 aByte
= stream
.GetC();
728 /* column = width; */
730 else if ( aByte
== 1 )
735 else if ( aByte
== 2 )
737 aByte
= stream
.GetC();
739 linepos
= column
* bpp
/ 8;
740 aByte
= stream
.GetC();
745 int absolute
= aByte
;
746 for (int k
= 0; k
< absolute
; k
++)
749 aByte
= stream
.GetC();
750 ptr
[poffset
] = cmap
[aByte
].r
;
751 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
752 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
755 if ( absolute
& 0x01 )
756 aByte
= stream
.GetC();
761 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
763 ptr
[poffset
] = cmap
[aByte
].r
;
764 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
765 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
773 ptr
[poffset
] = cmap
[aByte
].r
;
774 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
775 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
777 // linepos += size; seems to be wrong, RR
781 else if ( bpp
== 24 )
783 stream
.Read(bbuf
, 3);
785 ptr
[poffset
] = (unsigned char)bbuf
[2];
786 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
787 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
790 else if ( bpp
== 16 )
793 stream
.Read(&aWord
, 2);
794 aWord
= wxUINT16_SWAP_ON_BE(aWord
);
796 /* use the masks and calculated amonut of shift
797 to retrieve the color data out of the word. Then
798 shift it left by (8 - number of bits) such that
799 the image has the proper dynamic range */
800 temp
= (aWord
& rmask
) >> rshift
<< (8-rbits
);
802 temp
= (aWord
& gmask
) >> gshift
<< (8-gbits
);
803 ptr
[poffset
+ 1] = temp
;
804 temp
= (aWord
& bmask
) >> bshift
<< (8-bbits
);
805 ptr
[poffset
+ 2] = temp
;
811 stream
.Read(&aDword
, 4);
812 aDword
= wxINT32_SWAP_ON_BE(aDword
);
814 temp
= (aDword
& rmask
) >> rshift
;
816 temp
= (aDword
& gmask
) >> gshift
;
817 ptr
[poffset
+ 1] = temp
;
818 temp
= (aDword
& bmask
) >> bshift
;
819 ptr
[poffset
+ 2] = temp
;
823 while ( (linepos
< linesize
) && (comp
!= 1) && (comp
!= 2) )
825 stream
.Read(&aByte
, 1);
834 image
->SetMask(FALSE
);
836 const wxStreamError err
= stream
.GetLastError();
837 return err
== wxSTREAM_NO_ERROR
|| err
== wxSTREAM_EOF
;
840 bool wxBMPHandler::LoadDib(wxImage
*image
, wxInputStream
& stream
,
841 bool verbose
, bool IsBmp
)
848 offset
= 0; // keep gcc quiet
851 // read the header off the .BMP format file
853 offset
= stream
.TellI();
854 if (offset
== wxInvalidOffset
) offset
= 0;
856 stream
.Read(bbuf
, 2);
857 stream
.Read(dbuf
, 16);
861 stream
.Read(dbuf
, 4);
864 wxInt32 size
= wxINT32_SWAP_ON_BE(dbuf
[0]);
866 offset
= offset
+ wxINT32_SWAP_ON_BE(dbuf
[2]);
868 stream
.Read(dbuf
, 4 * 2);
869 int width
= (int)wxINT32_SWAP_ON_BE(dbuf
[0]);
870 int height
= (int)wxINT32_SWAP_ON_BE(dbuf
[1]);
871 if ( !IsBmp
)height
= height
/ 2; // for icons divide by 2
876 wxLogError( _("DIB Header: Image width > 32767 pixels for file.") );
879 if ( height
> 32767 )
882 wxLogError( _("DIB Header: Image height > 32767 pixels for file.") );
886 stream
.Read(&aWord
, 2);
889 int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
891 stream
.Read(&aWord
, 2);
892 int bpp
= (int)wxUINT16_SWAP_ON_BE(aWord
);
893 if ( bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
!= 16 && bpp
!= 24 && bpp
!= 32 )
896 wxLogError( _("DIB Header: Unknown bitdepth in file.") );
900 stream
.Read(dbuf
, 4 * 4);
901 int comp
= (int)wxINT32_SWAP_ON_BE(dbuf
[0]);
902 if ( comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&&
903 comp
!= BI_BITFIELDS
)
906 wxLogError( _("DIB Header: Unknown encoding in file.") );
910 stream
.Read(dbuf
, 4 * 2);
911 int ncolors
= (int)wxINT32_SWAP_ON_BE( dbuf
[0] );
914 /* some more sanity checks */
915 if (((comp
== BI_RLE4
) && (bpp
!= 4)) ||
916 ((comp
== BI_RLE8
) && (bpp
!= 8)) ||
917 ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
920 wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") );
924 //read DIB; this is the BMP image or the XOR part of an icon image
925 if ( !DoLoadDib(image
, width
, height
, bpp
, ncolors
, comp
, offset
, stream
,
926 verbose
, IsBmp
, TRUE
) )
929 wxLogError( _("Error in reading image DIB .") );
935 //read Icon mask which is monochrome
936 //there is no palette, so we will create one
938 if ( !DoLoadDib(&mask
, width
, height
, 1, 2, BI_RGB
, offset
, stream
,
939 verbose
, IsBmp
, FALSE
) )
942 wxLogError( _("ICO: Error in reading mask DIB.") );
945 image
->SetMaskFromImage(mask
, 255, 255, 255);
952 bool wxBMPHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
953 bool verbose
, int WXUNUSED(index
))
955 // Read a single DIB fom the file:
956 return LoadDib(image
, stream
, verbose
, TRUE
/*isBmp*/);
959 bool wxBMPHandler::DoCanRead(wxInputStream
& stream
)
961 unsigned char hdr
[2];
963 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
966 // do we have the BMP file signature?
967 return hdr
[0] == 'B' && hdr
[1] == 'M';
970 #endif // wxUSE_STREAMS
974 //-----------------------------------------------------------------------------
976 //-----------------------------------------------------------------------------
978 IMPLEMENT_DYNAMIC_CLASS(wxICOHandler
, wxBMPHandler
)
984 wxUint8 bWidth
; // Width of the image
985 wxUint8 bHeight
; // Height of the image (times 2)
986 wxUint8 bColorCount
; // Number of colors in image (0 if >=8bpp)
987 wxUint8 bReserved
; // Reserved
989 // these two are different in icons and cursors:
991 wxUint16 wPlanes
; // Color Planes or XHotSpot
992 wxUint16 wBitCount
; // Bits per pixel or YHotSpot
994 wxUint32 dwBytesInRes
; // how many bytes in this resource?
995 wxUint32 dwImageOffset
; // where in the file is this image
1000 wxUint16 idReserved
; // Reserved
1001 wxUint16 idType
; // resource type (1 for icons, 2 for cursors)
1002 wxUint16 idCount
; // how many images?
1006 bool wxICOHandler::SaveFile(wxImage
*image
,
1007 wxOutputStream
& stream
,
1011 bool bResult
= FALSE
;
1012 //sanity check; icon must be less than 127 pixels high and 255 wide
1013 if ( image
->GetHeight () > 127 )
1016 wxLogError(_("ICO: Image too tall for an icon."));
1019 if ( image
->GetWidth () > 255 )
1022 wxLogError(_("ICO: Image too wide for an icon."));
1026 int images
= 1; // only generate one image
1028 // VS: This is a hack of sort - since ICO and CUR files are almost
1029 // identical, we have all the meat in wxICOHandler and check for
1030 // the actual (handler) type when the code has to distinguish between
1032 int type
= (this->GetType() == wxBITMAP_TYPE_CUR
) ? 2 : 1;
1034 // write a header, (ICONDIR)
1035 // Calculate the header size
1036 wxUint32 offset
= 3 * sizeof(wxUint16
);
1039 IconDir
.idReserved
= 0;
1040 IconDir
.idType
= wxUINT16_SWAP_ON_BE(type
);
1041 IconDir
.idCount
= wxUINT16_SWAP_ON_BE(images
);
1042 stream
.Write(&IconDir
.idReserved
, sizeof(IconDir
.idReserved
));
1043 stream
.Write(&IconDir
.idType
, sizeof(IconDir
.idType
));
1044 stream
.Write(&IconDir
.idCount
, sizeof(IconDir
.idCount
));
1045 if ( !stream
.IsOk() )
1048 wxLogError(_("ICO: Error writing the image file!"));
1052 // for each iamage write a description ICONDIRENTRY:
1053 ICONDIRENTRY icondirentry
;
1054 for (int i
= 0; i
< images
; i
++)
1058 if ( image
->HasMask() )
1060 // make another image with black/white:
1061 mask
= image
->ConvertToMono (image
->GetMaskRed(), image
->GetMaskGreen(), image
->GetMaskBlue() );
1063 // now we need to change the masked regions to black:
1064 unsigned char r
= image
->GetMaskRed();
1065 unsigned char g
= image
->GetMaskGreen();
1066 unsigned char b
= image
->GetMaskBlue();
1067 if ( (r
!= 0) || (g
!= 0) || (b
!= 0) )
1069 // Go round and apply black to the masked bits:
1071 for (i
= 0; i
< mask
.GetWidth(); i
++)
1073 for (j
= 0; j
< mask
.GetHeight(); j
++)
1075 if ((r
== mask
.GetRed(i
, j
)) &&
1076 (g
== mask
.GetGreen(i
, j
))&&
1077 (b
== mask
.GetBlue(i
, j
)) )
1078 image
->SetRGB(i
, j
, 0, 0, 0 );
1085 // just make a black mask all over:
1086 mask
= image
->Copy();
1088 for (i
= 0; i
< mask
.GetWidth(); i
++)
1089 for (j
= 0; j
< mask
.GetHeight(); j
++)
1090 mask
.SetRGB(i
, j
, 0, 0, 0 );
1092 // Set the formats for image and mask
1093 // (Windows never saves with more than 8 colors):
1094 image
->SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_8BPP
);
1096 // monochome bitmap:
1097 mask
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_1BPP_BW
);
1099 bool IsMask
= FALSE
;
1101 //calculate size and offset of image and mask
1102 wxCountingOutputStream cStream
;
1103 bResult
= SaveDib(image
, cStream
, verbose
, IsBmp
, IsMask
);
1107 wxLogError(_("ICO: Error writing the image file!"));
1112 bResult
= SaveDib(&mask
, cStream
, verbose
, IsBmp
, IsMask
);
1116 wxLogError(_("ICO: Error writing the image file!"));
1119 wxUint32 Size
= cStream
.GetSize();
1121 // wxCountingOutputStream::Ok() always returns TRUE for now and this
1122 // "if" provokes VC++ warnings in optimized build
1124 if ( !cStream
.Ok() )
1127 wxLogError(_("ICO: Error writing the image file!"));
1132 offset
= offset
+ sizeof(ICONDIRENTRY
);
1134 icondirentry
.bWidth
= image
->GetWidth();
1135 icondirentry
.bHeight
= 2 * image
->GetHeight();
1136 icondirentry
.bColorCount
= 0;
1137 icondirentry
.bReserved
= 0;
1138 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE(1);
1139 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE(wxBMP_8BPP
);
1140 if ( type
== 2 /*CUR*/)
1142 int hx
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ?
1143 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) :
1144 image
->GetWidth() / 2;
1145 int hy
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ?
1146 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) :
1147 image
->GetHeight() / 2;
1149 // actually write the values of the hot spot here:
1150 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE((wxUint16
)hx
);
1151 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE((wxUint16
)hy
);
1153 icondirentry
.dwBytesInRes
= wxUINT32_SWAP_ON_BE(Size
);
1154 icondirentry
.dwImageOffset
= wxUINT32_SWAP_ON_BE(offset
);
1156 // increase size to allow for the data written:
1160 stream
.Write(&icondirentry
.bWidth
, sizeof(icondirentry
.bWidth
));
1161 stream
.Write(&icondirentry
.bHeight
, sizeof(icondirentry
.bHeight
));
1162 stream
.Write(&icondirentry
.bColorCount
, sizeof(icondirentry
.bColorCount
));
1163 stream
.Write(&icondirentry
.bReserved
, sizeof(icondirentry
.bReserved
));
1164 stream
.Write(&icondirentry
.wPlanes
, sizeof(icondirentry
.wPlanes
));
1165 stream
.Write(&icondirentry
.wBitCount
, sizeof(icondirentry
.wBitCount
));
1166 stream
.Write(&icondirentry
.dwBytesInRes
, sizeof(icondirentry
.dwBytesInRes
));
1167 stream
.Write(&icondirentry
.dwImageOffset
, sizeof(icondirentry
.dwImageOffset
));
1168 if ( !stream
.IsOk() )
1171 wxLogError(_("ICO: Error writing the image file!"));
1175 // actually save it:
1177 bResult
= SaveDib(image
, stream
, verbose
, IsBmp
, IsMask
);
1181 wxLogError(_("ICO: Error writing the image file!"));
1186 bResult
= SaveDib(&mask
, stream
, verbose
, IsBmp
, IsMask
);
1190 wxLogError(_("ICO: Error writing the image file!"));
1194 } // end of for loop
1199 bool wxICOHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1200 bool verbose
, int index
)
1203 return DoLoadFile(image
, stream
, verbose
, index
);
1206 bool wxICOHandler::DoLoadFile(wxImage
*image
, wxInputStream
& stream
,
1207 bool WXUNUSED(verbose
), int index
)
1209 bool bResult
= FALSE
;
1214 off_t iPos
= stream
.TellI();
1215 stream
.Read(&IconDir
, sizeof(IconDir
));
1216 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1217 // nType is 1 for Icons, 2 for Cursors:
1218 wxUint16 nType
= wxUINT16_SWAP_ON_BE(IconDir
.idType
);
1220 // loop round the icons and choose the best one:
1221 ICONDIRENTRY
*pIconDirEntry
= new ICONDIRENTRY
[nIcons
];
1222 ICONDIRENTRY
*pCurrentEntry
= pIconDirEntry
;
1225 int iSel
= wxNOT_FOUND
;
1227 for (int i
= 0; i
< nIcons
; i
++ )
1229 stream
.Read(pCurrentEntry
, sizeof(ICONDIRENTRY
));
1230 // bHeight and bColorCount are wxUint8
1231 if ( pCurrentEntry
->bWidth
>= wMax
)
1233 // see if we have more colors, ==0 indicates > 8bpp:
1234 if ( pCurrentEntry
->bColorCount
== 0 )
1235 pCurrentEntry
->bColorCount
= 255;
1236 if ( pCurrentEntry
->bColorCount
>= colmax
)
1239 wMax
= pCurrentEntry
->bWidth
;
1240 colmax
= pCurrentEntry
->bColorCount
;
1248 // VS: Note that we *have* to run the loop above even if index != -1, because
1249 // it reads ICONDIRENTRies.
1253 if ( iSel
== wxNOT_FOUND
|| iSel
< 0 || iSel
>= nIcons
)
1255 wxLogError(_("ICO: Invalid icon index."));
1260 // seek to selected icon:
1261 pCurrentEntry
= pIconDirEntry
+ iSel
;
1262 stream
.SeekI(iPos
+ wxUINT32_SWAP_ON_BE(pCurrentEntry
->dwImageOffset
), wxFromStart
);
1263 bResult
= LoadDib(image
, stream
, TRUE
, IsBmp
);
1264 bool bIsCursorType
= (this->GetType() == wxBITMAP_TYPE_CUR
) || (this->GetType() == wxBITMAP_TYPE_ANI
);
1265 if ( bResult
&& bIsCursorType
&& nType
== 2 )
1267 // it is a cursor, so let's set the hotspot:
1268 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wPlanes
));
1269 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wBitCount
));
1272 delete[] pIconDirEntry
;
1276 int wxICOHandler::GetImageCount(wxInputStream
& stream
)
1279 off_t iPos
= stream
.TellI();
1281 stream
.Read(&IconDir
, sizeof(IconDir
));
1282 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1287 bool wxICOHandler::DoCanRead(wxInputStream
& stream
)
1290 unsigned char hdr
[4];
1291 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
1294 // hdr[2] is one for an icon and two for a cursor
1295 return hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\1' && hdr
[3] == '\0';
1298 #endif // wxUSE_STREAMS
1301 //-----------------------------------------------------------------------------
1303 //-----------------------------------------------------------------------------
1305 IMPLEMENT_DYNAMIC_CLASS(wxCURHandler
, wxICOHandler
)
1309 bool wxCURHandler::DoCanRead(wxInputStream
& stream
)
1312 unsigned char hdr
[4];
1313 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
1316 // hdr[2] is one for an icon and two for a cursor
1317 return hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\2' && hdr
[3] == '\0';
1320 #endif // wxUSE_STREAMS
1322 //-----------------------------------------------------------------------------
1324 //-----------------------------------------------------------------------------
1326 IMPLEMENT_DYNAMIC_CLASS(wxANIHandler
, wxCURHandler
)
1330 bool wxANIHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1331 bool verbose
, int index
)
1337 memcpy( &riff32
, "RIFF", 4 );
1339 memcpy( &list32
, "LIST", 4 );
1341 memcpy( &ico32
, "icon", 4 );
1345 stream
.Read(&FCC1
, 4);
1346 if ( FCC1
!= riff32
)
1349 // we have a riff file:
1350 while (stream
.IsOk())
1352 // we always have a data size
1353 stream
.Read(&datalen
, 4);
1354 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
1355 //data should be padded to make even number of bytes
1356 if (datalen
% 2 == 1) datalen
++ ;
1357 //now either data or a FCC
1358 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
1360 stream
.Read(&FCC2
, 4);
1364 if (FCC1
== ico32
&& iIcon
>= index
)
1366 return DoLoadFile(image
, stream
, verbose
, -1);
1370 stream
.SeekI(stream
.TellI() + datalen
);
1371 if ( FCC1
== ico32
)
1376 // try to read next data chunk:
1377 stream
.Read(&FCC1
, 4);
1382 bool wxANIHandler::DoCanRead(wxInputStream
& stream
)
1388 memcpy( &riff32
, "RIFF", 4 );
1390 memcpy( &list32
, "LIST", 4 );
1392 memcpy( &ico32
, "icon", 4 );
1394 memcpy( &anih32
, "anih", 4 );
1397 if ( !stream
.Read(&FCC1
, 4) )
1400 if ( FCC1
!= riff32
)
1403 // we have a riff file:
1404 while ( stream
.IsOk() )
1406 if ( FCC1
== anih32
)
1408 // we always have a data size:
1409 stream
.Read(&datalen
, 4);
1410 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
1411 //data should be padded to make even number of bytes
1412 if (datalen
% 2 == 1) datalen
++ ;
1413 // now either data or a FCC:
1414 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
1416 stream
.Read(&FCC2
, 4);
1420 stream
.SeekI(stream
.TellI() + datalen
);
1423 // try to read next data chunk:
1424 if ( !stream
.Read(&FCC1
, 4) )
1426 // reading failed -- either EOF or IO error, bail out anyhow
1434 int wxANIHandler::GetImageCount(wxInputStream
& stream
)
1440 memcpy( &riff32
, "RIFF", 4 );
1442 memcpy( &list32
, "LIST", 4 );
1444 memcpy( &ico32
, "icon", 4 );
1446 memcpy( &anih32
, "anih", 4 );
1449 stream
.Read(&FCC1
, 4);
1450 if ( FCC1
!= riff32
)
1453 // we have a riff file:
1454 while ( stream
.IsOk() )
1456 // we always have a data size:
1457 stream
.Read(&datalen
, 4);
1458 datalen
= wxINT32_SWAP_ON_BE(datalen
) ;
1459 //data should be padded to make even number of bytes
1460 if (datalen
% 2 == 1) datalen
++ ;
1461 // now either data or a FCC:
1462 if ( (FCC1
== riff32
) || (FCC1
== list32
) )
1464 stream
.Read(&FCC2
, 4);
1468 if ( FCC1
== anih32
)
1470 wxUint32
*pData
= new wxUint32
[datalen
/4];
1471 stream
.Read(pData
, datalen
);
1472 int nIcons
= wxINT32_SWAP_ON_BE(*(pData
+ 1));
1477 stream
.SeekI(stream
.TellI() + datalen
);
1480 // try to read next data chunk:
1481 stream
.Read(&FCC1
, 4);
1487 #endif // wxUSE_STREAMS
1489 #endif // wxUSE_ICO_CUR
1491 #endif // wxUSE_IMAGE