about summary refs log tree commit diff
path: root/generator/pgmnoise.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2008-11-23 03:45:53 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2008-11-23 03:45:53 +0000
commit18c2a44326ad150107390738916c7d9a1a6fed46 (patch)
tree94d4adb25c30176b78af1d81a9c65b3ab045d528 /generator/pgmnoise.c
parentbafe684838066eb0fdbc6088d8b5b4e05b84beec (diff)
downloadnetpbm-mirror-18c2a44326ad150107390738916c7d9a1a6fed46.tar.gz
netpbm-mirror-18c2a44326ad150107390738916c7d9a1a6fed46.tar.xz
netpbm-mirror-18c2a44326ad150107390738916c7d9a1a6fed46.zip
Add -randomseed
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@771 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'generator/pgmnoise.c')
-rw-r--r--generator/pgmnoise.c134
1 files changed, 91 insertions, 43 deletions
diff --git a/generator/pgmnoise.c b/generator/pgmnoise.c
index af65a23b..9f6b98c8 100644
--- a/generator/pgmnoise.c
+++ b/generator/pgmnoise.c
@@ -1,66 +1,114 @@
+/*********************************************************************
+   pgmnoise -  create a PGM with white noise
+   Frank Neumann, October 1993
+*********************************************************************/
 
-/*********************************************************************/
-/* 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 "mallocvar.h"
+#include "shhopt.h"
 #include "pgm.h"
 
 
-int main(int    argc,
-         char * argv[]) {
-
-    int argn, rows, cols;
-    unsigned int row;
-    gray * destrow;
-
-    const char * const usage = "width height\n        width and height are picture dimensions in pixels\n";
+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;
+    }
+}
 
-    /* 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;
+static void
+pgmnoise(FILE * const ofP,
+         unsigned int const cols,
+         unsigned int const rows,
+         gray         const maxval) {
 
-    if (argn != argc)
-        pm_usage(usage);
+    unsigned int row;
+    gray * destrow;
 
     destrow = pgm_allocrow(cols);
 
-    pgm_writepgminit(stdout, cols, rows, PGM_MAXMAXVAL, 0);
-
-    srand(pm_randseed());
-
-    /* create the (gray) noise */
+    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() % (PGM_MAXMAXVAL + 1);
+            destrow[col] = rand() % (maxval + 1);
 
-        pgm_writepgmrow(stdout, destrow, cols, PGM_MAXMAXVAL, 0);
+        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;
 }
+