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
|
#include <assert.h>
#include <nstring.h>
#include <pam.h>
#include "pm_c_util.h"
#include "shhopt.h"
#include "mallocvar.h"
typedef struct {
unsigned int * target;
unsigned int targetDepth;
unsigned int machine;
const char * color; /* NULL means not specified */
const char * inputFileName;
} CmdLineInfo;
static CmdLineInfo
parsedCommandLine(int argc,
const char ** const argv) {
optEntry * option_def;
/* Instructions to OptParseOptions4 on how to parse our options.
*/
optStruct3 opt;
unsigned int option_def_index;
CmdLineInfo cmdLine;
unsigned int targetSpec, colorSpec;
const char ** target;
MALLOCARRAY_NOFAIL(option_def, 100);
option_def_index = 0; /* incremented by OPTENT3 */
OPTENT3(0, "target", OPT_STRINGLIST, &target, &targetSpec, 0);
OPTENT3(0, "color", OPT_STRING, &cmdLine.color, &colorSpec, 0);
OPTENT3(0, "machine", OPT_FLAG, NULL, &cmdLine.machine, 0);
OPTENT3(0, 0, OPT_END, NULL, NULL, 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_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
if (targetSpec + colorSpec > 1)
pm_error("You cannot specify both -target and -color");
else if (targetSpec + colorSpec == 0)
pm_error("You must specify either -target or -color");
else if (targetSpec) {
unsigned int i;
cmdLine.color = NULL;
cmdLine.target = NULL; /* initial value */
for (i = 0, cmdLine.targetDepth = 0; target[i]; ++i) {
unsigned int sampleVal;
const char * error;
pm_string_to_uint(target[i], &sampleVal, &error);
if (error) {
pm_error("Invalid sample value in -target option: '%s'. "
"%s", target[i], error);
}
REALLOCARRAY(cmdLine.target, i+1);
cmdLine.target[cmdLine.targetDepth++] = sampleVal;
}
free(target);
} else
assert (colorSpec == 1);
if (argc-1 < 1)
cmdLine.inputFileName = "-";
else {
cmdLine.inputFileName = argv[1];
if (argc-1 > 1)
pm_error("Too many arguments: %u. "
"The only possible argument is the input file name",
argc-1);
}
free(option_def);
return cmdLine;
}
static void
freeCmdLine(CmdLineInfo const cmdLine) {
if (!cmdLine.color)
pnm_freepamtuple(cmdLine.target);
}
static tuple
targetValue(CmdLineInfo const cmdLine,
struct pam * const inpamP) {
/*----------------------------------------------------------------------------
The tuple value user wants us to find in the image, per 'cmdLine'.
The return value is to be interpreted per *inpamP.
-----------------------------------------------------------------------------*/
tuple retval;
if (cmdLine.color) {
if (inpamP->depth != 3)
pm_error("You specified -color, but the input image has "
"depth %u, not 3", inpamP->depth);
else
retval = pnm_parsecolor(cmdLine.color, inpamP->maxval);
} else {
if (cmdLine.targetDepth != inpamP->depth)
pm_error("You specified a %u-tuple for -target, "
"but the input image is of depth %u",
cmdLine.targetDepth, inpamP->depth);
else {
unsigned int i;
retval = pnm_allocpamtuple(inpamP);
for (i = 0; i < inpamP->depth; ++i)
retval[i] = cmdLine.target[i];
}
}
return retval;
}
static void
printHeader(FILE * const ofP,
struct pam * const inpamP,
tuple const target) {
unsigned int plane;
fprintf(ofP, "Locations containing tuple ");
fprintf(ofP, "(");
for (plane = 0; plane < inpamP->depth; ++plane) {
fprintf(ofP, "%u", (unsigned)target[plane]);
if (plane + 1 < inpamP->depth)
fprintf(ofP, "/");
}
fprintf(ofP, ")");
fprintf(ofP, "/%u", (unsigned)inpamP->maxval);
fprintf(ofP, ":\n");
}
static unsigned int
decimalDigitCt(unsigned int const n) {
/*----------------------------------------------------------------------------
Minimum number of digits needed to display 'n' in decimal.
-----------------------------------------------------------------------------*/
unsigned int digitCt;
if (n == 0)
digitCt = 1;
else {
unsigned int x;
for (digitCt = 0, x = n; x > 0;) {
++digitCt;
x /= 10;
}
assert(digitCt > 0);
}
return digitCt;
}
static void
pamfind(FILE * const ifP,
struct pam * const inpamP,
CmdLineInfo const cmdLine,
FILE * const ofP) {
pnm_readpaminit(ifP, inpamP, PAM_STRUCT_SIZE(tuple_type));
{
tuple * const inputRow = pnm_allocpamrow(inpamP);
tuple const target = targetValue(cmdLine, inpamP);
unsigned int row;
const char * fmt;
if (cmdLine.machine) {
pm_asprintf(&fmt, "%%0%uu %%0%uu\n",
decimalDigitCt(inpamP->height-1),
decimalDigitCt(inpamP->width-1));
} else {
printHeader(ofP, inpamP, target);
fmt = pm_strdup("(%u, %u)\n");
}
for (row = 0; row < inpamP->height; ++row) {
unsigned int col;
pnm_readpamrow(inpamP, inputRow);
for (col = 0; col < inpamP->width; ++col) {
if (pnm_tupleequal(inpamP, target, inputRow[col])) {
fprintf(ofP, fmt, row, col);
}
}
}
pm_strfree(fmt);
pnm_freepamtuple(target);
pnm_freepamrow(inputRow);
}
}
int
main(int argc, const char *argv[]) {
FILE * ifP;
CmdLineInfo cmdLine;
struct pam inpam;
pm_proginit(&argc, argv);
cmdLine = parsedCommandLine(argc, argv);
ifP = pm_openr(cmdLine.inputFileName);
pamfind(ifP, &inpam, cmdLine, stdout);
freeCmdLine(cmdLine);
pm_close(inpam.file);
return 0;
}
|