]> git.imager.perl.org - imager.git/blobdiff - tga.c
work around compilation problem reported in
[imager.git] / tga.c
diff --git a/tga.c b/tga.c
index 04f684c8065f1e39f18ae3b673b45a8c40b4bbee..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"
 
@@ -38,7 +38,7 @@ Some of these functions are internal.
 
 
 typedef struct {
-  char  idlength;
+  unsigned char  idlength;
   char  colourmaptype;
   char  datatypecode;
   short int colourmaporigin;
@@ -320,33 +320,52 @@ tga_header_unpack(tga_header *header, unsigned char headbuf[18]) {
 }
 
 
-
+/* this function should never produce diagnostics to stdout, maybe to the logfile */
 int
 tga_header_verify(unsigned char headbuf[18]) {
   tga_header header;
   tga_header_unpack(&header, headbuf);
   switch (header.datatypecode) { 
   default:
-               printf("bad typecode!\n");
+    /*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");
+    /*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;
 }
@@ -464,7 +483,7 @@ destination is compressed.
 static
 int
 tga_dest_write(tga_dest *s, unsigned char *buf, size_t pixels) {
-  int cp = 0, j, k;
+  int cp = 0;
 
   if (!s->compressed) {
     if (s->ig->writecb(s->ig, buf, pixels*s->bytepp) != pixels*s->bytepp) return 0;
@@ -598,7 +617,7 @@ Returns NULL on error.
 i_img *
 i_readtga_wiol(io_glue *ig, int length) {
   i_img* img = NULL;
-  int x, y, i;
+  int x, y;
   int width, height, channels;
   int mapped;
   char *idstring = NULL;
@@ -607,7 +626,6 @@ i_readtga_wiol(io_glue *ig, int length) {
   tga_header header;
   unsigned char headbuf[18];
   unsigned char *databuf;
-  unsigned char *reorderbuf;
 
   i_color *linebuf = NULL;
   i_clear_error();
@@ -646,6 +664,7 @@ i_readtga_wiol(io_glue *ig, int length) {
 
   width = header.width;
   height = header.height;
+
   
   /* Set tags here */
   
@@ -696,7 +715,6 @@ i_readtga_wiol(io_glue *ig, int length) {
   
   mapped = 1;
   switch (header.datatypecode) {
-    int tbpp;
   case 2:  /* Uncompressed, rgb images          */
   case 10: /* Compressed,   rgb images          */
     mapped = 0;
@@ -715,10 +733,22 @@ 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) :
     i_img_empty_ch(NULL, width, height, channels);
+
+  if (!img) {
+    if (idstring) 
+      myfree(idstring);
+    return NULL;
+  }
   
   if (idstring) {
     i_tags_add(&img->tags, "tga_idstring", 0, idstring, header.idlength, 0);
@@ -758,6 +788,7 @@ i_readtga_wiol(io_glue *ig, int length) {
   myfree(databuf);
   if (linebuf) myfree(linebuf);
   
+  i_tags_add(&img->tags, "i_format", 0, "tga", -1, 0);
   i_tags_addn(&img->tags, "tga_bitspp", 0, mapped?header.colourmapdepth:header.bitsperpixel);
   if (src.compressed) i_tags_addn(&img->tags, "compressed", 0, 1);
   return img;
@@ -778,7 +809,6 @@ Writes an image in targa format.  Returns 0 on error.
 
 undef_int
 i_writetga_wiol(i_img *img, io_glue *ig, int wierdpack, int compress, char *idstring, size_t idlen) {
-  static int rgb_chan[] = { 2, 1, 0, 3 };
   tga_header header;
   tga_dest dest;
   unsigned char headbuf[18];
@@ -803,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:
@@ -827,9 +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;
   header.idlength = idlen;
   header.colourmaptype   = mapped ? 1 : 0;
   header.datatypecode    = mapped ? 1 : img->channels == 1 ? 3 : 2;
@@ -868,8 +896,6 @@ i_writetga_wiol(i_img *img, io_glue *ig, int wierdpack, int compress, char *idst
   mm_log((1, "dest.bytepp = %d\n", dest.bytepp));
 
   if (img->type == i_palette_type) {
-    int i;
-    int bytepp = bpp_to_bytes(bitspp);
     if (!tga_palette_write(ig, img, bitspp, i_colorcount(img))) return 0;
     
     if (!img->virtual && !dest.compressed) {