Cleaned up wxGIFDecoder.
Applied patch by troelsk which mostly makes the GIF decoder more readable by using named constants instead of magic numbers. Left out the edits that changed unsigned char to wxUint8. In addition removed unnecessary casts around wxInputStream.GetC() calls. Closes #12506. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66566 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -29,7 +29,16 @@
|
|||||||
#include "wx/scopedptr.h"
|
#include "wx/scopedptr.h"
|
||||||
#include "wx/scopeguard.h"
|
#include "wx/scopeguard.h"
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
GIF_MARKER_EXT = '!', // 0x21
|
||||||
|
GIF_MARKER_SEP = ',', // 0x2C
|
||||||
|
GIF_MARKER_ENDOFDATA = ';', // 0x3B
|
||||||
|
|
||||||
|
GIF_MARKER_EXT_GRAPHICS_CONTROL = 0xF9,
|
||||||
|
GIF_MARKER_EXT_COMMENT = 0xFE,
|
||||||
|
GIF_MARKER_EXT_APP = 0xFF
|
||||||
|
};
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
// GIFImage
|
// GIFImage
|
||||||
@@ -254,7 +263,7 @@ int wxGIFDecoder::getcode(wxInputStream& stream, int bits, int ab_fin)
|
|||||||
// if no bytes left in this block, read the next block
|
// if no bytes left in this block, read the next block
|
||||||
if (m_restbyte == 0)
|
if (m_restbyte == 0)
|
||||||
{
|
{
|
||||||
m_restbyte = (unsigned char)stream.GetC();
|
m_restbyte = stream.GetC();
|
||||||
|
|
||||||
/* Some encoders are a bit broken: instead of issuing
|
/* Some encoders are a bit broken: instead of issuing
|
||||||
* an end-of-image symbol (ab_fin) they come up with
|
* an end-of-image symbol (ab_fin) they come up with
|
||||||
@@ -667,11 +676,11 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
|
|||||||
bool done = false;
|
bool done = false;
|
||||||
while (!done)
|
while (!done)
|
||||||
{
|
{
|
||||||
type = (unsigned char)stream.GetC();
|
type = stream.GetC();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
If the end of file has been reached (or an error) and a ";"
|
If the end of file has been reached (or an error) and a ";"
|
||||||
(0x3B) hasn't been encountered yet, exit the loop. (Without this
|
(GIF_MARKER_ENDOFDATA) hasn't been encountered yet, exit the loop. (Without this
|
||||||
check the while loop would loop endlessly.) Later on, in the next while
|
check the while loop would loop endlessly.) Later on, in the next while
|
||||||
loop, the file will be treated as being truncated (But still
|
loop, the file will be treated as being truncated (But still
|
||||||
be decoded as far as possible). returning wxGIF_TRUNCATED is not
|
be decoded as far as possible). returning wxGIF_TRUNCATED is not
|
||||||
@@ -686,16 +695,13 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
|
|||||||
break; // Alternative : "return wxGIF_INVFORMAT;"
|
break; // Alternative : "return wxGIF_INVFORMAT;"
|
||||||
}
|
}
|
||||||
|
|
||||||
// end of data?
|
switch (type)
|
||||||
if (type == 0x3B)
|
|
||||||
{
|
{
|
||||||
|
case GIF_MARKER_ENDOFDATA:
|
||||||
done = true;
|
done = true;
|
||||||
}
|
break;
|
||||||
else
|
case GIF_MARKER_EXT:
|
||||||
// extension block?
|
if (stream.GetC() == GIF_MARKER_EXT_GRAPHICS_CONTROL)
|
||||||
if (type == 0x21)
|
|
||||||
{
|
|
||||||
if (((unsigned char)stream.GetC()) == 0xF9)
|
|
||||||
// graphics control extension, parse it
|
// graphics control extension, parse it
|
||||||
{
|
{
|
||||||
static const unsigned int gceSize = 6;
|
static const unsigned int gceSize = 6;
|
||||||
@@ -718,7 +724,7 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
|
|||||||
else
|
else
|
||||||
// other extension, skip
|
// other extension, skip
|
||||||
{
|
{
|
||||||
while ((i = (unsigned char)stream.GetC()) != 0)
|
while ((i = stream.GetC()) != 0)
|
||||||
{
|
{
|
||||||
if (stream.Eof() || (stream.LastRead() == 0) ||
|
if (stream.Eof() || (stream.LastRead() == 0) ||
|
||||||
stream.SeekI(i, wxFromCurrent) == wxInvalidOffset)
|
stream.SeekI(i, wxFromCurrent) == wxInvalidOffset)
|
||||||
@@ -728,10 +734,8 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
else
|
case GIF_MARKER_SEP:
|
||||||
// image descriptor block?
|
|
||||||
if (type == 0x2C)
|
|
||||||
{
|
{
|
||||||
// allocate memory for IMAGEN struct
|
// allocate memory for IMAGEN struct
|
||||||
GIFImagePtr pimg(new GIFImage());
|
GIFImagePtr pimg(new GIFImage());
|
||||||
@@ -749,10 +753,10 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
|
|||||||
|
|
||||||
pimg->left = buf[0] + 256 * buf[1];
|
pimg->left = buf[0] + 256 * buf[1];
|
||||||
pimg->top = buf[2] + 256 * buf[3];
|
pimg->top = buf[2] + 256 * buf[3];
|
||||||
/*
|
/*
|
||||||
pimg->left = buf[4] + 256 * buf[5];
|
pimg->left = buf[4] + 256 * buf[5];
|
||||||
pimg->top = buf[4] + 256 * buf[5];
|
pimg->top = buf[4] + 256 * buf[5];
|
||||||
*/
|
*/
|
||||||
pimg->w = buf[4] + 256 * buf[5];
|
pimg->w = buf[4] + 256 * buf[5];
|
||||||
pimg->h = buf[6] + 256 * buf[7];
|
pimg->h = buf[6] + 256 * buf[7];
|
||||||
|
|
||||||
@@ -812,7 +816,7 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get initial code size from first byte in raster data
|
// get initial code size from first byte in raster data
|
||||||
bits = (unsigned char)stream.GetC();
|
bits = stream.GetC();
|
||||||
if (bits == 0)
|
if (bits == 0)
|
||||||
return wxGIF_INVFORMAT;
|
return wxGIF_INVFORMAT;
|
||||||
|
|
||||||
@@ -830,6 +834,8 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
|
|||||||
// if this is not an animated GIF, exit after first image
|
// if this is not an animated GIF, exit after first image
|
||||||
if (!anim)
|
if (!anim)
|
||||||
done = true;
|
done = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -840,20 +846,21 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// try to read to the end of the stream
|
// try to read to the end of the stream
|
||||||
while (type != 0x3B)
|
while (type != GIF_MARKER_ENDOFDATA)
|
||||||
{
|
{
|
||||||
if (!stream.IsOk())
|
if (!stream.IsOk())
|
||||||
return wxGIF_TRUNCATED;
|
return wxGIF_TRUNCATED;
|
||||||
|
|
||||||
type = (unsigned char)stream.GetC();
|
type = stream.GetC();
|
||||||
|
|
||||||
if (type == 0x21)
|
switch (type)
|
||||||
{
|
{
|
||||||
|
case GIF_MARKER_EXT:
|
||||||
// extension type
|
// extension type
|
||||||
(void) stream.GetC();
|
(void) stream.GetC();
|
||||||
|
|
||||||
// skip all data
|
// skip all data
|
||||||
while ((i = (unsigned char)stream.GetC()) != 0)
|
while ((i = stream.GetC()) != 0)
|
||||||
{
|
{
|
||||||
if (stream.Eof() || (stream.LastRead() == 0) ||
|
if (stream.Eof() || (stream.LastRead() == 0) ||
|
||||||
stream.SeekI(i, wxFromCurrent) == wxInvalidOffset)
|
stream.SeekI(i, wxFromCurrent) == wxInvalidOffset)
|
||||||
@@ -862,8 +869,8 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
|
|||||||
return wxGIF_INVFORMAT;
|
return wxGIF_INVFORMAT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
else if (type == 0x2C)
|
case GIF_MARKER_SEP:
|
||||||
{
|
{
|
||||||
// image descriptor block
|
// image descriptor block
|
||||||
static const unsigned int idbSize = (2 + 2 + 2 + 2 + 1);
|
static const unsigned int idbSize = (2 + 2 + 2 + 2 + 1);
|
||||||
@@ -895,7 +902,7 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// skip all data
|
// skip all data
|
||||||
while ((i = (unsigned char)stream.GetC()) != 0)
|
while ((i = stream.GetC()) != 0)
|
||||||
{
|
{
|
||||||
if (stream.Eof() || (stream.LastRead() == 0) ||
|
if (stream.Eof() || (stream.LastRead() == 0) ||
|
||||||
stream.SeekI(i, wxFromCurrent) == wxInvalidOffset)
|
stream.SeekI(i, wxFromCurrent) == wxInvalidOffset)
|
||||||
@@ -904,12 +911,16 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
|
|||||||
return wxGIF_INVFORMAT;
|
return wxGIF_INVFORMAT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else if ((type != 0x3B) && (type != 00)) // testing
|
default:
|
||||||
|
if ((type != GIF_MARKER_ENDOFDATA) && (type != 00)) // testing
|
||||||
{
|
{
|
||||||
// images are OK, but couldn't read to the end of the stream
|
// images are OK, but couldn't read to the end of the stream
|
||||||
return wxGIF_TRUNCATED;
|
return wxGIF_TRUNCATED;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return wxGIF_OK;
|
return wxGIF_OK;
|
||||||
|
Reference in New Issue
Block a user