+ switch (RecordType) {
+ case IMAGE_DESC_RECORD_TYPE:
+ if (DGifGetImageDesc(GifFile) == GIF_ERROR) {
+ gif_push_error();
+ i_push_error(0, "Unable to get image descriptor");
+ free_images(results, *count);
+ DGifCloseFile(GifFile);
+ return NULL;
+ }
+
+ if (( ColorMap = (GifFile->Image.ColorMap ? GifFile->Image.ColorMap : GifFile->SColorMap) )) {
+ mm_log((1, "Adding local colormap\n"));
+ ColorMapSize = ColorMap->ColorCount;
+ } else {
+ /* No colormap and we are about to read in the image -
+ abandon for now */
+ mm_log((1, "Going in with no colormap\n"));
+ i_push_error(0, "Image does not have a local or a global color map");
+ free_images(results, *count);
+ DGifCloseFile(GifFile);
+ return NULL;
+ }
+
+ Width = GifFile->Image.Width;
+ Height = GifFile->Image.Height;
+ channels = 3;
+ if (got_gce && trans_index >= 0)
+ channels = 4;
+ if (!i_int_check_image_file_limits(Width, Height, channels, sizeof(i_sample_t))) {
+ free_images(results, *count);
+ mm_log((1, "i_readgif: image size exceeds limits\n"));
+ return NULL;
+ }
+ img = i_img_pal_new(Width, Height, channels, 256);
+ if (!img) {
+ free_images(results, *count);
+ return NULL;
+ }
+ /* populate the palette of the new image */
+ mm_log((1, "ColorMapSize %d\n", ColorMapSize));
+ for (i = 0; i < ColorMapSize; ++i) {
+ i_color col;
+ col.rgba.r = ColorMap->Colors[i].Red;
+ col.rgba.g = ColorMap->Colors[i].Green;
+ col.rgba.b = ColorMap->Colors[i].Blue;
+ if (channels == 4 && trans_index == i)
+ col.rgba.a = 0;
+ else
+ col.rgba.a = 255;
+
+ i_addcolors(img, &col, 1);
+ }
+ ++*count;
+ if (*count > result_alloc) {
+ if (result_alloc == 0) {
+ result_alloc = 5;
+ results = mymalloc(result_alloc * sizeof(i_img *));
+ }
+ else {
+ /* myrealloc never fails (it just dies if it can't allocate) */
+ result_alloc *= 2;
+ results = myrealloc(results, result_alloc * sizeof(i_img *));
+ }
+ }
+ results[*count-1] = img;
+ i_tags_add(&img->tags, "i_format", 0, "gif", -1, 0);
+ i_tags_addn(&img->tags, "gif_left", 0, GifFile->Image.Left);
+ /**(char *)0 = 1;*/
+ i_tags_addn(&img->tags, "gif_top", 0, GifFile->Image.Top);
+ i_tags_addn(&img->tags, "gif_interlace", 0, GifFile->Image.Interlace);
+ i_tags_addn(&img->tags, "gif_screen_width", 0, GifFile->SWidth);
+ i_tags_addn(&img->tags, "gif_screen_height", 0, GifFile->SHeight);
+ if (GifFile->SColorMap && !GifFile->Image.ColorMap) {
+ i_tags_addn(&img->tags, "gif_background", 0,
+ GifFile->SBackGroundColor);
+ }
+ if (GifFile->Image.ColorMap) {
+ i_tags_addn(&img->tags, "gif_localmap", 0, 1);
+ }
+ if (got_gce) {
+ if (trans_index >= 0) {
+ i_color trans;
+ i_tags_addn(&img->tags, "gif_trans_index", 0, trans_index);
+ i_getcolors(img, trans_index, &trans, 1);
+ i_tags_set_color(&img->tags, "gif_trans_color", 0, &trans);
+ }
+ i_tags_addn(&img->tags, "gif_delay", 0, gif_delay);
+ i_tags_addn(&img->tags, "gif_user_input", 0, user_input);
+ i_tags_addn(&img->tags, "gif_disposal", 0, disposal);
+ }
+ got_gce = 0;
+ if (got_ns_loop)
+ i_tags_addn(&img->tags, "gif_loop", 0, ns_loop);
+ if (comment) {
+ i_tags_add(&img->tags, "gif_comment", 0, comment, strlen(comment), 0);
+ myfree(comment);
+ comment = NULL;
+ }
+
+ ImageNum++;
+ mm_log((1,"i_readgif_multi_low: Image %d at (%d, %d) [%dx%d]: \n",
+ ImageNum, GifFile->Image.Left, GifFile->Image.Top, Width, Height));
+
+ if (GifFile->Image.Left + GifFile->Image.Width > GifFile->SWidth ||
+ GifFile->Image.Top + GifFile->Image.Height > GifFile->SHeight) {
+ i_push_errorf(0, "Image %d is not confined to screen dimension, aborted.\n",ImageNum);
+ free_images(results, *count);
+ DGifCloseFile(GifFile);
+ return(0);
+ }
+
+ if (GifFile->Image.Interlace) {
+ for (Count = i = 0; i < 4; i++) {
+ for (j = InterlacedOffset[i]; j < Height;
+ j += InterlacedJumps[i]) {
+ Count++;
+ if (DGifGetLine(GifFile, GifRow, Width) == GIF_ERROR) {
+ gif_push_error();
+ i_push_error(0, "Reading GIF line");
+ free_images(results, *count);
+ DGifCloseFile(GifFile);
+ return NULL;
+ }
+
+ i_ppal(img, 0, Width, j, GifRow);
+ }
+ }
+ }
+ else {
+ for (i = 0; i < Height; i++) {
+ if (DGifGetLine(GifFile, GifRow, Width) == GIF_ERROR) {
+ gif_push_error();
+ i_push_error(0, "Reading GIF line");
+ free_images(results, *count);
+ DGifCloseFile(GifFile);
+ return NULL;
+ }
+
+ i_ppal(img, 0, Width, i, GifRow);
+ }
+ }
+ break;
+ case EXTENSION_RECORD_TYPE:
+ /* Skip any extension blocks in file: */
+ if (DGifGetExtension(GifFile, &ExtCode, &Extension) == GIF_ERROR) {
+ gif_push_error();
+ i_push_error(0, "Reading extension record");
+ free_images(results, *count);
+ DGifCloseFile(GifFile);
+ return NULL;
+ }
+ if (ExtCode == 0xF9) {
+ got_gce = 1;
+ if (Extension[1] & 1)
+ trans_index = Extension[4];
+ else
+ trans_index = -1;
+ gif_delay = Extension[2] + 256 * Extension[3];
+ user_input = (Extension[0] & 2) != 0;
+ disposal = (Extension[0] >> 2) & 3;
+ }
+ if (ExtCode == 0xFF && *Extension == 11) {
+ if (memcmp(Extension+1, "NETSCAPE2.0", 11) == 0) {
+ if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR) {
+ gif_push_error();
+ i_push_error(0, "reading loop extension");
+ free_images(results, *count);
+ DGifCloseFile(GifFile);
+ return NULL;
+ }
+ if (Extension && *Extension == 3) {
+ got_ns_loop = 1;
+ ns_loop = Extension[2] + 256 * Extension[3];
+ }
+ }
+ }
+ else if (ExtCode == 0xFE) {
+ /* while it's possible for a GIF file to contain more than one
+ comment, I'm only implementing a single comment per image,
+ with the comment saved into the following image.
+ If someone wants more than that they can implement it.
+ I also don't handle comments that take more than one block.
+ */
+ if (!comment) {
+ comment = mymalloc(*Extension+1);
+ memcpy(comment, Extension+1, *Extension);
+ comment[*Extension] = '\0';
+ }
+ }
+ while (Extension != NULL) {
+ if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR) {
+ gif_push_error();
+ i_push_error(0, "reading next block of extension");
+ free_images(results, *count);
+ DGifCloseFile(GifFile);
+ return NULL;
+ }
+ }
+ break;
+ case TERMINATE_RECORD_TYPE:
+ break;
+ default: /* Should be trapped by DGifGetRecordType. */
+ break;
+ }
+ } while (RecordType != TERMINATE_RECORD_TYPE);
+
+ if (comment) {
+ if (*count) {
+ i_tags_add(&(results[*count-1]->tags), "gif_comment", 0, comment,
+ strlen(comment), 0);
+ }
+ myfree(comment);