]> mj.ucw.cz Git - libucw.git/blob - images/ucw-image-dup-test.c
Random: Implemented new pseudo-random interface.
[libucw.git] / images / ucw-image-dup-test.c
1 /*
2  *      Image duplicates 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 <ucw/lib.h>
11 #include <ucw/getopt.h>
12 #include <ucw/fastbuf.h>
13 #include <ucw/mempool.h>
14 #include <images/images.h>
15 #include <images/color.h>
16 #include <images/duplicates.h>
17
18 #include <stdlib.h>
19 #include <fcntl.h>
20 #include <errno.h>
21 #include <stdio.h>
22
23 static void NONRET
24 usage(void)
25 {
26   fputs("\
27 Usage: ucw-image-dup-test [options] image1 image2 \n\
28 \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\
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   { "transormations",   0, 0, 't' },
47   { NULL,               0, 0, 0 }
48 };
49
50 static uint 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 uint transformations = IMAGE_DUP_TRANS_ALL;
57
58 #define MSG(x...) do{ if (verbose) msg(L_INFO, ##x); }while(0)
59
60 int
61 main(int argc, char **argv)
62 {
63   log_init(argv[0]);
64   int opt;
65   while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
66     switch (opt)
67       {
68         case 'q':
69           verbose = 0;
70           break;
71         case 'f':
72           if (!(format_1 = image_extension_to_format(optarg)))
73             usage();
74           break;
75         case 'F':
76           if (!(format_2 = image_extension_to_format(optarg)))
77             usage();
78           break;
79         case 'g':
80           {
81             if (strlen(optarg) != 6)
82               usage();
83             errno = 0;
84             char *end;
85             long int v = strtol(optarg, &end, 16);
86             if (errno || *end || v < 0)
87               usage();
88             color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
89           }
90           break;
91         case 't':
92           {
93             errno = 0;
94             char *end;
95             long int v = strtol(optarg, &end, 16);
96             if (errno || *end || v < 0 || v > 0xff)
97               usage();
98             transformations = v;
99           }
100           break;
101         default:
102           usage();
103       }
104
105   if (argc != optind + 2)
106     usage();
107   file_name_1 = argv[optind++];
108   file_name_2 = argv[optind];
109
110 #define TRY(x) do{ if (!(x)) exit(1); }while(0)
111   MSG("Initializing image library");
112   struct image_context ctx;
113   struct image_dup_context idc;
114   struct image_io io;
115   image_context_init(&ctx);
116   image_dup_context_init(&ctx, &idc);
117
118   struct image *img1, *img2;
119
120   TRY(image_io_init(&ctx, &io));
121   MSG("Reading %s", file_name_1);
122   io.fastbuf = bopen(file_name_1, O_RDONLY, 1 << 18);
123   io.format = format_1 ? : image_file_name_to_format(file_name_1);
124   TRY(image_io_read_header(&io));
125   io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
126   if (background_color.color_space)
127     io.background_color = background_color;
128   else if (!io.background_color.color_space)
129     io.background_color = color_black;
130   TRY(image_io_read_data(&io, 1));
131   bclose(io.fastbuf);
132   img1 = io.image;
133   MSG("Image size=%ux%u", img1->cols, img1->rows);
134
135   image_io_reset(&io);
136   MSG("Reading %s", file_name_2);
137   io.fastbuf = bopen(file_name_2, O_RDONLY, 1 << 18);
138   io.format = format_2 ? : image_file_name_to_format(file_name_2);
139   TRY(image_io_read_header(&io));
140   io.flags = COLOR_SPACE_RGB | IMAGE_IO_USE_BACKGROUND;
141   if (background_color.color_space)
142     io.background_color = background_color;
143   else if (!io.background_color.color_space)
144     io.background_color = color_black;
145   TRY(image_io_read_data(&io, 1));
146   bclose(io.fastbuf);
147   img2 = io.image;
148   image_io_cleanup(&io);
149   MSG("Image size=%ux%u", img2->cols, img2->rows);
150
151   struct image_dup *dup1, *dup2;
152   struct mempool *pool = mp_new(1 << 18);
153   MSG("Creating internal structures");
154   dup1 = mp_start(pool, image_dup_estimate_size(img1->cols, img1->rows, 1, idc.qtree_limit));
155   uint size = image_dup_new(&idc, img1, dup1, 1);
156   TRY(size);
157   mp_end(pool, (void *)dup1 + size);
158   dup2 = mp_start(pool, image_dup_estimate_size(img2->cols, img2->rows, 1, idc.qtree_limit));
159   size = image_dup_new(&idc, img2, dup2, 1);
160   TRY(size);
161   mp_end(pool, (void *)dup2 + size);
162
163   idc.flags = transformations | IMAGE_DUP_SCALE | IMAGE_DUP_WANT_ALL;
164   MSG("Similarity bitmap %02x", image_dup_compare(&idc, dup1, dup2));
165
166   mp_delete(pool);
167
168   image_destroy(img1);
169   image_destroy(img2);
170   image_dup_context_cleanup(&idc);
171   image_context_cleanup(&ctx);
172   MSG("Done.");
173   return 0;
174 }