/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk

« back to all changes in this revision

Viewing changes to dict_hw/src/dict_hw_test.cpp

  • Committer: Suren A. Chilingaryan
  • Date: 2009-12-13 02:20:05 UTC
  • Revision ID: csa@dside.dyndns.org-20091213022005-m932to8hhihwuw5r
Support for TIFF images in C code and stand-alone console application

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <string.h>
 
4
 
 
5
#include <sys/types.h>
 
6
#include <sys/stat.h>
 
7
#include <unistd.h>
 
8
 
 
9
#include <getopt.h>
 
10
 
 
11
#include <glib.h>
 
12
 
 
13
#include <dict_hw.h>
 
14
 
 
15
int matlab_mode = 0;
 
16
 
 
17
typedef enum {
 
18
    OPT_MATLAB = 'm',
 
19
    OPT_HELP = 'h'
 
20
} Options;
 
21
 
 
22
static struct option long_options[] = {
 
23
    {"matlab",                  no_argument, 0, OPT_MATLAB },
 
24
    {"help",                    no_argument, 0, OPT_HELP },
 
25
    { 0, 0, 0, 0 }
 
26
};
 
27
 
 
28
void Usage(int argc, char *argv[]) {
 
29
    printf(
 
30
" %s [options] [mode] <directory> [maximages]\n"
 
31
"  Modes:\n"
 
32
"       --matlab        - Matlab mode (transposed data ordering)\n"
 
33
"       --help          - Help message\n"
 
34
"\n\n", argv[0]);
 
35
}
 
36
 
 
37
int readfile(char *name, float *x, int size) {
 
38
    int i;
 
39
 
 
40
    char first_line[16384];
 
41
 
 
42
    struct stat st;
 
43
    stat(name, &st);
 
44
 
 
45
    FILE *f = fopen(name, "r");
 
46
 
 
47
    if (!f) {
 
48
        printf("File %s is not found\n", name);
 
49
        g_free(name);
 
50
        exit(-1);
 
51
    }
 
52
 
 
53
    g_free(name);
 
54
 
 
55
 
 
56
    if (matlab_mode) {
 
57
        int len = 0;
 
58
        int columns = 0;
 
59
        int block_len;
 
60
        do {
 
61
            fgets(first_line, 16384, f);
 
62
            for (block_len = 0; first_line[block_len]; block_len++) {
 
63
                if (first_line[block_len] == '.') columns++;
 
64
            }
 
65
            len += block_len;
 
66
        } while (block_len == 16383);
 
67
    
 
68
        int lines = st.st_size / len;
 
69
    
 
70
        int pos = 0;
 
71
        rewind(f);
 
72
        for (i = 0; fscanf(f, "%f", x+pos) == 1; i++) {
 
73
            if ((pos+1) == size) {
 
74
                size *= 2;
 
75
                x = (float*)realloc(x, size);
 
76
            }
 
77
        
 
78
            pos = ((i+1)%columns)*lines + ((i+1)/columns);
 
79
        }
 
80
    } else {
 
81
        for (i = 0; fscanf(f, "%f", x+i) == 1; i++) {
 
82
            if ((i+1) == size) {
 
83
                size *= 2;
 
84
                x = (float*)realloc(x, size);
 
85
            }
 
86
        }
 
87
    }
 
88
 
 
89
    fclose(f);
 
90
 
 
91
    return i;
 
92
}
 
93
 
 
94
int writefile(char *name, float *x, int img, int ncp) {
 
95
    FILE *f = fopen(name, "w");
 
96
    if (!f) {
 
97
        printf("Can't write file %s\n", name);
 
98
        g_free(name);
 
99
        exit(-1);
 
100
    }
 
101
    g_free(name);
 
102
 
 
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]);
 
106
        }
 
107
        fprintf(f,"\n");
 
108
    }
 
109
    
 
110
    fclose(f);
 
111
}
 
112
 
 
113
int compare(const void *a, const void*b) {
 
114
    return strcmp(*(char**)a, *(char**)b);
 
115
}
 
116
 
 
117
int main(int argc, char *argv[]) {
 
118
    int i;
 
119
    int err;
 
120
    
 
121
    int maximg, allocimg;
 
122
    
 
123
    int size = 32768;
 
124
    int xsize, ysize;
 
125
    float *x;
 
126
    float *y;
 
127
 
 
128
    char **images;
 
129
    
 
130
    int option_index = 0;
 
131
    unsigned char c;
 
132
    while ((c = getopt_long(argc, argv, "yhe:f:l:t:c:", long_options, &option_index)) != (unsigned char)-1) {
 
133
        switch (c) {
 
134
            case 0:
 
135
            break;
 
136
            case OPT_MATLAB:
 
137
                matlab_mode = 1;
 
138
            break;
 
139
            case OPT_HELP:
 
140
                Usage(argc, argv);
 
141
                exit(0);                
 
142
            break;
 
143
            default:
 
144
                Usage(argc, argv);
 
145
                exit(0);
 
146
        }
 
147
    }
 
148
 
 
149
    if (optind > argc - 1) {
 
150
        Usage(argc, argv);
 
151
        exit(0);
 
152
    }
 
153
    
 
154
    x = (float*)malloc(size * sizeof(float));
 
155
    y = (float*)malloc(size * sizeof(float));
 
156
 
 
157
    gchar *name = g_strdup_printf("%s/data/grid_x.dat", argv[optind]);
 
158
    xsize = readfile(name, x, size);
 
159
 
 
160
    name = g_strdup_printf("%s/data/grid_y.dat", argv[optind]);
 
161
    ysize = readfile(name, y, size);
 
162
    
 
163
    if (xsize != ysize) {
 
164
        printf("Amount of X and Y coordinates of control points does not match\n");
 
165
        exit(-1);
 
166
    }
 
167
 
 
168
    if (argc > argc - 2) maximg = atoi(argv[optind+1]);
 
169
    else maximg = 0;
 
170
    
 
171
    images = (char**)malloc(size * sizeof(char*));
 
172
    
 
173
    name = g_strdup_printf("%s/images", argv[optind]);
 
174
    GDir *dir = g_dir_open(name, 0, NULL);
 
175
 
 
176
    if (!dir) {
 
177
        printf("Directory %s is not found\n", name);
 
178
        g_free(name);
 
179
        exit(-1);
 
180
    }
 
181
    
 
182
    g_free(name);
 
183
 
 
184
    const gchar *file = g_dir_read_name(dir);
 
185
    for (i = 0;file;file = g_dir_read_name(dir)) {
 
186
        if (i == size) {
 
187
            size *= 2;
 
188
            images = (char**)realloc(images, size);
 
189
        }
 
190
        images[i++] = g_strdup(file);
 
191
/*      if (!strstr(images[i-1], "tif")) {
 
192
            puts(images[i-1]);
 
193
            exit(-1);
 
194
        }*/
 
195
    }    
 
196
    g_dir_close(dir);
 
197
 
 
198
    allocimg = i;
 
199
    if ((!maximg)||(maximg > i)) maximg = i;
 
200
    
 
201
    qsort(images, allocimg, sizeof(char*), (int (*)(const void*, const void*))compare);
 
202
    
 
203
    if (maximg < 2) {
 
204
        printf("At least two images are required\n");
 
205
        exit(-1);
 
206
    }
 
207
 
 
208
    int ncp = xsize;
 
209
    
 
210
    DICTContext ps = dictCreateContext();
 
211
    if (matlab_mode) {
 
212
        err = dictSetup(ps, ncp, 15, 1000, DICT_FLAGS_MATLAB_MODE);
 
213
    } else {
 
214
        err = dictSetup(ps, ncp, 15, 1000, DICT_FLAGS_DEFAULT);
 
215
    }
 
216
    if (err) {
 
217
        printf("Setup is failed, error code %i\n", err);
 
218
        exit(-1);
 
219
    }
 
220
 
 
221
    float *res;
 
222
    int step;
 
223
    
 
224
    if (matlab_mode) {
 
225
        res = (float*) malloc(2 * ncp * (maximg - 1) * sizeof(float));
 
226
        step = ncp * (maximg - 1);
 
227
    } else {
 
228
        res = (float*) malloc(2 * ncp * sizeof(float));
 
229
        step = ncp;
 
230
        dictSetPointsBuffer(ps, res, res + ncp);
 
231
    }
 
232
 
 
233
    dictSetTemplatePoints(ps, x, y);
 
234
 
 
235
    name = g_strdup_printf("%s/images/%s", argv[optind], images[0]);
 
236
    dictLoadTemplateImageFile(ps, name);
 
237
    g_free(name);
 
238
 
 
239
    dictSetCurrentPoints(ps, x, y);
 
240
 
 
241
    FILE *fx, *fy;
 
242
    if (!matlab_mode) {
 
243
        name = g_strdup_printf("%s/data/validx.dat", argv[optind]);
 
244
        fx = fopen(name, "w");
 
245
        g_free(name);
 
246
        name = g_strdup_printf("%s/data/validy.dat", argv[optind]);
 
247
        fy = fopen(name, "w");
 
248
        g_free(name);
 
249
        
 
250
        if ((!fx)||(!fy)) {
 
251
            printf("Can't create output files...\n");
 
252
            exit(-1);
 
253
        }
 
254
    }
 
255
 
 
256
    for (i = 1; i < maximg; i++) {
 
257
        printf("%s\n", images[i]);
 
258
        
 
259
        name = g_strdup_printf("%s/images/%s", argv[optind], images[i]);
 
260
        dictLoadImageFile(ps, name);
 
261
        g_free(name);
 
262
        if (matlab_mode) {
 
263
            float *out = res + ncp * (i - 1);
 
264
            dictGetCurrentPoints(ps, out, out + step);
 
265
        } else {
 
266
            dictCompute(ps);
 
267
            for (int j = 0; j < ncp; j++) {
 
268
                fprintf(fx, "% 9.7le\t", res[j]);
 
269
            }
 
270
            fprintf(fx,"\n");
 
271
            
 
272
            float *out = res + step;
 
273
            for (int j = 0; j < ncp; j++) {
 
274
                fprintf(fy, "% 9.7le\t", out[j]);
 
275
            }
 
276
            fprintf(fy,"\n");
 
277
        }
 
278
    }
 
279
    
 
280
    if (matlab_mode) {
 
281
        name = g_strdup_printf("%s/data/validx.dat", argv[optind]);
 
282
        writefile(name, res, maximg - 1, ncp);
 
283
 
 
284
        name = g_strdup_printf("%s/data/validy.dat", argv[optind]);
 
285
        writefile(name, res+step, maximg - 1, ncp);
 
286
    } else {
 
287
        fclose(fx);
 
288
        fclose(fy);
 
289
    }
 
290
 
 
291
 
 
292
    for (i = 0; i < allocimg; i++) {
 
293
        g_free(images[i]);
 
294
    }
 
295
    
 
296
    dictDestroyContext(ps);
 
297
    
 
298
    free(res);
 
299
    free(images);        
 
300
    free(y);
 
301
    free(x);
 
302
}