/////////////////////////////////////////////////////////////////////////////
-// Name: gifdecod.cpp
+// Name: src/common/gifdecod.cpp
// Purpose: wxGIFDecoder, GIF reader for wxImage and wxAnimation
// Author: Guillermo Rodriguez Garcia <guille@iies.es>
// Version: 3.04
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
-#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
-#pragma implementation "gifdecod.h"
-#endif
-
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
-# pragma hdrstop
+ #pragma hdrstop
#endif
+#if wxUSE_STREAMS && wxUSE_GIF
+
#ifndef WX_PRECOMP
-# include "wx/defs.h"
-# include "wx/palette.h"
+ #include "wx/palette.h"
#endif
-#if wxUSE_STREAMS && wxUSE_GIF
-
#include <stdlib.h>
#include <string.h>
#include "wx/gifdecod.h"
delay = -1;
p = (unsigned char *) NULL;
pal = (unsigned char *) NULL;
+ ncolours = 0;
next = (GIFImage *) NULL;
prev = (GIFImage *) NULL;
}
/* set transparent colour mask */
if (transparent != -1)
{
- for (i = 0; i < 256; i++)
+ for (i = 0; i < GetNcolours(); i++)
{
if ((pal[3 * i + 0] == 255) &&
(pal[3 * i + 1] == 0) &&
image->SetMask(false);
#if wxUSE_PALETTE
- if (pal)
- {
- unsigned char r[256];
- unsigned char g[256];
- unsigned char b[256];
+ unsigned char r[256];
+ unsigned char g[256];
+ unsigned char b[256];
- for (i = 0; i < 256; i++)
- {
- r[i] = pal[3*i + 0];
- g[i] = pal[3*i + 1];
- b[i] = pal[3*i + 2];
- }
-
- image->SetPalette(wxPalette(256, r, g, b));
+ for (i = 0; i < 256; i++)
+ {
+ r[i] = pal[3*i + 0];
+ g[i] = pal[3*i + 1];
+ b[i] = pal[3*i + 2];
}
+
+ image->SetPalette(wxPalette(GetNcolours(), r, g, b));
#endif // wxUSE_PALETTE
/* copy image data */
int wxGIFDecoder::GetFrameIndex() const { return m_image; }
unsigned char* wxGIFDecoder::GetData() const { return (m_pimage->p); }
unsigned char* wxGIFDecoder::GetPalette() const { return (m_pimage->pal); }
+unsigned int wxGIFDecoder::GetNcolours() const { return (m_pimage->ncolours); }
unsigned int wxGIFDecoder::GetWidth() const { return (m_pimage->w); }
unsigned int wxGIFDecoder::GetHeight() const { return (m_pimage->h); }
unsigned int wxGIFDecoder::GetTop() const { return (m_pimage->top); }
/* make new entry in alphabet (only if NOT just cleared) */
if (lastcode != -1)
{
+ // Normally, after the alphabet is full and can't grow any
+ // further (ab_free == 4096), encoder should (must?) emit CLEAR
+ // to reset it. This checks whether we really got it, otherwise
+ // the GIF is damaged.
+ if (ab_free > ab_max)
+ {
+ delete[] ab_prefix;
+ delete[] ab_tail;
+ delete[] stack;
+ return wxGIF_INVFORMAT;
+ }
+
+ // This assert seems unnecessary since the condition above
+ // eliminates the only case in which it went false. But I really
+ // don't like being forced to ask "Who in .text could have
+ // written there?!" And I wouldn't have been forced to ask if
+ // this line had already been here.
+ wxASSERT(ab_free < allocSize);
+
ab_prefix[ab_free] = lastcode;
ab_tail[ab_free] = code;
ab_free++;
//
int wxGIFDecoder::ReadGIF()
{
- unsigned int ncolors;
+ unsigned int global_ncolors = 0;
int bits, interl, transparent, disposal, i;
long size;
long delay;
m_screenw = buf[0] + 256 * buf[1];
m_screenh = buf[2] + 256 * buf[3];
+ if ((m_screenw == 0) || (m_screenh == 0))
+ {
+ return wxGIF_INVFORMAT;
+ }
+
/* load global color map if available */
if ((buf[4] & 0x80) == 0x80)
{
m_background = buf[5];
- ncolors = 2 << (buf[4] & 0x07);
- size_t numBytes = 3 * ncolors;
+ global_ncolors = 2 << (buf[4] & 0x07);
+ size_t numBytes = 3 * global_ncolors;
m_f->Read(pal, numBytes);
if (m_f->LastRead() != numBytes)
{
bool done = false;
- while(!done)
+ while (!done)
{
type = (unsigned char)m_f->GetC();
pimg->w = buf[4] + 256 * buf[5];
pimg->h = buf[6] + 256 * buf[7];
- if (pimg->w == 0 || pimg->h == 0)
+ if ((pimg->w == 0) || (pimg->w > m_screenw) || (pimg->h == 0) || (pimg->h > m_screenh))
{
Destroy();
return wxGIF_INVFORMAT;
/* load local color map if available, else use global map */
if ((buf[8] & 0x80) == 0x80)
{
- ncolors = 2 << (buf[8] & 0x07);
- size_t numBytes = 3 * ncolors;
+ unsigned int local_ncolors = 2 << (buf[8] & 0x07);
+ size_t numBytes = 3 * local_ncolors;
m_f->Read(pimg->pal, numBytes);
+ pimg->ncolours = local_ncolors;
if (m_f->LastRead() != numBytes)
{
Destroy();
else
{
memcpy(pimg->pal, pal, 768);
+ pimg->ncolours = global_ncolors;
}
/* get initial code size from first byte in raster data */
bits = (unsigned char)m_f->GetC();
+ if (bits == 0)
+ {
+ Destroy();
+ return wxGIF_INVFORMAT;
+ }
/* decode image */
int result = dgif(pimg, interl, bits);
}
}
- if (m_nimages == 0)
+ if (m_nimages <= 0)
{
Destroy();
return wxGIF_INVFORMAT;
/* local color map */
if ((buf[8] & 0x80) == 0x80)
{
- ncolors = 2 << (buf[8] & 0x07);
+ unsigned int local_ncolors = 2 << (buf[8] & 0x07);
wxFileOffset pos = m_f->TellI();
- wxFileOffset numBytes = 3 * ncolors;
+ wxFileOffset numBytes = 3 * local_ncolors;
m_f->SeekI(numBytes, wxFromCurrent);
if (m_f->TellI() != (pos + numBytes))
{