about summary refs log tree commit diff
path: root/generator/pamtris/pamtris.c
blob: 5b851c67c91cc89b42d025ff81af630db27dc315 (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
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#include "netpbm/mallocvar.h"
#include "netpbm/shhopt.h"
#include "netpbm/pam.h"

#include "limits_pamtris.h"
#include "framebuffer.h"
#include "boundaries.h"
#include "input.h"

#define MAX_METRICS 8192



static int
parse_command_line(int           const argv_idx,
                   int *         const argc_ptr,
                   const char ** const argv,
                   int32_t *     const width,
                   int32_t *     const height,
                   int32_t *     const maxval,
                   int32_t *     const num_attribs,
                   char *        const tupletype) {

    optEntry * option_def;
    optStruct3 opt;
        /* Instructions to pm_optParseOptions3 on how to parse our options */
    unsigned int option_def_index;

    char * tupletype_ptr;

    unsigned int width_spec, height_spec, maxval_spec, attribs_spec;
    unsigned int tupletype_spec;

    MALLOCARRAY_NOFAIL(option_def, 100);

    option_def_index = 0;  /* incremented by OPTENT3 */
    OPTENT3(0, "width",       OPT_INT,    width,          &width_spec,      0);
    OPTENT3(0, "height",      OPT_INT,    height,         &height_spec,     0);
    OPTENT3(0, "maxval",      OPT_INT,    maxval,         &maxval_spec,     0);
    OPTENT3(0, "num_attribs", OPT_INT,    num_attribs,    &attribs_spec,    0);
    OPTENT3(0, "tupletype",   OPT_STRING, &tupletype_ptr, &tupletype_spec,  0);

    opt.opt_table     = option_def;
    opt.short_allowed = false;
    opt.allowNegNum   = false;

    pm_optParseOptions3(argc_ptr, (char **)argv, opt, sizeof(opt), 0);

    if (!width_spec || !height_spec || !attribs_spec) {
        pm_errormsg(
            "you must at least specify -width, -height and -num_attribs.");

        return 0;
    }

    if (*width < 1 || *width > MAX_METRICS) {
        pm_errormsg("invalid width.");

        return 0;
    }

    if (*height < 1 || *height > MAX_METRICS) {
        pm_errormsg("invalid height.");

        return 0;
    }

    if (maxval_spec) {
        if (*maxval < 1 || *maxval > PAM_OVERALL_MAXVAL) {


            pm_errormsg("invalid maxval.");

            return 0;
        }
    } else {
        *maxval = 255;
    }

    if (*num_attribs < 1 || *num_attribs > MAX_NUM_ATTRIBS) {
        pm_errormsg("invalid number of generic attributes per vertex.");

        return 0;
    }

    if (tupletype_spec) {
        if (!set_tupletype(tupletype_ptr, tupletype)) {
            pm_errormsg("warning: invalid tuple type; using the null string.");

            set_tupletype(NULL, tupletype);
        }
    }

    return 1;
}



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

    framebuffer_info fbi;
    boundary_info bi;
    input_info ii;

    pm_proginit(&argc, (const char**)argv);

    set_tupletype(NULL, fbi.outpam.tuple_type);

    if (!parse_command_line(1, &argc, argv,
                            &fbi.width, &fbi.height, &fbi.maxval,
                            &fbi.num_attribs, fbi.outpam.tuple_type)) {
        return 1;
    }

    if (!init_framebuffer(&fbi)) {
        pm_errormsg("out of memory.");

        return 3;
    }

    init_boundary_buffer(&bi, fbi.height);

    init_input_processor(&ii);

    while (process_next_command(&ii, &bi, &fbi));

    free_input_processor(&ii);
    free_boundary_buffer(&bi);
    free_framebuffer(&fbi);

    return 0;
}