about summary refs log tree commit diff
path: root/converter
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2008-07-13 23:27:52 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2008-07-13 23:27:52 +0000
commitb7ec37261845b154531e53ad0af7f597f47c86fc (patch)
tree4b800fc66dbd4dcdb95fc4f0dfded7936d4f0ccd /converter
parenta9311ae8756ef47f62ef4e4ce5e00f2fa1100ffe (diff)
downloadnetpbm-mirror-b7ec37261845b154531e53ad0af7f597f47c86fc.tar.gz
netpbm-mirror-b7ec37261845b154531e53ad0af7f597f47c86fc.tar.xz
netpbm-mirror-b7ec37261845b154531e53ad0af7f597f47c86fc.zip
Make color output if background is color
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@674 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter')
-rw-r--r--converter/other/pngtopnm.c44
1 files changed, 23 insertions, 21 deletions
diff --git a/converter/other/pngtopnm.c b/converter/other/pngtopnm.c
index b32bb682..38b9aab6 100644
--- a/converter/other/pngtopnm.c
+++ b/converter/other/pngtopnm.c
@@ -81,7 +81,7 @@ struct cmdlineInfo {
 };
 
 
-typedef struct pngcolor {
+typedef struct {
 /*----------------------------------------------------------------------------
    A color in a format compatible with the PNG library.
 
@@ -202,6 +202,14 @@ _get_png_val (png_byte ** const pp,
 
 
 
+static bool
+isGrayscale(pngcolor const color) {
+
+    return color.r == color.g && color.r == color.b;
+}
+
+
+
 static void 
 setXel(xel *               const xelP, 
        pngcolor            const foreground,
@@ -789,10 +797,12 @@ imageHasColor(png_info * const info_ptr) {
 static void
 determineOutputType(png_info *          const info_ptr,
                     enum alpha_handling const alphaHandling,
+                    pngcolor            const bgColor,
                     xelval              const maxval,
                     int *               const pnmTypeP) {
 
-    if (alphaHandling != ALPHA_ONLY && imageHasColor(info_ptr))
+    if (alphaHandling != ALPHA_ONLY &&
+        (imageHasColor(info_ptr) || !isGrayscale(bgColor)))
         *pnmTypeP = PPM_TYPE;
     else {
         if (maxval > 1)
@@ -805,11 +815,11 @@ determineOutputType(png_info *          const info_ptr,
 
 
 static void
-getBackgroundColor(png_info *        const info_ptr,
-                   const char *      const requestedColor,
-                   float             const totalgamma,
-                   xelval            const maxval,
-                   struct pngcolor * const bgColorP) {
+getBackgroundColor(png_info *   const info_ptr,
+                   const char * const requestedColor,
+                   float        const totalgamma,
+                   xelval       const maxval,
+                   pngcolor *   const bgColorP) {
 /*----------------------------------------------------------------------------
    Figure out what the background color should be.  If the user requested
    a particular color ('requestedColor' not null), that's the one.
@@ -822,19 +832,11 @@ getBackgroundColor(png_info *        const info_ptr,
            which is a bit arbitrary.  
         */
         pixel const backcolor = ppm_parsecolor(requestedColor, maxval);
-        switch (info_ptr->color_type) {
-        case PNG_COLOR_TYPE_GRAY:
-        case PNG_COLOR_TYPE_GRAY_ALPHA:
-            bgColorP->r = bgColorP->g = bgColorP->b = PNM_GET1(backcolor);
-            break;
-        case PNG_COLOR_TYPE_PALETTE:
-        case PNG_COLOR_TYPE_RGB:
-        case PNG_COLOR_TYPE_RGB_ALPHA:
-            bgColorP->r = PPM_GETR(backcolor);
-            bgColorP->g = PPM_GETG(backcolor);
-            bgColorP->b = PPM_GETB(backcolor);
-            break;
-        }
+
+        bgColorP->r = PPM_GETR(backcolor);
+        bgColorP->g = PPM_GETG(backcolor);
+        bgColorP->b = PPM_GETB(backcolor);
+
     } else if (info_ptr->valid & PNG_INFO_bKGD) {
         /* didn't manage to get libpng to work (bugs?) concerning background
            processing, therefore we do our own.
@@ -1060,7 +1062,7 @@ convertpng(FILE *             const ifp,
         }
     }
 
-    determineOutputType(info_ptr, cmdline.alpha, maxval, &pnm_type);
+    determineOutputType(info_ptr, cmdline.alpha, bgColor, maxval, &pnm_type);
 
     writePnm(stdout, maxval, pnm_type, info_ptr, png_image, bgColor, 
              cmdline.alpha, totalgamma);