#include "wx/filefn.h"
#include "wx/wfstream.h"
#include "wx/quantize.h"
+#include "wx/scopeguard.h"
#include "wx/anidecod.h"
// For memcpy
// get the resolution from the image options or fall back to 72dpi standard
// for the BMP format if not specified
- wxUint32 hres = image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX),
- vres = image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY);
- switch ( image->GetOptionInt(wxIMAGE_OPTION_RESOLUTION) )
+ int hres, vres;
+ switch ( GetResolutionFromOptions(*image, &hres, &vres) )
{
default:
wxFAIL_MSG( _T("unexpected image resolution units") );
case wxIMAGE_RESOLUTION_INCHES:
// convert resolution in inches to resolution in centimeters
- hres = (wxUint32)(100*mm2inches*hres);
- vres = (wxUint32)(100*mm2inches*vres);
+ hres = (int)(10*mm2inches*hres);
+ vres = (int)(10*mm2inches*vres);
// fall through to convert it to resolution in meters
case wxIMAGE_RESOLUTION_CM:
}
-typedef struct
+struct BMPPalette
{
+ static void Free(BMPPalette* pal) { delete [] pal; }
+
unsigned char r, g, b;
-} _cmap;
+};
bool wxBMPHandler::DoLoadDib(wxImage * image, int width, int height,
int bpp, int ncolors, int comp,
{
wxInt32 aDword, rmask = 0, gmask = 0, bmask = 0, amask = 0;
int rshift = 0, gshift = 0, bshift = 0, ashift = 0;
- int rbits = 0, gbits = 0, bbits = 0, abits = 0;
+ int rbits = 0, gbits = 0, bbits = 0;
wxInt32 dbuf[4];
wxInt8 bbuf[4];
wxUint8 aByte;
wxUint16 aWord;
// allocate space for palette if needed:
- _cmap *cmap;
+ BMPPalette *cmap;
if ( bpp < 16 )
{
- cmap = new _cmap[ncolors];
+ cmap = new BMPPalette[ncolors];
if ( !cmap )
{
if (verbose)
return false;
}
}
- else
+ else // no palette
+ {
cmap = NULL;
+ }
+
+ wxON_BLOCK_EXIT1(&BMPPalette::Free, cmap);
// destroy existing here instead of:
image->Destroy();
{
if ( verbose )
wxLogError( _("BMP: Couldn't allocate memory.") );
- delete[] cmap;
return false;
}
{
if ( verbose )
wxLogError(_("BMP: Couldn't allocate memory."));
- delete[] cmap;
return false;
}
}
rshift = 16;
gshift = 8;
bshift = 0;
- abits = 8;
rbits = 8;
gbits = 8;
bbits = 8;
* Reading the image data
*/
if ( IsBmp )
- stream.SeekI(bmpOffset); // else icon, just carry on
+ {
+ if (stream.SeekI(bmpOffset) == wxInvalidOffset)
+ return false;
+ //else: icon, just carry on
+ }
unsigned char *data = ptr;
}
}
- delete[] cmap;
-
image->SetMask(false);
const wxStreamError err = stream.GetLastError();
bool wxICOHandler::LoadFile(wxImage *image, wxInputStream& stream,
bool verbose, int index)
{
- stream.SeekI(0);
+ if (stream.SeekI(0) == wxInvalidOffset)
+ return false;
return DoLoadFile(image, stream, verbose, index);
}
{
// seek to selected icon:
pCurrentEntry = pIconDirEntry + iSel;
- stream.SeekI(iPos + wxUINT32_SWAP_ON_BE(pCurrentEntry->dwImageOffset), wxFromStart);
+ if (stream.SeekI(iPos + wxUINT32_SWAP_ON_BE(pCurrentEntry->dwImageOffset), wxFromStart) == wxInvalidOffset)
+ return false;
bResult = LoadDib(image, stream, true, IsBmp);
bool bIsCursorType = (this->GetType() == wxBITMAP_TYPE_CUR) || (this->GetType() == wxBITMAP_TYPE_ANI);
if ( bResult && bIsCursorType && nType == 2 )
{
ICONDIR IconDir;
wxFileOffset iPos = stream.TellI();
- stream.SeekI(0);
- stream.Read(&IconDir, sizeof(IconDir));
+ if (stream.SeekI(0) == wxInvalidOffset)
+ return 0;
+ if (stream.Read(&IconDir, sizeof(IconDir)).LastRead() != sizeof(IconDir))
+ return 0;
wxUint16 nIcons = wxUINT16_SWAP_ON_BE(IconDir.idCount);
- stream.SeekI(iPos);
+ if (stream.SeekI(iPos) == wxInvalidOffset)
+ return 0;
return (int)nIcons;
}
bool wxICOHandler::DoCanRead(wxInputStream& stream)
{
- stream.SeekI(0);
+ if (stream.SeekI(0) == wxInvalidOffset)
+ return false;
unsigned char hdr[4];
if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
return false;
bool wxCURHandler::DoCanRead(wxInputStream& stream)
{
- stream.SeekI(0);
+ if (stream.SeekI(0) == wxInvalidOffset)
+ return false;
unsigned char hdr[4];
if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
return false;