]> git.imager.perl.org - imager.git/blobdiff - tga.c
set eol-style so that generation doesn't make svn burp
[imager.git] / tga.c
diff --git a/tga.c b/tga.c
index 13758117f0576e4108b63b949410a9cc2cb9ddda..7426339e8c3ebb57352002c341530ea67dc6ab1d 100644 (file)
--- a/tga.c
+++ b/tga.c
@@ -1,4 +1,4 @@
-#include "image.h"
+#include "imageri.h"
 #include "log.h"
 #include "iolayer.h"
 
@@ -329,24 +329,43 @@ tga_header_verify(unsigned char headbuf[18]) {
   default:
     /*printf("bad typecode!\n");*/
     return 0;
-  case 0:
   case 1:  /* Uncompressed, color-mapped images */ 
-  case 2:  /* Uncompressed, rgb images          */ 
   case 3:  /* Uncompressed, grayscale images    */ 
   case 9:  /* Compressed,   color-mapped images */ 
-  case 10: /* Compressed,   rgb images          */ 
   case 11: /* Compressed,   grayscale images    */ 
-         break;
+    if (header.bitsperpixel != 8)
+      return 0;
+    break;
+  case 0:
+  case 2:  /* Uncompressed, rgb images          */ 
+  case 10: /* Compressed,   rgb images          */ 
+    if (header.bitsperpixel != 15 && header.bitsperpixel != 16
+       && header.bitsperpixel != 24 && header.bitsperpixel != 23)
+      return 0;
+    break;
        }
 
   switch (header.colourmaptype) { 
   default:
     /*printf("bad colourmaptype!\n");*/
     return 0;
-  case 0:
   case 1:
+    if (header.datatypecode != 1 && header.datatypecode != 9)
+      return 0; /* only get a color map on a color mapped image */
+  case 0:
        break;
        }
+
+  switch (header.colourmapdepth) {
+  default:
+    return 0;
+  case 0: /* can be 0 if no colour map */
+  case 15:
+  case 16:
+  case 24:
+  case 32:
+    break;
+  }
   
   return 1;
 }
@@ -645,6 +664,7 @@ i_readtga_wiol(io_glue *ig, int length) {
 
   width = header.width;
   height = header.height;
+
   
   /* Set tags here */
   
@@ -713,6 +733,12 @@ i_readtga_wiol(io_glue *ig, int length) {
     channels = 1;
     break;
   }
+
+  if (!i_int_check_image_file_limits(width, height, channels, 
+                                    sizeof(i_sample_t))) {
+    mm_log((1, "i_readtga_wiol: image size exceeds limits\n"));
+    return NULL;
+  }
   
   img = mapped ? 
     i_img_pal_new(width, height, channels, 256) :
@@ -807,6 +833,7 @@ i_writetga_wiol(i_img *img, io_glue *ig, int wierdpack, int compress, char *idst
   mm_log((1, "channels %d\n", img->channels));
   
   i_clear_error();
+  io_glue_commit_types(ig);
   
   switch (img->channels) {
   case 1:
@@ -831,8 +858,6 @@ i_writetga_wiol(i_img *img, io_glue *ig, int wierdpack, int compress, char *idst
     return 0;
   }
 
-  io_glue_commit_types(ig);
-  
   header.idlength = idlen;
   header.colourmaptype   = mapped ? 1 : 0;
   header.datatypecode    = mapped ? 1 : img->channels == 1 ? 3 : 2;