-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurvey_args.h
201 lines (175 loc) · 5.78 KB
/
survey_args.h
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
#include <argp.h>
#include <assert.h>
#include "termcolor.h"
#include <iostream>
#include <iomanip>
#include "util.h"
using namespace std;
using namespace termcolor;
// ------------------------
// ## survey command parser
//
struct arg_survey {
double amin, amax;
double bmin, bmax;
double cmin, cmax;
double dmin, dmax;
unsigned nodes;
char* ref_file;
char* work_plane_file;
bool exr;
bool verbose;
};
static char args_doc_survey[] = "reference.tiff work.tiff";
static char doc_survey[] =
"\n"
"Calculate the TCA metric over a regular grid\n"
"surrounding the chosen set of parameters\n"
"\n"
"Inputs:\n"
" A grayscale histogram-equalized TIFF reference (green channel)\n"
" A grayscale histogram-equalized TIFF to distort (blue or red)\n"
"\n"
"Output:\n"
" A table of parameter values and resulting TCA metric\n"
" piped to stdout\n"
"\n"
"\v"
"For each set of parameters on a grid, the radial distortion\n"
"is applied to the work_plane image and the TCA metric is calculated\n"
"as the average of absolute per-pixel differences between the\n"
"distorted work-plane image and the refernce image.\n"
"\n"
;
static error_t parse_survey_command(int key, char* arg, struct argp_state* state) {
struct arg_survey* arguments = (struct arg_survey*)state->input;
char **nonopt;
assert( arguments );
switch(key) {
case 'x':
arguments->exr = true;
break;
case 'n':
if (sscanf(arg, "%u", &(arguments->nodes)) != 1) {
cerr << on_red << "Expecting an integer number of grid nodes instead of " << bold << arg << reset << endl;
exit(EXIT_FAILURE);
}
break;
case 'a':
if (sscanf(arg, "%lf:%lf", &(arguments->amin), &(arguments->amax)) != 2) {
if (sscanf(arg, "%lf", &(arguments->amin)) == 1) {
arguments->amax = arguments->amin;
}
else {
cerr << on_red << "Expecting a floating-point number or a range (from:to) instead of " << bold << arg << reset << endl;
exit(EXIT_FAILURE);
}
}
break;
case 'b':
if (sscanf(arg, "%lf:%lf", &(arguments->bmin), &(arguments->bmax)) != 2) {
if (sscanf(arg, "%lf", &(arguments->bmin)) == 1) {
arguments->bmax = arguments->bmin;
}
else {
cerr << on_red << "Expecting a floating-point number or a range (from:to) instead of " << bold << arg << reset << endl;
exit(EXIT_FAILURE);
}
}
break;
case 'c':
if (sscanf(arg, "%lf:%lf", &(arguments->cmin), &(arguments->cmax)) != 2) {
if (sscanf(arg, "%lf", &(arguments->cmin)) == 1) {
arguments->cmax = arguments->cmin;
}
else {
cerr << on_red << "Expecting a floating-point number or a range (from:to) instead of " << bold << arg << reset << endl;
exit(EXIT_FAILURE);
}
}
break;
case 'd':
if (sscanf(arg, "%lf:%lf", &(arguments->dmin), &(arguments->dmax)) != 2) {
if (sscanf(arg, "%lf", &(arguments->dmin)) == 1) {
arguments->dmax = arguments->dmin;
}
else {
cerr << on_red << "Expecting a floating-point number or a range (from:to) instead of " << bold << arg << reset << endl;
exit(EXIT_FAILURE);
}
}
break;
case 'v':
arguments->verbose = true;
break;
case ARGP_KEY_NO_ARGS:
argp_usage (state);
case ARGP_KEY_ARG: // non-option argument
nonopt = &state->argv[state->next];
state->next = state->argc; // we're done
arguments->ref_file = arg;
arguments->work_plane_file = nonopt[0];
if (not file_exists(arguments->ref_file)) {
cerr << on_red << "File does not exist: " << bold << arguments->ref_file << reset << endl;
exit(EXIT_FAILURE);
}
if (not file_exists(arguments->work_plane_file)) {
cerr << on_red << "File does not exist: " << bold << arguments->work_plane_file << reset << endl;
exit(EXIT_FAILURE);
}
break;
case ARGP_KEY_END:
if (state->arg_num < 2) {
argp_error(state, "Not enough arguments");
}
if (state->arg_num > 2) {
argp_error(state, "Extra arguments");
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
static struct argp_option options_survey[] = {
{"range-a", 'a', "RANGE", 0, "the range of a to survey, or a fixed point" },
{"range-b", 'b', "RANGE", 0, "the range of b to survey, or a fixed point" },
{"range-c", 'c', "RANGE", 0, "the range of c to survey, or a fixed point" },
{"range-d", 'd', "RANGE", 0, "the range of d to survey, or a fixed point" },
{"nodes", 'n', "NUMBER", 0, "the number of grid nodes in eeach parameter range" },
{"exr", 'x', 0, 0, "input images are tilted EXR channels" },
{"verbose", 'v', 0, 0, "indicate processing stages" },
{ 0 }
};
static struct argp argp_survey = {
options_survey,
parse_survey_command,
args_doc_survey,
doc_survey
};
#pragma GCC diagnostic pop
#define PARSE_ARGS_SURVEY \
struct arg_survey args; \
int argc = state->argc - state->next + 1; \
char** argv = &state->argv[state->next - 1]; \
char* argv0 = argv[0]; \
argv[0] = (char *)malloc(strlen((char *)(state->name)) + strlen(" survey") + 1); \
if (!argv[0]) argp_failure(state, 1, ENOMEM, 0); \
sprintf(argv[0], "%s survey", state->name); \
args.amin = 0.995; \
args.amax = 1.005; \
args.bmin = -0.005; \
args.bmax = +0.005; \
args.cmin = 0.0; \
args.cmax = 0.0; \
args.dmin = 0.0; \
args.dmax = 0.0; \
args.nodes = 5; \
args.exr = false; \
args.verbose = false; \
argp_parse(&argp_survey, argc, argv, ARGP_IN_ORDER, &argc, &args); \
free(argv[0]); \
argv[0] = argv0; \
state->next += argc - 1;