projects
/
wxWidgets.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
call ProcessPendingEvents() from wxAppConsoleBase::ProcessIdle() too, not only from...
[wxWidgets.git]
/
src
/
common
/
imagtga.cpp
diff --git
a/src/common/imagtga.cpp
b/src/common/imagtga.cpp
index bf99b1af4cff216455598bc86f3a7e7a10fd99c3..fc7373822bb0090ac06b3751027ebeb3d70538d2 100644
(file)
--- a/
src/common/imagtga.cpp
+++ b/
src/common/imagtga.cpp
@@
-30,6
+30,7
@@
#include "wx/imagtga.h"
#include "wx/log.h"
#include "wx/imagtga.h"
#include "wx/log.h"
+#include "wx/scopeguard.h"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// constants
@@
-38,9
+39,10
@@
// TGA error codes.
enum
{
// TGA error codes.
enum
{
- wxTGA_OK = 0,
- wxTGA_INVFORMAT = 1,
- wxTGA_MEMERR = 2
+ wxTGA_OK,
+ wxTGA_INVFORMAT,
+ wxTGA_MEMERR,
+ wxTGA_IOERR
};
// TGA header bytes.
};
// TGA header bytes.
@@
-99,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)
{
unsigned long index = 0;
short pixelSize, wxInputStream& stream)
{
unsigned long index = 0;
@@
-110,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.
if ( current & 0x80 )
// RLE packet.
if ( current & 0x80 )
@@
-125,7
+132,8
@@
void DecodeRLE(unsigned char* imageData, unsigned long imageSize,
index += current * pixelSize;
// Repeat the pixel length times.
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++)
{
for (unsigned int i = 0; i < length; i++)
{
@@
-144,11
+152,14
@@
void DecodeRLE(unsigned char* imageData, unsigned long imageSize,
index += length;
// Write the next length pixels directly to the image data.
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;
}
}
imageData += length;
}
}
+
+ return wxTGA_OK;
}
static
}
static
@@
-187,6
+198,8
@@
int ReadTGA(wxImage* image, wxInputStream& stream)
return wxTGA_MEMERR;
}
return wxTGA_MEMERR;
}
+ wxON_BLOCK_EXIT1(free, imageData);
+
unsigned char *dst = image->GetData();
unsigned char* alpha = NULL;
unsigned char *dst = image->GetData();
unsigned char* alpha = NULL;
@@
-218,13
+231,10
@@
int ReadTGA(wxImage* image, wxInputStream& stream)
b[i] = buf[0];
}
b[i] = buf[0];
}
- #if wxUSE_PALETTE
-
+#if wxUSE_PALETTE
// Set the palette of the image.
// Set the palette of the image.
-
image->SetPalette(wxPalette(paletteLength, r, g, b));
image->SetPalette(wxPalette(paletteLength, r, g, b));
-
- #endif // wxUSE_PALETTE
+#endif // wxUSE_PALETTE
delete[] r;
delete[] g;
delete[] r;
delete[] g;
@@
-235,6
+245,7
@@
int ReadTGA(wxImage* image, wxInputStream& stream)
switch (imageType)
{
switch (imageType)
{
+#if wxUSE_PALETTE
// Raw indexed.
case 1:
// Raw indexed.
case 1:
@@
-296,6
+307,7
@@
int ReadTGA(wxImage* image, wxInputStream& stream)
}
}
break;
}
}
break;
+#endif // wxUSE_PALETTE
// Raw RGB.
// Raw RGB.
@@
-428,6
+440,7
@@
int ReadTGA(wxImage* image, wxInputStream& stream)
}
break;
}
break;
+#if wxUSE_PALETTE
// RLE indexed.
case 9:
// RLE indexed.
case 9:
@@
-439,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.
// We need to store it right side up.
// If orientation == 0, then the image is stored upside down.
// We need to store it right side up.
@@
-489,6
+504,7
@@
int ReadTGA(wxImage* image, wxInputStream& stream)
}
}
break;
}
}
break;
+#endif // wxUSE_PALETTE
// RLE RGB.
// RLE RGB.
@@
-496,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.
// We need to store it right side up.
// If orientation == 0, then the image is stored upside down.
// We need to store it right side up.
@@
-574,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.
// We need to store it right side up.
// If orientation == 0, then the image is stored upside down.
// We need to store it right side up.
@@
-625,8
+645,6
@@
int ReadTGA(wxImage* image, wxInputStream& stream)
return wxTGA_INVFORMAT;
}
return wxTGA_INVFORMAT;
}
- free(imageData);
-
return wxTGA_OK;
}
return wxTGA_OK;
}
@@
-672,6
+690,10
@@
bool wxTGAHandler::LoadFile(wxImage* image,
wxLogError(wxT("TGA: couldn't allocate memory."));
break;
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!"));
}
default:
wxLogError(wxT("TGA: unknown error!"));
}