2 * Image Library -- Duplicates Comparison
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 Lesser General Public License.
11 * - many possible optimization
12 * - compare normalized pictures (brightness, ...)
13 * - a blur should help to deal with scaling errors
14 * - maybe better/slower last step
15 * - different thresholds for various transformations
16 * - do not test all transformations for symetric pictures
17 * - allocated memory could be easily decreased to about 1/3
18 * for aspect ratio threshold near one
23 #include "sherlock/sherlock.h"
24 #include "lib/mempool.h"
25 #include "images/images.h"
26 #include "images/dup-cmp.h"
28 #include "lib/mempool.h"
29 #include "lib/fastbuf.h"
32 static uns image_dup_ratio_threshold = 140;
33 static uns image_dup_error_threshold = 800;
34 static uns image_dup_tab_limit = 8;
37 image_dup_block(struct image_dup *dup, uns tab_col, uns tab_row)
39 return dup->tab_pixels + (dup->tab_row_size << tab_row) + (3 << (tab_row + tab_col));
42 static inline struct image *
43 image_dup_subimage(struct image_thread *thread, struct image_dup *dup, struct image *block, uns tab_col, uns tab_row)
45 return image_init_matrix(thread, block, image_dup_block(dup, tab_col, tab_row),
46 1 << tab_col, 1 << tab_row, 3 << tab_col, COLOR_SPACE_RGB);
50 pixels_average(byte *dest, byte *src1, byte *src2)
52 dest[0] = ((uns)src1[0] + (uns)src2[0]) >> 1;
53 dest[1] = ((uns)src1[1] + (uns)src2[1]) >> 1;
54 dest[2] = ((uns)src1[2] + (uns)src2[2]) >> 1;
58 image_dup_estimate_size(uns cols, uns rows)
60 uns tab_cols, tab_rows;
61 for (tab_cols = 0; (uns)(2 << tab_cols) < cols && tab_cols < image_dup_tab_limit; tab_cols++);
62 for (tab_rows = 0; (uns)(2 << tab_rows) < rows && tab_rows < image_dup_tab_limit; tab_rows++);
63 return sizeof(struct image) + cols * rows * 3 + sizeof(struct image_dup) + (12 << (tab_cols + tab_rows)) + 64;
67 image_dup_init(struct image_thread *thread, struct image_dup *dup, struct image *img, struct mempool *pool)
69 DBG("image_dup_init()");
71 ASSERT((img->flags & IMAGE_PIXEL_FORMAT) == COLOR_SPACE_RGB);
74 for (dup->tab_cols = 0; (uns)(2 << dup->tab_cols) < img->cols && dup->tab_cols < image_dup_tab_limit; dup->tab_cols++);
75 for (dup->tab_rows = 0; (uns)(2 << dup->tab_rows) < img->rows && dup->tab_rows < image_dup_tab_limit; dup->tab_rows++);
76 dup->tab_pixels = mp_alloc(pool, dup->tab_size = (12 << (dup->tab_cols + dup->tab_rows)));
77 dup->tab_row_size = 6 << dup->tab_cols;
79 /* Scale original image to right bottom block */
82 if (!image_dup_subimage(thread, dup, &block, dup->tab_cols, dup->tab_rows))
84 if (!image_scale(thread, &block, img))
88 /* Complete bottom row */
89 for (uns i = dup->tab_cols; i--; )
91 byte *d = image_dup_block(dup, i, dup->tab_rows);
92 byte *s = image_dup_block(dup, i + 1, dup->tab_rows);
93 for (uns y = 0; y < (uns)(1 << dup->tab_rows); y++)
94 for (uns x = 0; x < (uns)(1 << i); x++)
96 pixels_average(d, s, s + 3);
102 /* Complete remaining blocks */
103 for (uns i = 0; i <= dup->tab_cols; i++)
105 uns line_size = (3 << i);
106 for (uns j = dup->tab_rows; j--; )
108 byte *d = image_dup_block(dup, i, j);
109 byte *s = image_dup_block(dup, i, j + 1);
110 for (uns y = 0; y < (uns)(1 << j); y++)
112 for (uns x = 0; x < (uns)(1 << i); x++)
114 pixels_average(d, s, s + line_size);
134 err_sum(byte *pos1, byte *pos2, uns count)
139 uns e = err(*pos1++, *pos2++);
140 e += err(*pos1++, *pos2++);
141 e += err(*pos1++, *pos2++);
148 err_sum_transformed(byte *pos1, byte *pos2, uns cols, uns rows, int row_step_1, int col_step_2, int row_step_2)
150 DBG("err_sum_transformed(pos1=%p pos2=%p cols=%u rows=%u row_step_1=%d col_step_2=%d row_step_2=%d)",
151 pos1, pos2, cols, rows, row_step_1, col_step_2, row_step_2);
153 for (uns j = rows; j--; )
158 for (uns i = cols; i--; )
160 e += err(p1[0], p2[0]);
161 e += err(p1[1], p2[1]);
162 e += err(p1[2], p2[2]);
174 aspect_ratio_test(uns cols1, uns rows1, uns cols2, uns rows2)
176 DBG("aspect_ratio_test(cols1=%u rows1=%u cols2=%u rows2=%u)", cols1, rows1, cols2, rows2);
177 uns r1 = cols1 * rows2;
178 uns r2 = rows1 * cols2;
180 r1 <= ((r2 * image_dup_ratio_threshold) >> 7) &&
181 r2 <= ((r1 * image_dup_ratio_threshold) >> 7);
185 average_compare(struct image_dup *dup1, struct image_dup *dup2)
187 byte *block1 = image_dup_block(dup1, 0, 0);
188 byte *block2 = image_dup_block(dup2, 0, 0);
190 err(block1[0], block2[0]) +
191 err(block1[1], block2[1]) +
192 err(block1[2], block2[2]);
193 return e <= image_dup_error_threshold;
197 blocks_compare(struct image_dup *dup1, struct image_dup *dup2, uns tab_col, uns tab_row, uns trans)
199 DBG("blocks_compare(tab_col=%d tab_row=%d trans=%d)", tab_col, tab_row, trans);
200 byte *block1 = image_dup_block(dup1, tab_col, tab_row);
202 int col_step, row_step;
204 block2 = image_dup_block(dup2, tab_col, tab_row);
206 block2 = image_dup_block(dup2, tab_row, tab_col);
210 uns err = (err_sum(block1, block2, 1 << (tab_col + tab_row)) >> (tab_col + tab_row));
211 DBG("average error=%d", err);
212 return err <= image_dup_error_threshold;
215 row_step = (3 << tab_col);
216 block2 += row_step - 3;
220 row_step = -(3 << tab_col);
221 block2 += (3 << (tab_col + tab_row)) + row_step;
225 row_step = -(3 << tab_col);
226 block2 += (3 << (tab_col + tab_row)) - 3;
229 col_step = (3 << tab_row);
233 col_step = -(3 << tab_row);
235 block2 += (3 << (tab_col + tab_row)) + col_step;
238 col_step = (3 << tab_row);
240 block2 += col_step - 3;
243 col_step = -(3 << tab_row);
245 block2 += (3 << (tab_col + tab_row)) - 3;
250 uns err = (err_sum_transformed(block1, block2, (1 << tab_col), (1 << tab_row), (3 << tab_col), col_step, row_step) >> (tab_col + tab_row));
251 DBG("average error=%d", err);
252 return err <= image_dup_error_threshold;
256 same_size_compare(struct image_dup *dup1, struct image_dup *dup2, uns trans)
258 struct image *img1 = dup1->image;
259 struct image *img2 = dup2->image;
260 byte *block1 = img1->pixels;
261 byte *block2 = img2->pixels;
262 int col_step, row_step;
263 DBG("same_size_compare(trans=%d)", trans);
268 row_step = img2->row_size;
272 row_step = img2->row_size;
273 block2 += 3 * (img2->cols - 1);
277 row_step = -img2->row_size;
278 block2 += img2->row_size * (img2->rows - 1);
282 row_step = -img2->row_size;
283 block2 += img2->row_size * (img2->rows - 1) + 3 * (img2->cols - 1);
286 col_step = img2->row_size;
290 col_step = -img2->row_size;
292 block2 += img2->row_size * (img2->rows - 1);
295 col_step = img2->row_size;
297 block2 += 3 * (img2->cols - 1);
300 col_step = -img2->row_size;
302 block2 += img2->row_size * (img2->rows - 1) + 3 * (img2->cols - 1);
307 uns err = (err_sum_transformed(block1, block2, img1->cols, img1->rows, img1->row_size, col_step, row_step) / ((u64)img1->cols * img1->rows));
308 DBG("average error=%d", err);
309 return err <= image_dup_error_threshold;
313 image_dup_compare(struct image_dup *dup1, struct image_dup *dup2, uns flags)
315 DBG("image_dup_compare()");
316 if (!average_compare(dup1, dup2))
318 struct image *img1 = dup1->image;
319 struct image *img2 = dup2->image;
320 if (flags & IMAGE_DUP_SCALE)
322 DBG("Scale support");
323 if (!aspect_ratio_test(img1->cols, img1->rows, img2->cols, img2->rows))
325 if (!aspect_ratio_test(img1->cols, img1->rows, img2->rows, img2->cols))
330 DBG("No scale support");
331 if (!(img1->cols == img2->cols && img1->rows == img2->rows))
333 if (!(img1->cols == img2->rows && img1->rows == img2->cols))
341 uns cols = MIN(dup1->tab_cols, dup2->tab_cols);
342 uns rows = MIN(dup1->tab_rows, dup2->tab_rows);
343 for (uns t = 0; t < 4; t++)
344 if (flags & (1 << t))
346 DBG("Testing trans %d", t);
347 for (uns i = MAX(cols, rows); i--; )
349 uns col = MAX(0, (int)(cols - i));
350 uns row = MAX(0, (int)(rows - i));
351 if (!blocks_compare(dup1, dup2, col, row, t))
354 (img1->cols != img2->cols || img1->rows != img2->rows ||
355 same_size_compare(dup1, dup2, t)))
358 if (!(flags & IMAGE_DUP_WANT_ALL))
368 uns cols = MIN(dup1->tab_cols, dup2->tab_rows);
369 uns rows = MIN(dup1->tab_rows, dup2->tab_cols);
370 for (uns t = 4; t < 8; t++)
371 if (flags & (1 << t))
373 DBG("Testing trans %d", t);
374 for (uns i = MAX(cols, rows); i--; )
376 uns col = MAX(0, (int)(cols - i));
377 uns row = MAX(0, (int)(rows - i));
378 if (!blocks_compare(dup1, dup2, col, row, t))
381 (img1->cols != img2->rows || img1->rows != img2->cols ||
382 same_size_compare(dup1, dup2, t)) )
385 if (!(flags & IMAGE_DUP_WANT_ALL))