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 --base64 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' },
53 { "base64", 0, 0, '6' },
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;
67 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
68 #define TRY(x) do{ if (!(x)) exit(1); }while(0)
71 msg_str(byte *s, void *param UNUSED)
77 dump_signature(struct image_signature *sig)
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++)
84 image_region_dump(buf, sig->reg + i);
85 MSG("region %u: %s", i, buf);
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);
93 MSG("base64 encoded: %s", buf);
97 static struct image_context ctx;
98 static struct image_io io;
101 write_segmentation(struct image_sig_data *data, byte *fn)
103 MSG("Writing segmentation to %s", fn);
105 struct fastbuf *fb = bopen(fn, O_WRONLY | O_CREAT | O_TRUNC, 4096);
107 TRY(img = image_new(&ctx, data->image->cols, data->image->rows, COLOR_SPACE_RGB, NULL));
108 image_clear(&ctx, img);
110 for (uns i = 0; i < data->regions_count; i++)
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)
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)
132 for (uns x = x1; x < x2; x++, p2 += 3)
144 io.format = image_file_name_to_format(fn);
145 TRY(image_io_write(&io));
153 main(int argc, char **argv)
157 while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
164 if (!(format_1 = image_extension_to_format(optarg)))
168 if (!(format_2 = image_extension_to_format(optarg)))
173 if (strlen(optarg) != 6)
177 long int v = strtol(optarg, &end, 16);
178 if (errno || *end || v < 0)
180 color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
184 segmentation_name_1 = optarg;
187 segmentation_name_2 = optarg;
196 if (argc != optind + 2 && argc != optind + 1)
198 file_name_1 = argv[optind++];
200 file_name_2 = argv[optind++];
202 MSG("Initializing image library");
203 srandom(time(NULL) ^ getpid());
205 image_context_init(&ctx);
207 struct image *img1, *img2;
209 TRY(image_io_init(&ctx, &io));
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));
225 MSG("Image size=%ux%u", img1->cols, img1->rows);
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));
245 MSG("Image size=%ux%u", img2->cols, img2->rows);
251 struct image_signature sig1, sig2;
252 MSG("Computing signatures");
255 struct image_sig_data data;
256 TRY(image_sig_init(&ctx, &data, img1));
257 image_sig_preprocess(&data);
260 image_sig_segmentation(&data);
261 image_sig_detect_textured(&data);
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);
271 struct image_sig_data data;
272 TRY(image_sig_init(&ctx, &data, img2));
273 image_sig_preprocess(&data);
276 image_sig_segmentation(&data);
277 image_sig_detect_textured(&data);
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);
291 struct fastbuf *fb = bfdopen(0, 4096);
292 dist = image_signatures_dist_explain(&sig1, &sig2, msg_str, NULL);
296 dist = image_signatures_dist(&sig1, &sig2);
297 MSG("dist=%u", dist);
305 image_io_cleanup(&io);
306 image_context_cleanup(&ctx);