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 = 600;
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_init(struct image_thread *thread, struct image_dup *dup, struct image *img, struct mempool *pool)
60 DBG("image_dup_init()");
62 ASSERT((img->flags & IMAGE_PIXEL_FORMAT) == COLOR_SPACE_RGB);
65 for (dup->tab_cols = 0; (uns)(2 << dup->tab_cols) < img->cols && dup->tab_cols < image_dup_tab_limit; dup->tab_cols++);
66 for (dup->tab_rows = 0; (uns)(2 << dup->tab_rows) < img->rows && dup->tab_rows < image_dup_tab_limit; dup->tab_rows++);
67 dup->tab_pixels = mp_alloc(pool, dup->tab_size = (12 << (dup->tab_cols + dup->tab_rows)));
68 dup->tab_row_size = 6 << dup->tab_cols;
70 /* Scale original image to right bottom block */
73 if (!image_dup_subimage(thread, dup, &block, dup->tab_cols, dup->tab_rows))
75 if (!image_scale(thread, &block, img))
79 /* Complete bottom row */
80 for (uns i = dup->tab_cols; i--; )
82 byte *d = image_dup_block(dup, i, dup->tab_rows);
83 byte *s = image_dup_block(dup, i + 1, dup->tab_rows);
84 for (uns y = 0; y < (uns)(1 << dup->tab_rows); y++)
85 for (uns x = 0; x < (uns)(1 << i); x++)
87 pixels_average(d, s, s + 3);
93 /* Complete remaining blocks */
94 for (uns i = 0; i <= dup->tab_cols; i++)
96 uns line_size = (3 << i);
97 for (uns j = dup->tab_rows; j--; )
99 byte *d = image_dup_block(dup, i, j);
100 byte *s = image_dup_block(dup, i, j + 1);
101 for (uns y = 0; y < (uns)(1 << j); y++)
103 for (uns x = 0; x < (uns)(1 << i); x++)
105 pixels_average(d, s, s + line_size);
125 err_sum(byte *pos1, byte *pos2, uns count)
130 uns e = err(*pos1++, *pos2++);
131 e += err(*pos1++, *pos2++);
132 e += err(*pos1++, *pos2++);
139 err_sum_transformed(byte *pos1, byte *pos2, uns cols, uns rows, int row_step_1, int col_step_2, int row_step_2)
141 DBG("err_sum_transformed(pos1=%p pos2=%p cols=%u rows=%u row_step_1=%d col_step_2=%d row_step_2=%d)",
142 pos1, pos2, cols, rows, row_step_1, col_step_2, row_step_2);
144 for (uns j = rows; j--; )
149 for (uns i = cols; i--; )
151 e += err(p1[0], p2[0]);
152 e += err(p1[1], p2[1]);
153 e += err(p1[2], p2[2]);
165 aspect_ratio_test(uns cols1, uns rows1, uns cols2, uns rows2)
167 DBG("aspect_ratio_test(cols1=%u rows1=%u cols2=%u rows2=%u)", cols1, rows1, cols2, rows2);
168 uns r1 = cols1 * rows2;
169 uns r2 = rows1 * cols2;
171 r1 <= ((r2 * image_dup_ratio_threshold) >> 7) &&
172 r2 <= ((r1 * image_dup_ratio_threshold) >> 7);
176 average_compare(struct image_dup *dup1, struct image_dup *dup2)
178 byte *block1 = image_dup_block(dup1, 0, 0);
179 byte *block2 = image_dup_block(dup2, 0, 0);
181 err(block1[0], block2[0]) +
182 err(block1[1], block2[1]) +
183 err(block1[2], block2[2]);
184 return e <= image_dup_error_threshold;
188 blocks_compare(struct image_dup *dup1, struct image_dup *dup2, uns tab_col, uns tab_row, uns trans)
190 DBG("blocks_compare(tab_col=%d tab_row=%d trans=%d)", tab_col, tab_row, trans);
191 byte *block1 = image_dup_block(dup1, tab_col, tab_row);
193 int col_step, row_step;
195 block2 = image_dup_block(dup2, tab_col, tab_row);
197 block2 = image_dup_block(dup2, tab_row, tab_col);
201 uns err = (err_sum(block1, block2, 1 << (tab_col + tab_row)) >> (tab_col + tab_row));
202 DBG("average error=%d", err);
203 return err <= image_dup_error_threshold;
206 row_step = (3 << tab_col);
207 block2 += row_step - 3;
211 row_step = -(3 << tab_col);
212 block2 += (3 << (tab_col + tab_row)) + row_step;
216 row_step = -(3 << tab_col);
217 block2 += (3 << (tab_col + tab_row)) - 3;
220 col_step = (3 << tab_row);
224 col_step = -(3 << tab_row);
226 block2 += (3 << (tab_col + tab_row)) + col_step;
229 col_step = (3 << tab_row);
231 block2 += col_step - 3;
234 col_step = -(3 << tab_row);
236 block2 += (3 << (tab_col + tab_row)) - 3;
241 uns err = (err_sum_transformed(block1, block2, (1 << tab_col), (1 << tab_row), (3 << tab_col), col_step, row_step) >> (tab_col + tab_row));
242 DBG("average error=%d", err);
243 return err <= image_dup_error_threshold;
247 same_size_compare(struct image_dup *dup1, struct image_dup *dup2, uns trans)
249 struct image *img1 = dup1->image;
250 struct image *img2 = dup2->image;
251 byte *block1 = img1->pixels;
252 byte *block2 = img2->pixels;
253 int col_step, row_step;
254 DBG("same_size_compare(trans=%d)", trans);
259 row_step = img2->row_size;
263 row_step = img2->row_size;
264 block2 += 3 * (img2->cols - 1);
268 row_step = -img2->row_size;
269 block2 += img2->row_size * (img2->rows - 1);
273 row_step = -img2->row_size;
274 block2 += img2->row_size * (img2->rows - 1) + 3 * (img2->cols - 1);
277 col_step = img2->row_size;
281 col_step = -img2->row_size;
283 block2 += img2->row_size * (img2->rows - 1);
286 col_step = img2->row_size;
288 block2 += 3 * (img2->cols - 1);
291 col_step = -img2->row_size;
293 block2 += img2->row_size * (img2->rows - 1) + 3 * (img2->cols - 1);
298 uns err = (err_sum_transformed(block1, block2, img1->cols, img1->rows, img1->row_size, col_step, row_step) / ((u64)img1->cols * img1->rows));
299 DBG("average error=%d", err);
300 return err <= image_dup_error_threshold;
304 image_dup_compare(struct image_dup *dup1, struct image_dup *dup2, uns flags)
306 DBG("image_dup_compare()");
307 if (!average_compare(dup1, dup2))
309 struct image *img1 = dup1->image;
310 struct image *img2 = dup2->image;
311 if (flags & IMAGE_DUP_SCALE)
313 DBG("Scale support");
314 if (!aspect_ratio_test(img1->cols, img1->rows, img2->cols, img2->rows))
316 if (!aspect_ratio_test(img1->cols, img1->rows, img2->rows, img2->cols))
321 DBG("No scale support");
322 if (!(img1->cols == img2->cols && img1->rows == img2->rows))
324 if (!(img1->cols == img2->rows && img1->rows == img2->cols))
332 uns cols = MIN(dup1->tab_cols, dup2->tab_cols);
333 uns rows = MIN(dup1->tab_rows, dup2->tab_rows);
334 for (uns t = 0; t < 4; t++)
335 if (flags & (1 << t))
337 DBG("Testing trans %d", t);
338 for (uns i = MAX(cols, rows); i--; )
340 uns col = MAX(0, (int)(cols - i));
341 uns row = MAX(0, (int)(rows - i));
342 if (!blocks_compare(dup1, dup2, col, row, t))
345 (img1->cols != img2->cols || img1->rows != img2->rows ||
346 same_size_compare(dup1, dup2, t)))
349 if (!(flags & IMAGE_DUP_WANT_ALL))
357 uns cols = MIN(dup1->tab_cols, dup2->tab_rows);
358 uns rows = MIN(dup1->tab_rows, dup2->tab_cols);
359 for (uns t = 4; t < 8; t++)
360 if (flags & (1 << t))
362 DBG("Testing trans %d", t);
363 for (uns i = MAX(cols, rows); i--; )
365 uns col = MAX(0, (int)(cols - i));
366 uns row = MAX(0, (int)(rows - i));
367 if (!blocks_compare(dup1, dup2, col, row, t))
370 (img1->cols != img2->rows || img1->rows != img2->cols ||
371 same_size_compare(dup1, dup2, t)) )
374 if (!(flags & IMAGE_DUP_WANT_ALL))