From afa1aed43637493282899056bfe9885a61696106 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Wed, 25 Nov 2020 01:27:18 +0000 Subject: Fail when depths are incompatible git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@3989 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- doc/HISTORY | 3 +++ other/pamarith.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/doc/HISTORY b/doc/HISTORY index c39c2a7c..776b3af8 100644 --- a/doc/HISTORY +++ b/doc/HISTORY @@ -12,6 +12,9 @@ not yet BJH Release 10.93.00 it makes sense (all but -subtract, -difference, -compare, -divide, -shiftleft, and -shiftright). + pamrith: fail if operand images have different depth and not + depth 1. + 20.09.26 BJH Release 10.92.00 pnmcrop: Make -margin effective with -blank-image=minimize. 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); -- cgit 1.4.1