]> mj.ucw.cz Git - libucw.git/blob - images/image-sig.c
Simple image search... under construction
[libucw.git] / images / image-sig.c
1 #define LOCAL_DEBUG
2
3 #include "sherlock/sherlock.h"
4 #include "lib/math.h"
5 #include "lib/fastbuf.h"
6 #include "images/images.h"
7
8 #include <magick/api.h>
9
10 /*
11  * Color spaces
12  * 
13  * http://www.tecgraf.puc-rio.br/~mgattass/color/ColorIndex.html
14  * 
15  */
16
17 #define REF_WHITE_X 0.96422
18 #define REF_WHITE_Y 1.
19 #define REF_WHITE_Z 0.82521
20
21 /* sRGB to XYZ */
22 static void
23 srgb_to_xyz_slow(double srgb[3], double xyz[3])
24 {
25   double a[3];
26   for (uns i = 0; i < 3; i++)
27     if (srgb[i] > 0.04045)
28       a[i] = pow((srgb[i] + 0.055) * (1 / 1.055), 2.4);
29     else
30       a[i] = srgb[i] * (1 / 12.92);
31   xyz[0] = 0.412424 * a[0] + 0.357579 * a[1] + 0.180464 * a[2];
32   xyz[1] = 0.212656 * a[0] + 0.715158 * a[1] + 0.072186 * a[2];
33   xyz[2] = 0.019332 * a[0] + 0.119193 * a[1] + 0.950444 * a[2];
34 }
35
36 /* XYZ to CIE-Luv */
37 static void
38 xyz_to_luv_slow(double xyz[3], double luv[3])
39 {
40   double sum = xyz[0] + 15 * xyz[1] + 3 * xyz[2];
41   if (sum < 0.000001)
42     luv[0] = luv[1] = luv[2] = 0;
43   else
44     {
45       double var_u = 4 * xyz[0] / sum;
46       double var_v = 9 * xyz[1] / sum;
47       if (xyz[1] > 0.008856)
48         luv[0] = 116 * pow(xyz[1], 1 / 3.) - 16;
49       else
50         luv[0] = (116 * 7.787) * xyz[1];
51       luv[1] = luv[0] * (13 * (var_u - 4 * REF_WHITE_X / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z)));
52       luv[2] = luv[0] * (13 * (var_v - 9 * REF_WHITE_Y / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z)));
53       /* intervals [0..100], [-134..220], [-140..122] */
54     }
55 }
56
57 struct block {
58   uns l, u, v;          /* average Luv coefficients */
59   uns lh, hl, hh;       /* energies in Daubechies wavelet bands */
60 };
61
62 static void
63 compute_image_area_signature(PixelPacket *pixels, uns width, uns height, struct image_signature *sig)
64 {
65   ASSERT(width >= 4 && height >= 4);
66
67   uns w = width >> 2;
68   uns h = height >> 2;
69   DBG("Computing signature for image %dx%d... %dx%d blocks", width, height, w, h);
70   uns blocks_count = w * h;
71   struct block *blocks = xmalloc(blocks_count * sizeof(struct block)), *block = blocks; /* FIXME: use mempool */
72   
73   /* Every 4x4 block (FIXME: deal with smaller blocks near the edges) */
74   PixelPacket *p = pixels;
75   for (uns block_y = 0; block_y < h; block_y++, p += width & 3 + width * 3)
76     for (uns block_x = 0; block_x < w; block_x++, p -= 4 * width - 4, block++)
77       {
78         int t[16], s[16], *tp = t;
79         
80         /* Convert pixels to Luv color space and compute average coefficients 
81          * FIXME:
82          * - could be MUCH faster with precomputed tables and integer arithmetic... 
83          *   I will propably use interpolation in 3-dim array */
84         uns l_sum = 0;
85         uns u_sum = 0;
86         uns v_sum = 0;
87         for (uns y = 0; y < 4; y++, p += width - 4)
88           for (uns x = 0; x < 4; x++, p++)
89             {
90               double rgb[3], luv[3], xyz[3];
91               rgb[0] = (p->red >> (QuantumDepth - 8)) / 255.;
92               rgb[1] = (p->green >> (QuantumDepth - 8)) / 255.;
93               rgb[2] = (p->blue >> (QuantumDepth - 8)) / 255.;
94               srgb_to_xyz_slow(rgb, xyz);
95               xyz_to_luv_slow(xyz, luv);
96               l_sum += *tp++ = luv[0];
97               u_sum += luv[1] + 150;
98               v_sum += luv[2] + 150;
99             }
100
101         block->l = l_sum;
102         block->u = u_sum;
103         block->v = v_sum;
104         
105         /* Apply Daubechies wavelet transformation 
106          * FIXME:
107          * - MMX/SSE instructions or tables could be faster 
108          * - maybe it would be better to compute Luv and wavelet separately because of processor cache or MMX/SSE 
109          * - eliminate slow square roots 
110          * - what about Haar transformation? */
111
112 #define DAUB_0  31651 /* (1 + sqrt 3) / (4 * sqrt 2) */
113 #define DAUB_1  54822 /* (3 + sqrt 3) / (4 * sqrt 2) */
114 #define DAUB_2  14689 /* (3 - sqrt 3) / (4 * sqrt 2) */
115 #define DAUB_3  -8481 /* (1 - sqrt 3) / (4 * sqrt 2) */
116
117         /* ... to the rows */
118         uns i;
119         for (i = 0; i < 16; i += 4)
120           {
121             s[i + 0] = (DAUB_0 * t[i + 2] + DAUB_1 * t[i + 3] + DAUB_2 * t[i + 0] + DAUB_3 * t[i + 1]) / 0x10000;
122             s[i + 1] = (DAUB_0 * t[i + 0] + DAUB_1 * t[i + 1] + DAUB_2 * t[i + 2] + DAUB_3 * t[i + 3]) / 0x10000;
123             s[i + 2] = (DAUB_3 * t[i + 2] - DAUB_2 * t[i + 3] + DAUB_1 * t[i + 0] - DAUB_0 * t[i + 1]) / 0x10000;
124             s[i + 3] = (DAUB_3 * t[i + 0] - DAUB_2 * t[i + 1] + DAUB_1 * t[i + 2] - DAUB_0 * t[i + 3]) / 0x10000;
125           }
126
127         /* ... and to the columns... skip LL band */
128         for (i = 0; i < 2; i++)
129           {
130             t[i + 8] = (DAUB_3 * s[i + 8] - DAUB_2 * s[i +12] + DAUB_1 * s[i + 0] - DAUB_0 * s[i + 4]) / 0x1000;
131             t[i +12] = (DAUB_3 * s[i + 0] - DAUB_2 * s[i + 4] + DAUB_1 * s[i + 8] - DAUB_0 * s[i +12]) / 0x1000;
132           }
133         for (; i < 4; i++)
134           {
135             t[i + 0] = (DAUB_0 * s[i + 8] + DAUB_1 * s[i +12] + DAUB_2 * s[i + 0] + DAUB_3 * s[i + 4]) / 0x1000;
136             t[i + 4] = (DAUB_0 * s[i + 0] + DAUB_1 * s[i + 4] + DAUB_2 * s[i + 8] + DAUB_3 * s[i +12]) / 0x1000;
137             t[i + 8] = (DAUB_3 * s[i + 8] - DAUB_2 * s[i +12] + DAUB_1 * s[i + 0] - DAUB_0 * s[i + 4]) / 0x1000;
138             t[i +12] = (DAUB_3 * s[i + 0] - DAUB_2 * s[i + 4] + DAUB_1 * s[i + 8] - DAUB_0 * s[i +12]) / 0x1000;
139           }
140
141         /* Extract energies in LH, HL and HH bands */
142         block->lh = sqrt(t[8] * t[8] + t[9] * t[9] + t[12] * t[12] + t[13] * t[13]);
143         block->hl = sqrt(t[2] * t[2] + t[3] * t[3] + t[6] * t[6] + t[7] * t[7]);
144         block->hh = sqrt(t[10] * t[10] + t[11] * t[11] + t[14] * t[14] + t[15] * t[15]);
145       }
146
147   /* FIXME: simple average is for testing pusposes only */
148   uns l_sum = 0;
149   uns u_sum = 0;
150   uns v_sum = 0;
151   uns lh_sum = 0;
152   uns hl_sum = 0;
153   uns hh_sum = 0;
154   for (uns i = 0; i < blocks_count; i++)
155     {
156       l_sum += blocks[i].l;
157       u_sum += blocks[i].u;
158       v_sum += blocks[i].v;
159       lh_sum += blocks[i].lh;
160       hl_sum += blocks[i].hl;
161       hh_sum += blocks[i].hh;
162     }
163
164   sig->vec.f[0] = l_sum / blocks_count;
165   sig->vec.f[1] = u_sum / blocks_count;
166   sig->vec.f[2] = v_sum / blocks_count;
167   sig->vec.f[3] = lh_sum / blocks_count;
168   sig->vec.f[4] = hl_sum / blocks_count;
169   sig->vec.f[5] = hh_sum / blocks_count;
170
171   sig->len = 0;
172
173   xfree(blocks);
174
175   DBG("Resulting signature is (%s)", stk_print_image_vector(&sig->vec));
176 }
177
178 static ExceptionInfo exception;
179 static QuantizeInfo quantize_info;
180 static ImageInfo *image_info;
181
182 void
183 compute_image_signature_prepare(void)
184 {
185   InitializeMagick(NULL); 
186   GetExceptionInfo(&exception);
187   image_info = CloneImageInfo(NULL);
188   image_info->subrange = 1;
189   GetQuantizeInfo(&quantize_info);
190   quantize_info.colorspace = RGBColorspace;
191 }
192
193 void
194 compute_image_signature_finish(void)
195 {
196   DestroyImageInfo(image_info);
197   DestroyExceptionInfo(&exception);
198   DestroyMagick();
199 }
200
201 int
202 compute_image_signature(void *data, uns len, struct image_signature *sig)
203 {
204   Image *image = BlobToImage(image_info, data, len, &exception); /* Damn slow... takes most of CPU time :-/ */
205   if (!image)
206     die("Invalid image format");
207   if (image->columns < 4 || image->rows < 4)
208     {
209       DBG("Image too small (%dx%d)", (int)image->columns, (int)image->rows);
210       DestroyImage(image);
211       return -1;
212     }
213   QuantizeImage(&quantize_info, image); /* Also slow... and propably not necessary... */
214   PixelPacket *pixels = (PixelPacket *) AcquireImagePixels(image, 0, 0, image->columns, image->rows, &exception);
215   compute_image_area_signature(pixels, image->columns, image->rows, sig);
216   DestroyImage(image);
217   return 0;
218 }
219