#include "wx/imagtga.h"
#include "wx/log.h"
+#include "wx/scopeguard.h"
// ----------------------------------------------------------------------------
// constants
// TGA error codes.
enum
{
- wxTGA_OK = 0,
- wxTGA_INVFORMAT = 1,
- wxTGA_MEMERR = 2
+ wxTGA_OK,
+ wxTGA_INVFORMAT,
+ wxTGA_MEMERR,
+ wxTGA_IOERR
};
// TGA header bytes.
}
}
+// return wxTGA_OK or wxTGA_IOERR
static
-void DecodeRLE(unsigned char* imageData, unsigned long imageSize,
+int DecodeRLE(unsigned char* imageData, unsigned long imageSize,
short pixelSize, wxInputStream& stream)
{
unsigned long index = 0;
while (index < imageSize)
{
- current = stream.GetC();
+ int ch = stream.GetC();
+ if ( ch == wxEOF )
+ return wxTGA_IOERR;
+
+ current = ch;
// RLE packet.
if ( current & 0x80 )
index += current * pixelSize;
// 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++)
{
index += length;
// Write the next length pixels directly to the image data.
- stream.Read(imageData, length);
+ if ( !stream.Read(imageData, length) )
+ return wxTGA_IOERR;
imageData += length;
}
}
+
+ return wxTGA_OK;
}
static
return wxTGA_MEMERR;
}
+ wxON_BLOCK_EXIT1(free, imageData);
+
unsigned char *dst = image->GetData();
unsigned char* alpha = NULL;
}
// Seek from the offset we got from the TGA header.
- stream.SeekI(offset, wxFromStart);
+ if (stream.SeekI(offset, wxFromStart) == wxInvalidOffset)
+ return wxTGA_INVFORMAT;
// Load a palette if we have one.
if (colorType == wxTGA_MAPPED)
b[i] = buf[0];
}
- #if wxUSE_PALETTE
-
+#if wxUSE_PALETTE
// Set the palette of the image.
-
image->SetPalette(wxPalette(paletteLength, r, g, b));
-
- #endif // wxUSE_PALETTE
+#endif // wxUSE_PALETTE
delete[] r;
delete[] g;
switch (imageType)
{
+#if wxUSE_PALETTE
// Raw indexed.
case 1:
}
}
break;
+#endif // wxUSE_PALETTE
// Raw RGB.
}
break;
+#if wxUSE_PALETTE
// RLE indexed.
case 9:
// 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.
// We need to store it right side up.
}
}
break;
+#endif // wxUSE_PALETTE
// RLE RGB.
{
// 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.
// We need to store it right side up.
{
// 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.
// We need to store it right side up.
return wxTGA_INVFORMAT;
}
- free(imageData);
-
return wxTGA_OK;
}
if ( !CanRead(stream) )
{
if ( verbose )
+ {
wxLogError(wxT("TGA: this is not a TGA file."));
+ }
return false;
}
wxLogError(wxT("TGA: couldn't allocate memory."));
break;
+ case wxTGA_IOERR:
+ wxLogError(wxT("TGA: couldn't read image data."));
+ break;
+
default:
wxLogError(wxT("TGA: unknown error!"));
}
{
// read the fixed-size TGA headers
unsigned char hdr[HDR_SIZE];
- stream.Read(hdr, HDR_SIZE);
+ stream.Read(hdr, HDR_SIZE); // it's ok to modify the stream position here
// Check wether we can read the file or not.