diff options
Diffstat (limited to 'generator/pgmnoise.c')
-rw-r--r-- | generator/pgmnoise.c | 182 |
1 files changed, 109 insertions, 73 deletions
diff --git a/generator/pgmnoise.c b/generator/pgmnoise.c index 708d0cd9..215cbfeb 100644 --- a/generator/pgmnoise.c +++ b/generator/pgmnoise.c @@ -1,79 +1,115 @@ +/********************************************************************* + pgmnoise - create a PGM with white noise + Frank Neumann, October 1993 +*********************************************************************/ + +#include "pm_c_util.h" +#include "mallocvar.h" +#include "shhopt.h" +#include "pgm.h" -/*********************************************************************/ -/* pgmnoise - create a portable graymap with white noise */ -/* Frank Neumann, October 1993 */ -/* V1.1 16.11.1993 */ -/* */ -/* version history: */ -/* V1.0 12.10.1993 first version */ -/* V1.1 16.11.1993 Rewritten to be NetPBM.programming conforming */ -/*********************************************************************/ -#include <unistd.h> +struct cmdlineInfo { + /* All the information the user supplied in the command line, + in a form easy for the program to use. + */ + unsigned int width; + unsigned int height; + unsigned int randomseed; + unsigned int randomseedSpec; +}; + + + + +static void +parseCommandLine(int argc, const 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; + /* Instructions to OptParseOptions3 on how to parse our options. + */ + optStruct3 opt; + unsigned int option_def_index; + + MALLOCARRAY_NOFAIL(option_def, 100); + + option_def_index = 0; /* incremented by OPTENT3 */ + OPTENT3(0, "randomseed", OPT_INT, &cmdlineP->randomseed, + &cmdlineP->randomseedSpec, 0); + + opt.opt_table = option_def; + opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ + opt.allowNegNum = FALSE; /* We may have parms that are negative numbers */ + + optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0); + /* Uses and sets argc, argv, and some of *cmdlineP and others. */ + + if (argc-1 != 2) + pm_error("Wrong number of arguments: %u. " + "Arguments are width and height of image, in pixels", + argc-1); + else { + int const width = atoi(argv[1]); + int const height = atoi(argv[2]); + + if (width <= 0) + pm_error("Width must be positive, not %d", width); + else + cmdlineP->width = width; + + if (height <= 0) + pm_error("Height must be positive, not %d", width); + else + cmdlineP->height = height; + } +} -#include "pgm.h" -/* global variables */ -#ifdef AMIGA -static char *version = "$VER: pgmnoise 1.1 (16.11.93)"; /* Amiga version identification */ -#endif - -/**************************/ -/* start of main function */ -/**************************/ -int main(argc, argv) -int argc; -char *argv[]; -{ - int argn, rows, cols, i, j; - gray *destrow; - const char * const usage = "width height\n width and height are picture dimensions in pixels\n"; - time_t timenow; - - /* parse in 'default' parameters */ - pgm_init(&argc, argv); - - argn = 1; - - /* parse in dim factor */ - if (argn == argc) - pm_usage(usage); - if (sscanf(argv[argn], "%d", &cols) != 1) - pm_usage(usage); - argn++; - if (argn == argc) - pm_usage(usage); - if (sscanf(argv[argn], "%d", &rows) != 1) - pm_usage(usage); - - if (cols <= 0 || rows <= 0) - pm_error("picture dimensions should be positive numbers"); - ++argn; - - if (argn != argc) - pm_usage(usage); - - /* no error checking required here, ppmlib does it all for us */ - destrow = pgm_allocrow(cols); - - pgm_writepgminit(stdout, cols, rows, PGM_MAXMAXVAL, 0); - - /* get time of day to feed the random number generator */ - timenow = time(NULL); - srand(timenow ^ getpid()); - - /* create the (gray) noise */ - for (i = 0; i < rows; i++) - { - for (j = 0; j < cols; j++) - destrow[j] = rand() % (PGM_MAXMAXVAL+1); - - /* write out one line of graphic data */ - pgm_writepgmrow(stdout, destrow, cols, PGM_MAXMAXVAL, 0); - } - - pgm_freerow(destrow); - - exit(0); + + +static void +pgmnoise(FILE * const ofP, + unsigned int const cols, + unsigned int const rows, + gray const maxval) { + + unsigned int row; + gray * destrow; + + destrow = pgm_allocrow(cols); + + pgm_writepgminit(ofP, cols, rows, maxval, 0); + + for (row = 0; row < rows; ++row) { + unsigned int col; + for (col = 0; col < cols; ++col) + destrow[col] = rand() % (maxval + 1); + + pgm_writepgmrow(ofP, destrow, cols, maxval, 0); + } + + pgm_freerow(destrow); +} + + + +int main(int argc, + const char * argv[]) { + + struct cmdlineInfo cmdline; + + pm_proginit(&argc, argv); + + parseCommandLine(argc, argv, &cmdline); + + srand(cmdline.randomseedSpec ? cmdline.randomseed : pm_randseed()); + + pgmnoise(stdout, cmdline.width, cmdline.height, PGM_MAXMAXVAL); + + return 0; } |