about summary refs log tree commit diff
path: root/other/pamsummcol.c
blob: ec0d2085f8a61ffe29c8554995d4d447bf5686fb (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
/******************************************************************************
                               pamsummcol
*******************************************************************************
  Summarize the columns of a PAM image with various functions.

  By Bryan Henderson, San Jose CA 2004.02.07.

  Contributed to the public domain


******************************************************************************/

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

enum function {FN_ADD, FN_MEAN, FN_MIN, FN_MAX};

struct cmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    const char * inputFilespec;  /* Filespec of input file */
    enum function function;
    unsigned int verbose;
};


static void
parseCommandLine(int argc, char ** const 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 = malloc(100*sizeof(optEntry));
        /* Instructions to OptParseOptions2 on how to parse our options.
         */
    optStruct3 opt;

    unsigned int option_def_index;

    unsigned int sumSpec, meanSpec, minSpec, maxSpec;

    option_def_index = 0;   /* incremented by OPTENTRY */
    OPTENT3(0,   "sum",      OPT_FLAG,  NULL, &sumSpec,           0);
    OPTENT3(0,   "mean",     OPT_FLAG,  NULL, &meanSpec,          0);
    OPTENT3(0,   "min",      OPT_FLAG,  NULL, &minSpec,           0);
    OPTENT3(0,   "max",      OPT_FLAG,  NULL, &maxSpec,           0);
    OPTENT3(0,   "verbose",  OPT_FLAG,  NULL, &cmdlineP->verbose, 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, argv, opt, sizeof(opt), 0);
        /* Uses and sets argc, argv, and some of *cmdlineP and others. */

    if (sumSpec + minSpec + maxSpec > 1)
        pm_error("You may specify at most one of -sum, -min, and -max");

    if (sumSpec) {
        cmdlineP->function = FN_ADD;
    } else if (meanSpec) {
        cmdlineP->function = FN_MEAN;
    } else if (minSpec) {
        cmdlineP->function = FN_MIN;
    } else if (maxSpec) {
        cmdlineP->function = FN_MAX;
    } else
        pm_error("You must specify one of -sum, -min, or -max");

    if (argc-1 > 1)
        pm_error("Too many arguments (%d).  File spec is the only argument.",
                 argc-1);

    if (argc-1 < 1)
        cmdlineP->inputFilespec = "-";
    else
        cmdlineP->inputFilespec = argv[1];

}


struct accum {
    union {
        unsigned int sum;
        unsigned int min;
        unsigned int max;
    } u;
};



static void
createAccumulator(enum function    const function,
                  unsigned int     const cols,
                  unsigned int     const planes,
                  struct accum *** const accumulatorP) {

    struct accum ** accumulator;
    unsigned int col;

    MALLOCARRAY_NOFAIL(accumulator, cols);

    for (col = 0; col < cols; ++col) {
        unsigned int plane;

        MALLOCARRAY_NOFAIL(accumulator[col], planes);

        for (plane = 0; plane < planes; ++plane) {
            switch(function) {
            case FN_ADD:  accumulator[col][plane].u.sum = 0;        break;
            case FN_MEAN: accumulator[col][plane].u.sum = 0;        break;
            case FN_MIN:  accumulator[col][plane].u.min = UINT_MAX; break;
            case FN_MAX:  accumulator[col][plane].u.max = 0;        break;
            }
        }
    }
    *accumulatorP = accumulator;
}



static void
destroyAccumulator(struct accum **    accumulator,
                   unsigned int const cols) {

    unsigned int col;
    for (col = 0; col < cols; ++col)
        free(accumulator[col]);

    free(accumulator);
}



static void
aggregate(struct pam *    const inpamP,
          tuple *         const tupleRow,
          enum function   const function,
          struct accum ** const accumulator) {

    unsigned int col;

    for (col = 0; col < inpamP->width; ++col) {
        unsigned int plane;
        for (plane = 0; plane < inpamP->depth; ++plane) {
            switch(function) {
            case FN_ADD:
            case FN_MEAN:
                if (accumulator[col][plane].u.sum >
                    UINT_MAX - tupleRow[col][plane])
                    pm_error("Numerical overflow in Column %u", col);
                accumulator[col][plane].u.sum += tupleRow[col][plane];
            break;
            case FN_MIN:
                if (tupleRow[col][plane] < accumulator[col][plane].u.min)
                    accumulator[col][plane].u.min = tupleRow[col][plane];
                break;
            case FN_MAX:
                if (tupleRow[col][plane] > accumulator[col][plane].u.min)
                    accumulator[col][plane].u.min = tupleRow[col][plane];
                break;
            }
        }
    }
}



static void
makeSummaryRow(struct accum ** const accumulator,
               unsigned int  const   count,
               struct pam *  const   pamP,
               enum function const   function,
               tuple *       const   tupleRow) {

    unsigned int col;

    for (col = 0; col < pamP->width; ++col) {
        unsigned int plane;
        for (plane = 0; plane < pamP->depth; ++plane) {
            switch(function) {
            case FN_ADD:
                tupleRow[col][plane] =
                    MIN(accumulator[col][plane].u.sum, pamP->maxval);
                break;
            case FN_MEAN:
                tupleRow[col][plane] =
                    ROUNDU((double)accumulator[col][plane].u.sum / count);
                break;
            case FN_MIN:
                tupleRow[col][plane] =
                    accumulator[col][plane].u.min;
                break;
            case FN_MAX:
                tupleRow[col][plane] =
                    accumulator[col][plane].u.max;
                break;
            }
        }
    }
}



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

    FILE* ifP;
    tuple* inputRow;   /* Row from input image */
    tuple* outputRow;  /* Output row */
    int row;
    struct cmdlineInfo cmdline;
    struct pam inpam;   /* Input PAM image */
    struct pam outpam;  /* Output PAM image */
    struct accum ** accumulator;  /* malloc'ed two-dimensional array */

    pnm_init( &argc, argv );

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilespec);

    pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));

    createAccumulator(cmdline.function, inpam.width, inpam.depth,
                      &accumulator);

    inputRow = pnm_allocpamrow(&inpam);

    outpam = inpam;    /* Initial value -- most fields should be same */
    outpam.file = stdout;
    outpam.height = 1;

    pnm_writepaminit(&outpam);

    outputRow = pnm_allocpamrow(&outpam);

    for (row = 0; row < inpam.height; row++) {
        pnm_readpamrow(&inpam, inputRow);

        aggregate(&inpam, inputRow, cmdline.function, accumulator);
    }
    makeSummaryRow(accumulator, inpam.height, &outpam, cmdline.function,
                   outputRow);
    pnm_writepamrow(&outpam, outputRow);

    pnm_freepamrow(outputRow);
    pnm_freepamrow(inputRow);
    destroyAccumulator(accumulator, inpam.width);
    pm_close(inpam.file);
    pm_close(outpam.file);

    return 0;
}