about summary refs log tree commit diff
path: root/generator/pgmnoise.c
blob: 643f260f23523183eb7b04bb2562cd253adf5e80 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

/*********************************************************************/
/* 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 "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";

    /* 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);

    destrow = pgm_allocrow(cols);

    pgm_writepgminit(stdout, cols, rows, PGM_MAXMAXVAL, 0);

    srand(pm_randseed());

    /* create the (gray) noise */

    for (row = 0; row < rows; ++row) {
        unsigned int col;
        for (col = 0; col < cols; ++col)
            destrow[col] = rand() % PGM_MAXMAXVAL;

        pgm_writepgmrow(stdout, destrow, cols, PGM_MAXMAXVAL, 0);
    }

    pgm_freerow(destrow);

    return 0;
}