home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pgm / pgmnoise.c < prev    next >
C/C++ Source or Header  |  1993-12-17  |  2KB  |  78 lines

  1.  
  2. /*********************************************************************/
  3. /* pgmnoise -  create a portable graymap with white noise            */
  4. /* Frank Neumann, October 1993                                       */
  5. /* V1.1 16.11.1993                                                   */
  6. /*                                                                   */
  7. /* version history:                                                  */
  8. /* V1.0 12.10.1993  first version                                    */
  9. /* V1.1 16.11.1993  Rewritten to be NetPBM.programming conforming    */
  10. /*********************************************************************/
  11.  
  12. #include "pgm.h"
  13.  
  14. /* global variables */
  15. #ifdef AMIGA
  16. static char *version = "$VER: pgmnoise 1.1 (16.11.93)"; /* Amiga version identification */
  17. #endif
  18.  
  19. /**************************/
  20. /* start of main function */
  21. /**************************/
  22. int main(argc, argv)
  23. int argc;
  24. char *argv[];
  25. {
  26.     int argn, rows, cols, i, j;
  27.     gray *destrow;
  28.     char *usage = "width height\n        width and height are picture dimensions in pixels\n";
  29.     time_t timenow;
  30.  
  31.     /* parse in 'default' parameters */
  32.     pgm_init(&argc, argv);
  33.  
  34.     argn = 1;
  35.  
  36.     /* parse in dim factor */
  37.     if (argn == argc)
  38.         pm_usage(usage);
  39.     if (sscanf(argv[argn], "%d", &cols) != 1)
  40.         pm_usage(usage);
  41.     argn++;
  42.     if (argn == argc)
  43.         pm_usage(usage);
  44.     if (sscanf(argv[argn], "%d", &rows) != 1)
  45.         pm_usage(usage);
  46.  
  47.     if (cols <= 0 || rows <= 0)
  48.         pm_error("picture dimensions should be positive numbers");
  49.     ++argn;
  50.  
  51.     if (argn != argc)
  52.         pm_usage(usage);
  53.  
  54.     /* no error checking required here, ppmlib does it all for us */
  55.     destrow = pgm_allocrow(cols);
  56.  
  57.     pgm_writepgminit(stdout, cols, rows, PGM_MAXMAXVAL, 0);
  58.  
  59.     /* get time of day to feed the random number generator */
  60.     timenow = time(NULL);
  61.     srandom(timenow);
  62.  
  63.     /* create the (gray) noise */
  64.     for (i = 0; i < rows; i++)
  65.     {
  66.         for (j = 0; j < cols; j++)
  67.             destrow[j] = random() % PGM_MAXMAXVAL;
  68.  
  69.         /* write out one line of graphic data */
  70.         pgm_writepgmrow(stdout, destrow, cols, PGM_MAXMAXVAL, 0);
  71.     }
  72.  
  73.     pgm_freerow(destrow);
  74.  
  75.     exit(0);
  76. }
  77.  
  78.