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.
13 #include "lib/mempool.h"
14 #include "lib/fastbuf.h"
15 #include "images/images.h"
16 #include "images/duplicates.h"
28 err_sum(byte *pos1, byte *pos2, uns count)
33 uns e = err(*pos1++, *pos2++);
34 e += err(*pos1++, *pos2++);
35 e += err(*pos1++, *pos2++);
42 err_sum_transformed(byte *pos1, byte *pos2, uns cols, uns rows, int row_step_1, int col_step_2, int row_step_2)
44 DBG("err_sum_transformed(pos1=%p pos2=%p cols=%u rows=%u row_step_1=%d col_step_2=%d row_step_2=%d)",
45 pos1, pos2, cols, rows, row_step_1, col_step_2, row_step_2);
47 for (uns j = rows; j--; )
52 for (uns i = cols; i--; )
54 e += err(p1[0], p2[0]);
55 e += err(p1[1], p2[1]);
56 e += err(p1[2], p2[2]);
68 aspect_ratio_test(struct image_dup_context *ctx, uns cols1, uns rows1, uns cols2, uns rows2)
70 DBG("aspect_ratio_test(cols1=%u rows1=%u cols2=%u rows2=%u)", cols1, rows1, cols2, rows2);
71 uns r1 = cols1 * rows2;
72 uns r2 = rows1 * cols2;
74 r1 <= ((r2 * ctx->ratio_threshold) >> 7) &&
75 r2 <= ((r1 * ctx->ratio_threshold) >> 7);
79 average_compare(struct image_dup_context *ctx, struct image_dup *dup1, struct image_dup *dup2)
81 byte *block1 = image_dup_block(dup1, 0, 0);
82 byte *block2 = image_dup_block(dup2, 0, 0);
84 err(block1[0], block2[0]) +
85 err(block1[1], block2[1]) +
86 err(block1[2], block2[2]);
87 return e <= ctx->error_threshold;
91 blocks_compare(struct image_dup_context *ctx, struct image_dup *dup1, struct image_dup *dup2, uns tab_col, uns tab_row, uns trans)
93 DBG("blocks_compare(tab_col=%d tab_row=%d trans=%d)", tab_col, tab_row, trans);
94 ctx->sum_pixels += 1 << (tab_col + tab_row);
95 byte *block1 = image_dup_block(dup1, tab_col, tab_row);
97 int col_step, row_step;
99 block2 = image_dup_block(dup2, tab_col, tab_row);
101 block2 = image_dup_block(dup2, tab_row, tab_col);
105 uns err = (err_sum(block1, block2, 1 << (tab_col + tab_row)) >> (tab_col + tab_row));
106 DBG("average error=%d", err);
107 return err <= ctx->error_threshold;
110 row_step = (3 << tab_col);
111 block2 += row_step - 3;
115 row_step = -(3 << tab_col);
116 block2 += (3 << (tab_col + tab_row)) + row_step;
120 row_step = -(3 << tab_col);
121 block2 += (3 << (tab_col + tab_row)) - 3;
124 col_step = (3 << tab_row);
128 col_step = -(3 << tab_row);
130 block2 += (3 << (tab_col + tab_row)) + col_step;
133 col_step = (3 << tab_row);
135 block2 += col_step - 3;
138 col_step = -(3 << tab_row);
140 block2 += (3 << (tab_col + tab_row)) - 3;
145 uns err = (err_sum_transformed(block1, block2, (1 << tab_col), (1 << tab_row), (3 << tab_col), col_step, row_step) >> (tab_col + tab_row));
146 DBG("average error=%d", err);
147 return err <= ctx->error_threshold;
151 same_size_compare(struct image_dup_context *ctx, struct image_dup *dup1, struct image_dup *dup2, uns trans)
153 struct image *img1 = &dup1->image;
154 struct image *img2 = &dup2->image;
155 ctx->sum_pixels += img1->cols * img1->rows;
156 byte *block1 = img1->pixels;
157 byte *block2 = img2->pixels;
158 int col_step, row_step;
159 DBG("same_size_compare(trans=%d)", trans);
164 row_step = img2->row_size;
168 row_step = img2->row_size;
169 block2 += 3 * (img2->cols - 1);
173 row_step = -img2->row_size;
174 block2 += img2->row_size * (img2->rows - 1);
178 row_step = -img2->row_size;
179 block2 += img2->row_size * (img2->rows - 1) + 3 * (img2->cols - 1);
182 col_step = img2->row_size;
186 col_step = -img2->row_size;
188 block2 += img2->row_size * (img2->rows - 1);
191 col_step = img2->row_size;
193 block2 += 3 * (img2->cols - 1);
196 col_step = -img2->row_size;
198 block2 += img2->row_size * (img2->rows - 1) + 3 * (img2->cols - 1);
203 uns err = (err_sum_transformed(block1, block2, img1->cols, img1->rows, img1->row_size, col_step, row_step) / ((u64)img1->cols * img1->rows));
204 DBG("average error=%d", err);
205 return err <= ctx->error_threshold;
209 image_dup_compare(struct image_dup_context *ctx, struct image_dup *dup1, struct image_dup *dup2)
211 DBG("image_dup_compare()");
212 if (!average_compare(ctx, dup1, dup2))
214 struct image *img1 = &dup1->image;
215 struct image *img2 = &dup2->image;
216 uns flags = ctx->flags;
217 if (flags & IMAGE_DUP_SCALE)
219 DBG("Scale support");
220 if (!aspect_ratio_test(ctx, img1->cols, img1->rows, img2->cols, img2->rows))
222 if (!aspect_ratio_test(ctx, 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 uns i = MAX(cols, rows), depth = 1;
248 uns col = MAX(0, (int)(cols - i));
249 uns row = MAX(0, (int)(rows - i));
250 if (!blocks_compare(ctx, dup1, dup2, col, row, t))
253 (img1->cols != img2->cols || img1->rows != img2->rows ||
254 same_size_compare(ctx, dup1, dup2, t)))
257 if (!(flags & IMAGE_DUP_WANT_ALL))
263 ctx->sum_depth += depth;
268 uns cols = MIN(dup1->tab_cols, dup2->tab_rows);
269 uns rows = MIN(dup1->tab_rows, dup2->tab_cols);
270 for (uns t = 4; t < 8; t++)
271 if (flags & (1 << t))
273 DBG("Testing trans %d", t);
274 uns i = MAX(cols, rows), depth = 1;
278 uns col = MAX(0, (int)(cols - i));
279 uns row = MAX(0, (int)(rows - i));
280 if (!blocks_compare(ctx, dup1, dup2, col, row, t))
283 (img1->cols != img2->rows || img1->rows != img2->cols ||
284 same_size_compare(ctx, dup1, dup2, t)) )
287 if (!(flags & IMAGE_DUP_WANT_ALL))
293 ctx->sum_depth += depth;