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