about summary refs log tree commit diff
path: root/generator/ppmrough.c
blob: 879bcb06bb675a23227dc5eaa270de80d1b6d387 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* ppmrough.c - create a PPM image containing two colors with a ragged
   border between them
**
** Copyright (C) 2002 by Eckard Specht.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.  */

#include <stdlib.h>
#include <math.h>
#include <sys/time.h>

#include "shhopt.h"
#include "ppm.h"

static pixel** PIX;
static pixval BG_RED, BG_GREEN, BG_BLUE;


struct cmdlineInfo {
  /* All the information the user supplied in the command line,
     in a form easy for the program to use.
  */
  unsigned int left, right, top, bottom;
  unsigned int width, height, var;
  const char *bg_rgb;
  const char *fg_rgb;
  unsigned init;
  unsigned int verbose;
};


static void
/*-------------------------------------------------------------------------- */
   parseCommandLine(int argc, char ** argv, struct cmdlineInfo *cmdlineP)
/*-------------------------------------------------------------------------- */
{
  optEntry *option_def = malloc(100*sizeof(optEntry));
    /* Instructions to OptParseOptions2 on how to parse our options.    */
  optStruct3 opt;

  unsigned int option_def_index;

  option_def_index = 0;   /* incremented by OPTENTRY */
  OPTENT3(0, "width",   OPT_UINT,   &cmdlineP->width,   NULL, 0);
  OPTENT3(0, "height",  OPT_UINT,   &cmdlineP->height,  NULL, 0);
  OPTENT3(0, "left",    OPT_UINT,   &cmdlineP->left,    NULL, 0);
  OPTENT3(0, "right",   OPT_UINT,   &cmdlineP->right,   NULL, 0);
  OPTENT3(0, "top",     OPT_UINT,   &cmdlineP->top,     NULL, 0);
  OPTENT3(0, "bottom",  OPT_UINT,   &cmdlineP->bottom,  NULL, 0);
  OPTENT3(0, "bg",      OPT_STRING, &cmdlineP->bg_rgb,  NULL, 0);
  OPTENT3(0, "fg",      OPT_STRING, &cmdlineP->fg_rgb,  NULL, 0);
  OPTENT3(0, "var",     OPT_UINT,   &cmdlineP->var,     NULL, 0);
  OPTENT3(0, "init",    OPT_UINT,   &cmdlineP->init,    NULL, 0);
  OPTENT3(0, "verbose", OPT_FLAG,   NULL, &cmdlineP->verbose, 0);

  /* Set the defaults */
  cmdlineP->width = 100;
  cmdlineP->height = 100;
  cmdlineP->left = cmdlineP->right = cmdlineP->top = cmdlineP->bottom = -1;
  cmdlineP->bg_rgb = NULL;
  cmdlineP->fg_rgb = NULL;
  cmdlineP->var = 10;

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

  optParseOptions3(&argc, argv, opt, sizeof(opt), 0);

  if (argc-1 != 0)
    pm_error("There are no arguments.  You specified %d.", argc-1);
}

static void
/* ----------------------------------------- */
   proc_left(int const r1, int const r2, int const c1, int const c2, 
             unsigned int const var)
/* ----------------------------------------- */
{
  int cm, rm, c;

  if (r1 + 1 == r2)  return;
  rm = (r1 + r2) >> 1;
  cm = (c1 + c2) >> 1;
  cm += (int)floor(((float)rand() / RAND_MAX - 0.5) * var + 0.5);

  for (c = 0; c < cm; c++)
    PPM_ASSIGN(PIX[rm][c], BG_RED, BG_GREEN, BG_BLUE);

  proc_left(r1, rm, c1, cm, var);
  proc_left(rm, r2, cm, c2, var);
}

static void
/* ------------------------------------------ */
   proc_right(int const r1, int const r2, int const c1, int const c2,
              unsigned int const width, unsigned int const var)
/* ------------------------------------------ */
{
  int cm, rm, c;

  if (r1 + 1 == r2)  return;
  rm = (r1 + r2) >> 1;
  cm = (c1 + c2) >> 1;
  cm += (int)floor(((float)rand() / RAND_MAX - 0.5) * var + 0.5);

  for (c = cm; c < width; c++)
    PPM_ASSIGN(PIX[rm][c], BG_RED, BG_GREEN, BG_BLUE);

  proc_right(r1, rm, c1, cm, width, var);
  proc_right(rm, r2, cm, c2, width, var);
}

static void
/* ---------------------------------------- */
   proc_top(int const c1, int const c2, int const r1, int const r2,
            unsigned int const var)
/* ---------------------------------------- */
{
  int rm, cm, r;

  if (c1 + 1 == c2)  return;
  cm = (c1 + c2) >> 1;
  rm = (r1 + r2) >> 1;
  rm += (int)floor(((float)rand() / RAND_MAX - 0.5) * var + 0.5);

  for (r = 0; r < rm; r++)
    PPM_ASSIGN(PIX[r][cm], BG_RED, BG_GREEN, BG_BLUE);

  proc_top(c1, cm, r1, rm, var);
  proc_top(cm, c2, rm, r2, var);
}

static void
/* ------------------------------------------- */
   proc_bottom(int const c1, int const c2, int const r1, int const r2,
               unsigned int const height, unsigned int const var)
/* ------------------------------------------- */
{
  int rm, cm, r;

  if (c1 + 1 == c2)  return;
  cm = (c1 + c2) >> 1;
  rm = (r1 + r2) >> 1;
  rm += (int)floor(((float)rand() / RAND_MAX - 0.5) * var + 0.5);

  for (r = rm; r < height; r++)
    PPM_ASSIGN(PIX[r][cm], BG_RED, BG_GREEN, BG_BLUE);

  proc_bottom(c1, cm, r1, rm, height, var);
  proc_bottom(cm, c2, rm, r2, height, var);
}


int
/* ============================ */
   main(int argc, char* argv[])
/* ============================ */
{
  struct cmdlineInfo cmdline;
  pixel bgcolor, fgcolor;
  pixval fg_red, fg_green, fg_blue;
  int rows, cols, row;
  int left, right, top, bottom;
  int left_r1, left_r2, left_c1, left_c2;
  int right_r1, right_r2, right_c1, right_c2;
  int top_r1, top_r2, top_c1, top_c2;
  int bottom_r1, bottom_r2, bottom_c1, bottom_c2;
  unsigned init;
  struct timeval tv;
  int col;

  ppm_init(&argc, argv);

  parseCommandLine(argc, argv, &cmdline);

  init = cmdline.init;
  if (init == 0) {
    gettimeofday(&tv, NULL);
    srand((unsigned int)tv.tv_usec);
  }
  else
    srand(init);

  cols = cmdline.width;
  rows = cmdline.height;
  left = cmdline.left;
  right = cmdline.right;
  top = cmdline.top;
  bottom = cmdline.bottom;

  if (cmdline.bg_rgb)
    bgcolor = ppm_parsecolor(cmdline.bg_rgb, PPM_MAXMAXVAL);
  else
    PPM_ASSIGN(bgcolor, 0, 0, 0);
  BG_RED = PPM_GETR(bgcolor);
  BG_GREEN = PPM_GETG(bgcolor);
  BG_BLUE = PPM_GETB(bgcolor);

  if (cmdline.fg_rgb)
    fgcolor = ppm_parsecolor(cmdline.fg_rgb, PPM_MAXMAXVAL);
  else
    PPM_ASSIGN(fgcolor, PPM_MAXMAXVAL, PPM_MAXMAXVAL, PPM_MAXMAXVAL);
  fg_red = PPM_GETR(fgcolor);
  fg_green = PPM_GETG(fgcolor);
  fg_blue = PPM_GETB(fgcolor);

  if (cmdline.verbose) {
    pm_message("width is %d, height is %d, variance is %d.", 
               cols, rows, cmdline.var);
    if (left >= 0) pm_message("ragged left border is required");
    if (right >= 0) pm_message("ragged right border is required");
    if (top >= 0) pm_message("ragged top border is required");
    if (bottom >= 0) pm_message("ragged bottom border is required");
    pm_message("background is %s", ppm_colorname(&bgcolor, PPM_MAXMAXVAL, 1));
    pm_message("foreground is %s", ppm_colorname(&fgcolor, PPM_MAXMAXVAL, 1));
    if (init >= 0) pm_message("srand() initialized with seed %u", init);
  }

  /* Allocate memory for the whole pixmap */
  PIX = ppm_allocarray(cols, rows);

  /* First, set all pixel to foreground color */
  for (row = 0; row < rows; row++)
    for (col = 0; col < cols; col++)
      PPM_ASSIGN(PIX[row][col], fg_red, fg_green, fg_blue);

  /* Make a ragged left border */
  if (left >= 0) {
    left_c1 = left_c2 = left;
    left_r1 = 0;
    left_r2 = rows - 1;
    for (col = 0; col < left_c1; col++)
      PPM_ASSIGN(PIX[left_r1][col], BG_RED, BG_GREEN, BG_BLUE);
    for (col = 0; col < left_c2; col++)
      PPM_ASSIGN(PIX[left_r2][col], BG_RED, BG_GREEN, BG_BLUE);

    proc_left(left_r1, left_r2, left_c1, left_c2, cmdline.var);
  }

  /* Make a ragged right border */
  if (right >= 0) {
    right_c1 = right_c2 = cols - right - 1;
    right_r1 = 0;
    right_r2 = rows - 1;
    for (col = right_c1; col < cols; col++)
      PPM_ASSIGN(PIX[right_r1][col], BG_RED, BG_GREEN, BG_BLUE);
    for (col = right_c2; col < cols; col++)
      PPM_ASSIGN(PIX[right_r2][col], BG_RED, BG_GREEN, BG_BLUE);

    proc_right(right_r1, right_r2, right_c1, right_c2, 
               cmdline.width, cmdline.var);
  }

  /* Make a ragged top border */
  if (top >= 0) {
    top_r1 = top_r2 = top;
    top_c1 = 0;
    top_c2 = cols - 1;
    for (row = 0; row < top_r1; row++)
      PPM_ASSIGN(PIX[row][top_c1], BG_RED, BG_GREEN, BG_BLUE);
    for (row = 0; row < top_r2; row++)
      PPM_ASSIGN(PIX[row][top_c2], BG_RED, BG_GREEN, BG_BLUE);

    proc_top(top_c1, top_c2, top_r1, top_r2, cmdline.var);
  }

  /* Make a ragged bottom border */
  if (bottom >= 0) {
    bottom_r1 = bottom_r2 = rows - bottom - 1;
    bottom_c1 = 0;
    bottom_c2 = cols - 1;
    for (row = bottom_r1; row < rows; row++)
      PPM_ASSIGN(PIX[row][bottom_c1], BG_RED, BG_GREEN, BG_BLUE);
    for (row = bottom_r2; row < rows; row++)
      PPM_ASSIGN(PIX[row][bottom_c2], BG_RED, BG_GREEN, BG_BLUE);

    proc_bottom(bottom_c1, bottom_c2, bottom_r1, bottom_r2, 
                cmdline.height, cmdline.var);
  }

  /* Write pixmap */
  ppm_writeppm(stdout, PIX, cols, rows, PPM_MAXMAXVAL, 0);

  ppm_freearray(PIX, rows);

  pm_close(stdout);
  exit(0);
}