- /* load local color map if available, else use global map */
- if ((buf[8] & 0x80) == 0x80)
- {
- ncolors = 2 << (buf[8] & 0x07);
- m_f->Read(pimg->pal, 3 * ncolors);
- }
- else
- memcpy(pimg->pal, pal, 768);
+ // allocate memory for image and palette
+ pimg->p = (unsigned char *) malloc((unsigned int)size);
+ pimg->pal = (unsigned char *) malloc(768);
+
+ if ((!pimg->p) || (!pimg->pal))
+ return wxGIF_MEMERR;
+
+ // load local color map if available, else use global map
+ if ((buf[8] & 0x80) == 0x80)
+ {
+ unsigned int local_ncolors = 2 << (buf[8] & 0x07);
+ unsigned int numBytes = 3 * local_ncolors;
+ stream.Read(pimg->pal, numBytes);
+ pimg->ncolours = local_ncolors;
+ if (stream.LastRead() != numBytes)
+ return wxGIF_INVFORMAT;
+ }
+ else
+ {
+ memcpy(pimg->pal, pal, 768);
+ pimg->ncolours = global_ncolors;
+ }
+
+ // get initial code size from first byte in raster data
+ bits = stream.GetC();
+ if (bits == 0)
+ return wxGIF_INVFORMAT;
+
+ // decode image
+ wxGIFErrorCode result = dgif(stream, pimg.get(), interl, bits);
+ if (result != wxGIF_OK)
+ return result;