]> git.imager.perl.org - imager.git/blobdiff - gif.c
- Imager::Font->new() for the tt (Freetype 1.x) driver now correctly
[imager.git] / gif.c
diff --git a/gif.c b/gif.c
index f22d5e105072c22151229451f1e35e3bcb20feea..a426f1c897a925c1c2c3579876c5b43613dcfab8 100644 (file)
--- a/gif.c
+++ b/gif.c
@@ -1,4 +1,4 @@
-#include "image.h"
+#include "imagei.h"
 #include <gif_lib.h>
 #ifdef _MSCVER
 #include <io.h>
@@ -177,8 +177,25 @@ i_readgif_low(GifFileType *GifFile, int **colour_table, int *colours) {
     cmapcnt++;
   }
   
+  if (!i_int_check_image_file_limits(GifFile->SWidth, GifFile->SHeight, 3, sizeof(i_sample_t))) {
+    if (colour_table && *colour_table) {
+      myfree(*colour_table);
+      *colour_table = NULL;
+    }
+    DGifCloseFile(GifFile);
+    mm_log((1, "i_readgif: image size exceeds limits\n"));
+    return NULL;
+  }
 
   im = i_img_empty_ch(NULL, GifFile->SWidth, GifFile->SHeight, 3);
+  if (!im) {
+    if (colour_table && *colour_table) {
+      myfree(*colour_table);
+      *colour_table = NULL;
+    }
+    DGifCloseFile(GifFile);
+    return NULL;
+  }
 
   Size = GifFile->SWidth * sizeof(GifPixelType); 
   
@@ -354,6 +371,9 @@ i_readgif_low(GifFileType *GifFile, int **colour_table, int *colours) {
     i_img_destroy(im);
     return NULL;
   }
+
+  i_tags_add(&im->tags, "i_format", 0, "gif", -1, 0);
+
   return im;
 }
 
@@ -484,7 +504,7 @@ standard.
 
 i_img **i_readgif_multi_low(GifFileType *GifFile, int *count) {
   i_img *img;
-  int i, j, Size, Width, Height, ExtCode, Count, x;
+  int i, j, Size, Width, Height, ExtCode, Count;
   int ImageNum = 0, BackGround = 0, ColorMapSize = 0;
   ColorMapObject *ColorMap;
  
@@ -553,7 +573,16 @@ i_img **i_readgif_multi_low(GifFileType *GifFile, int *count) {
       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) {
@@ -581,6 +610,7 @@ i_img **i_readgif_multi_low(GifFileType *GifFile, int *count) {
         }
       }
       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);
@@ -1370,9 +1400,8 @@ static int
 has_common_palette(i_img **imgs, int count, i_quantize *quant, 
                    int want_trans) {
   int size = quant->mc_count;
-  int i, j;
+  int i;
   int imgn;
-  int x, y;
   char used[256];
 
   /* we try to build a common palette here, if we can manage that, then
@@ -1482,7 +1511,6 @@ i_writegif_low(i_quantize *quant, GifFileType *gf, i_img **imgs, int count) {
   i_color *orig_colors = quant->mc_colors;
   i_color *glob_colors = NULL;
   int glob_color_count;
-  int glob_map_size;
   int glob_want_trans;
   int glob_paletted; /* the global map was made from the image palettes */
   int colors_paletted;
@@ -1663,6 +1691,7 @@ i_writegif_low(i_quantize *quant, GifFileType *gf, i_img **imgs, int count) {
       if ((map = make_gif_map(quant, imgs[0], want_trans)) == NULL) {
         i_mempool_destroy(&mp);
         EGifCloseFile(gf);
+        quant->mc_colors = orig_colors;
         mm_log((1, "Error in MakeMapObject"));
         return 0;
       }
@@ -1678,6 +1707,12 @@ i_writegif_low(i_quantize *quant, GifFileType *gf, i_img **imgs, int count) {
     result = quant_paletted(quant, imgs[0]);
   else
     result = quant_translate(quant, imgs[0]);
+  if (!result) {
+    i_mempool_destroy(&mp);
+    quant->mc_colors = orig_colors;
+    EGifCloseFile(gf);
+    return 0;
+  }
   if (want_trans) {
     quant_transparent(quant, result, imgs[0], quant->mc_count);
     trans_index = quant->mc_count;
@@ -1753,6 +1788,13 @@ i_writegif_low(i_quantize *quant, GifFileType *gf, i_img **imgs, int count) {
         quant_makemap(quant, imgs+imgn, 1);
         result = quant_translate(quant, imgs[imgn]);
       }
+      if (!result) {
+        i_mempool_destroy(&mp);
+        quant->mc_colors = orig_colors;
+        EGifCloseFile(gf);
+        mm_log((1, "error in quant_translate()"));
+        return 0;
+      }
       if (want_trans) {
         quant_transparent(quant, result, imgs[imgn], quant->mc_count);
         trans_index = quant->mc_count;
@@ -1838,6 +1880,12 @@ i_writegif_low(i_quantize *quant, GifFileType *gf, i_img **imgs, int count) {
     mm_log((1, "Error in EGifCloseFile\n"));
     return 0;
   }
+  if (glob_colors) {
+    int i;
+    for (i = 0; i < glob_color_count; ++i)
+      orig_colors[i] = glob_colors[i];
+  }
+
   i_mempool_destroy(&mp);
   quant->mc_colors = orig_colors;