+//-------------------------------------------------------------------
+
+inline void m_rend_gif_decodecolormap(unsigned char *cmb, unsigned char *rgbb, ColorMapObject *cm, int s, int l)
+{
+ GifColorType *cmentry;
+ int i;
+ for (i = 0; i < l; i++)
+ {
+ cmentry = &cm->Colors[cmb[i]];
+ *(rgbb++) = cmentry->Red;
+ *(rgbb++) = cmentry->Green;
+ *(rgbb++) = cmentry->Blue;
+ }
+}
+
+static int gif_load(const char *filename, int *x, int *y)
+{
+ int px, py, i, j, ibxs;
+ unsigned char *fbptr;
+ unsigned char *lb=NULL;
+ unsigned char *slb=NULL;
+ GifFileType *gft;
+ GifRecordType rt;
+ GifByteType *extension;
+ ColorMapObject *cmap;
+ int cmaps;
+ int extcode;
+
+ gft = DGifOpenFileName(filename);
+ if (gft == NULL)
+ return 0;
+ do
+ {
+ if (DGifGetRecordType(gft, &rt) == GIF_ERROR)
+ goto ERROR_R;
+ switch(rt)
+ {
+ case IMAGE_DESC_RECORD_TYPE:
+ if (DGifGetImageDesc(gft) == GIF_ERROR)
+ goto ERROR_R;
+ *x = px = gft->Image.Width;
+ *y = py = gft->Image.Height;
+ pic_buffer = new unsigned char[px * py * 3];
+ lb = (unsigned char *)malloc(px * 3);
+ slb = (unsigned char *) malloc(px);
+
+ if (lb != NULL && slb != NULL)
+ {
+ cmap = (gft->Image.ColorMap ? gft->Image.ColorMap : gft->SColorMap);
+ cmaps = cmap->ColorCount;
+
+ ibxs = ibxs * 3;
+ fbptr = pic_buffer;
+ if (!(gft->Image.Interlace))
+ {
+ for (i = 0; i < py; i++, fbptr += px * 3)
+ {
+ if (DGifGetLine(gft, slb, px) == GIF_ERROR)
+ goto ERROR_R;
+ m_rend_gif_decodecolormap(slb, lb, cmap, cmaps, px);
+ memcpy(fbptr, lb, px * 3);
+ }
+ }
+ else
+ {
+ for (j = 0; j < 4; j++)
+ {
+ fbptr = pic_buffer;
+ for (i = 0; i < py; i++, fbptr += px * 3)
+ {
+ if (DGifGetLine(gft, slb, px) == GIF_ERROR)
+ goto ERROR_R;
+ m_rend_gif_decodecolormap(slb, lb, cmap, cmaps, px);
+ memcpy(fbptr, lb, px * 3);
+ }
+ }
+ }
+ }
+ if (lb)
+ {
+ free(lb);
+ lb=NULL;
+ }
+ if (slb)
+ {
+ free(slb);
+ slb=NULL;
+ }
+ break;
+ case EXTENSION_RECORD_TYPE:
+ if (DGifGetExtension(gft, &extcode, &extension) == GIF_ERROR)
+ goto ERROR_R;
+ while (extension != NULL)
+ if (DGifGetExtensionNext(gft, &extension) == GIF_ERROR)
+ goto ERROR_R;
+ break;
+ default:
+ break;
+ }
+ }
+ while (rt != TERMINATE_RECORD_TYPE);
+
+ DGifCloseFile(gft);
+ return 1;
+ERROR_R:
+ eDebug("[GIF] Error");
+ if (lb) free(lb);
+ if (slb) free(slb);
+ DGifCloseFile(gft);
+ return 0;
+}
+