]> mj.ucw.cz Git - libucw.git/blob - images/image-sim-test.c
27a63ee14446f169734b9eda59e93cdfdff7b6e1
[libucw.git] / images / image-sim-test.c
1 /*
2  *      Image similarity testing
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU General Public License.
8  */
9
10 #include "lib/lib.h"
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"
16 #include <stdlib.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <stdio.h>
20 #include <time.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 static void NONRET
25 usage(void)
26 {
27   fputs("\
28 Usage: image-sim-test [options] image1 image2 \n\
29 \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 ", stderr);
35   exit(1);
36 }
37
38 static char *shortopts = "qf:F:g:t:" CF_SHORT_OPTS;
39 static struct option longopts[] =
40 {
41   CF_LONG_OPTS
42   { "quiet",            0, 0, 'q' },
43   { "format-1",         0, 0, 'f' },
44   { "format-2",         0, 0, 'F' },
45   { "background",       0, 0, 'g' },
46   { NULL,               0, 0, 0 }
47 };
48                                                           
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;
55
56 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
57
58 static void
59 dump_signature(struct image_signature *sig)
60 {
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++)
65     {
66       image_region_dump(buf, sig->reg + i);
67       MSG("region %u: %s", i, buf);
68     }
69 }
70
71 int
72 main(int argc, char **argv)
73 {
74   log_init(argv[0]);
75   int opt;
76   while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
77     switch (opt)
78       {
79         case 'q':
80           verbose = 0;
81           break;
82         case 'f':
83           if (!(format_1 = image_extension_to_format(optarg)))
84             usage();
85           break;
86         case 'F':
87           if (!(format_2 = image_extension_to_format(optarg)))
88             usage();
89           break;
90         case 'g':
91           {
92             if (strlen(optarg) != 6)
93               usage();
94             errno = 0;
95             char *end;
96             long int v = strtol(optarg, &end, 16);
97             if (errno || *end || v < 0)
98               usage();
99             color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
100           }
101           break;
102         default:
103           usage();
104       }
105
106   if (argc != optind + 2)
107     usage();
108   file_name_1 = argv[optind++];
109   file_name_2 = argv[optind];
110   
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());
114   srgb_to_luv_init();
115   struct image_thread it;
116   struct image_io io;
117   image_thread_init(&it);
118
119   struct image *img1, *img2;
120
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));
133   bclose(io.fastbuf);
134   img1 = io.image;
135   MSG("Image size=%ux%u", img1->cols, img1->rows);
136   
137   image_io_reset(&io);
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));
148   bclose(io.fastbuf);
149   img2 = io.image;
150   image_io_cleanup(&io);
151   MSG("Image size=%ux%u", img2->cols, img2->rows);
152
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);
159
160   uns dist = image_signatures_dist(&sig1, &sig1);
161   MSG("dist=%.6f", dist / (double)(1 << IMAGE_SIG_DIST_SCALE));
162   
163   image_destroy(img1);
164   image_destroy(img2);
165   image_thread_cleanup(&it);
166   MSG("Done.");
167   return 0;
168 }