about summary refs log tree commit diff
path: root/other
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2020-11-25 01:27:18 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2020-11-25 01:27:18 +0000
commitafa1aed43637493282899056bfe9885a61696106 (patch)
treeeb869368dc4d0ad2cbe30480af55f6249033fd3e /other
parent7e211bcd9a3bc1a050439248c0a9b805fbb9f8af (diff)
downloadnetpbm-mirror-afa1aed43637493282899056bfe9885a61696106.tar.gz
netpbm-mirror-afa1aed43637493282899056bfe9885a61696106.tar.xz
netpbm-mirror-afa1aed43637493282899056bfe9885a61696106.zip
Fail when depths are incompatible
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@3989 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'other')
-rw-r--r--other/pamarith.c70
1 files changed, 60 insertions, 10 deletions
diff --git a/other/pamarith.c b/other/pamarith.c
index 301f76a0..31f52a59 100644
--- a/other/pamarith.c
+++ b/other/pamarith.c
@@ -205,6 +205,64 @@ enum category {
 
 
 
+static void
+validateSameWidthHeight(const struct pam * const inpam  /* array */,
+                        unsigned int       const operandCt) {
+    unsigned int i;
+
+    for (i = 1; i < operandCt; ++i) {
+
+        if (i > 0) {
+            if (inpam[i].width  != inpam[0].width ||
+                inpam[i].height != inpam[0].height) {
+                pm_error("The images must be the same width and height.  "
+                         "The first is %ux%ux%u, but another is %ux%ux%u",
+                         inpam[0].width, inpam[0].height, inpam[0].depth,
+                         inpam[i].width, inpam[i].height, inpam[i].depth);
+            }
+        }
+    }
+}
+
+
+
+static void
+validateCompatibleDepth(const struct pam * const inpam  /* array */,
+                        unsigned int       const operandCt) {
+
+    unsigned int i;
+    bool         haveNonUnityDepth;
+    unsigned int nonUnityDepth;
+
+    for (i = 0, haveNonUnityDepth = false; i < operandCt; ++i) {
+        if (inpam[i].depth != 1) {
+            if (haveNonUnityDepth) {
+                if (inpam[i].depth != nonUnityDepth)
+                    pm_error("The images must have the same depth "
+                             "or depth 1.  "
+                             "But one has depth %u and another %u",
+                             nonUnityDepth, inpam[i].depth);
+            } else {
+                haveNonUnityDepth = true;
+                nonUnityDepth = inpam[i].depth;
+            }
+        }
+    }
+}
+
+
+
+static void
+validateConsistentDimensions(const struct pam * const inpam  /* array */,
+                             unsigned int       const operandCt) {
+
+    validateSameWidthHeight(inpam, operandCt);
+
+    validateCompatibleDepth(inpam, operandCt);
+}
+
+
+
 static enum category
 functionCategory(enum Function const function) {
 
@@ -998,18 +1056,10 @@ main(int argc, const char *argv[]) {
             ifP[i] = pm_openr(cmdline.operandFileNames[i]);
 
             pnm_readpaminit(ifP[i], &inpam[i], PAM_STRUCT_SIZE(tuple_type));
-
-            if (i > 0) {
-                if (inpam[i].width != inpam[0].width ||
-                    inpam[i].height != inpam[0].height) {
-                    pm_error("The images must be the same width and height.  "
-                             "The first is %ux%ux%u, but another is %ux%ux%u",
-                             inpam[0].width, inpam[0].height, inpam[0].depth,
-                             inpam[i].width, inpam[i].height, inpam[i].depth);
-                }
-            }
         }
     }
+    validateConsistentDimensions(inpam, cmdline.operandCt);
+
     computeOutputType(&outpam, inpam, cmdline.operandCt, cmdline.function);
 
     pnm_writepaminit(&outpam);