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();
201 wxLogError( _("ICO: Error calculating size of DIB .") );
205 m_offset
= m_offset
+ sizeof(ICONDIRENTRY
) ;
207 m_icondirentry
. bWidth
= image
-> GetWidth () ;
208 m_icondirentry
. bHeight
= 2 * image
-> GetHeight () ;
209 m_icondirentry
. bColorCount
= 0 ;
210 m_icondirentry
. bReserved
= 0 ;
211 m_icondirentry
. wPlanes
= wxUINT16_SWAP_ON_BE(1);
212 m_icondirentry
. wBitCount
= wxUINT16_SWAP_ON_BE(wxBMP_8BPP
) ;
213 m_icondirentry
. dwBytesInRes
= wxUINT32_SWAP_ON_BE(m_Size
);
214 m_icondirentry
. dwImageOffset
= wxUINT32_SWAP_ON_BE(m_offset
);
216 //increase size to allow for the data wriitten
217 m_offset
= m_offset
+ m_Size
;
220 stream
.Write(&m_icondirentry
. bWidth
, sizeof(m_icondirentry
. bWidth
) );
221 stream
.Write(&m_icondirentry
. bHeight
, sizeof(m_icondirentry
. bHeight
) );
222 stream
.Write(&m_icondirentry
. bColorCount
, sizeof(m_icondirentry
. bColorCount
) );
223 stream
.Write(&m_icondirentry
. bReserved
, sizeof(m_icondirentry
. bReserved
) );
224 stream
.Write(&m_icondirentry
. wPlanes
, sizeof(m_icondirentry
. wPlanes
) );
225 stream
.Write(&m_icondirentry
. wBitCount
, sizeof(m_icondirentry
. wBitCount
) );
226 stream
.Write(&m_icondirentry
. dwBytesInRes
, sizeof(m_icondirentry
. dwBytesInRes
) );
227 stream
.Write(&m_icondirentry
. dwImageOffset
, sizeof(m_icondirentry
. dwImageOffset
) );
228 if ( !stream
.IsOk () )
231 wxLogError( _("ICO: Error writing ICONDIRENTRY header.") );
236 bResult
= SaveDib ( image
, stream
, verbose
, IsBmp
, IsMask
) ;
240 wxLogError( _("ICO: Error writing XOR DIB .") );
244 bResult
= SaveDib ( &mask
, stream
, verbose
, IsBmp
, IsMask
) ;
248 wxLogError( _("ICO: Error writing Mask DIB .") );
257 bool wxBMPHandler::SaveFile(wxImage
*image
,
258 wxOutputStream
& stream
,
262 bool IsMask
= FALSE
;
263 return SaveDib( image
, stream
, verbose
, IsBmp
, IsMask
) ;
266 bool wxBMPHandler::SaveDib(wxImage
*image
,
267 wxOutputStream
& stream
,
273 wxCHECK_MSG( image
, FALSE
, _T("invalid pointer in wxBMPHandler::SaveFile") );
277 if (verbose
) wxLogError(_("BMP: Couldn't save invalid image."));
281 // get the format of the BMP file to save, else use 24bpp
282 unsigned format
= wxBMP_24BPP
;
283 if (image
->HasOption(wxBMP_FORMAT
))
284 format
= image
->GetOptionInt(wxBMP_FORMAT
);
286 unsigned bpp
; // # of bits per pixel
287 int palette_size
; // # of color map entries, ie. 2^bpp colors
289 // set the bpp and appropriate palette_size, and do additional checks
290 if ((format
== wxBMP_1BPP
) || (format
== wxBMP_1BPP_BW
))
295 else if (format
== wxBMP_4BPP
)
300 else if ((format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_GREY
) ||
301 (format
== wxBMP_8BPP_RED
) || (format
== wxBMP_8BPP_PALETTE
))
303 // need to set a wxPalette to use this, HOW TO CHECK IF VALID, SIZE?
304 if ((format
== wxBMP_8BPP_PALETTE
)
306 && !image
->HasPalette()
307 #endif // wxUSE_PALETTE
311 wxLogError(_("BMP: wImage doesn't have own wxPalette."));
317 else // you get 24bpp
319 format
= wxBMP_24BPP
;
324 unsigned width
= image
->GetWidth();
325 unsigned row_padding
= (4 - int(width
*bpp
/8.0) % 4) % 4; // # bytes to pad to dword
326 unsigned row_width
= int(width
* bpp
/8.0) + row_padding
; // # of bytes per row
331 wxUint16 magic
; // format magic, always 'BM'
332 wxUint32 filesize
; // total file size, inc. headers
333 wxUint32 reserved
; // for future use
334 wxUint32 data_offset
; // image data offset in the file
337 wxUint32 bih_size
; // 2nd part's size
338 wxUint32 width
, height
; // bitmap's dimensions
339 wxUint16 planes
; // num of planes
340 wxUint16 bpp
; // bits per pixel
341 wxUint32 compression
; // compression method
342 wxUint32 size_of_bmp
; // size of the bitmap
343 wxUint32 h_res
, v_res
; // image resolution in dpi
344 wxUint32 num_clrs
; // number of colors used
345 wxUint32 num_signif_clrs
;// number of significant colors
348 wxUint32 hdr_size
= 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/;
350 hdr
.magic
= wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/);
351 hdr
.filesize
= wxUINT32_SWAP_ON_BE( hdr_size
+ palette_size
*4 +
352 row_width
* image
->GetHeight() );
354 hdr
.data_offset
= wxUINT32_SWAP_ON_BE(hdr_size
+ palette_size
*4);
356 hdr
.bih_size
= wxUINT32_SWAP_ON_BE(hdr_size
- 14);
357 hdr
.width
= wxUINT32_SWAP_ON_BE(image
->GetWidth());
360 hdr
.height
= wxUINT32_SWAP_ON_BE(image
->GetHeight());
364 hdr
.height
= wxUINT32_SWAP_ON_BE(2 * image
->GetHeight());
366 hdr
.planes
= wxUINT16_SWAP_ON_BE(1); // always 1 plane
367 hdr
.bpp
= wxUINT16_SWAP_ON_BE(bpp
);
368 hdr
.compression
= 0; // RGB uncompressed
369 hdr
.size_of_bmp
= wxUINT32_SWAP_ON_BE(row_width
* image
->GetHeight());
370 hdr
.h_res
= hdr
.v_res
= wxUINT32_SWAP_ON_BE(72); // 72dpi is standard
371 hdr
.num_clrs
= wxUINT32_SWAP_ON_BE(palette_size
); // # colors in colormap
372 hdr
.num_signif_clrs
= 0; // all colors are significant
376 if (// VS: looks ugly but compilers tend to do ugly things with structs,
377 // like aligning hdr.filesize's ofset to dword :(
378 // VZ: we should add padding then...
379 !stream
.Write(&hdr
.magic
, 2) ||
380 !stream
.Write(&hdr
.filesize
, 4) ||
381 !stream
.Write(&hdr
.reserved
, 4) ||
382 !stream
.Write(&hdr
.data_offset
, 4)
386 wxLogError(_("BMP: Couldn't write the file (Bitmap) header."));
393 !stream
.Write(&hdr
.bih_size
, 4) ||
394 !stream
.Write(&hdr
.width
, 4) ||
395 !stream
.Write(&hdr
.height
, 4) ||
396 !stream
.Write(&hdr
.planes
, 2) ||
397 !stream
.Write(&hdr
.bpp
, 2) ||
398 !stream
.Write(&hdr
.compression
, 4) ||
399 !stream
.Write(&hdr
.size_of_bmp
, 4) ||
400 !stream
.Write(&hdr
.h_res
, 4) ||
401 !stream
.Write(&hdr
.v_res
, 4) ||
402 !stream
.Write(&hdr
.num_clrs
, 4) ||
403 !stream
.Write(&hdr
.num_signif_clrs
, 4)
407 wxLogError(_("BMP: Couldn't write the file (BitmapInfo) header."));
412 wxPalette
*palette
= NULL
; // entries for quantized images
413 wxUint8
*rgbquad
= NULL
; // for the RGBQUAD bytes for the colormap
414 wxImage
*q_image
= NULL
; // destination for quantized image
416 // if <24bpp use quantization to reduce colors for *some* of the formats
417 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_4BPP
) ||
418 (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_PALETTE
))
420 // make a new palette and quantize the image
421 if (format
!= wxBMP_8BPP_PALETTE
)
423 q_image
= new wxImage();
425 // I get a delete error using Quantize when desired colors > 236
426 int quantize
= ((palette_size
> 236) ? 236 : palette_size
);
427 // fill the destination too, it gives much nicer 4bpp images
428 wxQuantize::Quantize( *image
, *q_image
, &palette
, quantize
, 0,
429 wxQUANTIZE_FILL_DESTINATION_IMAGE
);
434 palette
= new wxPalette(image
->GetPalette());
435 #endif // wxUSE_PALETTE
439 unsigned char r
, g
, b
;
440 rgbquad
= new wxUint8
[palette_size
*4];
442 for (i
=0; i
<palette_size
; i
++)
445 if (!palette
->GetRGB( i
, &r
, &g
, &b
))
446 #endif // wxUSE_PALETTE
455 // make a 256 entry greyscale colormap or 2 entry black & white
456 else if ((format
== wxBMP_8BPP_GREY
) || (format
== wxBMP_8BPP_RED
) ||
457 (format
== wxBMP_1BPP_BW
))
460 rgbquad
= new wxUint8
[palette_size
*4];
462 for (i
=0; i
<palette_size
; i
++)
464 // if 1BPP_BW then just 0 and 255 then exit
465 if (( i
> 0) && (format
== wxBMP_1BPP_BW
)) i
= 255;
473 // if the colormap was made, then it needs to be written
478 if (!stream
.Write(rgbquad
, palette_size
*4))
481 wxLogError(_("BMP: Couldn't write RGB color map."));
485 #endif // wxUSE_PALETTE
493 // pointer to the image data, use quantized if available
494 wxUint8
*data
= (wxUint8
*) image
->GetData();
495 if (q_image
) if (q_image
->Ok()) data
= (wxUint8
*) q_image
->GetData();
497 wxUint8
*buffer
= new wxUint8
[row_width
];
498 memset(buffer
, 0, row_width
);
502 for (y
= image
->GetHeight() -1 ; y
>= 0; y
--)
504 if (format
== wxBMP_24BPP
) // 3 bytes per pixel red,green,blue
506 for (x
= 0; x
< width
; x
++)
508 pixel
= 3*(y
*width
+ x
);
510 buffer
[3*x
] = data
[pixel
+2];
511 buffer
[3*x
+ 1] = data
[pixel
+1];
512 buffer
[3*x
+ 2] = data
[pixel
];
515 else if ((format
== wxBMP_8BPP
) || // 1 byte per pixel in color
516 (format
== wxBMP_8BPP_PALETTE
))
518 for (x
= 0; x
< width
; x
++)
520 pixel
= 3*(y
*width
+ x
);
522 buffer
[x
] = palette
->GetPixel( data
[pixel
],
526 // FIXME: what should this be? use some std palette maybe?
528 #endif // wxUSE_PALETTE
531 else if (format
== wxBMP_8BPP_GREY
) // 1 byte per pix, rgb ave to grey
533 for (x
= 0; x
< width
; x
++)
535 pixel
= 3*(y
*width
+ x
);
536 buffer
[x
] = (wxUint8
)(.299*data
[pixel
] +
541 else if (format
== wxBMP_8BPP_RED
) // 1 byte per pixel, red as greys
543 for (x
= 0; x
< width
; x
++)
545 buffer
[x
] = (wxUint8
)data
[3*(y
*width
+ x
)];
548 else if (format
== wxBMP_4BPP
) // 4 bpp in color
550 for (x
= 0; x
< width
; x
+=2)
552 pixel
= 3*(y
*width
+ x
);
554 // fill buffer, ignore if > width
557 ((wxUint8
)palette
->GetPixel(data
[pixel
],
559 data
[pixel
+2]) << 4) |
562 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3],
566 // FIXME: what should this be? use some std palette maybe?
568 #endif // wxUSE_PALETTE
571 else if (format
== wxBMP_1BPP
) // 1 bpp in "color"
573 for (x
= 0; x
< width
; x
+=8)
575 pixel
= 3*(y
*width
+ x
);
578 buffer
[x
/8] = ((wxUint8
)palette
->GetPixel(data
[pixel
], data
[pixel
+1], data
[pixel
+2]) << 7) |
579 (((x
+1) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3], data
[pixel
+4], data
[pixel
+5]) << 6)) |
580 (((x
+2) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+6], data
[pixel
+7], data
[pixel
+8]) << 5)) |
581 (((x
+3) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+9], data
[pixel
+10], data
[pixel
+11]) << 4)) |
582 (((x
+4) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+12], data
[pixel
+13], data
[pixel
+14]) << 3)) |
583 (((x
+5) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+15], data
[pixel
+16], data
[pixel
+17]) << 2)) |
584 (((x
+6) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+18], data
[pixel
+19], data
[pixel
+20]) << 1)) |
585 (((x
+7) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+21], data
[pixel
+22], data
[pixel
+23]) ));
587 // FIXME: what should this be? use some std palette maybe?
589 #endif // wxUSE_PALETTE
592 else if (format
== wxBMP_1BPP_BW
) // 1 bpp B&W colormap from red color ONLY
594 for (x
= 0; x
< width
; x
+=8)
596 pixel
= 3*(y
*width
+ x
);
599 (((wxUint8
)(data
[pixel
] /128.)) << 7) |
600 ( ((x
+1) > width
) ? 0 : (((wxUint8
)(data
[pixel
+3] /128.)) << 6)) |
601 ( ((x
+2) > width
) ? 0 : (((wxUint8
)(data
[pixel
+6] /128.)) << 5)) |
602 ( ((x
+3) > width
) ? 0 : (((wxUint8
)(data
[pixel
+9] /128.)) << 4)) |
603 ( ((x
+4) > width
) ? 0 : (((wxUint8
)(data
[pixel
+12]/128.)) << 3)) |
604 ( ((x
+5) > width
) ? 0 : (((wxUint8
)(data
[pixel
+15]/128.)) << 2)) |
605 ( ((x
+6) > width
) ? 0 : (((wxUint8
)(data
[pixel
+18]/128.)) << 1)) |
606 ( ((x
+7) > width
) ? 0 : (((wxUint8
)(data
[pixel
+21]/128.)) ));
610 if (!stream
.Write(buffer
, row_width
))
613 wxLogError(_("BMP: Couldn't write data."));
617 #endif // wxUSE_PALETTE
625 #endif // wxUSE_PALETTE
634 bool wxBMPHandler::DoLoadDib (wxImage
* image
, int width
, int height
, int bpp
, int ncolors
, int comp
,
635 off_t bmpOffset
, wxInputStream
& stream
,
636 bool verbose
, bool IsBmp
, bool hasPalette
)
639 wxInt32 aDword
, rmask
= 0, gmask
= 0, bmask
= 0;
640 int rshift
= 0, gshift
= 0, bshift
= 0;
647 // allocate space for palette if needed
650 unsigned char r
, g
, b
;
656 cmap
= (struct _cmap
*)malloc(sizeof(struct _cmap
) * ncolors
);
660 wxLogError( _("Loading DIB : Couldn't allocate memory.") );
667 // destroy existing here instead of
669 image
->Create( width
, height
);
670 unsigned char *ptr
= image
->GetData();
674 wxLogError( _("Loading DIB : Couldn't allocate memory.") );
680 * Reading the palette, if it exists.
682 if (bpp
< 16 && ncolors
!= 0)
684 unsigned char* r
= new unsigned char[ncolors
];
685 unsigned char* g
= new unsigned char[ncolors
];
686 unsigned char* b
= new unsigned char[ncolors
];
687 for (int j
= 0; j
< ncolors
; j
++)
691 stream
.Read( bbuf
, 4 );
702 //used in reading .ico file mask
703 r
[j
] = cmap
[j
].r
= j
* 255;
704 g
[j
] = cmap
[j
].g
= j
* 255;
705 b
[j
] = cmap
[j
].b
= j
* 255;
710 // Set the palette for the wxImage
711 image
->SetPalette(wxPalette(ncolors
, r
, g
, b
));
712 #endif // wxUSE_PALETTE
718 else if (bpp
== 16 || bpp
== 32)
720 if (comp
== BI_BITFIELDS
)
723 stream
.Read( dbuf
, 4 * 3 );
724 bmask
= wxINT32_SWAP_ON_BE( dbuf
[0] );
725 gmask
= wxINT32_SWAP_ON_BE( dbuf
[1] );
726 rmask
= wxINT32_SWAP_ON_BE( dbuf
[2] );
727 /* find shift amount.. ugly, but i can't think of a better way */
728 for (bit
= 0; bit
< bpp
; bit
++)
730 if (bmask
& (1 << bit
))
732 if (gmask
& (1 << bit
))
734 if (rmask
& (1 << bit
))
759 * Reading the image data
761 if ( IsBmp
) stream
.SeekI( bmpOffset
); // else icon, just carry on
763 unsigned char *data
= ptr
;
765 /* set the whole image to the background color */
766 if (bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
))
768 for (int i
= 0; i
< width
* height
; i
++)
779 int linesize
= ((width
* bpp
+ 31) / 32) * 4;
781 /* BMPs are stored upside down */
782 for (line
= (height
- 1); line
>= 0; line
--)
785 for (column
= 0; column
< width
;)
791 aByte
= stream
.GetC();
795 for (bit
= 0; bit
< 8 && column
< width
; bit
++)
797 index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
798 ptr
[poffset
] = cmap
[index
].r
;
799 ptr
[poffset
+ 1] = cmap
[index
].g
;
800 ptr
[poffset
+ 2] = cmap
[index
].b
;
809 wxLogError( _("DIB Header: Cannot deal with 4bit encoded yet.") );
817 for (nibble
= 0; nibble
< 2 && column
< width
; nibble
++)
819 index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
822 ptr
[poffset
] = cmap
[index
].r
;
823 ptr
[poffset
+ 1] = cmap
[index
].g
;
824 ptr
[poffset
+ 2] = cmap
[index
].b
;
835 aByte
= stream
.GetC();
840 /* column = width; */
849 aByte
= stream
.GetC();
851 linepos
= column
* bpp
/ 8;
852 aByte
= stream
.GetC();
857 int absolute
= aByte
;
858 for (int k
= 0; k
< absolute
; k
++)
861 aByte
= stream
.GetC();
862 ptr
[poffset
] = cmap
[aByte
].r
;
863 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
864 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
868 aByte
= stream
.GetC();
873 for (int l
= 0; l
< first
&& column
< width
; l
++)
875 ptr
[poffset
] = cmap
[aByte
].r
;
876 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
877 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
885 ptr
[poffset
] = cmap
[aByte
].r
;
886 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
887 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
889 // linepos += size; seems to be wrong, RR
895 stream
.Read( bbuf
, 3 );
897 ptr
[poffset
] = (unsigned char)bbuf
[2];
898 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
899 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
905 stream
.Read( &aWord
, 2 );
906 aWord
= wxUINT16_SWAP_ON_BE( aWord
);
908 temp
= (aWord
& rmask
) >> rshift
;
910 temp
= (aWord
& gmask
) >> gshift
;
911 ptr
[poffset
+ 1] = temp
;
912 temp
= (aWord
& bmask
) >> bshift
;
913 ptr
[poffset
+ 2] = temp
;
919 stream
.Read( &aDword
, 4 );
920 aDword
= wxINT32_SWAP_ON_BE( aDword
);
922 temp
= (aDword
& rmask
) >> rshift
;
924 temp
= (aDword
& gmask
) >> gshift
;
925 ptr
[poffset
+ 1] = temp
;
926 temp
= (aDword
& bmask
) >> bshift
;
927 ptr
[poffset
+ 2] = temp
;
931 while ((linepos
< linesize
) && (comp
!= 1) && (comp
!= 2))
933 stream
.Read( &aByte
, 1 );
935 if (stream
.LastError() != wxStream_NOERROR
)
942 image
->SetMask( FALSE
);
944 return stream
.IsOk();
948 bool wxBMPHandler::LoadDib( wxImage
*image
, wxInputStream
& stream
, bool verbose
, bool IsBmp
)
955 offset
= 0; // keep gcc quiet
958 // read the header off the .BMP format file
960 offset
= stream
.TellI();
961 if (offset
== wxInvalidOffset
) offset
= 0;
963 stream
.Read( bbuf
, 2 );
965 stream
.Read( dbuf
, 16 );
969 stream
.Read( dbuf
, 4 );
972 wxInt32 size
= wxINT32_SWAP_ON_BE( dbuf
[0] );
974 offset
= offset
+ wxINT32_SWAP_ON_BE( dbuf
[2] );
976 stream
.Read(dbuf
, 4 * 2);
977 int width
= (int)wxINT32_SWAP_ON_BE( dbuf
[0] );
978 int height
= (int)wxINT32_SWAP_ON_BE( dbuf
[1] );
979 if ( !IsBmp
) height
= height
/ 2; // for icons divide by 2
984 wxLogError( _("DIB Header: Image width > 32767 pixels for file.") );
990 wxLogError( _("DIB Header: Image height > 32767 pixels for file.") );
994 stream
.Read( &aWord
, 2 );
997 int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
999 stream
.Read( &aWord
, 2 );
1000 int bpp
= (int)wxUINT16_SWAP_ON_BE( aWord
);
1001 if (bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
!= 16 && bpp
!= 24 && bpp
!= 32)
1004 wxLogError( _("DIB Header: Unknown bitdepth in file.") );
1008 stream
.Read( dbuf
, 4 * 4 );
1009 int comp
= (int)wxINT32_SWAP_ON_BE( dbuf
[0] );
1010 if (comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&& comp
!= BI_BITFIELDS
)
1013 wxLogError( _("DIB Header: Unknown encoding in file.") );
1017 stream
.Read( dbuf
, 4 * 2 );
1018 int ncolors
= (int)wxINT32_SWAP_ON_BE( dbuf
[0] );
1021 /* some more sanity checks */
1022 if (((comp
== BI_RLE4
) && (bpp
!= 4)) ||
1023 ((comp
== BI_RLE8
) && (bpp
!= 8)) ||
1024 ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
1027 wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") );
1031 //read DIB; this is the BMP image or the XOR part of an icon image
1032 if (!DoLoadDib (image
, width
, height
, bpp
, ncolors
, comp
, offset
, stream
,
1033 verbose
, IsBmp
, TRUE
) )
1036 wxLogError( _("Error in reading image DIB .") );
1042 //read Icon mask which is monochrome
1043 //there is no palette, so we will create one
1045 if (!DoLoadDib (&mask
, width
, height
, 1, 2, BI_RGB
, offset
, stream
,
1046 verbose
, IsBmp
, FALSE
) )
1049 wxLogError( _("ICO: Error in reading mask DIB.") );
1052 image
->SetMaskFromImage(mask
, 255, 255, 255);
1059 bool wxBMPHandler::LoadFile ( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
1062 //Read a single DIB fom the file
1063 return LoadDib ( image
, stream
, verbose
, IsBmp
) ;
1068 bool wxICOHandler::LoadFile ( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
1070 bool bResult
= FALSE
;
1074 stream
.Read (&m_IconDir
, sizeof(m_IconDir
));
1075 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE ( m_IconDir
.idCount
) ;
1077 //loop round the icons and choose the best one
1078 ICONDIRENTRY
* pIconDirEntry
= new ICONDIRENTRY
[nIcons
];
1079 ICONDIRENTRY
* pCurrentEntry
= pIconDirEntry
;
1083 int iSel
= wxNOT_FOUND
;
1084 for (i
=0; i
< nIcons
; i
++ )
1086 stream
.Read(pCurrentEntry
, sizeof(ICONDIRENTRY
));
1087 //bHeight and bColorCount are wxUint8
1088 if (pCurrentEntry
->bWidth
>= wMax
)
1090 // see if we have more colors, ==0 indicates > 8bpp
1091 if (pCurrentEntry
->bColorCount
== 0 ) pCurrentEntry
->bColorCount
= 255 ;
1092 if (pCurrentEntry
->bColorCount
>= colmax
)
1095 wMax
= pCurrentEntry
->bWidth
;
1096 colmax
= pCurrentEntry
->bColorCount
;
1101 if (iSel
== wxNOT_FOUND
)
1107 //seek to selected icon
1108 pCurrentEntry
= pIconDirEntry
+ iSel
;
1109 stream
.SeekI (wxUINT32_SWAP_ON_BE ( pCurrentEntry
-> dwImageOffset
), wxFromStart
) ;
1110 bResult
= LoadDib ( image
, stream
, TRUE
, IsBmp
);
1112 delete [] pIconDirEntry
;
1117 bool wxBMPHandler::DoCanRead( wxInputStream
& stream
)
1119 unsigned char hdr
[2];
1121 stream
.Read(hdr
, 2);
1122 stream
.SeekI(-2, wxFromCurrent
);
1123 return (hdr
[0] == 'B' && hdr
[1] == 'M');
1126 bool wxICOHandler::DoCanRead( wxInputStream
& stream
)
1128 unsigned char hdr
[4];
1130 stream
.Read(hdr
, 4);
1131 stream
.SeekI(-4, wxFromCurrent
);
1132 return (hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\1' && hdr
[3] == '\0');
1135 #endif // wxUSE_STREAMS
1137 #endif // wxUSE_IMAGE