about summary refs log tree commit diff
path: root/other/pamlookup.c
blob: d5f046a5e28eedd0c28ec9a2f5a3212b02458a07 (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
/*============================================================================
                               pamlookup
==============================================================================

  Look up integers or ordered pairs from an index image in a lookup table and
  produce a corresponding image containing the results of the lookups.
  
  The index image and lookup table are PAM images.  The output image is
  a PAM image with the width and height of the index image and tuples of
  the kind in the lookup table.

  By Bryan Henderson, San Jose CA 2002.11.10

============================================================================*/

#include "pm_c_util.h"
#include "pam.h"
#include "shhopt.h"
#include "pm_system.h"
#include "nstring.h"

struct cmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    const char *indexFilespec;  
    char *lookupFilespec;
    char *missingcolor;  /* -missingcolor value.  null if not specified */
    unsigned int fit;  /* -fit option */
};



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 lookupfileSpec, missingcolorSpec;

    option_def_index = 0;   /* incremented by OPTENTRY */
    OPTENT3(0, "lookupfile",     OPT_STRING, &cmdlineP->lookupFilespec,  
            &lookupfileSpec, 0);
    OPTENT3(0,   "missingcolor", OPT_STRING, 
            &cmdlineP->missingcolor,   &missingcolorSpec, 0);
    OPTENT3(0,   "fit", OPT_FLAG, 
            NULL,   &cmdlineP->fit, 0);

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = FALSE;  /* We may have 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 (!lookupfileSpec)
        pm_error("You must specify the -lookupfile option");

    if (!missingcolorSpec)
        cmdlineP->missingcolor = NULL;

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



static void
fitLookup(tuple **     const inputLookup, 
          struct pam   const inputLookuppam,
          tuple ***    const fitLookupP,
          struct pam * const fitLookuppamP,
          unsigned int const cols,
          unsigned int const rows) {
/*----------------------------------------------------------------------------
  Scale the lookup table image so that it has dimensions 'cols' x 'rows'.
-----------------------------------------------------------------------------*/
    const char * pamscaleCommand;
    struct pamtuples inPamtuples;
    struct pamtuples outPamtuples;

    *fitLookuppamP = inputLookuppam;
    fitLookuppamP->width = cols;
    fitLookuppamP->height = rows;

    pm_asprintf(&pamscaleCommand, "pamscale -width=%u -height=%u", cols, rows);

    inPamtuples.pamP = (struct pam *) &inputLookuppam;
    inPamtuples.tuplesP = (tuple ***) &inputLookup;
    outPamtuples.pamP = fitLookuppamP;
    outPamtuples.tuplesP = fitLookupP;
    
    pm_system(&pm_feed_from_pamtuples, &inPamtuples,
              &pm_accept_to_pamtuples, &outPamtuples,
              pamscaleCommand);

    pm_strfree(pamscaleCommand);
}



static void
getLookup(const char * const lookupFilespec, 
          unsigned int const indexDegree,
          unsigned int const indexMaxval,
          tuple ***    const lookupP,
          struct pam * const lookuppamP,
          bool         const fit) {

    FILE*  lookupfileP;

    struct pam inputLookuppam;
    tuple** inputLookup;

    lookupfileP = pm_openr(lookupFilespec);
    inputLookup = pnm_readpam(lookupfileP, 
                              &inputLookuppam, PAM_STRUCT_SIZE(tuple_type));

    pm_close(lookupfileP);
    
    if (fit) {
        fitLookup(inputLookup, inputLookuppam, lookupP, lookuppamP,
                  indexMaxval + 1, 
                  indexDegree > 1 ? indexMaxval + 1 : 1);
        pnm_freepamarray(inputLookup, &inputLookuppam);
    } else {
        *lookupP = inputLookup;
        *lookuppamP = inputLookuppam;
    }
        
    if (indexDegree == 1 && lookuppamP->height != 1)
        pm_error("Your index image has integer indices, "
                 "so the lookup table image must be one row.  "
                 "Yours is %d rows.", 
                 lookuppamP->height);

    if (lookuppamP->width - 1 > indexMaxval)
        pm_message("Warning:  your lookup table image is wider than "
                   "the maxval of "
                   "your index message, so the right end of the lookup "
                   "table image will have no effect on the output.");
    if (indexDegree == 2 && lookuppamP->height - 1 > indexMaxval)
        pm_message("Warning: your lookup table image is taller than "
                   "the maxval of "
                   "your index message, so the bottom end of the lookup "
                   "table image has no effect on the output.");
}



static void
computeDefaultTuple(struct cmdlineInfo const cmdline, 
                    tuple **           const lookup,
                    struct pam *       const lookuppamP, 
                    tuple *            const defaultTupleP) {

    tuple retval;

    retval = pnm_allocpamtuple(lookuppamP);

    /* Note: "missing color" really makes sense only for a color
       lookup, whereas this program allows an arbitrary PAM image as a
       lookup table.  We should probably check here for a lookup file
       that has a visual image tuple type, but we don't out of
       laziness.  The program probably ought to have a generic
       "missing tuple type" option too.  
    */
    if (cmdline.missingcolor) {
        pixel const color = 
            ppm_parsecolor(cmdline.missingcolor, lookuppamP->maxval);

        if (lookuppamP->depth >= 3) {
            retval[PAM_RED_PLANE] = PPM_GETR(color);
            retval[PAM_GRN_PLANE] = PPM_GETG(color);
            retval[PAM_BLU_PLANE] = PPM_GETB(color);
        } else {
            if (PPM_GETR(color) != PPM_GETG(color) ||
                PPM_GETR(color) != PPM_GETB(color))
                pm_error("You specified as a missing color something which "
                         "is not monochrome, but your lookup table file, "
                         "and thus your "
                         "output file, can contain only monochrome values");
            else
                retval[0] = PPM_GETR(color);
        }
    } else 
        pnm_assigntuple(lookuppamP, retval, lookup[0][0]);

    *defaultTupleP = retval;
}



static void
doLookup(struct pam const indexpam,
         struct pam const outpamarg,
         tuple      const defaultTuple,
         tuple **   const lookup,
         struct pam const lookuppam) {

    struct pam outpam;
    unsigned int row;

    tuple* tuplerowIndex;
    tuple* tuplerowOut;

    outpam = outpamarg;

    tuplerowIndex = pnm_allocpamrow(&indexpam);
    tuplerowOut = pnm_allocpamrow(&outpam);

    pnm_writepaminit(&outpam);

    for (row = 0; row < outpam.height; ++row) {
        unsigned int col;
        pnm_readpamrow(&indexpam, tuplerowIndex);
        
        for (col = 0; col < outpam.width; ++col) {
            unsigned int indexRow, indexCol;
            tuple v;
            
            if (indexpam.depth < 2) {
                indexRow = 0;
                indexCol = tuplerowIndex[col][0];
            } else {
                indexRow = tuplerowIndex[col][0];
                indexCol = tuplerowIndex[col][1];
            }

            if (indexRow >= lookuppam.height || indexCol >= lookuppam.width)
                v = defaultTuple;
            else
                v = lookup[indexRow][indexCol];

            pnm_assigntuple(&outpam, tuplerowOut[col], v);
        }
        pnm_writepamrow(&outpam, tuplerowOut);
    }
    pnm_freepamrow(tuplerowIndex);
    pnm_freepamrow(tuplerowOut);
}



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

    struct cmdlineInfo cmdline;
    struct pam indexpam;
    struct pam outpam;
    FILE*  ifP;
    struct pam lookuppam;
    tuple** lookup;

    tuple defaultTuple;
    
    pnm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.indexFilespec);

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

    if (indexpam.depth != 1 && indexpam.depth != 2)
        pm_error("The input (index) file must have depth 1 or 2.  "
                 "Yours has depth %d",
                 indexpam.depth);

    getLookup(cmdline.lookupFilespec, indexpam.depth, indexpam.maxval, 
              &lookup, &lookuppam, cmdline.fit);

    computeDefaultTuple(cmdline, lookup, &lookuppam, &defaultTuple);

    outpam = lookuppam;
    outpam.height = indexpam.height;
    outpam.width = indexpam.width;
    outpam.file = stdout;
    
    doLookup(indexpam, outpam, defaultTuple, lookup, lookuppam);

    pm_close(ifP);

    pnm_freepamtuple(defaultTuple);
    pnm_freepamarray(lookup, &lookuppam);
    
    exit(0);
}