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 "images/images.h"
14 #include "images/color.h"
15 #include "images/signature.h"
21 #include <sys/types.h>
28 Usage: image-sim-test [options] image1 image2 \n\
30 -q --quiet no progress messages\n\
31 -f --format-1 image1 format (jpeg, gif, png)\n\
32 -F --format-2 image2 format\n\
33 -g --background background color (hexadecimal RRGGBB)\n\
38 static char *shortopts = "qf:F:g:t:" CF_SHORT_OPTS;
39 static struct option longopts[] =
42 { "quiet", 0, 0, 'q' },
43 { "format-1", 0, 0, 'f' },
44 { "format-2", 0, 0, 'F' },
45 { "background", 0, 0, 'g' },
49 static uns verbose = 1;
50 static byte *file_name_1;
51 static byte *file_name_2;
52 static enum image_format format_1;
53 static enum image_format format_2;
54 static struct color background_color;
56 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
59 dump_signature(struct image_signature *sig)
61 byte buf[MAX(IMAGE_VECTOR_DUMP_MAX, IMAGE_REGION_DUMP_MAX)];
62 image_vector_dump(buf, &sig->vec);
63 MSG("vector: %s", buf);
64 for (uns i = 0; i < sig->len; i++)
66 image_region_dump(buf, sig->reg + i);
67 MSG("region %u: %s", i, buf);
72 main(int argc, char **argv)
76 while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
83 if (!(format_1 = image_extension_to_format(optarg)))
87 if (!(format_2 = image_extension_to_format(optarg)))
92 if (strlen(optarg) != 6)
96 long int v = strtol(optarg, &end, 16);
97 if (errno || *end || v < 0)
99 color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
106 if (argc != optind + 2)
108 file_name_1 = argv[optind++];
109 file_name_2 = argv[optind];
111 #define TRY(x) do{ if (!(x)) die("Error: %s", it.err_msg); }while(0)
112 MSG("Initializing image library");
113 srandom(time(NULL) ^ getpid());
115 struct image_thread it;
117 image_thread_init(&it);
119 struct image *img1, *img2;
121 if (!image_io_init(&it, &io))
122 die("Cannot initialize image I/O (%s)", it.err_msg);
123 MSG("Reading %s", file_name_1);
124 io.fastbuf = bopen(file_name_1, O_RDONLY, 1 << 18);
125 io.format = format_1 ? : image_file_name_to_format(file_name_1);
126 TRY(image_io_read_header(&io));
127 io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
128 if (background_color.color_space)
129 io.background_color = background_color;
130 else if (!io.background_color.color_space)
131 io.background_color = color_black;
132 TRY(image_io_read_data(&io, 1));
135 MSG("Image size=%ux%u", img1->cols, img1->rows);
138 MSG("Reading %s", file_name_2);
139 io.fastbuf = bopen(file_name_2, O_RDONLY, 1 << 18);
140 io.format = format_2 ? : image_file_name_to_format(file_name_2);
141 TRY(image_io_read_header(&io));
142 io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
143 if (background_color.color_space)
144 io.background_color = background_color;
145 else if (!io.background_color.color_space)
146 io.background_color = color_black;
147 TRY(image_io_read_data(&io, 1));
150 image_io_cleanup(&io);
151 MSG("Image size=%ux%u", img2->cols, img2->rows);
153 MSG("Computing signatures");
154 struct image_signature sig1, sig2;
155 TRY(compute_image_signature(&it, &sig1, img1));
156 TRY(compute_image_signature(&it, &sig2, img2));
157 dump_signature(&sig1);
158 dump_signature(&sig2);
160 uns dist = image_signatures_dist(&sig1, &sig1);
161 MSG("dist=%.6f", dist / (double)(1 << IMAGE_SIG_DIST_SCALE));
165 image_thread_cleanup(&it);