about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--doc/HISTORY3
-rw-r--r--editor/pnmrotate.c25
-rw-r--r--editor/pnmshear.c23
-rw-r--r--lib/libpnm3.c42
-rw-r--r--lib/pnm.h5
-rw-r--r--lib/ppm.h17
6 files changed, 71 insertions, 44 deletions
diff --git a/doc/HISTORY b/doc/HISTORY
index c62f64df..a4d9d757 100644
--- a/doc/HISTORY
+++ b/doc/HISTORY
@@ -6,8 +6,7 @@ CHANGE HISTORY
 
 not yet  BJH  Release 10.37.0
 
-              libnetpbm: Fix crash due to inconsistent definition of
-              pm_filepos.  Affects xxx_check of pgm images.
+              pnmrotate: fix -background option with PGM/PBM input.
 
               pnmshear: add -background.  Thanks Erik Auerswald
               <auerswal@unix-ag.uni-kl.de>.
diff --git a/editor/pnmrotate.c b/editor/pnmrotate.c
index fafb2f3a..a8cf62b0 100644
--- a/editor/pnmrotate.c
+++ b/editor/pnmrotate.c
@@ -15,9 +15,10 @@
 #include <math.h>
 #include <assert.h>
 
-#include "pnm.h"
-#include "shhopt.h"
 #include "mallocvar.h"
+#include "shhopt.h"
+#include "ppm.h"
+#include "pnm.h"
 
 #define SCALE 4096
 #define HALFSCALE 2048
@@ -169,25 +170,7 @@ backgroundColor(const char * const backgroundColorName,
     xel retval;
 
     if (backgroundColorName) {
-        retval = ppm_parsecolor(backgroundColorName, maxval);
-
-        switch(PNM_FORMAT_TYPE(format)) {
-        case PGM_TYPE:
-            if (!PPM_ISGRAY(retval))
-                pm_error("Image is PGM (grayscale), "
-                         "but you specified a non-gray "
-                         "background color '%s'", backgroundColorName);
-
-            break;
-        case PBM_TYPE:
-            if (!PNM_EQUAL(retval, pnm_whitexel(maxval, format)) &&
-                !PNM_EQUAL(retval, pnm_blackxel(maxval, format)))
-                pm_error("Image is PBM (black and white), "
-                         "but you specified '%s', which is neither black "
-                         "nor white, as background color", 
-                         backgroundColorName);
-            break;
-        }
+        retval = pnm_parsecolorxel(backgroundColorName, maxval, format);
     } else 
         retval = pnm_backgroundxelrow(topRow, cols, maxval, format);
 
diff --git a/editor/pnmshear.c b/editor/pnmshear.c
index 5ed6c5c5..9fd788a5 100644
--- a/editor/pnmshear.c
+++ b/editor/pnmshear.c
@@ -16,6 +16,7 @@
 #include <math.h>
 #include <string.h>
 
+#include "ppm.h"
 #include "pnm.h"
 #include "shhopt.h"
 
@@ -95,7 +96,7 @@ makeNewXel(xel *  const outputXelP,
    The format of the pixel is 'format'.
 -----------------------------------------------------------------------------*/
 
-    switch ( PNM_FORMAT_TYPE(format) ) {
+    switch (PNM_FORMAT_TYPE(format)) {
     case PPM_TYPE:
         PPM_ASSIGN(*outputXelP,
                    (fracnew0 * PPM_GETR(prevXel) 
@@ -178,7 +179,6 @@ shearRow(xel *        const xelrow,
 }
 
 
-
 static xel
 backgroundColor(const char * const backgroundColorName,
                 xel *        const topRow,
@@ -189,24 +189,7 @@ backgroundColor(const char * const backgroundColorName,
     xel retval;
 
     if (backgroundColorName) {
-        retval = ppm_parsecolor(backgroundColorName, maxval);
-
-        switch(PNM_FORMAT_TYPE(format)) {
-        case PGM_TYPE:
-            if (!PPM_ISGRAY(retval))
-                pm_error("Image is PGM (grayscale), "
-                         "but you specified a non-gray "
-                         "background color '%s'", backgroundColorName);
-            break;
-        case PBM_TYPE:
-            if (!PNM_EQUAL(retval, pnm_whitexel(maxval, format)) &&
-                !PNM_EQUAL(retval, pnm_blackxel(maxval, format)))
-                pm_error ("Image is PBM (black and white), "
-                          "but you specified '%s', which is neither black "
-                          "nor white, as background color", 
-                          backgroundColorName);
-            break;
-        }
+        retval = pnm_parsecolorxel(backgroundColorName, maxval, format);
     } else 
         retval = pnm_backgroundxelrow(topRow, cols, maxval, format);
 
diff --git a/lib/libpnm3.c b/lib/libpnm3.c
index 80ccadfa..c7951546 100644
--- a/lib/libpnm3.c
+++ b/lib/libpnm3.c
@@ -345,3 +345,45 @@ xeltopixel(xel const inputxel) {
                PNM_GET1(inputxel), PNM_GET1(inputxel), PNM_GET1(inputxel));
     return outputpixel;
 }
+
+
+
+xel
+pnm_parsecolorxel(const char * const colorName,
+                  xelval       const maxval,
+                  int          const format) {
+
+    pixel const bgColor = ppm_parsecolor(colorName, maxval);
+
+    xel retval;
+
+    switch(PNM_FORMAT_TYPE(format)) {
+    case PPM_TYPE:
+        PNM_ASSIGN(retval,
+                   PPM_GETR(bgColor), PPM_GETG(bgColor), PPM_GETB(bgColor));
+        break;
+    case PGM_TYPE:
+        if (PPM_ISGRAY(bgColor))
+            PNM_ASSIGN1(retval, PPM_GETB(bgColor));
+        else
+            pm_error("Non-gray color '%s' specified for a "
+                     "grayscale (PGM) image",
+                     colorName);
+                   break;
+    case PBM_TYPE:
+        if (PPM_EQUAL(bgColor, ppm_whitepixel(maxval)))
+            PNM_ASSIGN1(retval, maxval);
+        else if (PPM_EQUAL(bgColor, ppm_blackpixel()))
+            PNM_ASSIGN1(retval, 0);
+        else
+            pm_error ("Color '%s', which is neither black nor white, "
+                      "specified for a black and white (PBM) image",
+                      colorName);
+        break;
+    default:
+        pm_error("Invalid format code %d passed to pnm_parsecolor()",
+                 format);
+    }
+    
+    return retval;
+}
diff --git a/lib/pnm.h b/lib/pnm.h
index f4469bf5..354eacf9 100644
--- a/lib/pnm.h
+++ b/lib/pnm.h
@@ -126,6 +126,11 @@ pnm_promoteformatrow(xel* xelrow, int cols, xelval maxval, int format,
 pixel
 xeltopixel(xel const inputxel);
 
+xel
+pnm_parsecolorxel(const char * const colorName,
+                  xelval       const maxval,
+                  int          const format);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/ppm.h b/lib/ppm.h
index 622d3e09..7f7d6446 100644
--- a/lib/ppm.h
+++ b/lib/ppm.h
@@ -58,7 +58,22 @@ typedef struct {
   ((f) == PPM_FORMAT || (f) == RPPM_FORMAT ? PPM_TYPE : PGM_FORMAT_TYPE(f))
 
 
-/* Declarations of routines. */
+static __inline__ pixel
+ppm_whitepixel(pixval maxval) {
+
+    pixel retval;
+    PPM_ASSIGN(retval, maxval, maxval, maxval);
+
+    return retval;
+}
+
+static __inline__ pixel
+ppm_blackpixel(void) {
+
+    pixel const retval = {0, 0, 0};
+
+    return retval;
+}
 
 void ppm_init(int * argcP, char* argv[]);