long delay;                     /* delay in ms (-1 = unused) */
     unsigned char *p;               /* bitmap */
     unsigned char *pal;             /* palette */
+    unsigned int ncolours;          /* number of colours */
     GIFImage *next;                 /* next image */
     GIFImage *prev;                 /* prev image */
 
     int GetFrameIndex() const;
     unsigned char* GetData() const;
     unsigned char* GetPalette() const;
+    unsigned int GetNcolours() const;
     unsigned int GetWidth() const;
     unsigned int GetHeight() const;
     unsigned int GetLeft() const;
 
     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) &&
         b[i] = pal[3*i + 2];
     }
 
-    image->SetPalette(wxPalette(256, r, g, b));
+    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); }
 //
 int wxGIFDecoder::ReadGIF()
 {
-    unsigned int ncolors;
+    unsigned int  global_ncolors = 0;
     int           bits, interl, transparent, disposal, i;
     long          size;
     long          delay;
     {
         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)
         {
             /* 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 */
             /* 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))
                 {