2 * Image Library -- Duplicates Comparison
4 * (c) 2006--2007 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 <ucw/mempool.h>
14 #include <ucw/fastbuf.h>
15 #include <images/images.h>
16 #include <images/color.h>
17 #include <images/duplicates.h>
22 image_dup_context_init(struct image_context *ic, struct image_dup_context *ctx)
24 *ctx = (struct image_dup_context) {
26 .flags = IMAGE_DUP_TRANS_ID,
27 .ratio_threshold = 140,
28 .error_threshold = 100,
34 image_dup_context_cleanup(struct image_dup_context *ctx UNUSED)
38 static inline struct image *
39 image_dup_subimage(struct image_context *ctx, struct image_dup *dup, struct image *block, uint tab_col, uint tab_row)
41 return image_init_matrix(ctx, block, image_dup_block(dup, tab_col, tab_row),
42 1 << tab_col, 1 << tab_row, 3 << tab_col, COLOR_SPACE_RGB);
46 pixels_average(byte *dest, byte *src1, byte *src2)
48 dest[0] = ((uint)src1[0] + (uint)src2[0]) >> 1;
49 dest[1] = ((uint)src1[1] + (uint)src2[1]) >> 1;
50 dest[2] = ((uint)src1[2] + (uint)src2[2]) >> 1;
54 image_dup_estimate_size(uint cols, uint rows, uint same_size_compare, uint qtree_limit)
56 uint tab_cols, tab_rows;
57 for (tab_cols = 0; (uint)(2 << tab_cols) < cols && tab_cols < qtree_limit; tab_cols++);
58 for (tab_rows = 0; (uint)(2 << tab_rows) < rows && tab_rows < qtree_limit; tab_rows++);
59 uint size = sizeof(struct image_dup) + (12 << (tab_cols + tab_rows)) + 2 * CPU_STRUCT_ALIGN;
60 if (same_size_compare)
61 size += cols * rows * 3 + CPU_STRUCT_ALIGN;
62 return ALIGN_TO(size, CPU_STRUCT_ALIGN);
66 image_dup_new(struct image_dup_context *ctx, struct image *img, void *buffer, uint same_size_compare)
68 DBG("image_dup_init()");
69 ASSERT(!((uintptr_t)buffer & (CPU_STRUCT_ALIGN - 1)));
72 /* Allocate the structure */
73 struct image_dup *dup = ptr;
74 ptr += ALIGN_TO(sizeof(*dup), CPU_STRUCT_ALIGN);
75 bzero(dup, sizeof(*dup));
77 ASSERT((img->flags & IMAGE_PIXEL_FORMAT) == COLOR_SPACE_RGB);
80 if (same_size_compare)
82 if (!image_init_matrix(ctx->ic, &dup->image, ptr, img->cols, img->rows, img->cols * 3, COLOR_SPACE_RGB))
84 uint size = img->rows * img->cols * 3;
85 ptr += ALIGN_TO(size, CPU_STRUCT_ALIGN);
86 byte *s = img->pixels;
87 byte *d = dup->image.pixels;
88 for (uint row = img->rows; row--; )
90 memcpy(d, s, img->row_pixels_size);
91 d += dup->image.row_size;
97 dup->image.cols = img->cols;
98 dup->image.rows = img->rows;
101 for (dup->tab_cols = 0; (uint)(2 << dup->tab_cols) < img->cols && dup->tab_cols < ctx->qtree_limit; dup->tab_cols++);
102 for (dup->tab_rows = 0; (uint)(2 << dup->tab_rows) < img->rows && dup->tab_rows < ctx->qtree_limit; dup->tab_rows++);
103 dup->tab_row_size = 6 << dup->tab_cols;
104 dup->tab_pixels = ptr;
105 uint size = 12 << (dup->tab_cols + dup->tab_rows);
106 ptr += ALIGN_TO(size, CPU_STRUCT_ALIGN);
108 /* Scale original image to right bottom block */
111 if (!image_dup_subimage(ctx->ic, dup, &block, dup->tab_cols, dup->tab_rows))
113 if (!image_scale(ctx->ic, &block, img))
117 /* Complete bottom row */
118 for (uint i = dup->tab_cols; i--; )
120 byte *d = image_dup_block(dup, i, dup->tab_rows);
121 byte *s = image_dup_block(dup, i + 1, dup->tab_rows);
122 for (uint y = 0; y < (uint)(1 << dup->tab_rows); y++)
123 for (uint x = 0; x < (uint)(1 << i); x++)
125 pixels_average(d, s, s + 3);
131 /* Complete remaining blocks */
132 for (uint i = 0; i <= dup->tab_cols; i++)
134 uint line_size = (3 << i);
135 for (uint j = dup->tab_rows; j--; )
137 byte *d = image_dup_block(dup, i, j);
138 byte *s = image_dup_block(dup, i, j + 1);
139 for (uint y = 0; y < (uint)(1 << j); y++)
141 for (uint x = 0; x < (uint)(1 << i); x++)
143 pixels_average(d, s, s + line_size);