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.
12 #include "sherlock/sherlock.h"
13 #include "lib/mempool.h"
14 #include "lib/fastbuf.h"
15 #include "images/images.h"
16 #include "images/duplicates.h"
20 static uns image_dup_ratio_threshold = 140;
21 static uns image_dup_error_threshold = 800;
31 err_sum(byte *pos1, byte *pos2, uns count)
36 uns e = err(*pos1++, *pos2++);
37 e += err(*pos1++, *pos2++);
38 e += err(*pos1++, *pos2++);
45 err_sum_transformed(byte *pos1, byte *pos2, uns cols, uns rows, int row_step_1, int col_step_2, int row_step_2)
47 DBG("err_sum_transformed(pos1=%p pos2=%p cols=%u rows=%u row_step_1=%d col_step_2=%d row_step_2=%d)",
48 pos1, pos2, cols, rows, row_step_1, col_step_2, row_step_2);
50 for (uns j = rows; j--; )
55 for (uns i = cols; i--; )
57 e += err(p1[0], p2[0]);
58 e += err(p1[1], p2[1]);
59 e += err(p1[2], p2[2]);
71 aspect_ratio_test(uns cols1, uns rows1, uns cols2, uns rows2)
73 DBG("aspect_ratio_test(cols1=%u rows1=%u cols2=%u rows2=%u)", cols1, rows1, cols2, rows2);
74 uns r1 = cols1 * rows2;
75 uns r2 = rows1 * cols2;
77 r1 <= ((r2 * image_dup_ratio_threshold) >> 7) &&
78 r2 <= ((r1 * image_dup_ratio_threshold) >> 7);
82 average_compare(struct image_dup *dup1, struct image_dup *dup2)
84 byte *block1 = image_dup_block(dup1, 0, 0);
85 byte *block2 = image_dup_block(dup2, 0, 0);
87 err(block1[0], block2[0]) +
88 err(block1[1], block2[1]) +
89 err(block1[2], block2[2]);
90 return e <= image_dup_error_threshold;
94 blocks_compare(struct image_dup *dup1, struct image_dup *dup2, uns tab_col, uns tab_row, uns trans)
96 DBG("blocks_compare(tab_col=%d tab_row=%d trans=%d)", tab_col, tab_row, trans);
97 byte *block1 = image_dup_block(dup1, tab_col, tab_row);
99 int col_step, row_step;
101 block2 = image_dup_block(dup2, tab_col, tab_row);
103 block2 = image_dup_block(dup2, tab_row, tab_col);
107 uns err = (err_sum(block1, block2, 1 << (tab_col + tab_row)) >> (tab_col + tab_row));
108 DBG("average error=%d", err);
109 return err <= image_dup_error_threshold;
112 row_step = (3 << tab_col);
113 block2 += row_step - 3;
117 row_step = -(3 << tab_col);
118 block2 += (3 << (tab_col + tab_row)) + row_step;
122 row_step = -(3 << tab_col);
123 block2 += (3 << (tab_col + tab_row)) - 3;
126 col_step = (3 << tab_row);
130 col_step = -(3 << tab_row);
132 block2 += (3 << (tab_col + tab_row)) + col_step;
135 col_step = (3 << tab_row);
137 block2 += col_step - 3;
140 col_step = -(3 << tab_row);
142 block2 += (3 << (tab_col + tab_row)) - 3;
147 uns err = (err_sum_transformed(block1, block2, (1 << tab_col), (1 << tab_row), (3 << tab_col), col_step, row_step) >> (tab_col + tab_row));
148 DBG("average error=%d", err);
149 return err <= image_dup_error_threshold;
153 same_size_compare(struct image_dup *dup1, struct image_dup *dup2, uns trans)
155 struct image *img1 = dup1->image;
156 struct image *img2 = dup2->image;
157 byte *block1 = img1->pixels;
158 byte *block2 = img2->pixels;
159 int col_step, row_step;
160 DBG("same_size_compare(trans=%d)", trans);
165 row_step = img2->row_size;
169 row_step = img2->row_size;
170 block2 += 3 * (img2->cols - 1);
174 row_step = -img2->row_size;
175 block2 += img2->row_size * (img2->rows - 1);
179 row_step = -img2->row_size;
180 block2 += img2->row_size * (img2->rows - 1) + 3 * (img2->cols - 1);
183 col_step = img2->row_size;
187 col_step = -img2->row_size;
189 block2 += img2->row_size * (img2->rows - 1);
192 col_step = img2->row_size;
194 block2 += 3 * (img2->cols - 1);
197 col_step = -img2->row_size;
199 block2 += img2->row_size * (img2->rows - 1) + 3 * (img2->cols - 1);
204 uns err = (err_sum_transformed(block1, block2, img1->cols, img1->rows, img1->row_size, col_step, row_step) / ((u64)img1->cols * img1->rows));
205 DBG("average error=%d", err);
206 return err <= image_dup_error_threshold;
210 image_dup_compare(struct image_dup *dup1, struct image_dup *dup2, uns flags)
212 DBG("image_dup_compare()");
213 if (!average_compare(dup1, dup2))
215 struct image *img1 = dup1->image;
216 struct image *img2 = dup2->image;
217 if (flags & IMAGE_DUP_SCALE)
219 DBG("Scale support");
220 if (!aspect_ratio_test(img1->cols, img1->rows, img2->cols, img2->rows))
222 if (!aspect_ratio_test(img1->cols, img1->rows, img2->rows, img2->cols))
227 DBG("No scale support");
228 if (!(img1->cols == img2->cols && img1->rows == img2->rows))
230 if (!(img1->cols == img2->rows && img1->rows == img2->cols))
238 uns cols = MIN(dup1->tab_cols, dup2->tab_cols);
239 uns rows = MIN(dup1->tab_rows, dup2->tab_rows);
240 for (uns t = 0; t < 4; t++)
241 if (flags & (1 << t))
243 DBG("Testing trans %d", t);
244 for (uns i = MAX(cols, rows); i--; )
246 uns col = MAX(0, (int)(cols - i));
247 uns row = MAX(0, (int)(rows - i));
248 if (!blocks_compare(dup1, dup2, col, row, t))
251 (img1->cols != img2->cols || img1->rows != img2->rows ||
252 same_size_compare(dup1, dup2, t)))
255 if (!(flags & IMAGE_DUP_WANT_ALL))
265 uns cols = MIN(dup1->tab_cols, dup2->tab_rows);
266 uns rows = MIN(dup1->tab_rows, dup2->tab_cols);
267 for (uns t = 4; t < 8; t++)
268 if (flags & (1 << t))
270 DBG("Testing trans %d", t);
271 for (uns i = MAX(cols, rows); i--; )
273 uns col = MAX(0, (int)(cols - i));
274 uns row = MAX(0, (int)(rows - i));
275 if (!blocks_compare(dup1, dup2, col, row, t))
278 (img1->cols != img2->rows || img1->rows != img2->cols ||
279 same_size_compare(dup1, dup2, t)) )
282 if (!(flags & IMAGE_DUP_WANT_ALL))