]> mj.ucw.cz Git - libucw.git/blob - images/dup-init.c
Merge branch 'dev-uint'
[libucw.git] / images / dup-init.c
1 /*
2  *      Image Library -- Duplicates Comparison
3  *
4  *      (c) 2006--2007 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #undef LOCAL_DEBUG
11
12 #include <ucw/lib.h>
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>
18
19 #include <fcntl.h>
20
21 void
22 image_dup_context_init(struct image_context *ic, struct image_dup_context *ctx)
23 {
24   *ctx = (struct image_dup_context) {
25     .ic = ic,
26     .flags = IMAGE_DUP_TRANS_ID,
27     .ratio_threshold = 140,
28     .error_threshold = 100,
29     .qtree_limit = 8,
30   };
31 }
32
33 void
34 image_dup_context_cleanup(struct image_dup_context *ctx UNUSED)
35 {
36 }
37
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)
40 {
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);
43 }
44
45 static inline void
46 pixels_average(byte *dest, byte *src1, byte *src2)
47 {
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;
51 }
52
53 uint
54 image_dup_estimate_size(uint cols, uint rows, uint same_size_compare, uint qtree_limit)
55 {
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);
63 }
64
65 uint
66 image_dup_new(struct image_dup_context *ctx, struct image *img, void *buffer, uint same_size_compare)
67 {
68   DBG("image_dup_init()");
69   ASSERT(!((uintptr_t)buffer & (CPU_STRUCT_ALIGN - 1)));
70   void *ptr = buffer;
71
72   /* Allocate the structure */
73   struct image_dup *dup = ptr;
74   ptr += ALIGN_TO(sizeof(*dup), CPU_STRUCT_ALIGN);
75   bzero(dup, sizeof(*dup));
76
77   ASSERT((img->flags & IMAGE_PIXEL_FORMAT) == COLOR_SPACE_RGB);
78
79   /* Clone image */
80   if (same_size_compare)
81     {
82       if (!image_init_matrix(ctx->ic, &dup->image, ptr, img->cols, img->rows, img->cols * 3, COLOR_SPACE_RGB))
83         return 0;
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--; )
89         {
90           memcpy(d, s, img->row_pixels_size);
91           d += dup->image.row_size;
92           s += img->row_size;
93         }
94     }
95   else
96     {
97       dup->image.cols = img->cols;
98       dup->image.rows = img->rows;
99     }
100
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);
107
108   /* Scale original image to right bottom block */
109   {
110     struct image block;
111     if (!image_dup_subimage(ctx->ic, dup, &block, dup->tab_cols, dup->tab_rows))
112       return 0;
113     if (!image_scale(ctx->ic, &block, img))
114       return 0;
115   }
116
117   /* Complete bottom row */
118   for (uint i = dup->tab_cols; i--; )
119     {
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++)
124           {
125             pixels_average(d, s, s + 3);
126             d += 3;
127             s += 6;
128           }
129     }
130
131   /* Complete remaining blocks */
132   for (uint i = 0; i <= dup->tab_cols; i++)
133     {
134       uint line_size = (3 << i);
135       for (uint j = dup->tab_rows; j--; )
136         {
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++)
140             {
141               for (uint x = 0; x < (uint)(1 << i); x++)
142                 {
143                   pixels_average(d, s, s + line_size);
144                   d += 3;
145                   s += 3;
146                 }
147               s += line_size;
148             }
149         }
150     }
151
152   return ptr - buffer;
153 }