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
|
/*============================================================================
pamunlookup
==============================================================================
Find tuple values from an input image in a lookup table and
produce a corresponding index image containing table indices.
The lookup table is a one-row PAM image tuple type the same as the input
image. The output index image has the same width and height as the input
image depth 1, and maxval equal to the width of the lookup image (the
possible values include one for each column in the lookup image, plus one
for tuple values that are not in the lookup image).
By Bryan Henderson, San Jose CA 2015.08.08
============================================================================*/
#include <assert.h>
#include "pm_c_util.h"
#include "mallocvar.h"
#include "shhopt.h"
#include "nstring.h"
#include "pam.h"
#include "pammap.h"
struct CmdlineInfo {
/* All the information the user supplied in the command line,
in a form easy for the program to use.
*/
const char * inputFileName;
char * lookupfile;
};
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;
MALLOCARRAY_NOFAIL(option_def, 100);
option_def_index = 0; /* incremented by OPTENTRY */
OPTENT3(0, "lookupfile", OPT_STRING, &cmdlineP->lookupfile,
&lookupfileSpec, 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, (char **)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 (argc-1 < 1)
cmdlineP->inputFileName = "-";
else
cmdlineP->inputFileName = argv[1];
free(option_def);
}
static void
getLookup(const char * const lookupFileName,
tuple *** const lookupP,
struct pam * const lookuppamP) {
/*----------------------------------------------------------------------------
Get the lookup image (the one that maps integers to tuples, e.g. a
color index / color map / palette) from the file named
'lookupFileName'.
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 (inputLookuppam.height != 1)
pm_error("The lookup table image must be one row. "
"Yours is %u rows.",
inputLookuppam.height);
*lookupP = inputLookup;
*lookuppamP = inputLookuppam;
}
static void
makeReverseLookupHash(struct pam * const lookuppamP,
tuple ** const lookup,
tuplehash * const hashP) {
/*----------------------------------------------------------------------------
Create a tuple hash that maps each tuple values in the first row of
'lookup' to the number of the column in which it appears.
Abort the program with an error if the same tuple value occurs in two
columns of the first row.
-----------------------------------------------------------------------------*/
tuplehash hash;
unsigned int col;
hash = pnm_createtuplehash();
for (col = 0; col < lookuppamP->width; ++col) {
tuple const thisValue = lookup[0][col];
int found;
int priorValue;
pnm_lookuptuple(lookuppamP, hash, thisValue, &found, &priorValue);
if (found)
pm_error("Same tuple value occurs in both Column %u and "
"Column %u of the lookup image", priorValue, col);
else {
int fits;
pnm_addtotuplehash(lookuppamP, hash, lookup[0][col], col, &fits);
if (!fits)
pm_error("Out of memory constructing hash of lookup table");
}
}
*hashP = hash;
}
static void
doUnlookup(struct pam * const inpamP,
tuplehash const lookupHash,
sample const maxIndex,
FILE * const ofP) {
struct pam outpam;
unsigned int row;
tuple * inrow;
tuple * outrow;
inrow = pnm_allocpamrow(inpamP);
outpam.size = sizeof(outpam);
outpam.len = PAM_STRUCT_SIZE(tuple_type);
outpam.file = ofP;
outpam.format = PAM_FORMAT;
outpam.height = inpamP->height;
outpam.width = inpamP->width;
outpam.depth = 1;
outpam.maxval = maxIndex + 1; /* +1 for missing color */
strcpy(outpam.tuple_type, "INDEX");
pnm_writepaminit(&outpam);
outrow = pnm_allocpamrow(&outpam);
for (row = 0; row < inpamP->height; ++row) {
unsigned int col;
pnm_readpamrow(inpamP, inrow);
for (col = 0; col < inpamP->width; ++col) {
int found;
int index;
pnm_lookuptuple(inpamP, lookupHash, inrow[col], &found, &index);
if (found) {
assert(index <= outpam.maxval);
outrow[col][0] = index;
} else
outrow[col][0] = maxIndex + 1;
}
pnm_writepamrow(&outpam, outrow);
}
pnm_freepamrow(outrow);
pnm_freepamrow(inrow);
}
int
main(int argc, const char ** const argv) {
struct CmdlineInfo cmdline;
struct pam inpam;
FILE * ifP;
struct pam lookuppam;
tuple ** lookup;
tuplehash lookupHash;
pm_proginit(&argc, argv);
parseCommandLine(argc, argv, &cmdline);
ifP = pm_openr(cmdline.inputFileName);
pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));
getLookup(cmdline.lookupfile, &lookup, &lookuppam);
if (inpam.depth != lookuppam.depth)
pm_error("The lookup image has depth %u, but the input image "
"has depth %u. They must be the same",
lookuppam.depth, inpam.depth);
if (!streq(inpam.tuple_type, lookuppam.tuple_type))
pm_error("The lookup image has tuple type '%s', "
"but the input image "
"has tuple type '%s'. They must be the same",
lookuppam.tuple_type, inpam.tuple_type);
makeReverseLookupHash(&lookuppam, lookup, &lookupHash);
doUnlookup(&inpam, lookupHash, lookuppam.width-1, stdout);
pm_close(ifP);
pnm_destroytuplehash(lookupHash);
pnm_freepamarray(lookup, &lookuppam);
return 0;
}
|