// 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)(10*mm2inches*hres);
- vres = (wxUint32)(10*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:
{
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;
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
+ {
+ // NOTE: seeking a positive amount in wxFromCurrent mode allows us to
+ // load even non-seekable streams (see wxInputStream::SeekI docs)!
+ if (stream.SeekI(bmpOffset, wxFromCurrent) == wxInvalidOffset)
+ return false;
+ //else: icon, just carry on
+ }
unsigned char *data = ptr;
wxInt32 dbuf[4];
wxInt8 bbuf[4];
- wxFileOffset 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);
}
#if 0 // unused
wxInt32 size = wxINT32_SWAP_ON_BE(dbuf[0]);
#endif
- offset = offset + wxINT32_SWAP_ON_BE(dbuf[2]);
+ wxFileOffset offset = wxINT32_SWAP_ON_BE(dbuf[2]);
stream.Read(dbuf, 4 * 2);
int width = wxINT32_SWAP_ON_BE((int)dbuf[0]);
{
unsigned char hdr[2];
- if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
+ if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) // it's ok to modify the stream position here
return false;
// do we have the BMP file signature?
bool wxICOHandler::LoadFile(wxImage *image, wxInputStream& stream,
bool verbose, int index)
{
- stream.SeekI(0);
return DoLoadFile(image, stream, verbose, index);
}
ICONDIR IconDir;
- wxFileOffset iPos = stream.TellI();
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);
int colmax = 0;
int iSel = wxNOT_FOUND;
- for (int i = 0; i < nIcons; i++ )
+ // remember how many bytes we read from the stream:
+ wxFileOffset alreadySeeked = sizeof(IconDir);
+
+ for (unsigned int i = 0; i < nIcons; i++ )
{
- stream.Read(pCurrentEntry, sizeof(ICONDIRENTRY));
+ alreadySeeked += stream.Read(pCurrentEntry, sizeof(ICONDIRENTRY)).LastRead();
+
// bHeight and bColorCount are wxUint8
if ( pCurrentEntry->bWidth >= wMax )
{
colmax = pCurrentEntry->bColorCount;
}
}
+
pCurrentEntry++;
}
{
// seek to selected icon:
pCurrentEntry = pIconDirEntry + iSel;
- stream.SeekI(iPos + wxUINT32_SWAP_ON_BE(pCurrentEntry->dwImageOffset), wxFromStart);
+
+ // NOTE: seeking a positive amount in wxFromCurrent mode allows us to
+ // load even non-seekable streams (see wxInputStream::SeekI docs)!
+ wxFileOffset offset = wxUINT32_SWAP_ON_BE(pCurrentEntry->dwImageOffset) - alreadySeeked;
+ if (offset != 0 && stream.SeekI(offset, wxFromCurrent) == 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 )
image->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, wxUINT16_SWAP_ON_BE(pCurrentEntry->wBitCount));
}
}
- delete[] pIconDirEntry;
+
+ delete [] pIconDirEntry;
+
return bResult;
}
-int wxICOHandler::GetImageCount(wxInputStream& stream)
+int wxICOHandler::DoGetImageCount(wxInputStream& stream)
{
ICONDIR IconDir;
- wxFileOffset iPos = stream.TellI();
- stream.SeekI(0);
- stream.Read(&IconDir, sizeof(IconDir));
- wxUint16 nIcons = wxUINT16_SWAP_ON_BE(IconDir.idCount);
- stream.SeekI(iPos);
- return (int)nIcons;
+
+ if (stream.Read(&IconDir, sizeof(IconDir)).LastRead() != sizeof(IconDir))
+ // it's ok to modify the stream position here
+ return 0;
+
+ return (int)wxUINT16_SWAP_ON_BE(IconDir.idCount);
}
bool wxICOHandler::DoCanRead(wxInputStream& stream)
{
- stream.SeekI(0);
unsigned char hdr[4];
- if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
+ if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) // it's ok to modify the stream position here
return false;
// hdr[2] is one for an icon and two for a cursor
bool wxCURHandler::DoCanRead(wxInputStream& stream)
{
- stream.SeekI(0);
unsigned char hdr[4];
- if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
+ if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) // it's ok to modify the stream position here
return false;
// hdr[2] is one for an icon and two for a cursor
{
wxANIDecoder decod;
return decod.CanRead(stream);
+ // it's ok to modify the stream position here
}
-int wxANIHandler::GetImageCount(wxInputStream& stream)
+int wxANIHandler::DoGetImageCount(wxInputStream& stream)
{
wxANIDecoder decoder;
- if (!decoder.Load(stream))
+ if (!decoder.Load(stream)) // it's ok to modify the stream position here
return wxNOT_FOUND;
return decoder.GetFrameCount();