+
+ 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);
+ }
+
+ myfree(GifRow);
+
+ if (DGifCloseFile(GifFile) == GIF_ERROR) {
+ gif_push_error();
+ i_push_error(0, "Closing GIF file object");
+ free_images(results, *count);
+ return NULL;
+ }
+
+ return results;
+}
+
+#if IM_GIFMAJOR >= 4
+/* giflib declares this incorrectly as EgifOpen */
+extern GifFileType *EGifOpen(void *userData, OutputFunc writeFunc);
+
+static int io_glue_read_cb(GifFileType *gft, GifByteType *buf, int length);
+#endif
+
+/*
+=item i_readgif_multi_wiol(ig, int *count)
+
+=cut
+*/
+
+i_img **
+i_readgif_multi_wiol(io_glue *ig, int *count) {
+ io_glue_commit_types(ig);
+
+ if (ig->source.type == FDSEEK || ig->source.type == FDNOSEEK) {
+ return i_readgif_multi(ig->source.fdseek.fd, count);
+ }
+ else {
+#if IM_GIFMAJOR >= 4
+ GifFileType *GifFile;
+
+ i_clear_error();
+
+ if ((GifFile = DGifOpen((void *)ig, io_glue_read_cb )) == NULL) {
+ gif_push_error();
+ i_push_error(0, "Cannot create giflib callback object");
+ mm_log((1,"i_readgif_multi_wiol: Unable to open callback datasource.\n"));
+ return NULL;
+ }
+
+ return i_readgif_multi_low(GifFile, count);
+#else
+ i_clear_error();
+ i_push_error(0, "callbacks not supported with giflib3");
+
+ return NULL;
+#endif
+ }
+}
+
+/*
+=item i_readgif_multi(int fd, int *count)
+
+=cut
+*/
+i_img **
+i_readgif_multi(int fd, int *count) {
+ GifFileType *GifFile;
+
+ i_clear_error();
+
+ mm_log((1,"i_readgif_multi(fd %d, &count %p)\n", fd, count));
+
+ if ((GifFile = DGifOpenFileHandle(fd)) == NULL) {
+ gif_push_error();
+ i_push_error(0, "Cannot create giflib file object");
+ mm_log((1,"i_readgif: Unable to open file\n"));
+ return NULL;
+ }
+
+ return i_readgif_multi_low(GifFile, count);
+}
+
+/*
+=item i_readgif_multi_scalar(char *data, int length, int *count)
+
+=cut
+*/
+i_img **
+i_readgif_multi_scalar(char *data, int length, int *count) {
+#if IM_GIFMAJOR >= 4
+ GifFileType *GifFile;
+ struct gif_scalar_info gsi;
+
+ i_clear_error();
+
+ gsi.cpos=0;
+ gsi.length=length;
+ gsi.data=data;
+
+ mm_log((1,"i_readgif_multi_scalar(data %p, length %d, &count %p)\n",
+ data, length, count));
+
+ if ((GifFile = DGifOpen( (void*) &gsi, my_gif_inputfunc )) == NULL) {
+ gif_push_error();
+ i_push_error(0, "Cannot create giflib callback object");
+ mm_log((1,"i_readgif_multi_scalar: Unable to open scalar datasource.\n"));
+ return NULL;
+ }
+
+ return i_readgif_multi_low(GifFile, count);
+#else
+ return NULL;
+#endif
+}
+
+/*
+=item i_readgif_callback(i_read_callback_t cb, char *userdata, int **colour_table, int *colours)
+
+Read a GIF file into an Imager RGB file, the data of the GIF file is
+retreived by callin the user supplied callback function.
+
+This function is only used with giflib 4 and higher.
+
+=cut
+*/
+
+i_img**
+i_readgif_multi_callback(i_read_callback_t cb, char *userdata, int *count) {
+#if IM_GIFMAJOR >= 4
+ GifFileType *GifFile;
+ i_img **result;
+
+ i_gen_read_data *gci = i_gen_read_data_new(cb, userdata);
+
+ i_clear_error();
+
+ mm_log((1,"i_readgif_multi_callback(callback %p, userdata %p, count %p)\n", cb, userdata, count));
+ if ((GifFile = DGifOpen( (void*) gci, gif_read_callback )) == NULL) {
+ gif_push_error();
+ i_push_error(0, "Cannot create giflib callback object");
+ mm_log((1,"i_readgif_callback: Unable to open callback datasource.\n"));
+ myfree(gci);
+ return NULL;
+ }
+
+ result = i_readgif_multi_low(GifFile, count);
+ i_free_gen_read_data(gci);
+
+ return result;
+#else
+ return NULL;
+#endif
+}
+
+/*
+=item i_writegif(i_img *im, int fd, int max_colors, int pixdev, int fixedlen, i_color fixed[])
+
+Write I<img> to the file handle I<fd>. The resulting GIF will use a
+maximum of 1<<I<max_colours> colours, with the first I<fixedlen>
+colours taken from I<fixed>.
+
+Returns non-zero on success.
+
+=cut
+*/
+
+undef_int
+i_writegif(i_img *im, int fd, int max_colors, int pixdev, int fixedlen, i_color fixed[]) {
+ i_color colors[256];
+ i_quantize quant;
+
+ memset(&quant, 0, sizeof(quant));
+ quant.make_colors = mc_addi;
+ quant.mc_colors = colors;
+ quant.mc_size = 1<<max_colors;
+ quant.mc_count = fixedlen;
+ memcpy(colors, fixed, fixedlen * sizeof(i_color));
+ quant.translate = pt_perturb;
+ quant.perturb = pixdev;
+ return i_writegif_gen(&quant, fd, &im, 1);
+}
+
+/*
+=item i_writegifmc(i_img *im, int fd, int max_colors)
+
+Write I<img> to the file handle I<fd>. The resulting GIF will use a
+maximum of 1<<I<max_colours> colours.
+
+Returns non-zero on success.
+
+=cut
+*/
+
+undef_int
+i_writegifmc(i_img *im, int fd, int max_colors) {
+ i_color colors[256];
+ i_quantize quant;
+
+/* *(char *)0 = 1; */
+
+ memset(&quant, 0, sizeof(quant));
+ quant.make_colors = mc_none; /* ignored for pt_giflib */
+ quant.mc_colors = colors;
+ quant.mc_size = 1 << max_colors;
+ quant.mc_count = 0;
+ quant.translate = pt_giflib;
+ return i_writegif_gen(&quant, fd, &im, 1);
+}
+
+
+/*
+=item i_readgif_scalar(char *data, int length, int **colour_table, int *colours)
+
+Reads a GIF file from an in memory copy of the file. This can be used
+if you get the 'file' from some source other than an actual file (or
+some other file handle).
+
+This function is only available with giflib 4 and higher.
+
+=cut
+*/
+i_img*
+i_readgif_scalar(char *data, int length, int **colour_table, int *colours) {
+#if IM_GIFMAJOR >= 4
+ GifFileType *GifFile;
+ struct gif_scalar_info gsi;
+
+ i_clear_error();
+
+ gsi.cpos=0;
+ gsi.length=length;
+ gsi.data=data;
+
+ mm_log((1,"i_readgif_scalar(char* data, int length, colour_table %p, colours %p)\n", data, length, colour_table, colours));
+ if ((GifFile = DGifOpen( (void*) &gsi, my_gif_inputfunc )) == NULL) {
+ gif_push_error();
+ i_push_error(0, "Cannot create giflib callback object");
+ mm_log((1,"i_readgif_scalar: Unable to open scalar datasource.\n"));
+ return NULL;
+ }
+
+ return i_readgif_low(GifFile, colour_table, colours);
+#else
+ return NULL;
+#endif
+}
+
+#if IM_GIFMAJOR >= 4
+
+/*
+=item gif_read_callback(GifFileType *gft, GifByteType *buf, int length)
+
+Internal. The reader callback wrapper passed to giflib.
+
+This function is only used with giflib 4 and higher.
+
+=cut
+*/
+
+static int
+gif_read_callback(GifFileType *gft, GifByteType *buf, int length) {
+ return i_gen_reader((i_gen_read_data *)gft->UserData, (char*)buf, length);
+}
+
+#endif
+
+
+/*
+=item i_readgif_callback(i_read_callback_t cb, char *userdata, int **colour_table, int *colours)
+
+Read a GIF file into an Imager RGB file, the data of the GIF file is
+retreived by callin the user supplied callback function.
+
+This function is only used with giflib 4 and higher.
+
+=cut
+*/
+
+i_img*
+i_readgif_callback(i_read_callback_t cb, char *userdata, int **colour_table, int *colours) {
+#if IM_GIFMAJOR >= 4
+ GifFileType *GifFile;
+ i_img *result;
+
+ i_gen_read_data *gci = i_gen_read_data_new(cb, userdata);
+
+ i_clear_error();
+
+ mm_log((1,"i_readgif_callback(callback %p, userdata %p, colour_table %p, colours %p)\n", cb, userdata, colour_table, colours));
+ if ((GifFile = DGifOpen( (void*) gci, gif_read_callback )) == NULL) {
+ gif_push_error();
+ i_push_error(0, "Cannot create giflib callback object");
+ mm_log((1,"i_readgif_callback: Unable to open callback datasource.\n"));
+ myfree(gci);
+ return NULL;
+ }
+
+ result = i_readgif_low(GifFile, colour_table, colours);
+ i_free_gen_read_data(gci);
+
+ return result;
+#else
+ i_clear_error();
+ i_push_error(0, "callbacks not supported with giflib3");
+
+ return NULL;
+#endif
+}
+
+#if IM_GIFMAJOR >= 4
+
+static int
+io_glue_read_cb(GifFileType *gft, GifByteType *buf, int length) {
+ io_glue *ig = (io_glue *)gft->UserData;
+
+ return ig->readcb(ig, buf, length);
+}
+
+#endif
+
+i_img *
+i_readgif_wiol(io_glue *ig, int **color_table, int *colors) {
+ io_glue_commit_types(ig);
+
+ if (ig->source.type == FDSEEK || ig->source.type == FDNOSEEK) {
+ int fd = dup(ig->source.fdseek.fd);
+ if (fd < 0) {
+ i_push_error(errno, "dup() failed");
+ return 0;
+ }
+ return i_readgif(fd, color_table, colors);
+ }
+ else {
+#if IM_GIFMAJOR >= 4
+ GifFileType *GifFile;
+
+ i_clear_error();
+
+ if ((GifFile = DGifOpen((void *)ig, io_glue_read_cb )) == NULL) {
+ gif_push_error();
+ i_push_error(0, "Cannot create giflib callback object");
+ mm_log((1,"i_readgif_wiol: Unable to open callback datasource.\n"));
+ return NULL;
+ }
+
+ return i_readgif_low(GifFile, color_table, colors);
+
+#else
+ i_clear_error();
+ i_push_error(0, "callbacks not supported with giflib3");
+
+ return NULL;
+#endif
+ }
+}
+
+/*
+=item do_write(GifFileType *gf, i_gif_opts *opts, i_img *img, i_palidx *data)
+
+Internal. Low level image write function. Writes in interlace if
+that was requested in the GIF options.