diff options
Diffstat (limited to 'other/pamarith.c')
-rw-r--r-- | other/pamarith.c | 46 |
1 files changed, 26 insertions, 20 deletions
diff --git a/other/pamarith.c b/other/pamarith.c index 3fde1953..c67c7326 100644 --- a/other/pamarith.c +++ b/other/pamarith.c @@ -4,6 +4,7 @@ #include "pm_c_util.h" #include "mallocvar.h" +#include "nstring.h" #include "shhopt.h" #include "pam.h" @@ -19,7 +20,7 @@ static bool isDyadic(enum function const function) { bool retval; - + switch (function) { case FN_ADD: case FN_MEAN: @@ -71,7 +72,7 @@ parseCommandLine(int argc, const char ** const argv, optStruct3 opt; unsigned int option_def_index; - + unsigned int addSpec, subtractSpec, multiplySpec, divideSpec, differenceSpec, minimumSpec, maximumSpec, meanSpec, compareSpec, @@ -160,7 +161,7 @@ parseCommandLine(int argc, const char ** const argv, cmdlineP->operandFileNames = &argv[1]; } } -} +} @@ -183,7 +184,7 @@ static enum category functionCategory(enum function const function) { enum category retval; - + switch (function) { case FN_ADD: case FN_SUBTRACT: @@ -251,6 +252,7 @@ computeOutputType(struct pam * const outpamP, outpamP->width = inpam1.width; outpamP->depth = MAX(inpam1.depth, inpam2.depth); + if (function == FN_COMPARE) outpamP->format = outFmtForCompare(inpam1.format, inpam2.format); else @@ -267,7 +269,7 @@ computeOutputType(struct pam * const outpamP, if (inpam2.maxval != inpam1.maxval) pm_error("For a bit string operation, the maxvals of the " "left and right image must be the same. You have " - "left=%u and right=%u", + "left=%u and right=%u", (unsigned)inpam1.maxval, (unsigned)inpam2.maxval); if (pm_bitstomaxval(pm_maxvaltobits(inpam1.maxval)) != inpam1.maxval) @@ -288,7 +290,11 @@ computeOutputType(struct pam * const outpamP, outpamP->maxval = inpam1.maxval; } outpamP->bytes_per_sample = (pm_maxvaltobits(outpamP->maxval)+7)/8; - strcpy(outpamP->tuple_type, inpam1.tuple_type); + + if (outpamP->maxval > 1 && strneq(inpam1.tuple_type, "BLACKANDWHITE", 13)) + strcpy(outpamP->tuple_type, ""); + else + strcpy(outpamP->tuple_type, inpam1.tuple_type); } @@ -394,7 +400,7 @@ applyNormalizedFunction(enum function const function, operands[0] / operands[1] : 1.; break; case FN_DIFFERENCE: - result = operands[0] > operands[1] ? + result = operands[0] > operands[1] ? operands[0] - operands[1] : operands[1] - operands[0]; break; case FN_MINIMUM: @@ -407,7 +413,7 @@ applyNormalizedFunction(enum function const function, result = samplenMean(operands, operandCt); break; case FN_COMPARE: - result = + result = operands[0] > operands[1] ? 1. : operands[0] < operands[1] ? 0. : .5; @@ -444,7 +450,7 @@ doNormalizedArith(struct pam * const inpam1P, computation */ unsigned int * plane; - /* plane[0] is the plane number in the first operand image for + /* plane[0] is the plane number in the first operand image for the current one-sample computation. plane[1] is the plane number in the second operand image, etc. */ @@ -461,10 +467,10 @@ doNormalizedArith(struct pam * const inpam1P, unsigned int col; pnm_readpamrown(inpam1P, tuplerown[0]); pnm_readpamrown(inpam2P, tuplerown[1]); - + for (col = 0; col < outpamP->width; ++col) { unsigned int outplane; - + for (outplane = 0; outplane < outpamP->depth; ++outplane) { unsigned int op; @@ -474,8 +480,8 @@ doNormalizedArith(struct pam * const inpam1P, for (op = 0; op < operandCt; ++op) operands[op] = tuplerown[op][col][plane[op]]; - tuplerownOut[col][outplane] = - applyNormalizedFunction(function, operands, operandCt); + tuplerownOut[col][outplane] = + applyNormalizedFunction(function, operands, operandCt); assert(tuplerownOut[col][outplane] >= 0.); assert(tuplerownOut[col][outplane] <= 1.); } @@ -765,7 +771,7 @@ doUnNormalizedArith(struct pam * const inpam1P, computation */ unsigned int * plane; - /* plane[0] is the plane number in the first operand image for + /* plane[0] is the plane number in the first operand image for the current one-sample computation. plane[1] is the plane number in the second operand image, etc. */ @@ -787,10 +793,10 @@ doUnNormalizedArith(struct pam * const inpam1P, unsigned int col; pnm_readpamrow(inpam1P, tuplerow[0]); pnm_readpamrow(inpam2P, tuplerow[1]); - + for (col = 0; col < outpamP->width; ++col) { unsigned int outplane; - + for (outplane = 0; outplane < outpamP->depth; ++outplane) { unsigned int op; @@ -800,7 +806,7 @@ doUnNormalizedArith(struct pam * const inpam1P, for (op = 0; op < operandCt; ++op) operands[op] = tuplerow[op][col][plane[op]]; - tuplerowOut[col][outplane] = + tuplerowOut[col][outplane] = applyUnNormalizedFunction(function, operands, operandCt, maxval); @@ -830,7 +836,7 @@ main(int argc, const char *argv[]) { struct pam outpam; FILE * if1P; FILE * if2P; - + pm_proginit(&argc, argv); parseCommandLine(argc, argv, &cmdline); @@ -861,7 +867,7 @@ main(int argc, const char *argv[]) { pnm_writepaminit(&outpam); - switch (functionCategory(cmdline.function)) { + switch (functionCategory(cmdline.function)) { case CATEGORY_FRACTIONAL_ARITH: if (inpam1.maxval == inpam2.maxval && inpam2.maxval == outpam.maxval) doUnNormalizedArith(&inpam1, &inpam2, &outpam, cmdline.function); @@ -876,6 +882,6 @@ main(int argc, const char *argv[]) { pm_close(if1P); pm_close(if2P); - + return 0; } |