2 * Image similarity testing
4 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU General Public License.
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"
23 #include <sys/types.h>
30 Usage: image-sim-test [options] image1 [image2] \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\
43 static char *shortopts = "qf:F:g:t:r:R:6" CF_SHORT_OPTS;
44 static struct option longopts[] =
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' },
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;
66 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
67 #define TRY(x) do{ if (!(x)) exit(1); }while(0)
70 msg_str(byte *s, void *param UNUSED)
76 dump_signature(struct image_signature *sig)
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++)
83 image_region_dump(buf, sig->reg + i);
84 MSG("region %u: %s", i, buf);
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);
92 MSG("base64 encoded: %s", buf);
96 static struct image_context ctx;
97 static struct image_io io;
100 write_segmentation(struct image_sig_data *data, byte *fn)
102 MSG("Writing segmentation to %s", fn);
104 struct fastbuf *fb = bopen(fn, O_WRONLY | O_CREAT | O_TRUNC, 4096);
106 TRY(img = image_new(&ctx, data->image->cols, data->image->rows, COLOR_SPACE_RGB, NULL));
107 image_clear(&ctx, img);
109 for (uns i = 0; i < data->regions_count; i++)
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)
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)
131 for (uns x = x1; x < x2; x++, p2 += 3)
143 io.format = image_file_name_to_format(fn);
144 TRY(image_io_write(&io));
152 main(int argc, char **argv)
156 while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
163 if (!(format_1 = image_extension_to_format(optarg)))
167 if (!(format_2 = image_extension_to_format(optarg)))
172 if (strlen(optarg) != 6)
176 long int v = strtol(optarg, &end, 16);
177 if (errno || *end || v < 0)
179 color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
183 segmentation_name_1 = optarg;
186 segmentation_name_2 = optarg;
195 if (argc != optind + 2 && argc != optind + 1)
197 file_name_1 = argv[optind++];
199 file_name_2 = argv[optind++];
201 MSG("Initializing image library");
202 srandom(time(NULL) ^ getpid());
204 image_context_init(&ctx);
206 struct image *img1, *img2;
208 TRY(image_io_init(&ctx, &io));
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));
224 MSG("Image size=%ux%u", img1->cols, img1->rows);
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));
244 MSG("Image size=%ux%u", img2->cols, img2->rows);
250 struct image_signature sig1, sig2;
251 MSG("Computing signatures");
254 struct image_sig_data data;
255 TRY(image_sig_init(&ctx, &data, img1));
256 image_sig_preprocess(&data);
259 image_sig_segmentation(&data);
260 image_sig_detect_textured(&data);
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);
270 struct image_sig_data data;
271 TRY(image_sig_init(&ctx, &data, img2));
272 image_sig_preprocess(&data);
275 image_sig_segmentation(&data);
276 image_sig_detect_textured(&data);
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);
290 struct fastbuf *fb = bfdopen(0, 4096);
291 dist = image_signatures_dist_explain(&sig1, &sig2, msg_str, NULL);
295 dist = image_signatures_dist(&sig1, &sig2);
296 MSG("dist=%u", dist);
304 image_io_cleanup(&io);
305 image_context_cleanup(&ctx);