]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/gifdecod.cpp
VC6 compilation fix for native wxProgressDialog implementation.
[wxWidgets.git] / src / common / gifdecod.cpp
index 569b3e77ae653b6663bc6f92b43ea98c14bf0685..27669b8d2a7b8271cccd565ff580b78da92d469e 100644 (file)
 
 #ifndef WX_PRECOMP
     #include "wx/palette.h"
+    #include "wx/intl.h"
+    #include "wx/log.h"
 #endif
 
 #include <stdlib.h>
 #include <string.h>
 #include "wx/gifdecod.h"
-#include "wx/ptr_scpd.h"
+#include "wx/scopedptr.h"
 #include "wx/scopeguard.h"
 
 
@@ -51,7 +53,7 @@ public:
     unsigned char *pal;             // palette
     unsigned int ncolours;          // number of colours
 
-    DECLARE_NO_COPY_CLASS(GIFImage)
+    wxDECLARE_NO_COPY_CLASS(GIFImage);
 };
 
 wxDECLARE_SCOPED_PTR(GIFImage, GIFImagePtr)
@@ -191,7 +193,7 @@ bool wxGIFDecoder::ConvertToImage(unsigned int frame, wxImage *image) const
 
 // Get data for current frame
 
-wxSize wxGIFDecoder::GetFrameSize(unsigned int frame) const 
+wxSize wxGIFDecoder::GetFrameSize(unsigned int frame) const
 {
     return wxSize(GetFrame(frame)->w, GetFrame(frame)->h);
 }
@@ -573,15 +575,13 @@ as an End of Information itself)
 // CanRead:
 //  Returns true if the file looks like a valid GIF, false otherwise.
 //
-bool wxGIFDecoder::CanRead(wxInputStream &stream) const
+bool wxGIFDecoder::DoCanRead(wxInputStream &stream) const
 {
     unsigned char buf[3];
 
     if ( !stream.Read(buf, WXSIZEOF(buf)) )
         return false;
 
-    stream.SeekI(-(wxFileOffset)WXSIZEOF(buf), wxFromCurrent);
-
     return memcmp(buf, "GIF", WXSIZEOF(buf)) == 0;
 }
 
@@ -598,7 +598,7 @@ bool wxGIFDecoder::CanRead(wxInputStream &stream) const
 wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
 {
     unsigned int  global_ncolors = 0;
-    int           bits, interl, transparent, i;
+    int           bits, interl, i;
     wxAnimationDisposal disposal;
     long          size;
     long          delay;
@@ -660,7 +660,7 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
     }
 
     // transparent colour, disposal method and delay default to unused
-    transparent = -1;
+    int transparent = -1;
     disposal = wxANIM_UNSPECIFIED;
     delay = -1;
 
@@ -710,8 +710,7 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
                 delay = 10 * (buf[2] + 256 * buf[3]);
 
                 // read transparent colour index, if used
-                if (buf[1] & 0x01)
-                    transparent = buf[4];
+                transparent = buf[1] & 0x01 ? buf[4] : -1;
 
                 // read disposal method
                 disposal = (wxAnimationDisposal)(((buf[1] & 0x1C) >> 2) - 1);
@@ -721,12 +720,12 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
             {
                 while ((i = (unsigned char)stream.GetC()) != 0)
                 {
-                    if (stream.Eof() || (stream.LastRead() == 0))
+                    if (stream.Eof() || (stream.LastRead() == 0) ||
+                        stream.SeekI(i, wxFromCurrent) == wxInvalidOffset)
                     {
                         done = true;
                         break;
                     }
-                    stream.SeekI(i, wxFromCurrent);
                 }
             }
         }
@@ -757,9 +756,30 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
             pimg->w = buf[4] + 256 * buf[5];
             pimg->h = buf[6] + 256 * buf[7];
 
-            if ( anim && ((pimg->w == 0) || (pimg->w > (unsigned int)m_szAnimation.GetWidth()) || 
-                            (pimg->h == 0) || (pimg->h > (unsigned int)m_szAnimation.GetHeight())) )
-                return wxGIF_INVFORMAT;
+            if ( anim )
+            {
+                // some GIF images specify incorrect animation size but we can
+                // still open them if we fix up the animation size, see #9465
+                if ( m_nFrames == 0 )
+                {
+                    if ( pimg->w > (unsigned)m_szAnimation.x )
+                        m_szAnimation.x = pimg->w;
+                    if ( pimg->h > (unsigned)m_szAnimation.y )
+                        m_szAnimation.y = pimg->h;
+                }
+                else // subsequent frames
+                {
+                    // check that we have valid size
+                    if ( (!pimg->w || pimg->w > (unsigned)m_szAnimation.x) ||
+                            (!pimg->h || pimg->h > (unsigned)m_szAnimation.y) )
+                    {
+                        wxLogError(_("Incorrect GIF frame size (%u, %d) for "
+                                     "the frame #%u"),
+                                   pimg->w, pimg->h, m_nFrames);
+                        return wxGIF_INVFORMAT;
+                    }
+                }
+            }
 
             interl = ((buf[8] & 0x40)? 1 : 0);
             size = pimg->w * pimg->h;
@@ -835,12 +855,12 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
             // skip all data
             while ((i = (unsigned char)stream.GetC()) != 0)
             {
-                if (stream.Eof() || (stream.LastRead() == 0))
+                if (stream.Eof() || (stream.LastRead() == 0) ||
+                    stream.SeekI(i, wxFromCurrent) == wxInvalidOffset)
                 {
                     Destroy();
                     return wxGIF_INVFORMAT;
                 }
-                stream.SeekI(i, wxFromCurrent);
             }
         }
         else if (type == 0x2C)
@@ -859,7 +879,11 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
             {
                 unsigned int local_ncolors = 2 << (buf[8] & 0x07);
                 wxFileOffset numBytes = 3 * local_ncolors;
-                stream.SeekI(numBytes, wxFromCurrent);
+                if (stream.SeekI(numBytes, wxFromCurrent) == wxInvalidOffset)
+                {
+                    Destroy();
+                    return wxGIF_INVFORMAT;
+                }
             }
 
             // initial code size
@@ -873,12 +897,12 @@ wxGIFErrorCode wxGIFDecoder::LoadGIF(wxInputStream& stream)
             // skip all data
             while ((i = (unsigned char)stream.GetC()) != 0)
             {
-                if (stream.Eof() || (stream.LastRead() == 0))
+                if (stream.Eof() || (stream.LastRead() == 0) ||
+                    stream.SeekI(i, wxFromCurrent) == wxInvalidOffset)
                 {
                     Destroy();
                     return wxGIF_INVFORMAT;
                 }
-                stream.SeekI(i, wxFromCurrent);
             }
         }
         else if ((type != 0x3B) && (type != 00)) // testing