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_tab_limit = 8;
22 static inline struct image *
23 image_dup_subimage(struct image_thread *thread, struct image_dup *dup, struct image *block, uns tab_col, uns tab_row)
25 return image_init_matrix(thread, block, image_dup_block(dup, tab_col, tab_row),
26 1 << tab_col, 1 << tab_row, 3 << tab_col, COLOR_SPACE_RGB);
30 pixels_average(byte *dest, byte *src1, byte *src2)
32 dest[0] = ((uns)src1[0] + (uns)src2[0]) >> 1;
33 dest[1] = ((uns)src1[1] + (uns)src2[1]) >> 1;
34 dest[2] = ((uns)src1[2] + (uns)src2[2]) >> 1;
38 image_dup_estimate_size(uns cols, uns rows)
40 uns tab_cols, tab_rows;
41 for (tab_cols = 0; (uns)(2 << tab_cols) < cols && tab_cols < image_dup_tab_limit; tab_cols++);
42 for (tab_rows = 0; (uns)(2 << tab_rows) < rows && tab_rows < image_dup_tab_limit; tab_rows++);
43 return sizeof(struct image) + cols * rows * 3 + sizeof(struct image_dup) + (12 << (tab_cols + tab_rows)) + 64;
47 image_dup_init(struct image_thread *thread, struct image_dup *dup, struct image *img, struct mempool *pool)
49 DBG("image_dup_init()");
51 ASSERT((img->flags & IMAGE_PIXEL_FORMAT) == COLOR_SPACE_RGB);
54 for (dup->tab_cols = 0; (uns)(2 << dup->tab_cols) < img->cols && dup->tab_cols < image_dup_tab_limit; dup->tab_cols++);
55 for (dup->tab_rows = 0; (uns)(2 << dup->tab_rows) < img->rows && dup->tab_rows < image_dup_tab_limit; dup->tab_rows++);
56 dup->tab_pixels = mp_alloc(pool, dup->tab_size = (12 << (dup->tab_cols + dup->tab_rows)));
57 dup->tab_row_size = 6 << dup->tab_cols;
59 /* Scale original image to right bottom block */
62 if (!image_dup_subimage(thread, dup, &block, dup->tab_cols, dup->tab_rows))
64 if (!image_scale(thread, &block, img))
68 /* Complete bottom row */
69 for (uns i = dup->tab_cols; i--; )
71 byte *d = image_dup_block(dup, i, dup->tab_rows);
72 byte *s = image_dup_block(dup, i + 1, dup->tab_rows);
73 for (uns y = 0; y < (uns)(1 << dup->tab_rows); y++)
74 for (uns x = 0; x < (uns)(1 << i); x++)
76 pixels_average(d, s, s + 3);
82 /* Complete remaining blocks */
83 for (uns i = 0; i <= dup->tab_cols; i++)
85 uns line_size = (3 << i);
86 for (uns j = dup->tab_rows; j--; )
88 byte *d = image_dup_block(dup, i, j);
89 byte *s = image_dup_block(dup, i, j + 1);
90 for (uns y = 0; y < (uns)(1 << j); y++)
92 for (uns x = 0; x < (uns)(1 << i); x++)
94 pixels_average(d, s, s + line_size);