added IO errors handling to TGA reading code

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45161 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2007-03-30 16:33:12 +00:00
parent 853c39d78b
commit 8fee630648

View File

@@ -39,9 +39,10 @@
// TGA error codes. // TGA error codes.
enum enum
{ {
wxTGA_OK = 0, wxTGA_OK,
wxTGA_INVFORMAT = 1, wxTGA_INVFORMAT,
wxTGA_MEMERR = 2 wxTGA_MEMERR,
wxTGA_IOERR
}; };
// TGA header bytes. // TGA header bytes.
@@ -100,8 +101,9 @@ void FlipTGA(unsigned char* imageData, int width, int height, short pixelSize)
} }
} }
// return wxTGA_OK or wxTGA_IOERR
static static
void DecodeRLE(unsigned char* imageData, unsigned long imageSize, int DecodeRLE(unsigned char* imageData, unsigned long imageSize,
short pixelSize, wxInputStream& stream) short pixelSize, wxInputStream& stream)
{ {
unsigned long index = 0; unsigned long index = 0;
@@ -111,7 +113,11 @@ void DecodeRLE(unsigned char* imageData, unsigned long imageSize,
while (index < imageSize) while (index < imageSize)
{ {
current = stream.GetC(); int ch = stream.GetC();
if ( ch == wxEOF )
return wxTGA_IOERR;
current = ch;
// RLE packet. // RLE packet.
if ( current & 0x80 ) if ( current & 0x80 )
@@ -126,7 +132,8 @@ void DecodeRLE(unsigned char* imageData, unsigned long imageSize,
index += current * pixelSize; index += current * pixelSize;
// Repeat the pixel length times. // Repeat the pixel length times.
stream.Read(buf, pixelSize); if ( !stream.Read(buf, pixelSize) )
return wxTGA_IOERR;
for (unsigned int i = 0; i < length; i++) for (unsigned int i = 0; i < length; i++)
{ {
@@ -145,11 +152,14 @@ void DecodeRLE(unsigned char* imageData, unsigned long imageSize,
index += length; index += length;
// Write the next length pixels directly to the image data. // Write the next length pixels directly to the image data.
stream.Read(imageData, length); if ( !stream.Read(imageData, length) )
return wxTGA_IOERR;
imageData += length; imageData += length;
} }
} }
return wxTGA_OK;
} }
static static
@@ -442,7 +452,9 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
// Decode the RLE data. // Decode the RLE data.
DecodeRLE(imageData, imageSize, pixelSize, stream); int rc = DecodeRLE(imageData, imageSize, pixelSize, stream);
if ( rc != wxTGA_OK )
return rc;
// If orientation == 0, then the image is stored upside down. // If orientation == 0, then the image is stored upside down.
// We need to store it right side up. // We need to store it right side up.
@@ -500,7 +512,9 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
{ {
// Decode the RLE data. // Decode the RLE data.
DecodeRLE(imageData, imageSize, pixelSize, stream); int rc = DecodeRLE(imageData, imageSize, pixelSize, stream);
if ( rc != wxTGA_OK )
return rc;
// If orientation == 0, then the image is stored upside down. // If orientation == 0, then the image is stored upside down.
// We need to store it right side up. // We need to store it right side up.
@@ -578,7 +592,9 @@ int ReadTGA(wxImage* image, wxInputStream& stream)
{ {
// Decode the RLE data. // Decode the RLE data.
DecodeRLE(imageData, imageSize, pixelSize, stream); int rc = DecodeRLE(imageData, imageSize, pixelSize, stream);
if ( rc != wxTGA_OK )
return rc;
// If orientation == 0, then the image is stored upside down. // If orientation == 0, then the image is stored upside down.
// We need to store it right side up. // We need to store it right side up.
@@ -674,6 +690,10 @@ bool wxTGAHandler::LoadFile(wxImage* image,
wxLogError(wxT("TGA: couldn't allocate memory.")); wxLogError(wxT("TGA: couldn't allocate memory."));
break; break;
case wxTGA_IOERR:
wxLogError(wxT("TGA: couldn't read image data."));
break;
default: default:
wxLogError(wxT("TGA: unknown error!")); wxLogError(wxT("TGA: unknown error!"));
} }