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 <ucw/getopt.h>
12 #include <ucw/fastbuf.h>
13 #include <ucw/base64.h>
14 #include <ucw/base224.h>
15 #include <images/images.h>
16 #include <images/color.h>
17 #include <images/signature.h>
28 Usage: ucw-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\
34 -r --segmentation-1 writes image1 segmentation to given file\n\
35 -R --segmentation-2 writes image2 segmentation to given file\n\
36 -6 --base64 display base64 encoded signature(s)\n\
37 -2 --base224 display base224 encoded signature(s)\n\
42 static char *shortopts = "qf:F:g:t:r:R:62" CF_SHORT_OPTS;
43 static struct option longopts[] =
46 { "quiet", 0, 0, 'q' },
47 { "format-1", 0, 0, 'f' },
48 { "format-2", 0, 0, 'F' },
49 { "background", 0, 0, 'g' },
50 { "segmentation-1", 0, 0, 'r' },
51 { "segmentation-2", 0, 0, 'R' },
52 { "base64", 0, 0, '6' },
53 { "base224", 0, 0, '2' },
57 static uint 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 uint display_base64;
66 static uint display_base224;
68 #define MSG(x...) do{ if (verbose) msg(L_INFO, ##x); }while(0)
69 #define TRY(x) do{ if (!(x)) exit(1); }while(0)
72 msg_str(byte *s, void *param UNUSED)
78 dump_signature(struct image_signature *sig)
80 byte buf[MAX(IMAGE_VECTOR_DUMP_MAX, IMAGE_REGION_DUMP_MAX)];
81 image_vector_dump(buf, &sig->vec);
82 MSG("vector: %s", buf);
83 for (uint i = 0; i < sig->len; i++)
85 image_region_dump(buf, sig->reg + i);
86 MSG("region %u: %s", i, buf);
88 uint sig_size = image_signature_size(sig->len);
91 byte buf[BASE64_ENC_LENGTH(sig_size) + 1];
92 uint enc_size = base64_encode(buf, (byte *)sig, sig_size);
94 MSG("base64 encoded: %s", buf);
98 byte buf[BASE224_ENC_LENGTH(sig_size) + 1];
99 uint enc_size = base224_encode(buf, (byte *)sig, sig_size);
101 MSG("base224 encoded: %s", buf);
105 static struct image_context ctx;
106 static struct image_io io;
109 write_segmentation(struct image_sig_data *data, byte *fn)
111 MSG("Writing segmentation to %s", fn);
113 struct fastbuf *fb = bopen(fn, O_WRONLY | O_CREAT | O_TRUNC, 4096);
115 TRY(img = image_new(&ctx, data->image->cols, data->image->rows, COLOR_SPACE_RGB, NULL));
116 image_clear(&ctx, img);
118 for (uint i = 0; i < data->regions_count; i++)
121 double luv[3], xyz[3], srgb[3];
122 luv[0] = data->regions[i].a[0] * (4 / 2.55);
123 luv[1] = ((int)data->regions[i].a[1] - 128) * (4 / 2.55);
124 luv[2] = ((int)data->regions[i].a[2] - 128) * (4 / 2.55);
125 luv_to_xyz_exact(xyz, luv);
126 xyz_to_srgb_exact(srgb, xyz);
127 c[0] = CLAMP(srgb[0] * 255, 0, 255);
128 c[1] = CLAMP(srgb[1] * 255, 0, 255);
129 c[2] = CLAMP(srgb[2] * 255, 0, 255);
130 for (struct image_sig_block *block = data->regions[i].blocks; block; block = block->next)
132 uint x1 = block->x * 4;
133 uint y1 = block->y * 4;
134 uint x2 = MIN(x1 + 4, img->cols);
135 uint y2 = MIN(y1 + 4, img->rows);
136 byte *p = img->pixels + x1 * 3 + y1 * img->row_size;
137 for (uint y = y1; y < y2; y++, p += img->row_size)
140 for (uint x = x1; x < x2; x++, p2 += 3)
152 io.format = image_file_name_to_format(fn);
153 TRY(image_io_write(&io));
161 main(int argc, char **argv)
165 while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
172 if (!(format_1 = image_extension_to_format(optarg)))
176 if (!(format_2 = image_extension_to_format(optarg)))
181 if (strlen(optarg) != 6)
185 long int v = strtol(optarg, &end, 16);
186 if (errno || *end || v < 0)
188 color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
192 segmentation_name_1 = optarg;
195 segmentation_name_2 = optarg;
207 if (argc != optind + 2 && argc != optind + 1)
209 file_name_1 = argv[optind++];
211 file_name_2 = argv[optind++];
213 MSG("Initializing image library");
216 image_context_init(&ctx);
218 struct image *img1, *img2;
220 TRY(image_io_init(&ctx, &io));
224 MSG("Reading %s", file_name_1);
225 io.fastbuf = bopen(file_name_1, O_RDONLY, 1 << 18);
226 io.format = format_1 ? : image_file_name_to_format(file_name_1);
227 TRY(image_io_read_header(&io));
228 io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
229 if (background_color.color_space)
230 io.background_color = background_color;
231 else if (!io.background_color.color_space)
232 io.background_color = color_black;
233 TRY(image_io_read_data(&io, 1));
236 MSG("Image size=%ux%u", img1->cols, img1->rows);
244 MSG("Reading %s", file_name_2);
245 io.fastbuf = bopen(file_name_2, O_RDONLY, 1 << 18);
246 io.format = format_2 ? : image_file_name_to_format(file_name_2);
247 TRY(image_io_read_header(&io));
248 io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
249 if (background_color.color_space)
250 io.background_color = background_color;
251 else if (!io.background_color.color_space)
252 io.background_color = color_black;
253 TRY(image_io_read_data(&io, 1));
256 MSG("Image size=%ux%u", img2->cols, img2->rows);
262 struct image_signature sig1, sig2;
263 MSG("Computing signatures");
266 struct image_sig_data data;
267 TRY(image_sig_init(&ctx, &data, img1));
268 image_sig_preprocess(&data);
271 image_sig_segmentation(&data);
272 image_sig_detect_textured(&data);
274 if (segmentation_name_1)
275 write_segmentation(&data, segmentation_name_1);
276 image_sig_finish(&data, &sig1);
277 image_sig_cleanup(&data);
278 dump_signature(&sig1);
282 struct image_sig_data data;
283 TRY(image_sig_init(&ctx, &data, img2));
284 image_sig_preprocess(&data);
287 image_sig_segmentation(&data);
288 image_sig_detect_textured(&data);
290 if (segmentation_name_2)
291 write_segmentation(&data, segmentation_name_2);
292 image_sig_finish(&data, &sig2);
293 image_sig_cleanup(&data);
294 dump_signature(&sig2);
302 struct fastbuf *fb = bfdopen(0, 4096);
303 dist = image_signatures_dist_explain(&sig1, &sig2, msg_str, NULL);
307 dist = image_signatures_dist(&sig1, &sig2);
308 MSG("dist=%u", dist);
316 image_io_cleanup(&io);
317 image_context_cleanup(&ctx);