]> git.imager.perl.org - imager.git/blobdiff - bmp.c
correct an old bug link
[imager.git] / bmp.c
diff --git a/bmp.c b/bmp.c
index 58556fd471aa91916934241acbf42b2665855b4d..09cbf9c15197ca5b7416b4ff6d2987ba70a33e27 100644 (file)
--- a/bmp.c
+++ b/bmp.c
@@ -1,3 +1,4 @@
+#define IMAGER_NO_CONTEXT
 #include <stdarg.h>
 #include "imageri.h"
 
@@ -76,6 +77,7 @@ Never compresses the image.
 */
 int
 i_writebmp_wiol(i_img *im, io_glue *ig) {
+  dIMCTXim(im);
   i_clear_error();
 
   /* pick a format */
@@ -116,8 +118,9 @@ i_readbmp_wiol(io_glue *ig, int allow_incomplete) {
   i_packed_t xsize, ysize, planes, bit_count, compression, size_image, xres, yres;
   i_packed_t clr_used, clr_important, offbits;
   i_img *im;
+  dIMCTXio(ig);
 
-  mm_log((1, "i_readbmp_wiol(ig %p)\n", ig));
+  im_log((aIMCTX, 1, "i_readbmp_wiol(ig %p)\n", ig));
   
   i_clear_error();
 
@@ -135,7 +138,7 @@ i_readbmp_wiol(io_glue *ig, int allow_incomplete) {
     return 0;
   }
 
-  mm_log((1, " bmp header: filesize %d offbits %d xsize %d ysize %d planes %d "
+  im_log((aIMCTX, 1, " bmp header: filesize %d offbits %d xsize %d ysize %d planes %d "
           "bit_count %d compression %d size %d xres %d yres %d clr_used %d "
           "clr_important %d\n", (int)filesize, (int)offbits, (int)xsize,
          (int)ysize, (int)planes, (int)bit_count, (int)compression, 
@@ -143,7 +146,7 @@ i_readbmp_wiol(io_glue *ig, int allow_incomplete) {
           (int)clr_important));
 
   if (!i_int_check_image_file_limits(xsize, abs(ysize), 3, sizeof(i_sample_t))) {
-    mm_log((1, "i_readbmp_wiol: image size exceeds limits\n"));
+    im_log((aIMCTX, 1, "i_readbmp_wiol: image size exceeds limits\n"));
     return NULL;
   }
   
@@ -171,7 +174,7 @@ i_readbmp_wiol(io_glue *ig, int allow_incomplete) {
     break;
 
   default:
-    i_push_errorf(0, "unknown bit count for BMP file (%d)", (int)bit_count);
+    im_push_errorf(aIMCTX, 0, "unknown bit count for BMP file (%d)", (int)bit_count);
     return NULL;
   }
 
@@ -236,7 +239,7 @@ read_packed(io_glue *ig, char *format, ...) {
     switch (code) {
     case 'v':
       if (i_io_read(ig, buf, 2) != 2)
-       return 0;
+       goto fail;
       work = buf[0] + ((i_packed_t)buf[1] << 8);
       if (shrieking)
        *p = (work ^ SIGNBIT16) - SIGNBIT16;
@@ -246,7 +249,7 @@ read_packed(io_glue *ig, char *format, ...) {
 
     case 'V':
       if (i_io_read(ig, buf, 4) != 4)
-       return 0;
+       goto fail;
       work = buf[0] + (buf[1] << 8) + ((i_packed_t)buf[2] << 16) + ((i_packed_t)buf[3] << 24);
       if (shrieking)
        *p = (work ^ SIGNBIT32) - SIGNBIT32;
@@ -256,27 +259,35 @@ read_packed(io_glue *ig, char *format, ...) {
 
     case 'C':
       if (i_io_read(ig, buf, 1) != 1)
-       return 0;
+       goto fail;
       *p = buf[0];
       break;
 
     case 'c':
       if (i_io_read(ig, buf, 1) != 1)
-       return 0;
+       goto fail;
       *p = (char)buf[0];
       break;
       
     case '3': /* extension - 24-bit number */
       if (i_io_read(ig, buf, 3) != 3)
-        return 0;
+        goto fail;
       *p = buf[0] + (buf[1] << 8) + ((i_packed_t)buf[2] << 16);
       break;
       
     default:
-      i_fatal(1, "Unknown read_packed format code 0x%02x", code);
+      {
+       dIMCTXio(ig);
+       im_fatal(aIMCTX, 1, "Unknown read_packed format code 0x%02x", code);
+      }
     }
   }
+  va_end(ap);
   return 1;
+
+ fail:
+  va_end(ap);
+  return 0;
 }
 
 /*
@@ -305,7 +316,7 @@ write_packed(io_glue *ig, char *format, ...) {
       buf[0] = i & 255;
       buf[1] = i / 256;
       if (i_io_write(ig, buf, 2) == -1)
-       return 0;
+       goto fail;
       break;
 
     case 'V':
@@ -314,24 +325,31 @@ write_packed(io_glue *ig, char *format, ...) {
       buf[2] = (i >> 16) & 0xFF;
       buf[3] = (i >> 24) & 0xFF;
       if (i_io_write(ig, buf, 4) == -1)
-       return 0;
+       goto fail;
       break;
 
     case 'C':
     case 'c':
       buf[0] = i & 0xFF;
       if (i_io_write(ig, buf, 1) == -1)
-       return 0;
+       goto fail;
       break;
 
     default:
-      i_fatal(1, "Unknown write_packed format code 0x%02x", *format);
+      {
+       dIMCTXio(ig);
+       im_fatal(aIMCTX, 1, "Unknown write_packed format code 0x%02x", *format);
+      }
     }
     ++format;
   }
   va_end(ap);
 
   return 1;
+
+ fail:
+  va_end(ap);
+  return 0;
 }
 
 /*
@@ -350,6 +368,7 @@ int write_bmphead(io_glue *ig, i_img *im, int bit_count, int data_size) {
   int got_xres, got_yres, aspect_only;
   int colors_used = 0;
   int offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
+  dIMCTXim(im);
 
   if (im->xsize > SIGNMAX32 || im->ysize > SIGNMAX32) {
     i_push_error(0, "image too large to write to BMP");
@@ -451,6 +470,7 @@ write_1bit_data(io_glue *ig, i_img *im) {
   int line_size = (im->xsize+7) / 8;
   int x, y;
   int unpacked_size;
+  dIMCTXim(im);
 
   /* round up to nearest multiple of four */
   line_size = (line_size + 3) / 4 * 4;
@@ -522,6 +542,7 @@ write_4bit_data(io_glue *ig, i_img *im) {
   int line_size = (im->xsize+1) / 2;
   int x, y;
   int unpacked_size;
+  dIMCTXim(im);
 
   /* round up to nearest multiple of four */
   line_size = (line_size + 3) / 4 * 4;
@@ -580,6 +601,7 @@ write_8bit_data(io_glue *ig, i_img *im) {
   int line_size = im->xsize;
   int y;
   int unpacked_size;
+  dIMCTXim(im);
 
   /* round up to nearest multiple of four */
   line_size = (line_size + 3) / 4 * 4;
@@ -627,6 +649,7 @@ write_24bit_data(io_glue *ig, i_img *im) {
   int y;
   int line_size = 3 * im->xsize;
   i_color bg;
+  dIMCTXim(im);
 
   i_get_file_background(im, &bg);
 
@@ -681,6 +704,7 @@ read_bmp_pal(io_glue *ig, i_img *im, int count) {
   int i;
   i_packed_t r, g, b, x;
   i_color c;
+  dIMCTXio(ig);
   
   for (i = 0; i < count; ++i) {
     if (!read_packed(ig, "CCCC", &b, &g, &r, &x)) {
@@ -719,13 +743,14 @@ read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
   int bit;
   unsigned char *in;
   long base_offset;
+  dIMCTXio(ig);
 
   if (compression != BI_RGB) {
-    i_push_errorf(0, "unknown 1-bit BMP compression (%d)", compression);
+    im_push_errorf(aIMCTX, 0, "unknown 1-bit BMP compression (%d)", compression);
     return NULL;
   }
 
-  if (xsize + 8 < xsize) { /* if there was overflow */
+  if ((i_img_dim)((i_img_dim_u)xsize + 8) < xsize) { /* if there was overflow */
     /* we check with 8 because we allocate that much for the decoded 
        line buffer */
     i_push_error(0, "integer overflow during memory allocation");
@@ -752,13 +777,13 @@ read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
   if (!clr_used)
     clr_used = 2;
   if (clr_used < 0 || clr_used > 2) {
-    i_push_errorf(0, "out of range colors used (%d)", clr_used);
+    im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used);
     return NULL;
   }
 
   base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
   if (offbits < base_offset) {
-    i_push_errorf(0, "image data offset too small (%ld)", offbits);
+    im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
     return NULL;
   }
 
@@ -847,6 +872,7 @@ read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
   int size, i;
   long base_offset;
   int starty;
+  dIMCTXio(ig);
 
   /* line_size is going to be smaller than xsize in most cases (and
      when it's not, xsize is itself small), and hence not overflow */
@@ -869,13 +895,13 @@ read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
     clr_used = 16;
 
   if (clr_used > 16 || clr_used < 0) {
-    i_push_errorf(0, "out of range colors used (%d)", clr_used);
+    im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used);
     return NULL;
   }
 
   base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
   if (offbits < base_offset) {
-    i_push_errorf(0, "image data offset too small (%ld)", offbits);
+    im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
     return NULL;
   }
 
@@ -940,6 +966,7 @@ read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
   else if (compression == BI_RLE4) {
     int read_size;
     int count;
+    i_img_dim xlimit = (xsize + 1) / 2 * 2; /* rounded up */
 
     i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE4", -1, 0);
     x = 0;
@@ -961,25 +988,23 @@ read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
       }
       else if (packed[0]) {
        int count = packed[0];
-       if (x + count > xsize) {
+       if (x + count > xlimit) {
          /* this file is corrupt */
          myfree(packed);
          myfree(line);
          i_push_error(0, "invalid data during decompression");
-         mm_log((1, "read 4-bit: scanline overflow x %d + count %d vs xsize %d (y %d)\n",
-                 (int)x, count, (int)xsize, (int)y));
+         im_log((aIMCTX, 1, "read 4-bit: scanline overflow x %d + count %d vs xlimit %d (y %d)\n",
+                 (int)x, count, (int)xlimit, (int)y));
          i_img_destroy(im);
          return NULL;
        }
-        line[0] = packed[1] >> 4;
-        line[1] = packed[1] & 0x0F;
-        for (i = 0; i < count; i += 2) {
-          if (i < count-1) 
-            i_ppal(im, x, x+2, y, line);
-          else
-            i_ppal(im, x, x+(count-i), y, line);
-          x += 2;
-        }
+       /* fill in the line */
+       for (i = 0; i < count; i += 2)
+         line[i] = packed[1] >> 4;
+       for (i = 1; i < count; i += 2)
+         line[i] = packed[1] & 0x0F;
+       i_ppal(im, x, x+count, y, line);
+       x += count;
       } else {
         switch (packed[1]) {
         case BMPRLE_ENDOFLINE:
@@ -1013,11 +1038,13 @@ read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
 
         default:
           count = packed[1];
-         if (x + count > xsize) {
+         if (x + count > xlimit) {
            /* this file is corrupt */
            myfree(packed);
            myfree(line);
            i_push_error(0, "invalid data during decompression");
+           im_log((aIMCTX, 1, "read 4-bit: scanline overflow (unpacked) x %d + count %d vs xlimit %d (y %d)\n",
+                 (int)x, count, (int)xlimit, (int)y));
            i_img_destroy(im);
            return NULL;
          }
@@ -1051,7 +1078,7 @@ read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
   else { /*if (compression == BI_RLE4) {*/
     myfree(packed);
     myfree(line);
-    i_push_errorf(0, "unknown 4-bit BMP compression (%d)", compression);
+    im_push_errorf(aIMCTX, 0, "unknown 4-bit BMP compression (%d)", compression);
     i_img_destroy(im);
     return NULL;
   }
@@ -1076,6 +1103,7 @@ read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
   i_palidx *line;
   int line_size = xsize;
   long base_offset;
+  dIMCTXio(ig);
 
   line_size = (line_size+3) / 4 * 4;
   if (line_size < xsize) { /* if it overflowed (unlikely, but check) */
@@ -1099,13 +1127,13 @@ read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
   if (!clr_used)
     clr_used = 256;
   if (clr_used > 256 || clr_used < 0) {
-    i_push_errorf(0, "out of range colors used (%d)", clr_used);
+    im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used);
     return NULL;
   }
 
   base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
   if (offbits < base_offset) {
-    i_push_errorf(0, "image data offset too small (%ld)", offbits);
+    im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
     return NULL;
   }
 
@@ -1248,7 +1276,7 @@ read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
   }
   else { 
     myfree(line);
-    i_push_errorf(0, "unknown 8-bit BMP compression (%d)", compression);
+    im_push_errorf(aIMCTX, 0, "unknown 8-bit BMP compression (%d)", compression);
     i_img_destroy(im);
     return NULL;
   }
@@ -1321,6 +1349,7 @@ read_direct_bmp(io_glue *ig, int xsize, int ysize, int bit_count,
   const char *compression_name;
   int bytes;
   long base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
+  dIMCTXio(ig);
   
   unpack_code[0] = *("v3V"+pix_size-2);
   unpack_code[1] = '\0';
@@ -1367,7 +1396,7 @@ read_direct_bmp(io_glue *ig, int xsize, int ysize, int bit_count,
         return 0;
       }
       if (rmask == 0) {
-       i_push_errorf(0, "Zero mask for channel %d", i);
+       im_push_errorf(aIMCTX, 0, "Zero mask for channel %d", i);
        return NULL;
       }
       masks.masks[i] = rmask;
@@ -1391,12 +1420,12 @@ read_direct_bmp(io_glue *ig, int xsize, int ysize, int bit_count,
     base_offset += 3 * 4;
   }
   else {
-    i_push_errorf(0, "unknown 24-bit BMP compression (%d)", compression);
+    im_push_errorf(aIMCTX, 0, "unknown 24-bit BMP compression (%d)", compression);
     return NULL;
   }
 
   if (offbits < base_offset) {
-    i_push_errorf(0, "image data offset too small (%ld)", offbits);
+    im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
     return NULL;
   }