about summary refs log tree commit diff
path: root/analyzer/pamsumm.c
blob: 03ff6749c3bc8253c890d6e16bd85000dcccfec5 (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
/*=============================================================================
                               pamsumm
===============================================================================
  Summarize all the samples 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 *  inputFileName;  /* Name of input file */
    enum Function function;
    unsigned int  normalize;
    unsigned int  brief;
    unsigned int  verbose;
};



static void
parseCommandLine(int argc, const char ** const argv,
                 struct CmdlineInfo * const cmdlineP) {

    optEntry * option_def;
        /* Instructions to OptParseOptions3 on how to parse our options.
         */
    optStruct3 opt;

    unsigned int option_def_index;

    unsigned int sumSpec, meanSpec, minSpec, maxSpec;

    MALLOCARRAY(option_def, 100);

    option_def_index = 0;   /* incremented by OPTENT3 */
    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,   "normalize", OPT_FLAG,  NULL, &cmdlineP->normalize, 0);
    OPTENT3(0,   "brief",     OPT_FLAG,  NULL, &cmdlineP->brief,     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, (char **)argv, opt, sizeof(opt), 0);
        /* Uses and sets argc, argv, and some of *cmdlineP and others. */

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

    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, -max, or -mean");

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

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

    free(option_def);
}



struct Accum {
    union {
        double sum;
        unsigned int min;
        unsigned int max;
    } u;
};



static void
initAccumulator(struct Accum * const accumulatorP,
                enum Function  const function) {

    switch(function) {
    case FN_ADD:  accumulatorP->u.sum = 0.0;      break;
    case FN_MEAN: accumulatorP->u.sum = 0.0;      break;
    case FN_MIN:  accumulatorP->u.min = UINT_MAX; break;
    case FN_MAX:  accumulatorP->u.max = 0;        break;
    }
}



static void
aggregate(struct pam *   const inpamP,
          tuple *        const tupleRow,
          enum Function  const function,
          struct Accum * const accumulatorP) {

    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:
                accumulatorP->u.sum += tupleRow[col][plane];
            break;
            case FN_MIN:
                if (tupleRow[col][plane] < accumulatorP->u.min)
                    accumulatorP->u.min = tupleRow[col][plane];
                break;
            case FN_MAX:
                if (tupleRow[col][plane] > accumulatorP->u.min)
                    accumulatorP->u.min = tupleRow[col][plane];
                break;
            }
        }
    }
}



static void
printSummary(struct Accum  const accumulator,
             unsigned int  const scale,
             unsigned int  const count,
             enum Function const function,
             bool          const mustNormalize,
             bool          const brief) {

    switch (function) {
    case FN_ADD: {
        const char * const intro = brief ? "" : "the sum of all samples is ";

        if (mustNormalize)
            printf("%s%f\n", intro, accumulator.u.sum/scale);
        else
            printf("%s%u\n", intro, (unsigned int)accumulator.u.sum);
    }
    break;
    case FN_MEAN: {
        const char * const intro = brief ? "" : "the mean of all samples is ";

        if (mustNormalize)
            printf("%s%f\n", intro, accumulator.u.sum/count/scale);
        else
            printf("%s%f\n", intro, accumulator.u.sum/count);
    }
    break;
    case FN_MIN: {
        const char * const intro =
            brief ? "" : "the minimum of all samples is ";

        if (mustNormalize)
            printf("%s%f\n", intro, (double)accumulator.u.min/scale);
        else
            printf("%s%u\n", intro, accumulator.u.min);
    }
    break;
    case FN_MAX: {
        const char * const intro =
            brief ? "" : "the maximum of all samples is ";

        if (mustNormalize)
            printf("%s%f\n", intro, (double)accumulator.u.max/scale);
        else
            printf("%s%u\n", intro, accumulator.u.max);
    }
    break;
    }
}



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

    FILE * ifP;
    tuple * inputRow;   /* Row from input image */
    unsigned int row;
    struct CmdlineInfo cmdline;
    struct pam inpam;   /* Input PAM image */
    struct Accum accumulator;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFileName);

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

    inputRow = pnm_allocpamrow(&inpam);

    initAccumulator(&accumulator, cmdline.function);

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

        aggregate(&inpam, inputRow, cmdline.function, &accumulator);
    }
    printSummary(accumulator, (unsigned)inpam.maxval,
                 inpam.height * inpam.width * inpam.depth,
                 cmdline.function, cmdline.normalize, cmdline.brief);

    pnm_freepamrow(inputRow);
    pm_close(inpam.file);

    return 0;
}