1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage BMP handler
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
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"
49 //-----------------------------------------------------------------------------
50 // wxBMPHandler & wxICOHandler
51 //-----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
66 #define BI_BITFIELDS 3
69 #define poffset (line * width * 3 + column * 3)
75 wxUint8 bWidth
; // Width of the image
76 wxUint8 bHeight
; // Height of the image (times 2)
77 wxUint8 bColorCount
; // Number of colors in image (0 if >=8bpp)
78 wxUint8 bReserved
; // Reserved
79 wxUint16 wPlanes
; // Color Planes
80 wxUint16 wBitCount
; // Bits per pixel
81 wxUint32 dwBytesInRes
; // how many bytes in this resource?
82 wxUint32 dwImageOffset
; // where in the file is this image
88 wxUint16 idReserved
; // Reserved
89 wxUint16 idType
; // resource type (1 for icons)
90 wxUint16 idCount
; // how many images?
93 bool wxICOHandler::SaveFile(wxImage
*image
,
94 wxOutputStream
& stream
,
98 bool bResult
= FALSE
;
99 //sanity check; icon must be less than 127 pixels high and 255 wide
100 if (image
-> GetHeight () > 127 )
103 wxLogError( _("ICO: Error Image too tall for an icon.") );
106 if (image
-> GetWidth () > 255 )
109 wxLogError( _("ICO: Error Image too wide for an icon.") );
113 // only generate one image
116 // write a header, (ICONDIR)
117 // Calculate the header size
118 wxUint32 m_offset
= 3 * sizeof(wxUint16
);
121 m_IconDir
.idReserved
= 0 ;
122 m_IconDir
.idType
= wxUINT16_SWAP_ON_BE (1);
123 m_IconDir
.idCount
= wxUINT16_SWAP_ON_BE (m_images
);
124 stream
.Write(&m_IconDir
.idReserved
, sizeof(m_IconDir
.idReserved
));
125 stream
.Write(&m_IconDir
.idType
, sizeof(m_IconDir
.idType
));
126 stream
.Write(&m_IconDir
.idCount
, sizeof(m_IconDir
.idCount
));
127 if ( !stream
.IsOk () )
130 wxLogError( _("ICO: Error writing ICONDIR header.") );
134 // for each iamage write a description ICONDIRENTRY
135 ICONDIRENTRY m_icondirentry
;
137 for ( i
= 0; i
< m_images
; i
++ )
140 if (image
->HasMask())
142 //make another image with black/white
143 mask
= image
-> ConvertToMono (image
->GetMaskRed(), image
->GetMaskGreen(), image
->GetMaskBlue() );
144 //now we need to change the masked regions to black
145 unsigned char r
= image
-> GetMaskRed() ;
146 unsigned char g
= image
-> GetMaskGreen() ;
147 unsigned char b
= image
-> GetMaskBlue() ;
148 if ((r
!= 0) || (g
!= 0) || (b
!= 0) )
150 //Go round and apply black to the masked bits
152 for (i
=0; i
< mask
.GetWidth(); i
++)
153 for (j
=0; j
< mask
.GetHeight(); j
++)
155 if ((r
== mask
.GetRed(i
, j
)) &&
156 (g
== mask
.GetGreen(i
, j
) ) &&
157 (b
== mask
.GetBlue(i
, j
)) )
158 image
-> SetRGB ( i
, j
, 0, 0, 0 );
165 // just make a black mask all over
166 mask
= image
-> Copy ();
168 for (i
=0; i
< mask
.GetWidth(); i
++)
169 for (j
=0; j
< mask
.GetHeight(); j
++)
170 mask
.SetRGB ( i
, j
, 0, 0, 0 );
172 //Set the formats for image and mask
173 // windows never saves with more than 8 colors
174 image
-> SetOption (wxBMP_FORMAT
, wxBMP_8BPP
);
176 mask
. SetOption (wxBMP_FORMAT
, wxBMP_1BPP_BW
);
178 bool IsMask
= FALSE
;
180 //calculate size and offset of image and mask
181 wxCountingOutputStream cStream
;
182 bResult
= SaveDib ( image
, cStream
, verbose
, IsBmp
, IsMask
) ;
186 wxLogError( _("ICO: Error calculating size of XOR DIB .") );
190 bResult
= SaveDib ( &mask
, cStream
, verbose
, IsBmp
, IsMask
) ;
194 wxLogError( _("ICO: Error calculating size of Mask DIB .") );
197 wxUint32 m_Size
= cStream
.GetSize();
199 // wxCountingOutputStream::Ok() always returns TRUE for now and this
200 // "if" provokes VC++ warnings in optimized build
205 wxLogError( _("ICO: Error calculating size of DIB .") );
210 m_offset
= m_offset
+ sizeof(ICONDIRENTRY
) ;
212 m_icondirentry
. bWidth
= image
-> GetWidth () ;
213 m_icondirentry
. bHeight
= 2 * image
-> GetHeight () ;
214 m_icondirentry
. bColorCount
= 0 ;
215 m_icondirentry
. bReserved
= 0 ;
216 m_icondirentry
. wPlanes
= wxUINT16_SWAP_ON_BE(1);
217 m_icondirentry
. wBitCount
= wxUINT16_SWAP_ON_BE(wxBMP_8BPP
) ;
218 m_icondirentry
. dwBytesInRes
= wxUINT32_SWAP_ON_BE(m_Size
);
219 m_icondirentry
. dwImageOffset
= wxUINT32_SWAP_ON_BE(m_offset
);
221 //increase size to allow for the data wriitten
222 m_offset
= m_offset
+ m_Size
;
225 stream
.Write(&m_icondirentry
. bWidth
, sizeof(m_icondirentry
. bWidth
) );
226 stream
.Write(&m_icondirentry
. bHeight
, sizeof(m_icondirentry
. bHeight
) );
227 stream
.Write(&m_icondirentry
. bColorCount
, sizeof(m_icondirentry
. bColorCount
) );
228 stream
.Write(&m_icondirentry
. bReserved
, sizeof(m_icondirentry
. bReserved
) );
229 stream
.Write(&m_icondirentry
. wPlanes
, sizeof(m_icondirentry
. wPlanes
) );
230 stream
.Write(&m_icondirentry
. wBitCount
, sizeof(m_icondirentry
. wBitCount
) );
231 stream
.Write(&m_icondirentry
. dwBytesInRes
, sizeof(m_icondirentry
. dwBytesInRes
) );
232 stream
.Write(&m_icondirentry
. dwImageOffset
, sizeof(m_icondirentry
. dwImageOffset
) );
233 if ( !stream
.IsOk () )
236 wxLogError( _("ICO: Error writing ICONDIRENTRY header.") );
241 bResult
= SaveDib ( image
, stream
, verbose
, IsBmp
, IsMask
) ;
245 wxLogError( _("ICO: Error writing XOR DIB .") );
249 bResult
= SaveDib ( &mask
, stream
, verbose
, IsBmp
, IsMask
) ;
253 wxLogError( _("ICO: Error writing Mask DIB .") );
262 bool wxBMPHandler::SaveFile(wxImage
*image
,
263 wxOutputStream
& stream
,
267 bool IsMask
= FALSE
;
268 return SaveDib( image
, stream
, verbose
, IsBmp
, IsMask
) ;
271 bool wxBMPHandler::SaveDib(wxImage
*image
,
272 wxOutputStream
& stream
,
278 wxCHECK_MSG( image
, FALSE
, _T("invalid pointer in wxBMPHandler::SaveFile") );
282 if (verbose
) wxLogError(_("BMP: Couldn't save invalid image."));
286 // get the format of the BMP file to save, else use 24bpp
287 unsigned format
= wxBMP_24BPP
;
288 if (image
->HasOption(wxBMP_FORMAT
))
289 format
= image
->GetOptionInt(wxBMP_FORMAT
);
291 unsigned bpp
; // # of bits per pixel
292 int palette_size
; // # of color map entries, ie. 2^bpp colors
294 // set the bpp and appropriate palette_size, and do additional checks
295 if ((format
== wxBMP_1BPP
) || (format
== wxBMP_1BPP_BW
))
300 else if (format
== wxBMP_4BPP
)
305 else if ((format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_GREY
) ||
306 (format
== wxBMP_8BPP_RED
) || (format
== wxBMP_8BPP_PALETTE
))
308 // need to set a wxPalette to use this, HOW TO CHECK IF VALID, SIZE?
309 if ((format
== wxBMP_8BPP_PALETTE
)
311 && !image
->HasPalette()
312 #endif // wxUSE_PALETTE
316 wxLogError(_("BMP: wImage doesn't have own wxPalette."));
322 else // you get 24bpp
324 format
= wxBMP_24BPP
;
329 unsigned width
= image
->GetWidth();
330 unsigned row_padding
= (4 - int(width
*bpp
/8.0) % 4) % 4; // # bytes to pad to dword
331 unsigned row_width
= int(width
* bpp
/8.0) + row_padding
; // # of bytes per row
336 wxUint16 magic
; // format magic, always 'BM'
337 wxUint32 filesize
; // total file size, inc. headers
338 wxUint32 reserved
; // for future use
339 wxUint32 data_offset
; // image data offset in the file
342 wxUint32 bih_size
; // 2nd part's size
343 wxUint32 width
, height
; // bitmap's dimensions
344 wxUint16 planes
; // num of planes
345 wxUint16 bpp
; // bits per pixel
346 wxUint32 compression
; // compression method
347 wxUint32 size_of_bmp
; // size of the bitmap
348 wxUint32 h_res
, v_res
; // image resolution in dpi
349 wxUint32 num_clrs
; // number of colors used
350 wxUint32 num_signif_clrs
;// number of significant colors
353 wxUint32 hdr_size
= 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/;
355 hdr
.magic
= wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/);
356 hdr
.filesize
= wxUINT32_SWAP_ON_BE( hdr_size
+ palette_size
*4 +
357 row_width
* image
->GetHeight() );
359 hdr
.data_offset
= wxUINT32_SWAP_ON_BE(hdr_size
+ palette_size
*4);
361 hdr
.bih_size
= wxUINT32_SWAP_ON_BE(hdr_size
- 14);
362 hdr
.width
= wxUINT32_SWAP_ON_BE(image
->GetWidth());
365 hdr
.height
= wxUINT32_SWAP_ON_BE(image
->GetHeight());
369 hdr
.height
= wxUINT32_SWAP_ON_BE(2 * image
->GetHeight());
371 hdr
.planes
= wxUINT16_SWAP_ON_BE(1); // always 1 plane
372 hdr
.bpp
= wxUINT16_SWAP_ON_BE(bpp
);
373 hdr
.compression
= 0; // RGB uncompressed
374 hdr
.size_of_bmp
= wxUINT32_SWAP_ON_BE(row_width
* image
->GetHeight());
375 hdr
.h_res
= hdr
.v_res
= wxUINT32_SWAP_ON_BE(72); // 72dpi is standard
376 hdr
.num_clrs
= wxUINT32_SWAP_ON_BE(palette_size
); // # colors in colormap
377 hdr
.num_signif_clrs
= 0; // all colors are significant
381 if (// VS: looks ugly but compilers tend to do ugly things with structs,
382 // like aligning hdr.filesize's ofset to dword :(
383 // VZ: we should add padding then...
384 !stream
.Write(&hdr
.magic
, 2) ||
385 !stream
.Write(&hdr
.filesize
, 4) ||
386 !stream
.Write(&hdr
.reserved
, 4) ||
387 !stream
.Write(&hdr
.data_offset
, 4)
391 wxLogError(_("BMP: Couldn't write the file (Bitmap) header."));
398 !stream
.Write(&hdr
.bih_size
, 4) ||
399 !stream
.Write(&hdr
.width
, 4) ||
400 !stream
.Write(&hdr
.height
, 4) ||
401 !stream
.Write(&hdr
.planes
, 2) ||
402 !stream
.Write(&hdr
.bpp
, 2) ||
403 !stream
.Write(&hdr
.compression
, 4) ||
404 !stream
.Write(&hdr
.size_of_bmp
, 4) ||
405 !stream
.Write(&hdr
.h_res
, 4) ||
406 !stream
.Write(&hdr
.v_res
, 4) ||
407 !stream
.Write(&hdr
.num_clrs
, 4) ||
408 !stream
.Write(&hdr
.num_signif_clrs
, 4)
412 wxLogError(_("BMP: Couldn't write the file (BitmapInfo) header."));
417 wxPalette
*palette
= NULL
; // entries for quantized images
418 wxUint8
*rgbquad
= NULL
; // for the RGBQUAD bytes for the colormap
419 wxImage
*q_image
= NULL
; // destination for quantized image
421 // if <24bpp use quantization to reduce colors for *some* of the formats
422 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_4BPP
) ||
423 (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_PALETTE
))
425 // make a new palette and quantize the image
426 if (format
!= wxBMP_8BPP_PALETTE
)
428 q_image
= new wxImage();
430 // I get a delete error using Quantize when desired colors > 236
431 int quantize
= ((palette_size
> 236) ? 236 : palette_size
);
432 // fill the destination too, it gives much nicer 4bpp images
433 wxQuantize::Quantize( *image
, *q_image
, &palette
, quantize
, 0,
434 wxQUANTIZE_FILL_DESTINATION_IMAGE
);
439 palette
= new wxPalette(image
->GetPalette());
440 #endif // wxUSE_PALETTE
444 unsigned char r
, g
, b
;
445 rgbquad
= new wxUint8
[palette_size
*4];
447 for (i
=0; i
<palette_size
; i
++)
450 if (!palette
->GetRGB( i
, &r
, &g
, &b
))
451 #endif // wxUSE_PALETTE
460 // make a 256 entry greyscale colormap or 2 entry black & white
461 else if ((format
== wxBMP_8BPP_GREY
) || (format
== wxBMP_8BPP_RED
) ||
462 (format
== wxBMP_1BPP_BW
))
465 rgbquad
= new wxUint8
[palette_size
*4];
467 for (i
=0; i
<palette_size
; i
++)
469 // if 1BPP_BW then just 0 and 255 then exit
470 if (( i
> 0) && (format
== wxBMP_1BPP_BW
)) i
= 255;
478 // if the colormap was made, then it needs to be written
483 if (!stream
.Write(rgbquad
, palette_size
*4))
486 wxLogError(_("BMP: Couldn't write RGB color map."));
490 #endif // wxUSE_PALETTE
498 // pointer to the image data, use quantized if available
499 wxUint8
*data
= (wxUint8
*) image
->GetData();
500 if (q_image
) if (q_image
->Ok()) data
= (wxUint8
*) q_image
->GetData();
502 wxUint8
*buffer
= new wxUint8
[row_width
];
503 memset(buffer
, 0, row_width
);
507 for (y
= image
->GetHeight() -1 ; y
>= 0; y
--)
509 if (format
== wxBMP_24BPP
) // 3 bytes per pixel red,green,blue
511 for (x
= 0; x
< width
; x
++)
513 pixel
= 3*(y
*width
+ x
);
515 buffer
[3*x
] = data
[pixel
+2];
516 buffer
[3*x
+ 1] = data
[pixel
+1];
517 buffer
[3*x
+ 2] = data
[pixel
];
520 else if ((format
== wxBMP_8BPP
) || // 1 byte per pixel in color
521 (format
== wxBMP_8BPP_PALETTE
))
523 for (x
= 0; x
< width
; x
++)
525 pixel
= 3*(y
*width
+ x
);
527 buffer
[x
] = palette
->GetPixel( data
[pixel
],
531 // FIXME: what should this be? use some std palette maybe?
533 #endif // wxUSE_PALETTE
536 else if (format
== wxBMP_8BPP_GREY
) // 1 byte per pix, rgb ave to grey
538 for (x
= 0; x
< width
; x
++)
540 pixel
= 3*(y
*width
+ x
);
541 buffer
[x
] = (wxUint8
)(.299*data
[pixel
] +
546 else if (format
== wxBMP_8BPP_RED
) // 1 byte per pixel, red as greys
548 for (x
= 0; x
< width
; x
++)
550 buffer
[x
] = (wxUint8
)data
[3*(y
*width
+ x
)];
553 else if (format
== wxBMP_4BPP
) // 4 bpp in color
555 for (x
= 0; x
< width
; x
+=2)
557 pixel
= 3*(y
*width
+ x
);
559 // fill buffer, ignore if > width
562 ((wxUint8
)palette
->GetPixel(data
[pixel
],
564 data
[pixel
+2]) << 4) |
567 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3],
571 // FIXME: what should this be? use some std palette maybe?
573 #endif // wxUSE_PALETTE
576 else if (format
== wxBMP_1BPP
) // 1 bpp in "color"
578 for (x
= 0; x
< width
; x
+=8)
580 pixel
= 3*(y
*width
+ x
);
583 buffer
[x
/8] = ((wxUint8
)palette
->GetPixel(data
[pixel
], data
[pixel
+1], data
[pixel
+2]) << 7) |
584 (((x
+1) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3], data
[pixel
+4], data
[pixel
+5]) << 6)) |
585 (((x
+2) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+6], data
[pixel
+7], data
[pixel
+8]) << 5)) |
586 (((x
+3) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+9], data
[pixel
+10], data
[pixel
+11]) << 4)) |
587 (((x
+4) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+12], data
[pixel
+13], data
[pixel
+14]) << 3)) |
588 (((x
+5) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+15], data
[pixel
+16], data
[pixel
+17]) << 2)) |
589 (((x
+6) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+18], data
[pixel
+19], data
[pixel
+20]) << 1)) |
590 (((x
+7) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+21], data
[pixel
+22], data
[pixel
+23]) ));
592 // FIXME: what should this be? use some std palette maybe?
594 #endif // wxUSE_PALETTE
597 else if (format
== wxBMP_1BPP_BW
) // 1 bpp B&W colormap from red color ONLY
599 for (x
= 0; x
< width
; x
+=8)
601 pixel
= 3*(y
*width
+ x
);
604 (((wxUint8
)(data
[pixel
] /128.)) << 7) |
605 ( ((x
+1) > width
) ? 0 : (((wxUint8
)(data
[pixel
+3] /128.)) << 6)) |
606 ( ((x
+2) > width
) ? 0 : (((wxUint8
)(data
[pixel
+6] /128.)) << 5)) |
607 ( ((x
+3) > width
) ? 0 : (((wxUint8
)(data
[pixel
+9] /128.)) << 4)) |
608 ( ((x
+4) > width
) ? 0 : (((wxUint8
)(data
[pixel
+12]/128.)) << 3)) |
609 ( ((x
+5) > width
) ? 0 : (((wxUint8
)(data
[pixel
+15]/128.)) << 2)) |
610 ( ((x
+6) > width
) ? 0 : (((wxUint8
)(data
[pixel
+18]/128.)) << 1)) |
611 ( ((x
+7) > width
) ? 0 : (((wxUint8
)(data
[pixel
+21]/128.)) ));
615 if (!stream
.Write(buffer
, row_width
))
618 wxLogError(_("BMP: Couldn't write data."));
622 #endif // wxUSE_PALETTE
630 #endif // wxUSE_PALETTE
639 bool wxBMPHandler::DoLoadDib (wxImage
* image
, int width
, int height
, int bpp
, int ncolors
, int comp
,
640 off_t bmpOffset
, wxInputStream
& stream
,
641 bool verbose
, bool IsBmp
, bool hasPalette
)
644 wxInt32 aDword
, rmask
= 0, gmask
= 0, bmask
= 0;
645 int rshift
= 0, gshift
= 0, bshift
= 0;
652 // allocate space for palette if needed
655 unsigned char r
, g
, b
;
661 cmap
= (struct _cmap
*)malloc(sizeof(struct _cmap
) * ncolors
);
665 wxLogError( _("Loading DIB : Couldn't allocate memory.") );
672 // destroy existing here instead of
674 image
->Create( width
, height
);
675 unsigned char *ptr
= image
->GetData();
679 wxLogError( _("Loading DIB : Couldn't allocate memory.") );
685 * Reading the palette, if it exists.
687 if (bpp
< 16 && ncolors
!= 0)
689 unsigned char* r
= new unsigned char[ncolors
];
690 unsigned char* g
= new unsigned char[ncolors
];
691 unsigned char* b
= new unsigned char[ncolors
];
692 for (int j
= 0; j
< ncolors
; j
++)
696 stream
.Read( bbuf
, 4 );
707 //used in reading .ico file mask
708 r
[j
] = cmap
[j
].r
= j
* 255;
709 g
[j
] = cmap
[j
].g
= j
* 255;
710 b
[j
] = cmap
[j
].b
= j
* 255;
715 // Set the palette for the wxImage
716 image
->SetPalette(wxPalette(ncolors
, r
, g
, b
));
717 #endif // wxUSE_PALETTE
723 else if (bpp
== 16 || bpp
== 32)
725 if (comp
== BI_BITFIELDS
)
728 stream
.Read( dbuf
, 4 * 3 );
729 bmask
= wxINT32_SWAP_ON_BE( dbuf
[0] );
730 gmask
= wxINT32_SWAP_ON_BE( dbuf
[1] );
731 rmask
= wxINT32_SWAP_ON_BE( dbuf
[2] );
732 /* find shift amount.. ugly, but i can't think of a better way */
733 for (bit
= 0; bit
< bpp
; bit
++)
735 if (bmask
& (1 << bit
))
737 if (gmask
& (1 << bit
))
739 if (rmask
& (1 << bit
))
764 * Reading the image data
766 if ( IsBmp
) stream
.SeekI( bmpOffset
); // else icon, just carry on
768 unsigned char *data
= ptr
;
770 /* set the whole image to the background color */
771 if (bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
))
773 for (int i
= 0; i
< width
* height
; i
++)
784 int linesize
= ((width
* bpp
+ 31) / 32) * 4;
786 /* BMPs are stored upside down */
787 for (line
= (height
- 1); line
>= 0; line
--)
790 for (column
= 0; column
< width
;)
796 aByte
= stream
.GetC();
800 for (bit
= 0; bit
< 8 && column
< width
; bit
++)
802 index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
803 ptr
[poffset
] = cmap
[index
].r
;
804 ptr
[poffset
+ 1] = cmap
[index
].g
;
805 ptr
[poffset
+ 2] = cmap
[index
].b
;
814 wxLogError( _("DIB Header: Cannot deal with 4bit encoded yet.") );
822 for (nibble
= 0; nibble
< 2 && column
< width
; nibble
++)
824 index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
827 ptr
[poffset
] = cmap
[index
].r
;
828 ptr
[poffset
+ 1] = cmap
[index
].g
;
829 ptr
[poffset
+ 2] = cmap
[index
].b
;
840 aByte
= stream
.GetC();
845 /* column = width; */
854 aByte
= stream
.GetC();
856 linepos
= column
* bpp
/ 8;
857 aByte
= stream
.GetC();
862 int absolute
= aByte
;
863 for (int k
= 0; k
< absolute
; k
++)
866 aByte
= stream
.GetC();
867 ptr
[poffset
] = cmap
[aByte
].r
;
868 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
869 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
873 aByte
= stream
.GetC();
878 for (int l
= 0; l
< first
&& column
< width
; l
++)
880 ptr
[poffset
] = cmap
[aByte
].r
;
881 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
882 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
890 ptr
[poffset
] = cmap
[aByte
].r
;
891 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
892 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
894 // linepos += size; seems to be wrong, RR
900 stream
.Read( bbuf
, 3 );
902 ptr
[poffset
] = (unsigned char)bbuf
[2];
903 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
904 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
910 stream
.Read( &aWord
, 2 );
911 aWord
= wxUINT16_SWAP_ON_BE( aWord
);
913 temp
= (aWord
& rmask
) >> rshift
;
915 temp
= (aWord
& gmask
) >> gshift
;
916 ptr
[poffset
+ 1] = temp
;
917 temp
= (aWord
& bmask
) >> bshift
;
918 ptr
[poffset
+ 2] = temp
;
924 stream
.Read( &aDword
, 4 );
925 aDword
= wxINT32_SWAP_ON_BE( aDword
);
927 temp
= (aDword
& rmask
) >> rshift
;
929 temp
= (aDword
& gmask
) >> gshift
;
930 ptr
[poffset
+ 1] = temp
;
931 temp
= (aDword
& bmask
) >> bshift
;
932 ptr
[poffset
+ 2] = temp
;
936 while ((linepos
< linesize
) && (comp
!= 1) && (comp
!= 2))
938 stream
.Read( &aByte
, 1 );
940 if (stream
.LastError() != wxStream_NOERROR
)
947 image
->SetMask( FALSE
);
949 return stream
.IsOk();
953 bool wxBMPHandler::LoadDib( wxImage
*image
, wxInputStream
& stream
, bool verbose
, bool IsBmp
)
960 offset
= 0; // keep gcc quiet
963 // read the header off the .BMP format file
965 offset
= stream
.TellI();
966 if (offset
== wxInvalidOffset
) offset
= 0;
968 stream
.Read( bbuf
, 2 );
970 stream
.Read( dbuf
, 16 );
974 stream
.Read( dbuf
, 4 );
977 wxInt32 size
= wxINT32_SWAP_ON_BE( dbuf
[0] );
979 offset
= offset
+ wxINT32_SWAP_ON_BE( dbuf
[2] );
981 stream
.Read(dbuf
, 4 * 2);
982 int width
= (int)wxINT32_SWAP_ON_BE( dbuf
[0] );
983 int height
= (int)wxINT32_SWAP_ON_BE( dbuf
[1] );
984 if ( !IsBmp
) height
= height
/ 2; // for icons divide by 2
989 wxLogError( _("DIB Header: Image width > 32767 pixels for file.") );
995 wxLogError( _("DIB Header: Image height > 32767 pixels for file.") );
999 stream
.Read( &aWord
, 2 );
1002 int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
1004 stream
.Read( &aWord
, 2 );
1005 int bpp
= (int)wxUINT16_SWAP_ON_BE( aWord
);
1006 if (bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
!= 16 && bpp
!= 24 && bpp
!= 32)
1009 wxLogError( _("DIB Header: Unknown bitdepth in file.") );
1013 stream
.Read( dbuf
, 4 * 4 );
1014 int comp
= (int)wxINT32_SWAP_ON_BE( dbuf
[0] );
1015 if (comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&& comp
!= BI_BITFIELDS
)
1018 wxLogError( _("DIB Header: Unknown encoding in file.") );
1022 stream
.Read( dbuf
, 4 * 2 );
1023 int ncolors
= (int)wxINT32_SWAP_ON_BE( dbuf
[0] );
1026 /* some more sanity checks */
1027 if (((comp
== BI_RLE4
) && (bpp
!= 4)) ||
1028 ((comp
== BI_RLE8
) && (bpp
!= 8)) ||
1029 ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
1032 wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") );
1036 //read DIB; this is the BMP image or the XOR part of an icon image
1037 if (!DoLoadDib (image
, width
, height
, bpp
, ncolors
, comp
, offset
, stream
,
1038 verbose
, IsBmp
, TRUE
) )
1041 wxLogError( _("Error in reading image DIB .") );
1047 //read Icon mask which is monochrome
1048 //there is no palette, so we will create one
1050 if (!DoLoadDib (&mask
, width
, height
, 1, 2, BI_RGB
, offset
, stream
,
1051 verbose
, IsBmp
, FALSE
) )
1054 wxLogError( _("ICO: Error in reading mask DIB.") );
1057 image
->SetMaskFromImage(mask
, 255, 255, 255);
1064 bool wxBMPHandler::LoadFile ( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
1067 //Read a single DIB fom the file
1068 return LoadDib ( image
, stream
, verbose
, IsBmp
) ;
1073 bool wxICOHandler::LoadFile ( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
1075 bool bResult
= FALSE
;
1079 stream
.Read (&m_IconDir
, sizeof(m_IconDir
));
1080 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE ( m_IconDir
.idCount
) ;
1082 //loop round the icons and choose the best one
1083 ICONDIRENTRY
* pIconDirEntry
= new ICONDIRENTRY
[nIcons
];
1084 ICONDIRENTRY
* pCurrentEntry
= pIconDirEntry
;
1088 int iSel
= wxNOT_FOUND
;
1089 for (i
=0; i
< nIcons
; i
++ )
1091 stream
.Read(pCurrentEntry
, sizeof(ICONDIRENTRY
));
1092 //bHeight and bColorCount are wxUint8
1093 if (pCurrentEntry
->bWidth
>= wMax
)
1095 // see if we have more colors, ==0 indicates > 8bpp
1096 if (pCurrentEntry
->bColorCount
== 0 ) pCurrentEntry
->bColorCount
= 255 ;
1097 if (pCurrentEntry
->bColorCount
>= colmax
)
1100 wMax
= pCurrentEntry
->bWidth
;
1101 colmax
= pCurrentEntry
->bColorCount
;
1106 if (iSel
== wxNOT_FOUND
)
1112 //seek to selected icon
1113 pCurrentEntry
= pIconDirEntry
+ iSel
;
1114 stream
.SeekI (wxUINT32_SWAP_ON_BE ( pCurrentEntry
-> dwImageOffset
), wxFromStart
) ;
1115 bResult
= LoadDib ( image
, stream
, TRUE
, IsBmp
);
1117 delete [] pIconDirEntry
;
1122 bool wxBMPHandler::DoCanRead( wxInputStream
& stream
)
1124 unsigned char hdr
[2];
1126 stream
.Read(hdr
, 2);
1127 stream
.SeekI(-2, wxFromCurrent
);
1128 return (hdr
[0] == 'B' && hdr
[1] == 'M');
1131 bool wxICOHandler::DoCanRead( wxInputStream
& stream
)
1133 unsigned char hdr
[4];
1135 stream
.Read(hdr
, 4);
1136 stream
.SeekI(-4, wxFromCurrent
);
1137 return (hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\1' && hdr
[3] == '\0');
1140 #endif // wxUSE_STREAMS
1142 #endif // wxUSE_IMAGE