]> mj.ucw.cz Git - libucw.git/blob - images/image-sim-test.c
Merge with git+ssh://cvs.ucw.cz/projects/sherlock/GIT/sherlock.git
[libucw.git] / images / image-sim-test.c
1 /*
2  *      Image similarity testing
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/getopt.h"
12 #include "lib/fastbuf.h"
13 #include "images/images.h"
14 #include "images/color.h"
15 #include "images/signature.h"
16 #include <stdlib.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <stdio.h>
20 #include <time.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 static void NONRET
25 usage(void)
26 {
27   fputs("\
28 Usage: image-sim-test [options] image1 [image2] \n\
29 \n\
30 -q --quiet           no progress messages\n\
31 -f --format-1        image1 format (jpeg, gif, png)\n\
32 -F --format-2        image2 format\n\
33 -g --background      background color (hexadecimal RRGGBB)\n\
34 -r --segmentation-1  writes image1 segmentation to given file\n\
35 -R --segmentation-2  writes image2 segmentation to given file\n\
36 ", stderr);
37   exit(1);
38 }
39
40 static char *shortopts = "qf:F:g:t:r:R:" CF_SHORT_OPTS;
41 static struct option longopts[] =
42 {
43   CF_LONG_OPTS
44   { "quiet",            0, 0, 'q' },
45   { "format-1",         0, 0, 'f' },
46   { "format-2",         0, 0, 'F' },
47   { "background",       0, 0, 'g' },
48   { "segmentation-1",   0, 0, 'r' },
49   { "segmentation-2",   0, 0, 'R' },
50   { NULL,               0, 0, 0 }
51 };
52
53 static uns verbose = 1;
54 static byte *file_name_1;
55 static byte *file_name_2;
56 static enum image_format format_1;
57 static enum image_format format_2;
58 static struct color background_color;
59 static byte *segmentation_name_1;
60 static byte *segmentation_name_2;
61
62 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
63 #define TRY(x) do{ if (!(x)) die("Error: %s", it.err_msg); }while(0)
64
65 static void
66 msg_str(byte *s, void *param UNUSED)
67 {
68   MSG("%s", s);
69 }
70
71 static void
72 dump_signature(struct image_signature *sig)
73 {
74   byte buf[MAX(IMAGE_VECTOR_DUMP_MAX, IMAGE_REGION_DUMP_MAX)];
75   image_vector_dump(buf, &sig->vec);
76   MSG("vector: %s", buf);
77   for (uns i = 0; i < sig->len; i++)
78     {
79       image_region_dump(buf, sig->reg + i);
80       MSG("region %u: %s", i, buf);
81     }
82 }
83
84 static struct image_thread it;
85 static struct image_io io;
86
87 static void
88 write_segmentation(struct image_sig_data *data, byte *fn)
89 {
90   MSG("Writing segmentation to %s", fn);
91   
92   struct fastbuf *fb = bopen(fn, O_WRONLY | O_CREAT | O_TRUNC, 4096);
93   struct image *img;
94   TRY(img = image_new(&it, data->image->cols, data->image->rows, COLOR_SPACE_RGB, NULL));
95   image_clear(&it, img);
96
97   for (uns i = 0; i < data->regions_count; i++)
98     {
99       byte c[3];
100       double luv[3], xyz[3], srgb[3];
101       luv[0] = data->regions[i].a[0] * (4 / 2.55);
102       luv[1] = ((int)data->regions[i].a[1] - 128) * (4 / 2.55);
103       luv[2] = ((int)data->regions[i].a[2] - 128) * (4 / 2.55);
104       luv_to_xyz_slow(xyz, luv);
105       xyz_to_srgb_slow(srgb, xyz);
106       c[0] = CLAMP(srgb[0] * 255, 0, 255); 
107       c[1] = CLAMP(srgb[1] * 255, 0, 255); 
108       c[2] = CLAMP(srgb[2] * 255, 0, 255); 
109       for (struct image_sig_block *block = data->regions[i].blocks; block; block = block->next)
110         {
111           uns x1 = block->x * 4;
112           uns y1 = block->y * 4;
113           uns x2 = MIN(x1 + 4, img->cols);
114           uns y2 = MIN(y1 + 4, img->rows);
115           byte *p = img->pixels + x1 * 3 + y1 * img->row_size;
116           for (uns y = y1; y < y2; y++, p += img->row_size)
117             {
118               byte *p2 = p;
119               for (uns x = x1; x < x2; x++, p2 += 3)
120                 {
121                   p2[0] = c[0];
122                   p2[1] = c[1];
123                   p2[2] = c[2];
124                 }
125             }
126         }
127     }
128
129   io.fastbuf = fb;
130   io.image = img;
131   io.format = image_file_name_to_format(fn); 
132   TRY(image_io_write(&io));
133   image_io_reset(&io);
134
135   image_destroy(img);
136   bclose(fb);
137 }
138
139 int
140 main(int argc, char **argv)
141 {
142   log_init(argv[0]);
143   int opt;
144   while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
145     switch (opt)
146       {
147         case 'q':
148           verbose = 0;
149           break;
150         case 'f':
151           if (!(format_1 = image_extension_to_format(optarg)))
152             usage();
153           break;
154         case 'F':
155           if (!(format_2 = image_extension_to_format(optarg)))
156             usage();
157           break;
158         case 'g':
159           {
160             if (strlen(optarg) != 6)
161               usage();
162             errno = 0;
163             char *end;
164             long int v = strtol(optarg, &end, 16);
165             if (errno || *end || v < 0)
166               usage();
167             color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
168           }
169           break;
170         case 'r':
171           segmentation_name_1 = optarg;
172           break;
173         case 'R':
174           segmentation_name_2 = optarg;
175           break;
176         default:
177           usage();
178       }
179
180   if (argc != optind + 2 && argc != optind + 1)
181     usage();
182   file_name_1 = argv[optind++];
183   if (argc > optind)
184     file_name_2 = argv[optind++];
185
186   MSG("Initializing image library");
187   srandom(time(NULL) ^ getpid());
188   srgb_to_luv_init();
189   image_thread_init(&it);
190
191   struct image *img1, *img2;
192
193   if (!image_io_init(&it, &io))
194     die("Cannot initialize image I/O (%s)", it.err_msg);
195
196   if (file_name_1)
197     {
198       MSG("Reading %s", file_name_1);
199       io.fastbuf = bopen(file_name_1, O_RDONLY, 1 << 18);
200       io.format = format_1 ? : image_file_name_to_format(file_name_1);
201       TRY(image_io_read_header(&io));
202       io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
203       if (background_color.color_space)
204         io.background_color = background_color;
205       else if (!io.background_color.color_space)
206         io.background_color = color_black;
207       TRY(image_io_read_data(&io, 1));
208       bclose(io.fastbuf);
209       img1 = io.image;
210       MSG("Image size=%ux%u", img1->cols, img1->rows);
211       image_io_reset(&io);
212     }
213   else
214     img1 = NULL;
215
216   if (file_name_2)
217     {
218       MSG("Reading %s", file_name_2);
219       io.fastbuf = bopen(file_name_2, O_RDONLY, 1 << 18);
220       io.format = format_2 ? : image_file_name_to_format(file_name_2);
221       TRY(image_io_read_header(&io));
222       io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
223       if (background_color.color_space)
224         io.background_color = background_color;
225       else if (!io.background_color.color_space)
226         io.background_color = color_black;
227       TRY(image_io_read_data(&io, 1));
228       bclose(io.fastbuf);
229       img2 = io.image;
230       MSG("Image size=%ux%u", img2->cols, img2->rows);
231       image_io_reset(&io);
232     }
233   else
234     img2 = NULL;
235
236   struct image_signature sig1, sig2;
237   MSG("Computing signatures");
238   if (img1)
239     {
240       struct image_sig_data data;
241       TRY(image_sig_init(&it, &data, img1));
242       image_sig_preprocess(&data);
243       if (data.valid)
244         {
245           image_sig_segmentation(&data);
246           image_sig_detect_textured(&data);
247         }
248       if (segmentation_name_1)
249         write_segmentation(&data, segmentation_name_1);
250       image_sig_finish(&data, &sig1);
251       image_sig_cleanup(&data);
252       dump_signature(&sig1);
253     }
254   if (img2)
255     {
256       struct image_sig_data data;
257       TRY(image_sig_init(&it, &data, img2));
258       image_sig_preprocess(&data);
259       if (data.valid)
260         {
261           image_sig_segmentation(&data);
262           image_sig_detect_textured(&data);
263         }
264       if (segmentation_name_2)
265         write_segmentation(&data, segmentation_name_2);
266       image_sig_finish(&data, &sig2);
267       image_sig_cleanup(&data);
268       dump_signature(&sig2);
269     }
270
271   if (img1 && img2)
272     {
273       uns dist;
274       if (verbose)
275         {
276           struct fastbuf *fb = bfdopen(0, 4096);
277           dist = image_signatures_dist_explain(&sig1, &sig2, msg_str, NULL);
278           bclose(fb);
279         }
280       else
281         dist = image_signatures_dist(&sig1, &sig2);
282       MSG("dist=%u", dist);
283     }
284
285   if (img1)
286     image_destroy(img1);
287   if (img2)
288     image_destroy(img2);
289
290   image_io_cleanup(&io);
291   image_thread_cleanup(&it);
292   MSG("Done.");
293   return 0;
294 }