]> mj.ucw.cz Git - libucw.git/blob - images/image-dup-test.c
fixed bug in image allocation
[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/duplicates.h"
16
17 #include <stdlib.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <stdio.h>
21
22 static void NONRET
23 usage(void)
24 {
25   fputs("\
26 Usage: image-dup-test [options] image1 image2 \n\
27 \n\
28 -q --quiet           no progress messages\n\
29 -f --format-1        image1 format (jpeg, gif, png)\n\
30 -F --format-2        image2 format\n\
31 -g --background      background color (hexadecimal RRGGBB)\n\
32 -t --transformations hexadecimal value of allowed transformtion (1=identity, FF=all)\n\
33 ", stderr);
34   exit(1);
35 }
36
37 static char *shortopts = "qf:F:g:t:" CF_SHORT_OPTS;
38 static struct option longopts[] =
39 {
40   CF_LONG_OPTS
41   { "quiet",            0, 0, 'q' },
42   { "format-1",         0, 0, 'f' },
43   { "format-2",         0, 0, 'F' },
44   { "background",       0, 0, 'g' },
45   { "transormations",   0, 0, 't' },
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 static uns transformations = IMAGE_DUP_TRANS_ALL;
56
57 #define MSG(x...) do{ if (verbose) log(L_INFO, ##x); }while(0)
58
59 int
60 main(int argc, char **argv)
61 {
62   log_init(argv[0]);
63   int opt;
64   while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
65     switch (opt)
66       {
67         case 'q':
68           verbose = 0;
69           break;
70         case 'f':
71           if (!(format_1 = image_extension_to_format(optarg)))
72             usage();
73           break;
74         case 'F':
75           if (!(format_2 = image_extension_to_format(optarg)))
76             usage();
77           break;
78         case 'g':
79           {
80             if (strlen(optarg) != 6)
81               usage();
82             errno = 0;
83             char *end;
84             long int v = strtol(optarg, &end, 16);
85             if (errno || *end || v < 0)
86               usage();
87             color_make_rgb(&background_color, (v >> 16) & 255, (v >> 8) & 255, v & 255);
88           }
89           break;
90         case 't':
91           {
92             errno = 0;
93             char *end;
94             long int v = strtol(optarg, &end, 16);
95             if (errno || *end || v < 0 || v > 0xff)
96               usage();
97             transformations = v;
98           }
99           break;
100         default:
101           usage();
102       }
103
104   if (argc != optind + 2)
105     usage();
106   file_name_1 = argv[optind++];
107   file_name_2 = argv[optind];
108   
109 #define TRY(x) do{ if (!(x)) die("Error: %s", it.err_msg); }while(0)
110   MSG("Initializing image library");
111   struct image_thread it;
112   struct image_io io;
113   image_thread_init(&it);
114
115   struct image *img1, *img2;
116
117   if (!image_io_init(&it, &io))
118     die("Cannot initialize image I/O (%s)", it.err_msg);
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));
129   bclose(io.fastbuf);
130   img1 = io.image;
131   MSG("Image size=%ux%u", img1->cols, img1->rows);
132   
133   image_io_reset(&io);
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));
144   bclose(io.fastbuf);
145   img2 = io.image;
146   image_io_cleanup(&io);
147   MSG("Image size=%ux%u", img2->cols, img2->rows);
148
149   struct image_dup dup1, dup2;
150   struct mempool *pool = mp_new(1 << 18);
151   MSG("Creating internal structures");
152   TRY(image_dup_init(&it, &dup1, img1, pool));
153   TRY(image_dup_init(&it, &dup2, img2, pool));
154
155   MSG("Similarity bitmap %02x", image_dup_compare(&dup1, &dup2, transformations | IMAGE_DUP_SCALE | IMAGE_DUP_WANT_ALL));
156
157   mp_delete(pool);
158   
159   image_destroy(img1);
160   image_destroy(img2);
161   image_thread_cleanup(&it);
162   MSG("Done.");
163   return 0;
164 }