about summary refs log tree commit diff
path: root/generator/pamgradient.c
blob: bbbaad301ac91741eba8a2181f2978d8537ebf64 (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
#include <string.h>

#include "pm_c_util.h"
#include "pam.h"
#include "shhopt.h"
#include "mallocvar.h"



struct CmdlineInfo {
    tuple colorTopLeft;
    tuple colorTopRight;
    tuple colorBottomLeft;
    tuple colorBottomRight;
    unsigned int cols;
    unsigned int rows;
    unsigned int maxval;
};

static void
parseCommandLine(int argc, const char **argv,
                 struct CmdlineInfo * const cmdlineP) {
/*----------------------------------------------------------------------------
  Convert program invocation arguments (argc,argv) into a format the
  program can use easily, struct CmdlineInfo.  Validate arguments along
  the way and exit program with message if invalid.

  Note that some string information we return as *cmdlineP is in the storage
  argv[] points to.
-----------------------------------------------------------------------------*/
    optEntry * option_def;
        /* Instructions to OptParseOptions3 on how to parse our options.
         */
    optStruct3 opt;

    unsigned int maxvalSpec;
    unsigned int option_def_index;

    MALLOCARRAY_NOFAIL(option_def, 100);

    option_def_index = 0;
    OPTENT3(0, "maxval", OPT_UINT, &cmdlineP->maxval, &maxvalSpec, 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 (!maxvalSpec)
        cmdlineP->maxval = 255;
    else {
        if (cmdlineP->maxval > PAM_OVERALL_MAXVAL)
            pm_error("The value you specified for -maxval (%u) is too big.  "
                     "Max allowed is %lu", cmdlineP->maxval,
                     (unsigned long int) PAM_OVERALL_MAXVAL);

        if (cmdlineP->maxval < 1)
            pm_error("You cannot specify 0 for -maxval");
    }

    if (argc-1 != 6) {
        pm_error("Need 6 arguments: colorTopLeft, colorTopRight, "
                 "colorBottomLeft, colorBottomRight, width, height");
    } else {
        cmdlineP->colorTopLeft     = pnm_parsecolor(argv[1], cmdlineP->maxval);
        cmdlineP->colorTopRight    = pnm_parsecolor(argv[2], cmdlineP->maxval);
        cmdlineP->colorBottomLeft  = pnm_parsecolor(argv[3], cmdlineP->maxval);
        cmdlineP->colorBottomRight = pnm_parsecolor(argv[4], cmdlineP->maxval);
        cmdlineP->cols = pm_parse_width(argv[5]);
        cmdlineP->rows = pm_parse_height(argv[6]);
        if (cmdlineP->cols <= 0)
            pm_error("width argument must be a positive number.  You "
                     "specified '%s'", argv[5]);
        if (cmdlineP->rows <= 0)
            pm_error("height argument must be a positive number.  You "
                     "specified '%s'", argv[6]);
    }
    free(option_def);
}



static void
freeCmdline(struct CmdlineInfo const cmdline) {

    pnm_freepamtuple(cmdline.colorTopLeft);
    pnm_freepamtuple(cmdline.colorTopRight);
    pnm_freepamtuple(cmdline.colorBottomLeft);
    pnm_freepamtuple(cmdline.colorBottomRight);
}



static void
interpolate(struct pam * const pamP,
            tuple *      const tuplerow,
            tuple        const first,
            tuple        const last) {

    unsigned int plane;

    for (plane = 0; plane < pamP->depth; ++plane) {
        int const spread = last[plane] - first[plane];

        int col;

        if (INT_MAX / pamP->width < abs(spread))
            pm_error("Arithmetic overflow.  You must reduce the width of "
                     "the image (now %u) or the range of color values "
                     "(%u in plane %u) so that their "
                     "product is less than %d",
                     pamP->width, abs(spread), plane, INT_MAX);

        for (col = 0; col < pamP->width; ++col)
            tuplerow[col][plane] =
                first[plane] + (spread * col / (int)pamP->width);
    }
}



static int
isgray(tuple const color) {

    return (color[PAM_RED_PLANE] == color[PAM_GRN_PLANE])
            && (color[PAM_RED_PLANE] == color[PAM_BLU_PLANE]);
}



static tuple *
createEdge(const struct pam * const pamP,
           tuple              const topColor,
           tuple              const bottomColor) {
/*----------------------------------------------------------------------------
   Create a left or right edge, interpolating from top to bottom.
-----------------------------------------------------------------------------*/
    struct pam interpPam;
    tuple * tupleRow;

    interpPam = *pamP;  /* initial value */
    interpPam.width = pamP->height;
    interpPam.height = 1;

    tupleRow = pnm_allocpamrow(&interpPam);

    interpolate(&interpPam, tupleRow, topColor, bottomColor);

    return tupleRow;
}



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

    struct CmdlineInfo cmdline;
    struct pam pam;
    tuple * tupleRow;
    tuple * leftEdge;
    tuple * rightEdge;
    unsigned int row;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    pam.size             = sizeof pam;
    pam.len              = PAM_STRUCT_SIZE(tuple_type);
    pam.file             = stdout;
    pam.plainformat      = 0;
    pam.width            = cmdline.cols;
    pam.height           = cmdline.rows;
    pam.maxval           = cmdline.maxval;
    pam.bytes_per_sample = pnm_bytespersample(pam.maxval);
    pam.format           = PAM_FORMAT;
    if (isgray(cmdline.colorTopLeft)
            && isgray(cmdline.colorTopRight)
            && isgray(cmdline.colorBottomLeft)
            && isgray(cmdline.colorBottomRight)) {
        pam.depth = 1;
        strcpy(pam.tuple_type, PAM_PGM_TUPLETYPE);
    } else {
        pam.depth = 3;
        strcpy(pam.tuple_type, PAM_PPM_TUPLETYPE);
    }

    pnm_writepaminit(&pam);

    tupleRow = pnm_allocpamrow(&pam);

    leftEdge  = createEdge(&pam,
                           cmdline.colorTopLeft, cmdline.colorBottomLeft);
    rightEdge = createEdge(&pam,
                           cmdline.colorTopRight, cmdline.colorBottomRight);

    /* interpolate each row between the left edge and the right edge */
    for (row = 0; row < pam.height; ++row) {
        interpolate(&pam, tupleRow, leftEdge[row], rightEdge[row]);
        pnm_writepamrow(&pam, tupleRow);
    }

    pm_close(stdout);
    pnm_freepamrow(rightEdge);
    pnm_freepamrow(leftEdge);
    pnm_freepamrow(tupleRow);

    freeCmdline(cmdline);

    return 0;
}