+ return stream.IsOk();
+}
+
+
+bool wxBMPHandler::LoadDib( wxImage *image, wxInputStream& stream, bool verbose, bool IsBmp )
+{
+ wxUint16 aWord;
+ wxInt32 dbuf[4];
+ wxInt8 bbuf[4];
+ off_t offset;
+
+ offset = 0; // keep gcc quiet
+ if ( IsBmp )
+ {
+ // read the header off the .BMP format file
+
+ offset = stream.TellI();
+ if (offset == wxInvalidOffset) offset = 0;
+
+ stream.Read( bbuf, 2 );
+
+ stream.Read( dbuf, 16 );
+ }
+ else
+ {
+ stream.Read( dbuf, 4 );
+ }
+ #if 0 // unused
+ wxInt32 size = wxINT32_SWAP_ON_BE( dbuf[0] );
+ #endif
+ offset = offset + wxINT32_SWAP_ON_BE( dbuf[2] );
+
+ stream.Read(dbuf, 4 * 2);
+ int width = (int)wxINT32_SWAP_ON_BE( dbuf[0] );
+ int height = (int)wxINT32_SWAP_ON_BE( dbuf[1] );
+ if ( !IsBmp ) height = height / 2; // for icons divide by 2
+
+ if (width > 32767)
+ {
+ if (verbose)
+ wxLogError( _("DIB Header: Image width > 32767 pixels for file.") );
+ return FALSE;
+ }
+ if (height > 32767)
+ {
+ if (verbose)
+ wxLogError( _("DIB Header: Image height > 32767 pixels for file.") );
+ return FALSE;
+ }
+
+ stream.Read( &aWord, 2 );
+ /*
+ TODO
+ int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
+ */
+ stream.Read( &aWord, 2 );
+ int bpp = (int)wxUINT16_SWAP_ON_BE( aWord );
+ if (bpp != 1 && bpp != 4 && bpp != 8 && bpp != 16 && bpp != 24 && bpp != 32)
+ {
+ if (verbose)
+ wxLogError( _("DIB Header: Unknown bitdepth in file.") );
+ return FALSE;
+ }
+
+ stream.Read( dbuf, 4 * 4 );
+ int comp = (int)wxINT32_SWAP_ON_BE( dbuf[0] );
+ if (comp != BI_RGB && comp != BI_RLE4 && comp != BI_RLE8 && comp != BI_BITFIELDS)
+ {
+ if (verbose)
+ wxLogError( _("DIB Header: Unknown encoding in file.") );
+ return FALSE;
+ }
+
+ stream.Read( dbuf, 4 * 2 );
+ int ncolors = (int)wxINT32_SWAP_ON_BE( dbuf[0] );
+ if (ncolors == 0)
+ ncolors = 1 << bpp;
+ /* some more sanity checks */
+ if (((comp == BI_RLE4) && (bpp != 4)) ||
+ ((comp == BI_RLE8) && (bpp != 8)) ||
+ ((comp == BI_BITFIELDS) && (bpp != 16 && bpp != 32)))
+ {
+ if (verbose)
+ wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") );
+ return FALSE;
+ }
+
+ //read DIB; this is the BMP image or the XOR part of an icon image
+ if (!DoLoadDib (image, width, height, bpp, ncolors, comp, offset, stream,
+ verbose, IsBmp, TRUE ) )
+ {
+ if (verbose)
+ wxLogError( _("Error in reading image DIB .") );
+ return FALSE;
+ }
+
+ if ( !IsBmp )
+ {
+ //read Icon mask which is monochrome
+ //there is no palette, so we will create one
+ wxImage mask;
+ if (!DoLoadDib (&mask, width, height, 1, 2, BI_RGB, offset, stream,
+ verbose, IsBmp, FALSE ) )
+ {
+ if (verbose)
+ wxLogError( _("ICO: Error in reading mask DIB.") );
+ return FALSE;
+ }
+ image->SetMaskFromImage(mask, 255, 255, 255);
+
+ }