]> mj.ucw.cz Git - libucw.git/blob - images/image-sim-test.c
more verbose IMAGESIM explain for debug purposes
[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 dump_signature(struct image_signature *sig)
67 {
68   byte buf[MAX(IMAGE_VECTOR_DUMP_MAX, IMAGE_REGION_DUMP_MAX)];
69   image_vector_dump(buf, &sig->vec);
70   MSG("vector: %s", buf);
71   for (uns i = 0; i < sig->len; i++)
72     {
73       image_region_dump(buf, sig->reg + i);
74       MSG("region %u: %s", i, buf);
75     }
76 }
77
78 static struct image_thread it;
79 static struct image_io io;
80
81 static void
82 write_segmentation(struct image_sig_data *data, byte *fn)
83 {
84   MSG("Writing segmentation to %s", fn);
85   
86   struct fastbuf *fb = bopen(fn, O_WRONLY | O_CREAT | O_TRUNC, 4096);
87   struct image *img;
88   TRY(img = image_new(&it, data->image->cols, data->image->rows, COLOR_SPACE_RGB, NULL));
89   image_clear(&it, img);
90
91   for (uns i = 0; i < data->regions_count; i++)
92     {
93       byte c[3];
94       double luv[3], xyz[3], srgb[3];
95       luv[0] = data->regions[i].a[0] * (4 / 2.55);
96       luv[1] = ((int)data->regions[i].a[1] - 128) * (4 / 2.55);
97       luv[2] = ((int)data->regions[i].a[2] - 128) * (4 / 2.55);
98       luv_to_xyz_slow(xyz, luv);
99       xyz_to_srgb_slow(srgb, xyz);
100       c[0] = CLAMP(srgb[0] * 255, 0, 255); 
101       c[1] = CLAMP(srgb[1] * 255, 0, 255); 
102       c[2] = CLAMP(srgb[2] * 255, 0, 255); 
103       for (struct image_sig_block *block = data->regions[i].blocks; block; block = block->next)
104         {
105           uns x1 = block->x * 4;
106           uns y1 = block->y * 4;
107           uns x2 = MIN(x1 + 4, img->cols);
108           uns y2 = MIN(y1 + 4, img->rows);
109           byte *p = img->pixels + x1 * 3 + y1 * img->row_size;
110           for (uns y = y1; y < y2; y++, p += img->row_size)
111             {
112               byte *p2 = p;
113               for (uns x = x1; x < x2; x++, p2 += 3)
114                 {
115                   p2[0] = c[0];
116                   p2[1] = c[1];
117                   p2[2] = c[2];
118                 }
119             }
120         }
121     }
122
123   io.fastbuf = fb;
124   io.image = img;
125   io.format = image_file_name_to_format(fn); 
126   TRY(image_io_write(&io));
127   image_io_reset(&io);
128
129   image_destroy(img);
130   bclose(fb);
131 }
132
133 int
134 main(int argc, char **argv)
135 {
136   log_init(argv[0]);
137   int opt;
138   while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
139     switch (opt)
140       {
141         case 'q':
142           verbose = 0;
143           break;
144         case 'f':
145           if (!(format_1 = image_extension_to_format(optarg)))
146             usage();
147           break;
148         case 'F':
149           if (!(format_2 = image_extension_to_format(optarg)))
150             usage();
151           break;
152         case 'g':
153           {
154             if (strlen(optarg) != 6)
155               usage();
156             errno = 0;
157             char *end;
158             long int v = strtol(optarg, &end, 16);
159             if (errno || *end || v < 0)
160               usage();
161             color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
162           }
163           break;
164         case 'r':
165           segmentation_name_1 = optarg;
166           break;
167         case 'R':
168           segmentation_name_2 = optarg;
169           break;
170         default:
171           usage();
172       }
173
174   if (argc != optind + 2 && argc != optind + 1)
175     usage();
176   file_name_1 = argv[optind++];
177   if (argc > optind)
178     file_name_2 = argv[optind++];
179
180   MSG("Initializing image library");
181   srandom(time(NULL) ^ getpid());
182   srgb_to_luv_init();
183   image_thread_init(&it);
184
185   struct image *img1, *img2;
186
187   if (!image_io_init(&it, &io))
188     die("Cannot initialize image I/O (%s)", it.err_msg);
189
190   if (file_name_1)
191     {
192       MSG("Reading %s", file_name_1);
193       io.fastbuf = bopen(file_name_1, O_RDONLY, 1 << 18);
194       io.format = format_1 ? : image_file_name_to_format(file_name_1);
195       TRY(image_io_read_header(&io));
196       io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
197       if (background_color.color_space)
198         io.background_color = background_color;
199       else if (!io.background_color.color_space)
200         io.background_color = color_black;
201       TRY(image_io_read_data(&io, 1));
202       bclose(io.fastbuf);
203       img1 = io.image;
204       MSG("Image size=%ux%u", img1->cols, img1->rows);
205       image_io_reset(&io);
206     }
207   else
208     img1 = NULL;
209
210   if (file_name_2)
211     {
212       MSG("Reading %s", file_name_2);
213       io.fastbuf = bopen(file_name_2, O_RDONLY, 1 << 18);
214       io.format = format_2 ? : image_file_name_to_format(file_name_2);
215       TRY(image_io_read_header(&io));
216       io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
217       if (background_color.color_space)
218         io.background_color = background_color;
219       else if (!io.background_color.color_space)
220         io.background_color = color_black;
221       TRY(image_io_read_data(&io, 1));
222       bclose(io.fastbuf);
223       img2 = io.image;
224       MSG("Image size=%ux%u", img2->cols, img2->rows);
225       image_io_reset(&io);
226     }
227   else
228     img2 = NULL;
229
230   struct image_signature sig1, sig2;
231   MSG("Computing signatures");
232   if (img1)
233     {
234       struct image_sig_data data;
235       TRY(image_sig_init(&it, &data, img1));
236       image_sig_preprocess(&data);
237       if (data.valid)
238         {
239           image_sig_segmentation(&data);
240           image_sig_detect_textured(&data);
241         }
242       if (segmentation_name_1)
243         write_segmentation(&data, segmentation_name_1);
244       image_sig_finish(&data, &sig1);
245       image_sig_cleanup(&data);
246       dump_signature(&sig1);
247     }
248   if (img2)
249     {
250       struct image_sig_data data;
251       TRY(image_sig_init(&it, &data, img2));
252       image_sig_preprocess(&data);
253       if (data.valid)
254         {
255           image_sig_segmentation(&data);
256           image_sig_detect_textured(&data);
257         }
258       if (segmentation_name_2)
259         write_segmentation(&data, segmentation_name_2);
260       image_sig_finish(&data, &sig2);
261       image_sig_cleanup(&data);
262       dump_signature(&sig2);
263     }
264
265   if (img1 && img2)
266     {
267       uns dist;
268       if (verbose)
269         {
270           struct fastbuf *fb = bfdopen(0, 4096);
271           dist = image_signatures_dist_explain(&sig1, &sig2, fb);
272           bclose(fb);
273         }
274       else
275         dist = image_signatures_dist(&sig1, &sig2);
276       MSG("dist=%u", dist);
277     }
278
279   if (img1)
280     image_destroy(img1);
281   if (img2)
282     image_destroy(img2);
283
284   image_io_cleanup(&io);
285   image_thread_cleanup(&it);
286   MSG("Done.");
287   return 0;
288 }