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