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