1. applied (heavily modified) patch for reading CUR files by Chris

2. fixed MIME types (changed to image/x-{bmp,ico,cur})
3. (hopefully) reduced excessive amount of spaces used in code formatting
4. made error messages human readable


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13305 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Václav Slavík
2002-01-01 23:13:02 +00:00
parent a103111bae
commit 05813ada9a
4 changed files with 582 additions and 477 deletions

View File

@@ -72,6 +72,8 @@ wxBase:
All (GUI): All (GUI):
- fixed using custom renderers in wxGrid which was broken in 2.3.2 - fixed using custom renderers in wxGrid which was broken in 2.3.2
- support for multiple images in one file added to wxImage (TIFF and ICO formats)
- support for CUR files in wxImage added (Chris Elliott)
wxMSW: wxMSW:

View File

@@ -1,9 +1,9 @@
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// Name: imagbmp.h // Name: imagbmp.h
// Purpose: wxImage BMP handler // Purpose: wxImage BMP, ICO and CUR handlers
// Author: Robert Roebling // Author: Robert Roebling, Chris Elliott
// RCS-ID: $Id$ // RCS-ID: $Id$
// Copyright: (c) Robert Roebling // Copyright: (c) Robert Roebling, Chris Elliott
// Licence: wxWindows licence // Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
@@ -43,25 +43,24 @@ class WXDLLEXPORT wxBMPHandler : public wxImageHandler
public: public:
wxBMPHandler() wxBMPHandler()
{ {
m_name = _T("BMP file"); m_name = _T("Windows bitmap file");
m_extension = _T("bmp"); m_extension = _T("bmp");
m_type = wxBITMAP_TYPE_BMP; m_type = wxBITMAP_TYPE_BMP;
m_mime = _T("image/bmp"); m_mime = _T("image/x-bmp");
}; };
#if wxUSE_STREAMS #if wxUSE_STREAMS
virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE ); virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=0 ); virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=0 );
virtual bool DoCanRead( wxInputStream& stream ); virtual bool DoCanRead( wxInputStream& stream );
protected: protected:
bool SaveDib(wxImage *image, wxOutputStream& stream, bool verbose, bool IsBmp, bool IsMask); bool SaveDib(wxImage *image, wxOutputStream& stream, bool verbose,
bool DoLoadDib (wxImage * image, int width, int height, int bpp, int ncolors, int comp, bool IsBmp, bool IsMask);
off_t bmpOffset, wxInputStream& stream, bool DoLoadDib(wxImage *image, int width, int height, int bpp, int ncolors,
int comp, off_t bmpOffset, wxInputStream& stream,
bool verbose, bool IsBmp, bool hasPalette); bool verbose, bool IsBmp, bool hasPalette);
bool LoadDib(wxImage *image, wxInputStream& stream, bool verbose, bool IsBmp); bool LoadDib(wxImage *image, wxInputStream& stream, bool verbose, bool IsBmp);
#endif // wxUSE_STREAMS #endif // wxUSE_STREAMS
private: private:
@@ -78,25 +77,54 @@ class WXDLLEXPORT wxICOHandler : public wxBMPHandler
public: public:
wxICOHandler() wxICOHandler()
{ {
m_name = _T("ICO file"); m_name = _T("Windows icon file");
m_extension = _T("ico"); m_extension = _T("ico");
m_type = wxBITMAP_TYPE_ICO; m_type = wxBITMAP_TYPE_ICO;
m_mime = _T("image/icon"); m_mime = _T("image/x-ico");
}; };
#if wxUSE_STREAMS #if wxUSE_STREAMS
virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE ); virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=0 ); virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=0 );
virtual bool DoCanRead( wxInputStream& stream ); virtual bool DoCanRead( wxInputStream& stream );
virtual int GetImagesCount( wxInputStream& stream );
#endif // wxUSE_STREAMS #endif // wxUSE_STREAMS
private: private:
DECLARE_DYNAMIC_CLASS(wxBMPHandler) DECLARE_DYNAMIC_CLASS(wxICOHandler)
}; };
#endif // ----------------------------------------------------------------------------
// _WX_IMAGBMP_H_ // wxCURHandler
// ----------------------------------------------------------------------------
// These two options are filled in upon reading CUR file and can (should) be
// specified when saving a CUR file - they define the hotspot of the cursor:
#define wxCUR_HOTSPOT_X wxT("HotSpotX")
#define wxCUR_HOTSPOT_Y wxT("HotSpotY")
class WXDLLEXPORT wxCURHandler : public wxICOHandler
{
public:
wxCURHandler()
{
m_name = _T("Windows cursor file");
m_extension = _T("cur");
m_type = wxBITMAP_TYPE_CUR;
m_mime = _T("image/x-cur");
};
// VS: This handler's meat is implemented inside wxICOHandler (the two
// formats are almost identical), but we hide this fact at
// the API level, since it is a mere implementation detail.
#if wxUSE_STREAMS
virtual bool DoCanRead( wxInputStream& stream );
#endif // wxUSE_STREAMS
private:
DECLARE_DYNAMIC_CLASS(wxCURHandler)
};
#endif // _WX_IMAGBMP_H_

View File

@@ -56,6 +56,7 @@ void wxInitAllImageHandlers()
wxImage::AddHandler( new wxXPMHandler ); wxImage::AddHandler( new wxXPMHandler );
#endif #endif
wxImage::AddHandler( new wxICOHandler ); wxImage::AddHandler( new wxICOHandler );
wxImage::AddHandler( new wxCURHandler );
} }
#endif // wxUSE_IMAGE #endif // wxUSE_IMAGE

View File

@@ -1,9 +1,9 @@
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// Name: imagbmp.cpp // Name: imagbmp.cpp
// Purpose: wxImage BMP handler // Purpose: wxImage BMP,ICO and CUR handlers
// Author: Robert Roebling // Author: Robert Roebling, Chris Elliott
// RCS-ID: $Id$ // RCS-ID: $Id$
// Copyright: (c) Robert Roebling // Copyright: (c) Robert Roebling, Chris Elliott
// Licence: wxWindows licence // Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
@@ -20,7 +20,7 @@
#include "wx/defs.h" #include "wx/defs.h"
#if wxUSE_IMAGE #if wxUSE_IMAGE && wxUSE_STREAMS
#include "wx/imagbmp.h" #include "wx/imagbmp.h"
#include "wx/bitmap.h" #include "wx/bitmap.h"
@@ -47,15 +47,11 @@
#endif #endif
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// wxBMPHandler & wxICOHandler // wxBMPHandler
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler,wxImageHandler) IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler,wxImageHandler)
#if wxUSE_STREAMS
#ifndef BI_RGB #ifndef BI_RGB
#define BI_RGB 0 #define BI_RGB 0
#define BI_RLE8 1 #define BI_RLE8 1
@@ -68,204 +64,11 @@ IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler,wxImageHandler)
#define poffset (line * width * 3 + column * 3) #define poffset (line * width * 3 + column * 3)
struct ICONDIRENTRY
{
wxUint8 bWidth; // Width of the image
wxUint8 bHeight; // Height of the image (times 2)
wxUint8 bColorCount; // Number of colors in image (0 if >=8bpp)
wxUint8 bReserved; // Reserved
wxUint16 wPlanes; // Color Planes
wxUint16 wBitCount; // Bits per pixel
wxUint32 dwBytesInRes; // how many bytes in this resource?
wxUint32 dwImageOffset; // where in the file is this image
} ;
struct ICONDIR
{
wxUint16 idReserved; // Reserved
wxUint16 idType; // resource type (1 for icons)
wxUint16 idCount; // how many images?
} ;
bool wxICOHandler::SaveFile(wxImage *image,
wxOutputStream& stream,
bool verbose)
{
bool bResult = FALSE ;
//sanity check; icon must be less than 127 pixels high and 255 wide
if (image -> GetHeight () > 127 )
{
if (verbose)
wxLogError( _("ICO: Error Image too tall for an icon.") );
return FALSE;
}
if (image -> GetWidth () > 255 )
{
if (verbose)
wxLogError( _("ICO: Error Image too wide for an icon.") );
return FALSE;
}
// only generate one image
int m_images = 1 ;
// write a header, (ICONDIR)
// Calculate the header size
wxUint32 m_offset = 3 * sizeof(wxUint16);
ICONDIR m_IconDir ;
m_IconDir.idReserved = 0 ;
m_IconDir.idType = wxUINT16_SWAP_ON_BE (1);
m_IconDir.idCount = wxUINT16_SWAP_ON_BE (m_images);
stream.Write(&m_IconDir.idReserved, sizeof(m_IconDir.idReserved));
stream.Write(&m_IconDir.idType, sizeof(m_IconDir.idType));
stream.Write(&m_IconDir.idCount, sizeof(m_IconDir.idCount));
if ( !stream.IsOk () )
{
if (verbose)
wxLogError( _("ICO: Error writing ICONDIR header.") );
return FALSE;
}
// for each iamage write a description ICONDIRENTRY
ICONDIRENTRY m_icondirentry ;
int i ;
for ( i = 0; i < m_images; i++ )
{
wxImage mask ;
if (image->HasMask())
{
//make another image with black/white
mask = image -> ConvertToMono (image->GetMaskRed(), image->GetMaskGreen(), image->GetMaskBlue() );
//now we need to change the masked regions to black
unsigned char r = image -> GetMaskRed() ;
unsigned char g = image -> GetMaskGreen() ;
unsigned char b = image -> GetMaskBlue() ;
if ((r != 0) || (g != 0) || (b != 0) )
{
//Go round and apply black to the masked bits
int i,j;
for (i=0; i < mask.GetWidth(); i++)
for (j=0; j < mask.GetHeight(); j++)
{
if ((r == mask.GetRed(i, j)) &&
(g == mask.GetGreen(i, j) ) &&
(b == mask.GetBlue(i, j)) )
image -> SetRGB ( i, j, 0, 0, 0 );
}
}
}
else
{
// just make a black mask all over
mask = image -> Copy ();
int i,j;
for (i=0; i < mask.GetWidth(); i++)
for (j=0; j < mask.GetHeight(); j++)
mask.SetRGB ( i, j, 0, 0, 0 );
}
//Set the formats for image and mask
// windows never saves with more than 8 colors
image -> SetOption (wxBMP_FORMAT, wxBMP_8BPP);
// monochome bitmap
mask . SetOption (wxBMP_FORMAT, wxBMP_1BPP_BW);
bool IsBmp = FALSE ;
bool IsMask = FALSE ;
//calculate size and offset of image and mask
wxCountingOutputStream cStream ;
bResult = SaveDib ( image, cStream, verbose, IsBmp, IsMask ) ;
if (!bResult)
{
if (verbose)
wxLogError( _("ICO: Error calculating size of XOR DIB .") );
return FALSE;
}
IsMask = TRUE ;
bResult = SaveDib ( &mask, cStream, verbose, IsBmp, IsMask ) ;
if (!bResult)
{
if (verbose)
wxLogError( _("ICO: Error calculating size of Mask DIB .") );
return FALSE;
}
wxUint32 m_Size = cStream.GetSize();
// wxCountingOutputStream::Ok() always returns TRUE for now and this
// "if" provokes VC++ warnings in optimized build
#if 0
if (!cStream.Ok())
{
if (verbose)
wxLogError( _("ICO: Error calculating size of DIB .") );
return FALSE;
}
#endif // 0
m_offset = m_offset + sizeof(ICONDIRENTRY) ;
m_icondirentry. bWidth = image -> GetWidth () ;
m_icondirentry. bHeight = 2 * image -> GetHeight () ;
m_icondirentry. bColorCount = 0 ;
m_icondirentry. bReserved = 0 ;
m_icondirentry. wPlanes = wxUINT16_SWAP_ON_BE(1);
m_icondirentry. wBitCount = wxUINT16_SWAP_ON_BE(wxBMP_8BPP) ;
m_icondirentry. dwBytesInRes = wxUINT32_SWAP_ON_BE(m_Size);
m_icondirentry. dwImageOffset = wxUINT32_SWAP_ON_BE(m_offset);
//increase size to allow for the data wriitten
m_offset = m_offset + m_Size ;
//write to stream
stream.Write(&m_icondirentry. bWidth, sizeof(m_icondirentry. bWidth) );
stream.Write(&m_icondirentry. bHeight, sizeof(m_icondirentry. bHeight) );
stream.Write(&m_icondirentry. bColorCount, sizeof(m_icondirentry. bColorCount) );
stream.Write(&m_icondirentry. bReserved, sizeof(m_icondirentry. bReserved) );
stream.Write(&m_icondirentry. wPlanes, sizeof(m_icondirentry. wPlanes) );
stream.Write(&m_icondirentry. wBitCount, sizeof(m_icondirentry. wBitCount) );
stream.Write(&m_icondirentry. dwBytesInRes, sizeof(m_icondirentry. dwBytesInRes) );
stream.Write(&m_icondirentry. dwImageOffset, sizeof(m_icondirentry. dwImageOffset) );
if ( !stream.IsOk () )
{
if (verbose)
wxLogError( _("ICO: Error writing ICONDIRENTRY header.") );
return FALSE;
}
//actually save it
IsMask = FALSE ;
bResult = SaveDib ( image, stream, verbose, IsBmp, IsMask ) ;
if (!bResult)
{
if (verbose)
wxLogError( _("ICO: Error writing XOR DIB .") );
return FALSE;
}
IsMask = TRUE ;
bResult = SaveDib ( &mask, stream, verbose, IsBmp, IsMask ) ;
if (!bResult)
{
if (verbose)
wxLogError( _("ICO: Error writing Mask DIB .") );
return FALSE;
}
} // end of for loop
return TRUE ;
}
bool wxBMPHandler::SaveFile(wxImage *image, bool wxBMPHandler::SaveFile(wxImage *image,
wxOutputStream& stream, wxOutputStream& stream,
bool verbose) bool verbose)
{ {
bool IsBmp = TRUE; return SaveDib(image, stream, verbose, TRUE/*IsBmp*/, FALSE/*IsMask*/);
bool IsMask = FALSE ;
return SaveDib( image, stream, verbose, IsBmp, IsMask ) ;
} }
bool wxBMPHandler::SaveDib(wxImage *image, bool wxBMPHandler::SaveDib(wxImage *image,
@@ -279,7 +82,8 @@ bool wxBMPHandler::SaveDib(wxImage *image,
if ( !image->Ok() ) if ( !image->Ok() )
{ {
if (verbose) wxLogError(_("BMP: Couldn't save invalid image.")); if ( verbose )
wxLogError(_("BMP: Couldn't save invalid image."));
return FALSE; return FALSE;
} }
@@ -634,13 +438,16 @@ bool wxBMPHandler::SaveDib(wxImage *image,
} }
typedef struct
{
unsigned char r, g, b;
} _cmap;
bool wxBMPHandler::DoLoadDib(wxImage * image, int width, int height,
bool wxBMPHandler::DoLoadDib (wxImage * image, int width, int height, int bpp, int ncolors, int comp, int bpp, int ncolors, int comp,
off_t bmpOffset, wxInputStream& stream, off_t bmpOffset, wxInputStream& stream,
bool verbose, bool IsBmp, bool hasPalette) bool verbose, bool IsBmp, bool hasPalette)
{ {
wxInt32 aDword, rmask = 0, gmask = 0, bmask = 0; wxInt32 aDword, rmask = 0, gmask = 0, bmask = 0;
int rshift = 0, gshift = 0, bshift = 0; int rshift = 0, gshift = 0, bshift = 0;
wxInt32 dbuf[4]; wxInt32 dbuf[4];
@@ -648,42 +455,38 @@ bool wxBMPHandler::DoLoadDib (wxImage * image, int width, int height, int bpp, i
wxUint8 aByte; wxUint8 aByte;
wxUint16 aWord; wxUint16 aWord;
// allocate space for palette if needed:
// allocate space for palette if needed _cmap *cmap = NULL;
struct _cmap
{
unsigned char r, g, b;
}
*cmap = NULL;
if ( bpp < 16 ) if ( bpp < 16 )
{ {
cmap = (struct _cmap *)malloc(sizeof(struct _cmap) * ncolors); cmap = new _cmap[ncolors];
if ( !cmap ) if ( !cmap )
{ {
if (verbose) if (verbose)
wxLogError( _("Loading DIB : Couldn't allocate memory.") ); wxLogError(_("BMP: Couldn't allocate memory."));
return FALSE; return FALSE;
} }
} }
else else
cmap = NULL; cmap = NULL;
// destroy existing here instead of // destroy existing here instead of:
image->Destroy(); image->Destroy();
image->Create(width, height); image->Create(width, height);
unsigned char *ptr = image->GetData(); unsigned char *ptr = image->GetData();
if ( !ptr ) if ( !ptr )
{ {
if ( verbose ) if ( verbose )
wxLogError( _("Loading DIB : Couldn't allocate memory.") ); wxLogError( _("BMP: Couldn't allocate memory.") );
if ( cmap ) if ( cmap )
free(cmap); delete[] cmap;
return FALSE; return FALSE;
} }
/*
* Reading the palette, if it exists. // Reading the palette, if it exists:
*/
if ( bpp < 16 && ncolors != 0 ) if ( bpp < 16 && ncolors != 0 )
{ {
unsigned char* r = new unsigned char[ncolors]; unsigned char* r = new unsigned char[ncolors];
@@ -729,7 +532,7 @@ bool wxBMPHandler::DoLoadDib (wxImage * image, int width, int height, int bpp, i
bmask = wxINT32_SWAP_ON_BE(dbuf[0]); bmask = wxINT32_SWAP_ON_BE(dbuf[0]);
gmask = wxINT32_SWAP_ON_BE(dbuf[1]); gmask = wxINT32_SWAP_ON_BE(dbuf[1]);
rmask = wxINT32_SWAP_ON_BE(dbuf[2]); rmask = wxINT32_SWAP_ON_BE(dbuf[2]);
/* find shift amount.. ugly, but i can't think of a better way */ // find shift amount.. ugly, but i can't think of a better way:
for (bit = 0; bit < bpp; bit++) for (bit = 0; bit < bpp; bit++)
{ {
if (bmask & (1 << bit)) if (bmask & (1 << bit))
@@ -763,7 +566,8 @@ bool wxBMPHandler::DoLoadDib (wxImage * image, int width, int height, int bpp, i
/* /*
* Reading the image data * Reading the image data
*/ */
if ( IsBmp ) stream.SeekI( bmpOffset ); // else icon, just carry on if ( IsBmp )
stream.SeekI(bmpOffset); // else icon, just carry on
unsigned char *data = ptr; unsigned char *data = ptr;
@@ -813,7 +617,7 @@ bool wxBMPHandler::DoLoadDib (wxImage * image, int width, int height, int bpp, i
if ( verbose ) if ( verbose )
wxLogError(_("DIB Header: Cannot deal with 4bit encoded yet.")); wxLogError(_("DIB Header: Cannot deal with 4bit encoded yet."));
image->Destroy(); image->Destroy();
free(cmap); delete[] cmap;
return FALSE; return FALSE;
} }
else else
@@ -942,15 +746,15 @@ bool wxBMPHandler::DoLoadDib (wxImage * image, int width, int height, int bpp, i
} }
} }
if (cmap) if (cmap)
free(cmap); delete[] cmap;
image->SetMask(FALSE); image->SetMask(FALSE);
return stream.IsOk(); return stream.IsOk();
} }
bool wxBMPHandler::LoadDib(wxImage *image, wxInputStream& stream,
bool wxBMPHandler::LoadDib( wxImage *image, wxInputStream& stream, bool verbose, bool IsBmp ) bool verbose, bool IsBmp)
{ {
wxUint16 aWord; wxUint16 aWord;
wxInt32 dbuf[4]; wxInt32 dbuf[4];
@@ -966,7 +770,6 @@ bool wxBMPHandler::LoadDib( wxImage *image, wxInputStream& stream, bool verbose,
if (offset == wxInvalidOffset) offset = 0; if (offset == wxInvalidOffset) offset = 0;
stream.Read(bbuf, 2); stream.Read(bbuf, 2);
stream.Read(dbuf, 16); stream.Read(dbuf, 16);
} }
else else
@@ -1012,7 +815,8 @@ bool wxBMPHandler::LoadDib( wxImage *image, wxInputStream& stream, bool verbose,
stream.Read(dbuf, 4 * 4); stream.Read(dbuf, 4 * 4);
int comp = (int)wxINT32_SWAP_ON_BE(dbuf[0]); int comp = (int)wxINT32_SWAP_ON_BE(dbuf[0]);
if (comp != BI_RGB && comp != BI_RLE4 && comp != BI_RLE8 && comp != BI_BITFIELDS) if ( comp != BI_RGB && comp != BI_RLE4 && comp != BI_RLE8 &&
comp != BI_BITFIELDS )
{ {
if (verbose) if (verbose)
wxLogError( _("DIB Header: Unknown encoding in file.") ); wxLogError( _("DIB Header: Unknown encoding in file.") );
@@ -1057,68 +861,17 @@ bool wxBMPHandler::LoadDib( wxImage *image, wxInputStream& stream, bool verbose,
image->SetMaskFromImage(mask, 255, 255, 255); image->SetMaskFromImage(mask, 255, 255, 255);
} }
return TRUE; return TRUE;
} }
bool wxBMPHandler::LoadFile(wxImage *image, wxInputStream& stream,
bool wxBMPHandler::LoadFile ( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) ) bool verbose, int WXUNUSED(index))
{ {
bool IsBmp = TRUE; // Read a single DIB fom the file:
//Read a single DIB fom the file return LoadDib(image, stream, verbose, TRUE/*isBmp*/);
return LoadDib ( image, stream, verbose, IsBmp ) ;
} }
bool wxICOHandler::LoadFile ( wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index) )
{
bool bResult = FALSE ;
bool IsBmp = FALSE;
ICONDIR m_IconDir ;
stream.Read (&m_IconDir, sizeof(m_IconDir));
wxUint16 nIcons = wxUINT16_SWAP_ON_BE ( m_IconDir.idCount ) ;
//loop round the icons and choose the best one
ICONDIRENTRY * pIconDirEntry = new ICONDIRENTRY [nIcons];
ICONDIRENTRY * pCurrentEntry = pIconDirEntry ;
int i ;
int wMax = 0 ;
int colmax = 0 ;
int iSel = wxNOT_FOUND ;
for (i=0; i < nIcons ; i++ )
{
stream.Read(pCurrentEntry, sizeof(ICONDIRENTRY));
//bHeight and bColorCount are wxUint8
if (pCurrentEntry->bWidth >= wMax )
{
// see if we have more colors, ==0 indicates > 8bpp
if (pCurrentEntry->bColorCount == 0 ) pCurrentEntry->bColorCount = 255 ;
if (pCurrentEntry->bColorCount >= colmax)
{
iSel = i ;
wMax = pCurrentEntry->bWidth ;
colmax = pCurrentEntry->bColorCount ;
}
}
pCurrentEntry ++ ;
}
if (iSel == wxNOT_FOUND)
{
bResult = FALSE;
}
else
{
//seek to selected icon
pCurrentEntry = pIconDirEntry + iSel ;
stream.SeekI (wxUINT32_SWAP_ON_BE ( pCurrentEntry -> dwImageOffset ), wxFromStart ) ;
bResult = LoadDib ( image, stream, TRUE, IsBmp );
}
delete [] pIconDirEntry ;
return bResult ;
}
bool wxBMPHandler::DoCanRead(wxInputStream& stream) bool wxBMPHandler::DoCanRead(wxInputStream& stream)
{ {
unsigned char hdr[2]; unsigned char hdr[2];
@@ -1128,15 +881,336 @@ bool wxBMPHandler::DoCanRead( wxInputStream& stream )
return (hdr[0] == 'B' && hdr[1] == 'M'); return (hdr[0] == 'B' && hdr[1] == 'M');
} }
//-----------------------------------------------------------------------------
// wxICOHandler
//-----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC_CLASS(wxICOHandler, wxBMPHandler)
struct ICONDIRENTRY
{
wxUint8 bWidth; // Width of the image
wxUint8 bHeight; // Height of the image (times 2)
wxUint8 bColorCount; // Number of colors in image (0 if >=8bpp)
wxUint8 bReserved; // Reserved
// these two are different in icons and cursors:
// icon or cursor
wxUint16 wPlanes; // Color Planes or XHotSpot
wxUint16 wBitCount; // Bits per pixel or YHotSpot
wxUint32 dwBytesInRes; // how many bytes in this resource?
wxUint32 dwImageOffset; // where in the file is this image
};
struct ICONDIR
{
wxUint16 idReserved; // Reserved
wxUint16 idType; // resource type (1 for icons, 2 for cursors)
wxUint16 idCount; // how many images?
};
bool wxICOHandler::SaveFile(wxImage *image,
wxOutputStream& stream,
bool verbose)
{
bool bResult = FALSE;
//sanity check; icon must be less than 127 pixels high and 255 wide
if ( image->GetHeight () > 127 )
{
if ( verbose )
wxLogError(_("ICO: Image too tall for an icon."));
return FALSE;
}
if ( image->GetWidth () > 255 )
{
if ( verbose )
wxLogError(_("ICO: Image too wide for an icon."));
return FALSE;
}
int images = 1; // only generate one image
// VS: This is a hack of sort - since ICO and CUR files are almost
// identical, we have all the meat in wxICOHandler and check for
// the actual (handler) type when the code has to distinguish between
// the two formats
int type = (this->GetType() == wxBITMAP_TYPE_CUR) ? 2 : 1;
// write a header, (ICONDIR)
// Calculate the header size
wxUint32 offset = 3 * sizeof(wxUint16);
ICONDIR IconDir;
IconDir.idReserved = 0;
IconDir.idType = wxUINT16_SWAP_ON_BE(type);
IconDir.idCount = wxUINT16_SWAP_ON_BE(images);
stream.Write(&IconDir.idReserved, sizeof(IconDir.idReserved));
stream.Write(&IconDir.idType, sizeof(IconDir.idType));
stream.Write(&IconDir.idCount, sizeof(IconDir.idCount));
if ( !stream.IsOk() )
{
if ( verbose )
wxLogError(_("ICO: Error writing the image file!"));
return FALSE;
}
// for each iamage write a description ICONDIRENTRY:
ICONDIRENTRY icondirentry;
for (int i = 0; i < images; i++)
{
wxImage mask;
if ( image->HasMask() )
{
// make another image with black/white:
mask = image->ConvertToMono (image->GetMaskRed(), image->GetMaskGreen(), image->GetMaskBlue() );
// now we need to change the masked regions to black:
unsigned char r = image->GetMaskRed();
unsigned char g = image->GetMaskGreen();
unsigned char b = image->GetMaskBlue();
if ( (r != 0) || (g != 0) || (b != 0) )
{
// Go round and apply black to the masked bits:
int i, j;
for (i = 0; i < mask.GetWidth(); i++)
{
for (j = 0; j < mask.GetHeight(); j++)
{
if ((r == mask.GetRed(i, j)) &&
(g == mask.GetGreen(i, j))&&
(b == mask.GetBlue(i, j)) )
image->SetRGB(i, j, 0, 0, 0 );
}
}
}
}
else
{
// just make a black mask all over:
mask = image->Copy();
int i, j;
for (i = 0; i < mask.GetWidth(); i++)
for (j = 0; j < mask.GetHeight(); j++)
mask.SetRGB(i, j, 0, 0, 0 );
}
// Set the formats for image and mask
// (Windows never saves with more than 8 colors):
image->SetOption(wxBMP_FORMAT, wxBMP_8BPP);
// monochome bitmap:
mask.SetOption(wxBMP_FORMAT, wxBMP_1BPP_BW);
bool IsBmp = FALSE;
bool IsMask = FALSE;
//calculate size and offset of image and mask
wxCountingOutputStream cStream;
bResult = SaveDib(image, cStream, verbose, IsBmp, IsMask);
if ( !bResult )
{
if ( verbose )
wxLogError(_("ICO: Error writing the image file!"));
return FALSE;
}
IsMask = TRUE;
bResult = SaveDib(&mask, cStream, verbose, IsBmp, IsMask);
if ( !bResult )
{
if ( verbose )
wxLogError(_("ICO: Error writing the image file!"));
return FALSE;
}
wxUint32 Size = cStream.GetSize();
// wxCountingOutputStream::Ok() always returns TRUE for now and this
// "if" provokes VC++ warnings in optimized build
#if 0
if ( !cStream.Ok() )
{
if ( verbose )
wxLogError(_("ICO: Error writing the image file!"));
return FALSE;
}
#endif // 0
offset = offset + sizeof(ICONDIRENTRY);
icondirentry.bWidth = image->GetWidth();
icondirentry.bHeight = 2 * image->GetHeight();
icondirentry.bColorCount = 0;
icondirentry.bReserved = 0;
icondirentry.wPlanes = wxUINT16_SWAP_ON_BE(1);
icondirentry.wBitCount = wxUINT16_SWAP_ON_BE(wxBMP_8BPP);
if ( type == 2 /*CUR*/)
{
int hx = image->HasOption(wxCUR_HOTSPOT_X) ?
image->GetOptionInt(wxCUR_HOTSPOT_X) :
image->GetWidth() / 2;
int hy = image->HasOption(wxCUR_HOTSPOT_Y) ?
image->GetOptionInt(wxCUR_HOTSPOT_Y) :
image->GetHeight() / 2;
// actually write the values of the hot spot here:
icondirentry.wPlanes = wxUINT16_SWAP_ON_BE((wxUint16)hx);
icondirentry.wBitCount = wxUINT16_SWAP_ON_BE((wxUint16)hy);
}
icondirentry.dwBytesInRes = wxUINT32_SWAP_ON_BE(Size);
icondirentry.dwImageOffset = wxUINT32_SWAP_ON_BE(offset);
// increase size to allow for the data written:
offset += Size;
// write to stream:
stream.Write(&icondirentry.bWidth, sizeof(icondirentry.bWidth));
stream.Write(&icondirentry.bHeight, sizeof(icondirentry.bHeight));
stream.Write(&icondirentry.bColorCount, sizeof(icondirentry.bColorCount));
stream.Write(&icondirentry.bReserved, sizeof(icondirentry.bReserved));
stream.Write(&icondirentry.wPlanes, sizeof(icondirentry.wPlanes));
stream.Write(&icondirentry.wBitCount, sizeof(icondirentry.wBitCount));
stream.Write(&icondirentry.dwBytesInRes, sizeof(icondirentry.dwBytesInRes));
stream.Write(&icondirentry.dwImageOffset, sizeof(icondirentry.dwImageOffset));
if ( !stream.IsOk() )
{
if ( verbose )
wxLogError(_("ICO: Error writing the image file!"));
return FALSE;
}
// actually save it:
IsMask = FALSE;
bResult = SaveDib(image, stream, verbose, IsBmp, IsMask);
if ( !bResult )
{
if ( verbose )
wxLogError(_("ICO: Error writing the image file!"));
return FALSE;
}
IsMask = TRUE;
bResult = SaveDib(&mask, stream, verbose, IsBmp, IsMask);
if ( !bResult )
{
if ( verbose )
wxLogError(_("ICO: Error writing the image file!"));
return FALSE;
}
} // end of for loop
return TRUE;
}
bool wxICOHandler::LoadFile(wxImage *image, wxInputStream& stream,
bool verbose, int index)
{
bool bResult = FALSE;
bool IsBmp = FALSE;
ICONDIR IconDir;
stream.SeekI(0);
stream.Read(&IconDir, sizeof(IconDir));
wxUint16 nIcons = wxUINT16_SWAP_ON_BE(IconDir.idCount);
// nType is 1 for Icons, 2 for Cursors:
wxUint16 nType = wxUINT16_SWAP_ON_BE(IconDir.idType);
// loop round the icons and choose the best one:
ICONDIRENTRY *pIconDirEntry = new ICONDIRENTRY[nIcons];
ICONDIRENTRY *pCurrentEntry = pIconDirEntry;
int wMax = 0;
int colmax = 0;
int iSel = wxNOT_FOUND;
for (int i = 0; i < nIcons; i++ )
{
stream.Read(pCurrentEntry, sizeof(ICONDIRENTRY));
// bHeight and bColorCount are wxUint8
if ( pCurrentEntry->bWidth >= wMax )
{
// see if we have more colors, ==0 indicates > 8bpp:
if ( pCurrentEntry->bColorCount == 0 )
pCurrentEntry->bColorCount = 255;
if ( pCurrentEntry->bColorCount >= colmax )
{
iSel = i;
wMax = pCurrentEntry->bWidth;
colmax = pCurrentEntry->bColorCount;
}
}
pCurrentEntry++;
}
if ( index != -1 )
{
// VS: Note that we *have* to run the loop above even if index != -1, because
// it reads ICONDIRENTRies.
iSel = index;
}
if ( iSel == wxNOT_FOUND || iSel < 0 || iSel >= nIcons )
{
wxLogError(_("ICO: Invalid icon index."));
bResult = FALSE;
}
else
{
// seek to selected icon:
pCurrentEntry = pIconDirEntry + iSel;
stream.SeekI(wxUINT32_SWAP_ON_BE(pCurrentEntry->dwImageOffset), wxFromStart);
bResult = LoadDib(image, stream, TRUE, IsBmp);
if ( bResult && this->GetType() == wxBITMAP_TYPE_CUR && nType == 2 )
{
// it is a cursor, so let's set the hotspot:
image->SetOption(wxCUR_HOTSPOT_X, wxUINT16_SWAP_ON_BE(pCurrentEntry->wPlanes));
image->SetOption(wxCUR_HOTSPOT_Y, wxUINT16_SWAP_ON_BE(pCurrentEntry->wBitCount));
}
}
delete[] pIconDirEntry;
return bResult;
}
int wxICOHandler::GetImagesCount(wxInputStream& stream)
{
ICONDIR IconDir;
stream.SeekI(0);
stream.Read(&IconDir, sizeof(IconDir));
wxUint16 nIcons = wxUINT16_SWAP_ON_BE(IconDir.idCount);
return (int)nIcons;
}
bool wxICOHandler::DoCanRead(wxInputStream& stream) bool wxICOHandler::DoCanRead(wxInputStream& stream)
{ {
unsigned char hdr[4]; unsigned char hdr[4];
stream.SeekI (0);
stream.Read(hdr, 4); stream.Read(hdr, 4);
stream.SeekI(-4, wxFromCurrent); stream.SeekI(-4, wxFromCurrent);
//hdr[2] is one for an icon and two for a cursor
return (hdr[0] == '\0' && hdr[1] == '\0' && hdr[2] == '\1' && hdr[3] == '\0'); return (hdr[0] == '\0' && hdr[1] == '\0' && hdr[2] == '\1' && hdr[3] == '\0');
} }
#endif // wxUSE_STREAMS
#endif // wxUSE_IMAGE
//-----------------------------------------------------------------------------
// wxCURHandler
//-----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC_CLASS(wxCURHandler, wxICOHandler)
bool wxCURHandler::DoCanRead(wxInputStream& stream)
{
unsigned char hdr[4];
stream.SeekI (0);
stream.Read(hdr, 4);
stream.SeekI(-4, wxFromCurrent);
//hdr[2] is one for an icon and two for a cursor
return (hdr[0] == '\0' && hdr[1] == '\0' && hdr[2] == '\2' && hdr[3] == '\0');
}
#endif // wxUSE_IMAGE && wxUSE_STREAMS