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