22
static struct option long_options[] = {
23
{"matlab", no_argument, 0, OPT_MATLAB },
24
{"help", no_argument, 0, OPT_HELP },
28
void Usage(int argc, char *argv[]) {
30
" %s [options] [mode] <directory> [maximages]\n"
32
" --matlab - Matlab mode (transposed data ordering)\n"
33
" --help - Help message\n"
37
int readfile(char *name, float *x, int size) {
40
char first_line[16384];
45
FILE *f = fopen(name, "r");
48
printf("File %s is not found\n", name);
61
fgets(first_line, 16384, f);
62
for (block_len = 0; first_line[block_len]; block_len++) {
63
if (first_line[block_len] == '.') columns++;
66
} while (block_len == 16383);
68
int lines = st.st_size / len;
72
for (i = 0; fscanf(f, "%f", x+pos) == 1; i++) {
73
if ((pos+1) == size) {
75
x = (float*)realloc(x, size);
78
pos = ((i+1)%columns)*lines + ((i+1)/columns);
81
for (i = 0; fscanf(f, "%f", x+i) == 1; i++) {
84
x = (float*)realloc(x, size);
94
int writefile(char *name, float *x, int img, int ncp) {
95
FILE *f = fopen(name, "w");
97
printf("Can't write file %s\n", name);
103
for (int j = 0; j < ncp ; j++) {
104
for (int i = 0; i < img ; i++) {
105
fprintf(f, "% 9.7le\t", x[i*ncp + j]);
113
int compare(const void *a, const void*b) {
114
return strcmp(*(char**)a, *(char**)b);
117
int main(int argc, char *argv[]) {
121
int maximg, allocimg;
130
int option_index = 0;
132
while ((c = getopt_long(argc, argv, "yhe:f:l:t:c:", long_options, &option_index)) != (unsigned char)-1) {
149
if (optind > argc - 1) {
154
x = (float*)malloc(size * sizeof(float));
155
y = (float*)malloc(size * sizeof(float));
157
gchar *name = g_strdup_printf("%s/data/grid_x.dat", argv[optind]);
158
xsize = readfile(name, x, size);
160
name = g_strdup_printf("%s/data/grid_y.dat", argv[optind]);
161
ysize = readfile(name, y, size);
163
if (xsize != ysize) {
164
printf("Amount of X and Y coordinates of control points does not match\n");
168
if (argc > argc - 2) maximg = atoi(argv[optind+1]);
171
images = (char**)malloc(size * sizeof(char*));
173
name = g_strdup_printf("%s/images", argv[optind]);
174
GDir *dir = g_dir_open(name, 0, NULL);
177
printf("Directory %s is not found\n", name);
184
const gchar *file = g_dir_read_name(dir);
185
for (i = 0;file;file = g_dir_read_name(dir)) {
188
images = (char**)realloc(images, size);
190
images[i++] = g_strdup(file);
191
/* if (!strstr(images[i-1], "tif")) {
199
if ((!maximg)||(maximg > i)) maximg = i;
201
qsort(images, allocimg, sizeof(char*), (int (*)(const void*, const void*))compare);
204
printf("At least two images are required\n");
210
DICTContext ps = dictCreateContext();
212
err = dictSetup(ps, ncp, 15, 1000, DICT_FLAGS_MATLAB_MODE);
214
err = dictSetup(ps, ncp, 15, 1000, DICT_FLAGS_DEFAULT);
217
printf("Setup is failed, error code %i\n", err);
225
res = (float*) malloc(2 * ncp * (maximg - 1) * sizeof(float));
226
step = ncp * (maximg - 1);
228
res = (float*) malloc(2 * ncp * sizeof(float));
230
dictSetPointsBuffer(ps, res, res + ncp);
233
dictSetTemplatePoints(ps, x, y);
235
name = g_strdup_printf("%s/images/%s", argv[optind], images[0]);
236
dictLoadTemplateImageFile(ps, name);
239
dictSetCurrentPoints(ps, x, y);
243
name = g_strdup_printf("%s/data/validx.dat", argv[optind]);
244
fx = fopen(name, "w");
246
name = g_strdup_printf("%s/data/validy.dat", argv[optind]);
247
fy = fopen(name, "w");
251
printf("Can't create output files...\n");
256
for (i = 1; i < maximg; i++) {
257
printf("%s\n", images[i]);
259
name = g_strdup_printf("%s/images/%s", argv[optind], images[i]);
260
dictLoadImageFile(ps, name);
263
float *out = res + ncp * (i - 1);
264
dictGetCurrentPoints(ps, out, out + step);
267
for (int j = 0; j < ncp; j++) {
268
fprintf(fx, "% 9.7le\t", res[j]);
272
float *out = res + step;
273
for (int j = 0; j < ncp; j++) {
274
fprintf(fy, "% 9.7le\t", out[j]);
281
name = g_strdup_printf("%s/data/validx.dat", argv[optind]);
282
writefile(name, res, maximg - 1, ncp);
284
name = g_strdup_printf("%s/data/validy.dat", argv[optind]);
285
writefile(name, res+step, maximg - 1, ncp);
292
for (i = 0; i < allocimg; i++) {
296
dictDestroyContext(ps);