about summary refs log tree commit diff
path: root/generator/pbmpage.c
blob: 96dca876439cfde23bfcb0b5cf09e99eee5b20f6 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*=============================================================================
                                pbmpage
===============================================================================
  This program produces a printed page test pattern in PBM format.

  This was adapted from Tim Norman's 'pbmtpg' program, part of his
  'pbm2ppa' package, by Bryan Henderson on 2000.05.01.  The only
  change was to make it use the Netpbm libraries to generate the
  output.

  For copyright and licensing information, see the pbmtoppa program,
  which was also derived from the same package.
=============================================================================*/

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>

#include "pm_c_util.h"
#include "mallocvar.h"
#include "shhopt.h"
#include "nstring.h"
#include "pbm.h"

enum Pattern {PAT_GRID, PAT_VERTICAL, PAT_DIAGONAL};

/* US is 8.5 in by 11 in */

static unsigned int const usWidth  = 5100;
static unsigned int const usHeight = 6600;

/* A4 is 210 mm by 297 mm == 8.27 in by 11.69 in */

static unsigned int const a4Width  = 4960;
static unsigned int const a4Height = 7016;


struct CmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    enum Pattern pattern;
    unsigned int a4;
};



static void
parseCommandLine(int argc, const char ** 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 OPTENTRY */
    OPTENT3(0, "a4",         OPT_FLAG, NULL, &cmdlineP->a4,       0);

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

    pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
    /* Uses and sets argc, argv, and some of *cmdlineP and others. */

    if (argc-1 < 1)
        cmdlineP->pattern = PAT_GRID;
    else {
        if (argc-1 > 1)
            pm_error("Too many arguments (%u).  The only possible argument "
                     "is the pattern number", argc-1);
        if (streq(argv[1], "1"))
            cmdlineP->pattern = PAT_GRID;
        else if (streq(argv[1], "2"))
            cmdlineP->pattern = PAT_VERTICAL;
        else if (streq(argv[1], "3"))
            cmdlineP->pattern = PAT_DIAGONAL;
        else
            pm_error("Invalid test pattern name '%s'.  "
                     "We recognize only '1', '2', and '3'", argv[1]);
    }
    free(option_def);
}



struct Bitmap {
    /* width and height in 600ths of an inch */
    unsigned int width;
    unsigned int height;

    unsigned char ** bitmap;
};



static void
setpixel(struct Bitmap * const bitmapP,
         unsigned int    const x,
         unsigned int    const y,
         bit             const c) {

    char const bitmask = 128 >> (x % 8);

    if (x < 0 || x >= bitmapP->width) {
        /* Off the edge of the canvas */
    } else if (y < 0 || y >= bitmapP->height) {
        /* Off the edge of the canvas */
    } else {
        if (c == PBM_BLACK)
            bitmapP->bitmap[y][x/8] |= bitmask;
        else
            bitmapP->bitmap[y][x/8] &= ~bitmask;
    }
}



static void
setplus(struct Bitmap * const bitmapP,
        unsigned int    const x,
        unsigned int    const y,
        unsigned int    const s) {
/*----------------------------------------------------------------------------
   Draw a black plus sign centered at (x,y) with arms 's' pixels long.
   Leave the exact center of the plus white.
-----------------------------------------------------------------------------*/
    unsigned int i;

    for (i = 0; i < s; ++i) {
        setpixel(bitmapP, x + i, y,     PBM_BLACK);
        setpixel(bitmapP, x - i, y,     PBM_BLACK);
        setpixel(bitmapP, x ,    y + i, PBM_BLACK);
        setpixel(bitmapP, x ,    y - i, PBM_BLACK);
    }
}



static void
setblock(struct Bitmap * const bitmapP,
         unsigned int    const x,
         unsigned int    const y,
         unsigned int    const s) {

    unsigned int i;

    for (i = 0; i < s; ++i) {
        unsigned int j;

        for (j = 0; j < s; ++j)
            setpixel(bitmapP, x + i, y + j, PBM_BLACK);
    }
}



static void
setchar(struct Bitmap * const bitmapP,
        unsigned int    const x,
        unsigned int    const y,
        char            const c) {

    static char const charmap[10][5]= { { 0x3e, 0x41, 0x41, 0x41, 0x3e },
                                        { 0x00, 0x42, 0x7f, 0x40, 0x00 },
                                        { 0x42, 0x61, 0x51, 0x49, 0x46 },
                                        { 0x22, 0x41, 0x49, 0x49, 0x36 },
                                        { 0x18, 0x14, 0x12, 0x7f, 0x10 },
                                        { 0x27, 0x45, 0x45, 0x45, 0x39 },
                                        { 0x3e, 0x49, 0x49, 0x49, 0x32 },
                                        { 0x01, 0x01, 0x61, 0x19, 0x07 },
                                        { 0x36, 0x49, 0x49, 0x49, 0x36 },
                                        { 0x26, 0x49, 0x49, 0x49, 0x3e } };

    if (c <= '9' && c >= '0') {
        unsigned int xo;

        for (xo = 0; xo < 5; ++xo) {
            unsigned int yo;

            for (yo = 0; yo < 8; ++yo) {
                if ((charmap[c-'0'][xo] >> yo) & 0x01)
                    setblock(bitmapP, x + xo*3, y + yo*3, 3);
            }
        }
    }
}



static void
setstring(struct Bitmap * const bitmapP,
          unsigned int    const x,
          unsigned int    const y,
          const char *    const s) {

    const char * p;
    unsigned int xo;

    for (xo = 0, p = s; *p; xo += 21, ++p)
        setchar(bitmapP, x + xo, y, *p);
}



static void
setCG(struct Bitmap * const bitmapP,
      unsigned int    const x,
      unsigned int    const y) {

    unsigned int xo;

    for (xo = 0; xo <= 50; ++xo)   {
        unsigned int const yo = sqrt(SQR(50.0) - SQR(xo));

        unsigned int zo;

        setpixel(bitmapP, x + xo    , y + yo    , PBM_BLACK);
        setpixel(bitmapP, x + yo    , y + xo    , PBM_BLACK);
        setpixel(bitmapP, x - 1 - xo, y - 1 - yo, PBM_BLACK);
        setpixel(bitmapP, x - 1 - yo, y - 1 - xo, PBM_BLACK);
        setpixel(bitmapP, x + xo    , y - 1 - yo, PBM_BLACK);
        setpixel(bitmapP, x - 1 - xo, y + yo    , PBM_BLACK);

        for (zo = 0; zo < yo; ++zo) {
            setpixel(bitmapP, x + xo    , y - 1 - zo, PBM_BLACK);
            setpixel(bitmapP, x - 1 - xo, y + zo    , PBM_BLACK);
        }
    }
}



static void
outputPbm(FILE *        const ofP,
          struct Bitmap const bitmap) {
/*----------------------------------------------------------------------------
  Create a pbm file containing the image from the global variable bitmap[].
-----------------------------------------------------------------------------*/
    int const forceplain = 0;

    unsigned int row;

    pbm_writepbminit(ofP, bitmap.width, bitmap.height, forceplain);

    for (row = 0; row < bitmap.height; ++row) {
        pbm_writepbmrow_packed(ofP, bitmap.bitmap[row],
                               bitmap.width, forceplain);
    }
}



static void
framePerimeter(struct Bitmap * const bitmapP) {

    unsigned int x, y;

    /* Top edge */
    for (x = 0; x < bitmapP->width; ++x)
        setpixel(bitmapP, x, 0, PBM_BLACK);

    /* Bottom edge */
    for (x = 0; x < bitmapP->width; ++x)
        setpixel(bitmapP, x, bitmapP->height - 1, PBM_BLACK);

    /* Left edge */
    for (y = 0; y < bitmapP->height; ++y)
        setpixel(bitmapP, 0, y, PBM_BLACK);

    /* Right edge */
    for (y = 0; y < bitmapP->height; ++y)
        setpixel(bitmapP, bitmapP->width - 1, y, PBM_BLACK);
}



static void
makeWhite(struct Bitmap * const bitmapP) {

    unsigned int y;

    for (y = 0; y < bitmapP->height; ++y) {
        unsigned int x;
        for (x = 0; x < pbm_packed_bytes(bitmapP->width); ++x)
            bitmapP->bitmap[y][x] = 0x00;  /* 8 white pixels */
    }
}



static void
drawGrid(struct Bitmap * const bitmapP) {

    char buf[128];

    framePerimeter(bitmapP);
    {
        unsigned int x;
        for (x = 0; x < bitmapP->width; x += 100) {
            unsigned int y;
            for (y = 0; y < bitmapP->height; y += 100)
                setplus(bitmapP, x, y, 4);
        }
    }
    {
        unsigned int x;
        for (x = 0; x < bitmapP->width; x += 100) {
            sprintf(buf,"%u", x);
            setstring(bitmapP, x + 3, (bitmapP->height/200) * 100 + 3, buf);
        }
    }
    {
        unsigned int y;
        for (y = 0; y < bitmapP->height; y += 100) {
            sprintf(buf, "%u", y);
            setstring(bitmapP, (bitmapP->width/200) * 100 + 3, y + 3, buf);
        }
    }
    {
        unsigned int x;
        for (x = 0; x < bitmapP->width; x += 10) {
            unsigned int y;
            for (y = 0; y < bitmapP->height; y += 100)
                setplus(bitmapP, x, y, ((x%100) == 50) ? 2 : 1);
        }
    }
    {
        unsigned int x;
        for (x = 0; x < bitmapP->width; x += 100) {
            unsigned int y;
            for (y = 0; y < bitmapP->height; y += 10)
                setplus(bitmapP, x, y, ((y%100) == 50) ? 2 : 1);
        }
    }
    setCG(bitmapP, bitmapP->width/2, bitmapP->height/2);
}



static void
drawVertical(struct Bitmap * const bitmapP) {

    unsigned int y;

    for (y = 0; y < 300; ++y)
        setpixel(bitmapP, bitmapP->width/2, bitmapP->height/2 - y, PBM_BLACK);
}



static void
drawDiagonal(struct Bitmap * const bitmapP) {

    unsigned int y;

    for (y = 0; y < 300; ++y) {
        setpixel(bitmapP, y, y, PBM_BLACK);
        setpixel(bitmapP, bitmapP->width - 1 - y, bitmapP->height - 1 - y,
                 PBM_BLACK);
    }
}



int
main(int argc, const char** argv) {

    struct CmdlineInfo cmdline;
    /* width and height in 600ths of an inch */
    unsigned int width;
    unsigned int height;
    struct Bitmap bitmap;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    if (cmdline.a4) {
        width  = a4Width;
        height = a4Height;
    } else {
        width  = usWidth;
        height = usHeight;
    }

    bitmap.width  = width;
    bitmap.height = height;
    bitmap.bitmap = pbm_allocarray_packed(width, height);

    makeWhite(&bitmap);

    switch (cmdline.pattern) {
    case PAT_GRID:
        drawGrid(&bitmap);
        break;
    case PAT_VERTICAL:
        drawVertical(&bitmap);
        break;
    case PAT_DIAGONAL:
        drawDiagonal(&bitmap);
        break;
    }

    outputPbm(stdout, bitmap);

    pbm_freearray(bitmap.bitmap, height);

    pm_close(stdout);

    return 0;
}