diff options
Diffstat (limited to 'analyzer/pamsumm.c')
-rw-r--r-- | analyzer/pamsumm.c | 121 |
1 files changed, 60 insertions, 61 deletions
diff --git a/analyzer/pamsumm.c b/analyzer/pamsumm.c index c427fa7d..9b74e789 100644 --- a/analyzer/pamsumm.c +++ b/analyzer/pamsumm.c @@ -1,43 +1,38 @@ -/****************************************************************************** +/*============================================================================= pamsumm -******************************************************************************* +=============================================================================== Summarize all the samples of a PAM image with various functions. By Bryan Henderson, San Jose CA 2004.02.07. Contributed to the public domain - - -******************************************************************************/ - +=============================================================================*/ #include "pm_c_util.h" #include "pam.h" #include "shhopt.h" #include "mallocvar.h" -enum function {FN_ADD, FN_MEAN, FN_MIN, FN_MAX}; +enum Function {FN_ADD, FN_MEAN, FN_MIN, FN_MAX}; -struct cmdlineInfo { +struct CmdlineInfo { /* All the information the user supplied in the command line, in a form easy for the program to use. */ - const char *inputFilespec; /* Filespec of input file */ - enum function function; - unsigned int normalize; - unsigned int brief; - unsigned int verbose; + const char * inputFileName; /* Name of input file */ + enum Function function; + unsigned int normalize; + unsigned int brief; + unsigned int verbose; }; + static void -parseCommandLine(int argc, char ** const argv, - struct cmdlineInfo * const cmdlineP) { -/*---------------------------------------------------------------------------- - Note that the file spec array we return is stored in the storage that - was passed to us as the argv array. ------------------------------------------------------------------------------*/ - optEntry *option_def = malloc(100*sizeof(optEntry)); - /* Instructions to OptParseOptions2 on how to parse our options. +parseCommandLine(int argc, const char ** const argv, + struct CmdlineInfo * const cmdlineP) { + + optEntry * option_def; + /* Instructions to OptParseOptions3 on how to parse our options. */ optStruct3 opt; @@ -45,7 +40,9 @@ parseCommandLine(int argc, char ** const argv, unsigned int sumSpec, meanSpec, minSpec, maxSpec; - option_def_index = 0; /* incremented by OPTENTRY */ + MALLOCARRAY(option_def, 100); + + option_def_index = 0; /* incremented by OPTENT3 */ OPTENT3(0, "sum", OPT_FLAG, NULL, &sumSpec, 0); OPTENT3(0, "mean", OPT_FLAG, NULL, &meanSpec, 0); OPTENT3(0, "min", OPT_FLAG, NULL, &minSpec, 0); @@ -58,7 +55,7 @@ parseCommandLine(int argc, char ** const argv, opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ - pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0); + pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0); /* Uses and sets argc, argv, and some of *cmdlineP and others. */ if (sumSpec + minSpec + maxSpec > 1) @@ -72,22 +69,24 @@ parseCommandLine(int argc, char ** const argv, cmdlineP->function = FN_MIN; } else if (maxSpec) { cmdlineP->function = FN_MAX; - } else + } else pm_error("You must specify one of -sum, -min, -max, or -mean"); - + if (argc-1 > 1) - pm_error("Too many arguments (%d). File spec is the only argument.", + pm_error("Too many arguments (%d). File name is the only argument.", argc-1); if (argc-1 < 1) - cmdlineP->inputFilespec = "-"; - else - cmdlineP->inputFilespec = argv[1]; - + cmdlineP->inputFileName = "-"; + else + cmdlineP->inputFileName = argv[1]; + + free(option_def); } -struct accum { + +struct Accum { union { double sum; unsigned int min; @@ -98,8 +97,8 @@ struct accum { static void -initAccumulator(struct accum * const accumulatorP, - enum function const function) { +initAccumulator(struct Accum * const accumulatorP, + enum Function const function) { switch(function) { case FN_ADD: accumulatorP->u.sum = 0.0; break; @@ -114,8 +113,8 @@ initAccumulator(struct accum * const accumulatorP, static void aggregate(struct pam * const inpamP, tuple * const tupleRow, - enum function const function, - struct accum * const accumulatorP) { + enum Function const function, + struct Accum * const accumulatorP) { unsigned int col; @@ -123,11 +122,11 @@ aggregate(struct pam * const inpamP, unsigned int plane; for (plane = 0; plane < inpamP->depth; ++plane) { switch(function) { - case FN_ADD: - case FN_MEAN: + case FN_ADD: + case FN_MEAN: accumulatorP->u.sum += tupleRow[col][plane]; break; - case FN_MIN: + case FN_MIN: if (tupleRow[col][plane] < accumulatorP->u.min) accumulatorP->u.min = tupleRow[col][plane]; break; @@ -135,7 +134,7 @@ aggregate(struct pam * const inpamP, if (tupleRow[col][plane] > accumulatorP->u.min) accumulatorP->u.min = tupleRow[col][plane]; break; - } + } } } } @@ -143,18 +142,18 @@ aggregate(struct pam * const inpamP, static void -printSummary(struct accum const accumulator, +printSummary(struct Accum const accumulator, unsigned int const scale, unsigned int const count, - enum function const function, - bool const normalize, + enum Function const function, + bool const mustNormalize, bool const brief) { - switch(function) { - case FN_ADD: { + switch (function) { + case FN_ADD: { const char * const intro = brief ? "" : "the sum of all samples is "; - if (normalize) + if (mustNormalize) printf("%s%f\n", intro, accumulator.u.sum/scale); else printf("%s%u\n", intro, (unsigned int)accumulator.u.sum); @@ -163,27 +162,27 @@ printSummary(struct accum const accumulator, case FN_MEAN: { const char * const intro = brief ? "" : "the mean of all samples is "; - if (normalize) + if (mustNormalize) printf("%s%f\n", intro, accumulator.u.sum/count/scale); else printf("%s%f\n", intro, accumulator.u.sum/count); } break; case FN_MIN: { - const char * const intro = + const char * const intro = brief ? "" : "the minimum of all samples is "; - if (normalize) + if (mustNormalize) printf("%s%f\n", intro, (double)accumulator.u.min/scale); else printf("%s%u\n", intro, accumulator.u.min); } break; case FN_MAX: { - const char * const intro = + const char * const intro = brief ? "" : "the maximum of all samples is "; - if (normalize) + if (mustNormalize) printf("%s%f\n", intro, (double)accumulator.u.max/scale); else printf("%s%u\n", intro, accumulator.u.max); @@ -195,20 +194,20 @@ printSummary(struct accum const accumulator, int -main(int argc, char *argv[]) { +main(int argc, const char *argv[]) { - FILE* ifP; - tuple* inputRow; /* Row from input image */ - int row; - struct cmdlineInfo cmdline; + FILE * ifP; + tuple * inputRow; /* Row from input image */ + unsigned int row; + struct CmdlineInfo cmdline; struct pam inpam; /* Input PAM image */ - struct accum accumulator; + struct Accum accumulator; - pnm_init(&argc, argv); + pm_proginit(&argc, argv); parseCommandLine(argc, argv, &cmdline); - ifP = pm_openr(cmdline.inputFilespec); + ifP = pm_openr(cmdline.inputFileName); pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type)); @@ -216,17 +215,17 @@ main(int argc, char *argv[]) { initAccumulator(&accumulator, cmdline.function); - for (row = 0; row < inpam.height; row++) { + for (row = 0; row < inpam.height; ++row) { pnm_readpamrow(&inpam, inputRow); aggregate(&inpam, inputRow, cmdline.function, &accumulator); } printSummary(accumulator, (unsigned)inpam.maxval, - inpam.height * inpam.width * inpam.depth, + inpam.height * inpam.width * inpam.depth, cmdline.function, cmdline.normalize, cmdline.brief); pnm_freepamrow(inputRow); pm_close(inpam.file); - + return 0; } |