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 "lib/base224.h"
15 #include "images/images.h"
16 #include "images/color.h"
17 #include "images/signature.h"
24 #include <sys/types.h>
31 Usage: image-sim-test [options] image1 [image2] \n\
33 -q --quiet no progress messages\n\
34 -f --format-1 image1 format (jpeg, gif, png)\n\
35 -F --format-2 image2 format\n\
36 -g --background background color (hexadecimal RRGGBB)\n\
37 -r --segmentation-1 writes image1 segmentation to given file\n\
38 -R --segmentation-2 writes image2 segmentation to given file\n\
39 -6 --base64 display base64 encoded signature(s)\n\
40 -2 --base224 display base224 encoded signature(s)\n\
45 static char *shortopts = "qf:F:g:t:r:R:62" CF_SHORT_OPTS;
46 static struct option longopts[] =
49 { "quiet", 0, 0, 'q' },
50 { "format-1", 0, 0, 'f' },
51 { "format-2", 0, 0, 'F' },
52 { "background", 0, 0, 'g' },
53 { "segmentation-1", 0, 0, 'r' },
54 { "segmentation-2", 0, 0, 'R' },
55 { "base64", 0, 0, '6' },
56 { "base224", 0, 0, '2' },
60 static uns verbose = 1;
61 static byte *file_name_1;
62 static byte *file_name_2;
63 static enum image_format format_1;
64 static enum image_format format_2;
65 static struct color background_color;
66 static byte *segmentation_name_1;
67 static byte *segmentation_name_2;
68 static uns display_base64;
69 static uns display_base224;
71 #define MSG(x...) do{ if (verbose) msg(L_INFO, ##x); }while(0)
72 #define TRY(x) do{ if (!(x)) exit(1); }while(0)
75 msg_str(byte *s, void *param UNUSED)
81 dump_signature(struct image_signature *sig)
83 byte buf[MAX(IMAGE_VECTOR_DUMP_MAX, IMAGE_REGION_DUMP_MAX)];
84 image_vector_dump(buf, &sig->vec);
85 MSG("vector: %s", buf);
86 for (uns i = 0; i < sig->len; i++)
88 image_region_dump(buf, sig->reg + i);
89 MSG("region %u: %s", i, buf);
91 uns sig_size = image_signature_size(sig->len);
94 byte buf[BASE64_ENC_LENGTH(sig_size) + 1];
95 uns enc_size = base64_encode(buf, (byte *)sig, sig_size);
97 MSG("base64 encoded: %s", buf);
101 byte buf[BASE224_ENC_LENGTH(sig_size) + 1];
102 uns enc_size = base224_encode(buf, (byte *)sig, sig_size);
104 MSG("base224 encoded: %s", buf);
108 static struct image_context ctx;
109 static struct image_io io;
112 write_segmentation(struct image_sig_data *data, byte *fn)
114 MSG("Writing segmentation to %s", fn);
116 struct fastbuf *fb = bopen(fn, O_WRONLY | O_CREAT | O_TRUNC, 4096);
118 TRY(img = image_new(&ctx, data->image->cols, data->image->rows, COLOR_SPACE_RGB, NULL));
119 image_clear(&ctx, img);
121 for (uns i = 0; i < data->regions_count; i++)
124 double luv[3], xyz[3], srgb[3];
125 luv[0] = data->regions[i].a[0] * (4 / 2.55);
126 luv[1] = ((int)data->regions[i].a[1] - 128) * (4 / 2.55);
127 luv[2] = ((int)data->regions[i].a[2] - 128) * (4 / 2.55);
128 luv_to_xyz_exact(xyz, luv);
129 xyz_to_srgb_exact(srgb, xyz);
130 c[0] = CLAMP(srgb[0] * 255, 0, 255);
131 c[1] = CLAMP(srgb[1] * 255, 0, 255);
132 c[2] = CLAMP(srgb[2] * 255, 0, 255);
133 for (struct image_sig_block *block = data->regions[i].blocks; block; block = block->next)
135 uns x1 = block->x * 4;
136 uns y1 = block->y * 4;
137 uns x2 = MIN(x1 + 4, img->cols);
138 uns y2 = MIN(y1 + 4, img->rows);
139 byte *p = img->pixels + x1 * 3 + y1 * img->row_size;
140 for (uns y = y1; y < y2; y++, p += img->row_size)
143 for (uns x = x1; x < x2; x++, p2 += 3)
155 io.format = image_file_name_to_format(fn);
156 TRY(image_io_write(&io));
164 main(int argc, char **argv)
168 while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
175 if (!(format_1 = image_extension_to_format(optarg)))
179 if (!(format_2 = image_extension_to_format(optarg)))
184 if (strlen(optarg) != 6)
188 long int v = strtol(optarg, &end, 16);
189 if (errno || *end || v < 0)
191 color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
195 segmentation_name_1 = optarg;
198 segmentation_name_2 = optarg;
210 if (argc != optind + 2 && argc != optind + 1)
212 file_name_1 = argv[optind++];
214 file_name_2 = argv[optind++];
216 MSG("Initializing image library");
217 srandom(time(NULL) ^ getpid());
219 image_context_init(&ctx);
221 struct image *img1, *img2;
223 TRY(image_io_init(&ctx, &io));
227 MSG("Reading %s", file_name_1);
228 io.fastbuf = bopen(file_name_1, O_RDONLY, 1 << 18);
229 io.format = format_1 ? : image_file_name_to_format(file_name_1);
230 TRY(image_io_read_header(&io));
231 io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
232 if (background_color.color_space)
233 io.background_color = background_color;
234 else if (!io.background_color.color_space)
235 io.background_color = color_black;
236 TRY(image_io_read_data(&io, 1));
239 MSG("Image size=%ux%u", img1->cols, img1->rows);
247 MSG("Reading %s", file_name_2);
248 io.fastbuf = bopen(file_name_2, O_RDONLY, 1 << 18);
249 io.format = format_2 ? : image_file_name_to_format(file_name_2);
250 TRY(image_io_read_header(&io));
251 io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
252 if (background_color.color_space)
253 io.background_color = background_color;
254 else if (!io.background_color.color_space)
255 io.background_color = color_black;
256 TRY(image_io_read_data(&io, 1));
259 MSG("Image size=%ux%u", img2->cols, img2->rows);
265 struct image_signature sig1, sig2;
266 MSG("Computing signatures");
269 struct image_sig_data data;
270 TRY(image_sig_init(&ctx, &data, img1));
271 image_sig_preprocess(&data);
274 image_sig_segmentation(&data);
275 image_sig_detect_textured(&data);
277 if (segmentation_name_1)
278 write_segmentation(&data, segmentation_name_1);
279 image_sig_finish(&data, &sig1);
280 image_sig_cleanup(&data);
281 dump_signature(&sig1);
285 struct image_sig_data data;
286 TRY(image_sig_init(&ctx, &data, img2));
287 image_sig_preprocess(&data);
290 image_sig_segmentation(&data);
291 image_sig_detect_textured(&data);
293 if (segmentation_name_2)
294 write_segmentation(&data, segmentation_name_2);
295 image_sig_finish(&data, &sig2);
296 image_sig_cleanup(&data);
297 dump_signature(&sig2);
305 struct fastbuf *fb = bfdopen(0, 4096);
306 dist = image_signatures_dist_explain(&sig1, &sig2, msg_str, NULL);
310 dist = image_signatures_dist(&sig1, &sig2);
311 MSG("dist=%u", dist);
319 image_io_cleanup(&io);
320 image_context_cleanup(&ctx);