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 "lib/mempool.h"
14 #include "lib/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,
33 image_dup_context_cleanup(struct image_dup_context *ctx UNUSED)
37 static uns image_dup_tab_limit = 8;
39 static inline struct image *
40 image_dup_subimage(struct image_context *ctx, struct image_dup *dup, struct image *block, uns tab_col, uns tab_row)
42 return image_init_matrix(ctx, block, image_dup_block(dup, tab_col, tab_row),
43 1 << tab_col, 1 << tab_row, 3 << tab_col, COLOR_SPACE_RGB);
47 pixels_average(byte *dest, byte *src1, byte *src2)
49 dest[0] = ((uns)src1[0] + (uns)src2[0]) >> 1;
50 dest[1] = ((uns)src1[1] + (uns)src2[1]) >> 1;
51 dest[2] = ((uns)src1[2] + (uns)src2[2]) >> 1;
55 image_dup_estimate_size(uns cols, uns rows)
57 uns tab_cols, tab_rows;
58 for (tab_cols = 0; (uns)(2 << tab_cols) < cols && tab_cols < image_dup_tab_limit; tab_cols++);
59 for (tab_rows = 0; (uns)(2 << tab_rows) < rows && tab_rows < image_dup_tab_limit; tab_rows++);
60 uns size = sizeof(struct image_dup) + cols * rows * 3 + (12 << (tab_cols + tab_rows)) + 3 * CPU_STRUCT_ALIGN;
61 return ALIGN_TO(size, CPU_STRUCT_ALIGN);
65 image_dup_new(struct image_context *ctx, struct image *img, void *buffer)
67 DBG("image_dup_init()");
68 ASSERT(!((uintptr_t)buffer & (CPU_STRUCT_ALIGN - 1)));
71 /* Allocate the structure */
72 struct image_dup *dup = ptr;
73 ptr += ALIGN_TO(sizeof(*dup), CPU_STRUCT_ALIGN);
74 bzero(dup, sizeof(*dup));
76 ASSERT((img->flags & IMAGE_PIXEL_FORMAT) == COLOR_SPACE_RGB);
79 if (!image_init_matrix(ctx, &dup->image, ptr, img->cols, img->rows, img->cols * 3, COLOR_SPACE_RGB))
81 uns size = img->rows * img->cols * 3;
82 ptr += ALIGN_TO(size, CPU_STRUCT_ALIGN);
83 byte *s = img->pixels;
84 byte *d = dup->image.pixels;
85 for (uns row = img->rows; row--; )
87 memcpy(d, s, img->row_pixels_size);
88 d += dup->image.row_size;
92 for (dup->tab_cols = 0; (uns)(2 << dup->tab_cols) < img->cols && dup->tab_cols < image_dup_tab_limit; dup->tab_cols++);
93 for (dup->tab_rows = 0; (uns)(2 << dup->tab_rows) < img->rows && dup->tab_rows < image_dup_tab_limit; dup->tab_rows++);
94 dup->tab_row_size = 6 << dup->tab_cols;
95 dup->tab_pixels = ptr;
96 size = 12 << (dup->tab_cols + dup->tab_rows);
97 ptr += ALIGN_TO(size, CPU_STRUCT_ALIGN);
99 /* Scale original image to right bottom block */
102 if (!image_dup_subimage(ctx, dup, &block, dup->tab_cols, dup->tab_rows))
104 if (!image_scale(ctx, &block, img))
108 /* Complete bottom row */
109 for (uns i = dup->tab_cols; i--; )
111 byte *d = image_dup_block(dup, i, dup->tab_rows);
112 byte *s = image_dup_block(dup, i + 1, dup->tab_rows);
113 for (uns y = 0; y < (uns)(1 << dup->tab_rows); y++)
114 for (uns x = 0; x < (uns)(1 << i); x++)
116 pixels_average(d, s, s + 3);
122 /* Complete remaining blocks */
123 for (uns i = 0; i <= dup->tab_cols; i++)
125 uns line_size = (3 << i);
126 for (uns j = dup->tab_rows; j--; )
128 byte *d = image_dup_block(dup, i, j);
129 byte *s = image_dup_block(dup, i, j + 1);
130 for (uns y = 0; y < (uns)(1 << j); y++)
132 for (uns x = 0; x < (uns)(1 << i); x++)
134 pixels_average(d, s, s + line_size);