about summary refs log tree commit diff
path: root/converter/other/winicontopam.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2013-04-14 23:49:33 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2013-04-14 23:49:33 +0000
commitdc34a2f365db558fce869556789aa618da7fafa4 (patch)
tree9881030049d38b62f663c47091a4379151b8cbe2 /converter/other/winicontopam.c
parent54b1ea7b194667b9d124c90eb19f5594d8910024 (diff)
downloadnetpbm-mirror-dc34a2f365db558fce869556789aa618da7fafa4.tar.gz
netpbm-mirror-dc34a2f365db558fce869556789aa618da7fafa4.tar.xz
netpbm-mirror-dc34a2f365db558fce869556789aa618da7fafa4.zip
Fix some bugs from commit a few days ago
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1885 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter/other/winicontopam.c')
-rw-r--r--converter/other/winicontopam.c85
1 files changed, 51 insertions, 34 deletions
diff --git a/converter/other/winicontopam.c b/converter/other/winicontopam.c
index ba852083..d846ad85 100644
--- a/converter/other/winicontopam.c
+++ b/converter/other/winicontopam.c
@@ -2,13 +2,13 @@
 #include <stdio.h>
 #include <string.h>
 
-#include "pm_config.h"
-#include "pm_c_util.h"
-#include "mallocvar.h"
-#include "nstring.h"
-#include "shhopt.h"
-#include "pam.h"
-#include "pm_system.h"
+#include "netpbm/pm_config.h"
+#include "netpbm/pm_c_util.h"
+#include "netpbm/mallocvar.h"
+#include "netpbm/nstring.h"
+#include "netpbm/shhopt.h"
+#include "netpbm/pam.h"
+#include "netpbm/pm_system.h"
 
 #include "winicon.h"
 
@@ -239,15 +239,21 @@ readIconDir(struct File * const fP,
     if (head.zero != 0 || head.type != ICONDIR_TYPE_ICO)
         pm_error("Not a valid windows icon file");
 
-    dirP = malloc(sizeof(struct IconDir)
-                  - sizeof(struct IconDirEntry)
-                  + head.count * sizeof(struct IconDirEntry));
+    MALLOCVAR(dirP);
+
     if (dirP == NULL)
-        pm_error("out of memory.");
+        pm_error("Could't allocate memory for Icon directory");
+
+    MALLOCARRAY(dirP->entries, head.count);
 
-    dirP->zero  = head.zero;
-    dirP->type  = head.type;
-    dirP->count = head.count;
+    if (dirP->entries == NULL)
+        pm_error("Could not allocate memory for %u entries in icon directory",
+                 head.count);
+
+    dirP->zero           = head.zero;
+    dirP->type           = head.type;
+    dirP->count          = head.count;
+    dirP->entriesAllocCt = head.count;
 
     for (imageIndex = 0; imageIndex < head.count; ++imageIndex) {
         struct IconDirEntry * const dirEntryP = &dirP->entries[imageIndex];
@@ -326,6 +332,15 @@ readIconDir(struct File * const fP,
 
 
 
+static void
+freeIconDir(struct IconDir * const dirP) {
+
+    free(dirP->entries);
+    free(dirP);
+}
+
+
+
 static const unsigned char *
 readImage(struct File *         const fP,
           struct IconDirEntry * const dirEntryP) {
@@ -643,7 +658,7 @@ readXorBitfields(struct BitmapInfoHeader * const hdrP,
 
         bitmapCursor  += 12;
         sizeRemaining -= 12;
-        bytesConsumed     += 12;
+        bytesConsumed += 12;
     }
 
     /*  determine shift and maxval from bit field for each channel */
@@ -681,10 +696,7 @@ readXorBitfields(struct BitmapInfoHeader * const hdrP,
                        "reading XOR mask: image truncated.", index);
             truncatedXorSize = sizeRemaining;
         } else
-              truncatedXorSize = xorSize;
-        bitmapCursor  += truncatedXorSize;
-        sizeRemaining -= truncatedXorSize;
-        bytesConsumed     += truncatedXorSize;
+            truncatedXorSize = xorSize;
     }
 
     bytesPerRow = ((hdrP->bits_per_pixel * hdrP->bm_width + 31) / 32) * 4;
@@ -735,6 +747,10 @@ readXorBitfields(struct BitmapInfoHeader * const hdrP,
         }
     }
 
+    bitmapCursor  += truncatedXorSize;
+    sizeRemaining -= truncatedXorSize;
+    bytesConsumed += truncatedXorSize;
+
     /*  A fully transparent alpha channel (all zero) in XOR mask is
         defined to be void by Microsoft, and a fully opaque alpha
         channel (all maxval) is trivial and will be dropped.
@@ -954,7 +970,7 @@ convertBmp(const unsigned char * const image,
            FILE *                const ofP,
            struct IconDirEntry * const dirEntryP,
            bool                  const needHeaderDump,
-           bool                  const andMasks) {
+           bool                  const wantAndMaskPlane) {
     
     struct BitmapInfoHeader hdr;
     uint32_t                offset;
@@ -999,6 +1015,10 @@ convertBmp(const unsigned char * const image,
     outpam.height = hdr.bm_height / 2;
     outpam.maxval = 255;
     outpam.allocation_depth = 5;
+    outpam.depth  = 0;
+        /* Just for tuple array allocation; we set the value for the actual
+           output image below.
+        */
 
     tuples = pnm_allocpamarray(&outpam);
 
@@ -1022,7 +1042,7 @@ convertBmp(const unsigned char * const image,
             andPlane = PAM_TRN_PLANE;
             strcpy (outpam.tuple_type, "RGB_ALPHA");
             outpam.depth  = 4;
-        } else if (andMasks) {
+        } else if (wantAndMaskPlane) {
             haveAnd = true;
             andPlane = PAM_TRN_PLANE + 1;
             outpam.depth  = 5;
@@ -1086,12 +1106,12 @@ reportPngInfo(const unsigned char * const image,
             break;
 
         case 4:
-            colorType = "grayscale +alpha";
+            colorType = "grayscale + alpha";
             depth     = ihdr.bit_depth * 2;
             break;
 
         case 6:
-            colorType = "RGB +alpha";
+            colorType = "RGB + alpha";
             depth     = ihdr.bit_depth * 4;
             break;
 
@@ -1112,13 +1132,10 @@ reportPngInfo(const unsigned char * const image,
                        dirEntryP->index, dirEntryP->width, dirEntryP->height,
                        ihdr.width, ihdr.height);
         }
-        if (dirEntryP->bits_per_pixel != 0
-            && dirEntryP->bits_per_pixel != depth) {
-            pm_message("image %2u:"
-                       " mismatch in header and image bpp value"
-                       " (%u vs %u)",
-                       dirEntryP->index, dirEntryP->bits_per_pixel, depth);
-        }
+        /* Mismatch between dirEntryP->bits_per_pixel and 'depth' is
+           normal, because the creator of the winicon file doesn't necessarily
+           know the true color resolution.
+        */
     }
 }
 
@@ -1196,7 +1213,7 @@ convertImage(struct File *         const icoP,
              struct IconDirEntry * const dirEntryP,
              FILE *                const ofP,
              bool                  const needHeaderDump,
-             bool                  const andMasks) {
+             bool                  const wantAndMaskPlane) {
 
     const unsigned char * image;  /* malloced */
 
@@ -1205,7 +1222,7 @@ convertImage(struct File *         const icoP,
     if (MEMEQ(image, pngHeader, sizeof (pngHeader)))
         convertPng(image, ofP, dirEntryP);
     else
-        convertBmp(image, ofP, dirEntryP, needHeaderDump, andMasks);
+        convertBmp(image, ofP, dirEntryP, needHeaderDump, wantAndMaskPlane);
 
     free((void *)image);
 }
@@ -1253,8 +1270,8 @@ main (int argc, const char *argv []) {
         convertImage(&ico, &dirP->entries[bestImage(dirP)], stdout,
                      cmdline.headerdump, cmdline.andmasks);
     }
-
-    free(dirP);
+    
+    freeIconDir(dirP);
 
     if (ico.fileP != stdin)
         pm_close(ico.fileP);