]> mj.ucw.cz Git - libucw.git/blob - images/image-dup-test.c
sources backup... changed:
[libucw.git] / images / 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 "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/dup-cmp.h"
16 #include <stdlib.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <stdio.h>
20
21 static void NONRET
22 usage(void)
23 {
24   fputs("\
25 Usage: image-dup-test [options] image1 image2 \n\
26 \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\
32 ", stderr);
33   exit(1);
34 }
35
36 static char *shortopts = "qf:F:g:t:" CF_SHORT_OPTS;
37 static struct option longopts[] =
38 {
39   CF_LONG_OPTS
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' },
45   { NULL,               0, 0, 0 }
46 };
47                                                           
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;
55
56 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
57
58 int
59 main(int argc, char **argv)
60 {
61   log_init(argv[0]);
62   int opt;
63   while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
64     switch (opt)
65       {
66         case 'q':
67           verbose = 0;
68           break;
69         case 'f':
70           if (!(format_1 = image_extension_to_format(optarg)))
71             usage();
72           break;
73         case 'F':
74           if (!(format_2 = image_extension_to_format(optarg)))
75             usage();
76           break;
77         case 'g':
78           {
79             if (strlen(optarg) != 6)
80               usage();
81             errno = 0;
82             char *end;
83             long int v = strtol(optarg, &end, 16);
84             if (errno || *end || v < 0)
85               usage();
86             color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
87           }
88           break;
89         case 't':
90           {
91             errno = 0;
92             char *end;
93             long int v = strtol(optarg, &end, 16);
94             if (errno || *end || v < 0 || v > 0xff)
95               usage();
96             transformations = v;
97           }
98           break;
99         default:
100           usage();
101       }
102
103   if (argc != optind + 2)
104     usage();
105   file_name_1 = argv[optind++];
106   file_name_2 = argv[optind];
107   
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;
111   struct image_io io;
112   image_thread_init(&it);
113
114   struct image *img1, *img2;
115
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));
127   bclose(io.fastbuf);
128   img1 = io.image;
129   MSG("Image size=%ux%u", img1->cols, img1->rows);
130   
131   image_io_reset(&io);
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));
142   bclose(io.fastbuf);
143   img2 = io.image;
144   image_io_cleanup(&io);
145   MSG("Image size=%ux%u", img2->cols, img2->rows);
146
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));
152
153   MSG("Similarity bitmap %02x", image_dup_compare(&dup1, &dup2, transformations | IMAGE_DUP_SCALE | IMAGE_DUP_WANT_ALL));
154
155   mp_delete(pool);
156   
157   image_destroy(img1);
158   image_destroy(img2);
159   image_thread_cleanup(&it);
160   MSG("Done.");
161   return 0;
162 }