diff options
Diffstat (limited to 'analyzer')
-rw-r--r-- | analyzer/Makefile | 21 | ||||
-rw-r--r-- | analyzer/pamsumm.c | 95 | ||||
-rw-r--r-- | analyzer/pamtable.c | 197 | ||||
-rw-r--r-- | analyzer/pamtilt.c | 2 |
4 files changed, 263 insertions, 52 deletions
diff --git a/analyzer/Makefile b/analyzer/Makefile index c96a6d0a..18be9d8c 100644 --- a/analyzer/Makefile +++ b/analyzer/Makefile @@ -14,9 +14,24 @@ include $(BUILDDIR)/config.mk # This package is so big, it's useful even when some parts won't # build. -PORTBINARIES = pamfile pammosaicknit pamslice pamsumm pamtilt \ - pbmminkowski pgmhist pnmhistmap ppmhist pgmminkowski -MATHBINARIES = pamsharpmap pamsharpness pgmtexture pnmpsnr +PORTBINARIES = \ + pamfile \ + pammosaicknit \ + pamslice \ + pamsumm \ + pamtable \ + pamtilt \ + pbmminkowski \ + pgmhist \ + pnmhistmap \ + ppmhist \ + pgmminkowski \ + +MATHBINARIES = \ + pamsharpmap \ + pamsharpness \ + pgmtexture \ + pnmpsnr \ BINARIES = $(PORTBINARIES) $(MATHBINARIES) SCRIPT = diff --git a/analyzer/pamsumm.c b/analyzer/pamsumm.c index c427fa7d..7d2c000a 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) @@ -76,18 +73,20 @@ parseCommandLine(int argc, char ** const argv, 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 = "-"; + cmdlineP->inputFileName = "-"; else - cmdlineP->inputFilespec = argv[1]; + 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; @@ -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) { + 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,7 +162,7 @@ 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); @@ -173,7 +172,7 @@ printSummary(struct accum const accumulator, 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); @@ -183,7 +182,7 @@ printSummary(struct accum const accumulator, 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,7 +215,7 @@ 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); diff --git a/analyzer/pamtable.c b/analyzer/pamtable.c new file mode 100644 index 00000000..9422ff19 --- /dev/null +++ b/analyzer/pamtable.c @@ -0,0 +1,197 @@ +/*============================================================================= + pamtable +=============================================================================== + Print the raster as a table of numbers. + + By Bryan Henderson, San Jose CA 2017.04.15. + + Contributed to the public domain + +=============================================================================*/ +#include <math.h> +#include "pm_c_util.h" +#include "pam.h" +#include "shhopt.h" +#include "mallocvar.h" +#include "nstring.h" + +struct CmdlineInfo { + /* All the information the user supplied in the command line, + in a form easy for the program to use. + */ + const char * inputFileName; /* Name of input file */ + unsigned int verbose; +}; + + +static void +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; + + unsigned int option_def_index; + + MALLOCARRAY(option_def, 100); + + option_def_index = 0; /* incremented by OPTENT3 */ + + OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0); + /* For future expansion */ + + opt.opt_table = option_def; + 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, (char **)argv, opt, sizeof(opt), 0); + /* Uses and sets argc, argv, and some of *cmdlineP and others. */ + + if (argc-1 > 1) + pm_error("Too many arguments (%d). File name is the only argument.", + argc-1); + + if (argc-1 < 1) + cmdlineP->inputFileName = "-"; + else + cmdlineP->inputFileName = argv[1]; + + free(option_def); +} + + + +typedef struct { + + const char * sampleFmt; + /* Printf format of a sample, e.g. %3u */ + + const char * interSampleGutter; + /* What we print between samples within a tuple */ + + const char * interTupleGutter; + /* What we print between tuples within a row */ + +} Format; + + + +static const char * +sampleFormat(const struct pam * const pamP) { +/*---------------------------------------------------------------------------- + The printf format string for a single sample in the output table. + + E.g "%03u". + + This format does not include any spacing between samples. +-----------------------------------------------------------------------------*/ + unsigned int const decimalWidth = ROUNDU(ceil(log10(pamP->maxval + 1))); + + const char * retval; + + pm_asprintf(&retval, "%%%uu", decimalWidth); + + return retval; +} + + + +static void +makeFormat(const struct pam * const pamP, + Format * const formatP) { + + formatP->sampleFmt = sampleFormat(pamP); + + formatP->interSampleGutter = " "; + + formatP->interTupleGutter = pamP->depth > 1 ? "|" : " "; +} + + + +static void +unmakeFormat(Format * const formatP) { + + pm_strfree(formatP->sampleFmt); +} + + + +static void +printRow(const struct pam * const pamP, + tuple * const tupleRow, + Format const format, + FILE * const ofP) { + + unsigned int col; + + for (col = 0; col < pamP->width; ++col) { + unsigned int plane; + + if (col > 0) + fprintf(ofP, format.interTupleGutter); + + for (plane = 0; plane < pamP->depth; ++plane) { + + if (plane > 0) + fprintf(ofP, format.interSampleGutter); + + fprintf(ofP, format.sampleFmt, tupleRow[col][plane]); + } + } + + fprintf(ofP, "\n"); +} + + + +static void +printRaster(FILE * const ifP, + const struct pam * const pamP, + FILE * const ofP) { + + Format format; + + tuple * inputRow; /* Row from input image */ + unsigned int row; + + makeFormat(pamP, &format); + + inputRow = pnm_allocpamrow(pamP); + + for (row = 0; row < pamP->height; ++row) { + pnm_readpamrow(pamP, inputRow); + + printRow(pamP, inputRow, format, ofP); + } + + pnm_freepamrow(inputRow); + + unmakeFormat(&format); +} + + + +int +main(int argc, const char *argv[]) { + + FILE * ifP; + struct CmdlineInfo cmdline; + struct pam inpam; /* Input PAM image */ + + pm_proginit(&argc, argv); + + parseCommandLine(argc, argv, &cmdline); + + ifP = pm_openr(cmdline.inputFileName); + + pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type)); + + printRaster(ifP, &inpam, stdout); + + pm_close(inpam.file); + + return 0; +} diff --git a/analyzer/pamtilt.c b/analyzer/pamtilt.c index 302c6247..70338545 100644 --- a/analyzer/pamtilt.c +++ b/analyzer/pamtilt.c @@ -10,7 +10,7 @@ All work has been contributed to the public domain by its authors. =============================================================================*/ -#define _XOPEN_SOURCE /* get M_PI in math.h */ +#define _XOPEN_SOURCE 500 /* get M_PI in math.h */ #include <assert.h> #include <math.h> |