about summary refs log tree commit diff
path: root/other/pamlookup.c
blob: 9682a65ef6f7490591a2d4fe60b3b72318e2dbc3 (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*============================================================================
                               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 <assert.h>

#include "pm_c_util.h"
#include "mallocvar.h"
#include "shhopt.h"
#include "pm_system.h"
#include "nstring.h"
#include "pam.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;  /* null if not specified */
    unsigned int fit;
    unsigned int byplane;
};



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

    unsigned int option_def_index;

    unsigned int lookupfileSpec, missingcolorSpec;

    MALLOCARRAY_NOFAIL(option_def, 100);

    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);
    OPTENT3(0,   "byplane", OPT_FLAG,
            NULL,   &cmdlineP->byplane, 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_optParseOptions4(&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 (cmdlineP->byplane && cmdlineP->missingcolor)
        pm_error("You cannot specify -missingcolor with -byplane");

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

    free(option_def);
}



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 * widthArg;
    const char * heightArg;
    struct pamtuples inPamtuples;
    struct pamtuples outPamtuples;

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

    pm_asprintf(&widthArg,  "-width=%u", cols);
    pm_asprintf(&heightArg, "-height=%u", rows);

    inPamtuples.pamP = (struct pam *) &inputLookuppam;
    inPamtuples.tuplesP = (tuple ***) &inputLookup;
    outPamtuples.pamP = fitLookuppamP;
    outPamtuples.tuplesP = fitLookupP;

    pm_system_lp("pamscale",
                 &pm_feed_from_pamtuples, &inPamtuples,
                 &pm_accept_to_pamtuples, &outPamtuples,
                 "pamscale", widthArg, heightArg, NULL);

    pm_strfree(heightArg);
    pm_strfree(widthArg);
}



static void
getLookup(const char * const lookupFileName,
          unsigned int const indexDegree,
          unsigned int const indexMaxval,
          tuple ***    const lookupP,
          struct pam * const lookuppamP,
          bool         const fit) {
/*----------------------------------------------------------------------------
   Get the lookup image (the one that maps integers to tuples, e.g. a
   color index / color map / palette) from the file named
   'lookupFileName'.

   Interpret the lookup image for use with indices that are ntuples of size
   'indexDegree' (normally 1, could be 2) whose elements range from 0 through
   'indexMaxval'

   Iff 'fit' is true, stretch or compress the image in the file to fit
   exactly the range identified by 'indexMaxval'.

   Return the image as *lookupP and *lookuppamP.
-----------------------------------------------------------------------------*/
    FILE *  lookupfileP;

    struct pam inputLookuppam;
    tuple ** inputLookup;

    lookupfileP = pm_openr(lookupFileName);
    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 image, 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
doLookupByPlane(struct pam const indexpam,
                tuple **   const lookup,
                struct pam const lookuppam,
                FILE *     const ofP) {
/*----------------------------------------------------------------------------
   Write an output image to *ofP derived from the input image read per
   'indexpam' (now positioned to its raster).

   Base each tuple of the output on the tuple in the same place in the input.
   Look up each sample of the input tuple in the lookupt image given by
   'lookup' and 'lookuppam' to get the corresponding sample of the output
   image.

   Our output image has the same width, height, depth, image type, and tuple
   type as the input image and the same maxval as the lookup image.

   We ignore any plane or row after the first in the lookup image.  We expect
   its width to match the maxval of the input image.
-----------------------------------------------------------------------------*/
    struct pam outpam;
    unsigned int row;

    tuple* tuplerowIndex;
    tuple* tuplerowOut;

    outpam = indexpam;  /* initial value */
    outpam.maxval = lookuppam.maxval;
    outpam.file = ofP;

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

    pnm_writepaminit(&outpam);

    assert(lookuppam.width == indexpam.maxval + 1);
        /* Calling condition */

    for (row = 0; row < indexpam.height; ++row) {
        unsigned int col;
        pnm_readpamrow(&indexpam, tuplerowIndex);

        for (col = 0; col < indexpam.width; ++col) {
            unsigned int plane;

            for (plane = 0; plane < indexpam.depth; ++plane) {
                unsigned int const index = tuplerowIndex[col][plane];

                if (index > lookuppam.maxval)
                    pm_error("Sample value %u in the lookup image exceeds "
                             "the lookup image's maxval (%u)",
                             index, (unsigned)lookuppam.maxval);

                tuplerowOut[col][plane] = lookup[0][index][0];
            }
        }
        pnm_writepamrow(&outpam, tuplerowOut);
    }
    pnm_freepamrow(tuplerowIndex);
    pnm_freepamrow(tuplerowOut);
}



static void
doLookupWholeTuple(struct pam const indexpam,
                   tuple      const defaultTuple,
                   tuple **   const lookup,
                   struct pam const lookuppam,
                   FILE *     const ofP) {
/*----------------------------------------------------------------------------
   Write an output image to *ofP derived from the input image read per
   'indexpam' (now positioned to its raster).

   For each tuple of the output, use the corresponding tuple of the input as
   an index into the lookup image given by 'lookup' and 'lookuppam'.  If that
   index is not present in the lookup image, put 'defaultTuple' in the output.
-----------------------------------------------------------------------------*/
    struct pam outpam;
    unsigned int row;

    tuple* tuplerowIndex;
    tuple* tuplerowOut;

    outpam = lookuppam;  /* initial value */
    outpam.height = indexpam.height;
    outpam.width = indexpam.width;
    outpam.file = ofP;

    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, const char ** const argv) {

    struct CmdlineInfo cmdline;
    struct pam indexpam;
    FILE * ifP;
    unsigned int indexDegree;
    struct pam lookuppam;
    tuple ** lookup;

    tuple defaultTuple;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.indexFilespec);

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

    if (!cmdline.byplane && (indexpam.depth != 1 && indexpam.depth != 2))
        pm_error("Unless you specify -byplane, "
                 "the input (index) file must have depth 1 or 2.  "
                 "Yours has depth %u",
                 indexpam.depth);

    indexDegree = cmdline.byplane ? 1 : indexpam.depth;

    getLookup(cmdline.lookupFilespec, indexDegree, indexpam.maxval,
              &lookup, &lookuppam, cmdline.fit || cmdline.byplane);

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

    if (cmdline.byplane)
        doLookupByPlane(indexpam, lookup, lookuppam, stdout);
    else
        doLookupWholeTuple(indexpam, defaultTuple, lookup, lookuppam, stdout);

    pm_close(ifP);

    pnm_freepamtuple(defaultTuple);
    pnm_freepamarray(lookup, &lookuppam);

    return 0;
}