]> mj.ucw.cz Git - libucw.git/blob - images/dup-init.c
fixes in libimages Makefile
[libucw.git] / images / dup-init.c
1 /*
2  *      Image Library -- Duplicates Comparison
3  *
4  *      (c) 2006 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 "sherlock/sherlock.h"
13 #include "lib/mempool.h"
14 #include "lib/fastbuf.h"
15 #include "images/images.h"
16 #include "images/duplicates.h"
17
18 #include <fcntl.h>
19
20 static uns image_dup_tab_limit = 8;
21
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)
24 {
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);
27 }
28
29 static inline void
30 pixels_average(byte *dest, byte *src1, byte *src2)
31 {
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;
35 }
36
37 uns
38 image_dup_estimate_size(uns cols, uns rows)
39 {
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;
44 }
45
46 uns
47 image_dup_init(struct image_thread *thread, struct image_dup *dup, struct image *img, struct mempool *pool)
48 {
49   DBG("image_dup_init()");
50
51   ASSERT((img->flags & IMAGE_PIXEL_FORMAT) == COLOR_SPACE_RGB);
52
53   dup->image = img;
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;
58
59   /* Scale original image to right bottom block */
60   {
61     struct image block;
62     if (!image_dup_subimage(thread, dup, &block, dup->tab_cols, dup->tab_rows))
63       return 0;
64     if (!image_scale(thread, &block, img))
65       return 0;
66   }
67
68   /* Complete bottom row */
69   for (uns i = dup->tab_cols; i--; )
70     {
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++)
75           {
76             pixels_average(d, s, s + 3);
77             d += 3;
78             s += 6;
79           }
80     }
81
82   /* Complete remaining blocks */
83   for (uns i = 0; i <= dup->tab_cols; i++)
84     {
85       uns line_size = (3 << i);
86       for (uns j = dup->tab_rows; j--; )
87         {
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++)
91             {
92               for (uns x = 0; x < (uns)(1 << i); x++)
93                 {
94                   pixels_average(d, s, s + line_size);
95                   d += 3;
96                   s += 3;
97                 }
98               s += line_size;
99             }
100         }
101     }
102
103   return 1;
104 }