2 * Image duplicates 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/dup-cmp.h"
25 Usage: image-dup-test [options] image1 image2 \n\
27 -q --quiet no progress messages\n\
28 -f --format-1 image1 format (jpeg, gif, png)\n\
29 -F --format-2 image2 format\n\
30 -g --background background color (hexadecimal RRGGBB)\n\
31 -t --transformations hexadecimal value of allowed transformtion (1=identity, FF=all)\n\
36 static char *shortopts = "qf:F:g:t:" CF_SHORT_OPTS;
37 static struct option longopts[] =
40 { "quiet", 0, 0, 'q' },
41 { "format-1", 0, 0, 'f' },
42 { "format-2", 0, 0, 'F' },
43 { "background", 0, 0, 'g' },
44 { "transormations", 0, 0, 't' },
48 static uns verbose = 1;
49 static byte *file_name_1;
50 static byte *file_name_2;
51 static enum image_format format_1;
52 static enum image_format format_2;
53 static struct color background_color;
54 static uns transformations = IMAGE_DUP_TRANS_ALL;
56 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
59 main(int argc, char **argv)
63 while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
70 if (!(format_1 = image_extension_to_format(optarg)))
74 if (!(format_2 = image_extension_to_format(optarg)))
79 if (strlen(optarg) != 6)
83 long int v = strtol(optarg, &end, 16);
84 if (errno || *end || v < 0)
86 color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
93 long int v = strtol(optarg, &end, 16);
94 if (errno || *end || v < 0 || v > 0xff)
103 if (argc != optind + 2)
105 file_name_1 = argv[optind++];
106 file_name_2 = argv[optind];
108 #define TRY(x) do{ if (!(x)) die("Error: %s", it.err_msg); }while(0)
109 MSG("Initializing image library");
110 struct image_thread it;
112 image_thread_init(&it);
114 struct image *img1, *img2;
116 image_io_init(&it, &io);
117 MSG("Reading %s", file_name_1);
118 io.fastbuf = bopen(file_name_1, O_RDONLY, 1 << 18);
119 io.format = format_1 ? : image_file_name_to_format(file_name_1);
120 TRY(image_io_read_header(&io));
121 io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
122 if (background_color.color_space)
123 io.background_color = background_color;
124 else if (!io.background_color.color_space)
125 io.background_color = color_black;
126 TRY(image_io_read_data(&io, 1));
129 MSG("Image size=%ux%u", img1->cols, img1->rows);
132 MSG("Reading %s", file_name_2);
133 io.fastbuf = bopen(file_name_2, O_RDONLY, 1 << 18);
134 io.format = format_2 ? : image_file_name_to_format(file_name_2);
135 TRY(image_io_read_header(&io));
136 io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
137 if (background_color.color_space)
138 io.background_color = background_color;
139 else if (!io.background_color.color_space)
140 io.background_color = color_black;
141 TRY(image_io_read_data(&io, 1));
144 image_io_cleanup(&io);
145 MSG("Image size=%ux%u", img2->cols, img2->rows);
147 struct image_dup dup1, dup2;
148 struct mempool *pool = mp_new(1 << 18);
149 MSG("Creating internal structures");
150 TRY(image_dup_init(&it, &dup1, img1, pool));
151 TRY(image_dup_init(&it, &dup2, img2, pool));
153 MSG("Similarity bitmap %02x", image_dup_compare(&dup1, &dup2, transformations | IMAGE_DUP_SCALE | IMAGE_DUP_WANT_ALL));
159 image_thread_cleanup(&it);