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 "lib/mempool.h"
14 #include "images/images.h"
15 #include "images/color.h"
16 #include "images/duplicates.h"
27 Usage: image-dup-test [options] image1 image2 \n\
29 -q --quiet no progress messages\n\
30 -f --format-1 image1 format (jpeg, gif, png)\n\
31 -F --format-2 image2 format\n\
32 -g --background background color (hexadecimal RRGGBB)\n\
33 -t --transformations hexadecimal value of allowed transformtion (1=identity, FF=all)\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' },
46 { "transormations", 0, 0, 't' },
50 static uns verbose = 1;
51 static byte *file_name_1;
52 static byte *file_name_2;
53 static enum image_format format_1;
54 static enum image_format format_2;
55 static struct color background_color;
56 static uns transformations = IMAGE_DUP_TRANS_ALL;
58 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
61 main(int argc, char **argv)
65 while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
72 if (!(format_1 = image_extension_to_format(optarg)))
76 if (!(format_2 = image_extension_to_format(optarg)))
81 if (strlen(optarg) != 6)
85 long int v = strtol(optarg, &end, 16);
86 if (errno || *end || v < 0)
88 color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
95 long int v = strtol(optarg, &end, 16);
96 if (errno || *end || v < 0 || v > 0xff)
105 if (argc != optind + 2)
107 file_name_1 = argv[optind++];
108 file_name_2 = argv[optind];
110 #define TRY(x) do{ if (!(x)) exit(1); }while(0)
111 MSG("Initializing image library");
112 struct image_context ctx;
114 image_context_init(&ctx);
116 struct image *img1, *img2;
118 TRY(image_io_init(&ctx, &io));
119 MSG("Reading %s", file_name_1);
120 io.fastbuf = bopen(file_name_1, O_RDONLY, 1 << 18);
121 io.format = format_1 ? : image_file_name_to_format(file_name_1);
122 TRY(image_io_read_header(&io));
123 io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
124 if (background_color.color_space)
125 io.background_color = background_color;
126 else if (!io.background_color.color_space)
127 io.background_color = color_black;
128 TRY(image_io_read_data(&io, 1));
131 MSG("Image size=%ux%u", img1->cols, img1->rows);
134 MSG("Reading %s", file_name_2);
135 io.fastbuf = bopen(file_name_2, O_RDONLY, 1 << 18);
136 io.format = format_2 ? : image_file_name_to_format(file_name_2);
137 TRY(image_io_read_header(&io));
138 io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
139 if (background_color.color_space)
140 io.background_color = background_color;
141 else if (!io.background_color.color_space)
142 io.background_color = color_black;
143 TRY(image_io_read_data(&io, 1));
146 image_io_cleanup(&io);
147 MSG("Image size=%ux%u", img2->cols, img2->rows);
149 struct image_dup dup1, dup2;
150 struct mempool *pool = mp_new(1 << 18);
151 MSG("Creating internal structures");
152 TRY(image_dup_init(&ctx, &dup1, img1, pool));
153 TRY(image_dup_init(&ctx, &dup2, img2, pool));
155 MSG("Similarity bitmap %02x", image_dup_compare(&dup1, &dup2, transformations | IMAGE_DUP_SCALE | IMAGE_DUP_WANT_ALL));
161 image_context_cleanup(&ctx);