3 #include "sherlock/sherlock.h"
5 #include "lib/fastbuf.h"
6 #include "images/images.h"
11 * http://www.tecgraf.puc-rio.br/~mgattass/color/ColorIndex.html
15 #define REF_WHITE_X 0.96422
16 #define REF_WHITE_Y 1.
17 #define REF_WHITE_Z 0.82521
21 srgb_to_xyz_slow(double srgb[3], double xyz[3])
24 for (uns i = 0; i < 3; i++)
25 if (srgb[i] > 0.04045)
26 a[i] = pow((srgb[i] + 0.055) * (1 / 1.055), 2.4);
28 a[i] = srgb[i] * (1 / 12.92);
29 xyz[0] = 0.412424 * a[0] + 0.357579 * a[1] + 0.180464 * a[2];
30 xyz[1] = 0.212656 * a[0] + 0.715158 * a[1] + 0.072186 * a[2];
31 xyz[2] = 0.019332 * a[0] + 0.119193 * a[1] + 0.950444 * a[2];
36 xyz_to_luv_slow(double xyz[3], double luv[3])
38 double sum = xyz[0] + 15 * xyz[1] + 3 * xyz[2];
40 luv[0] = luv[1] = luv[2] = 0;
43 double var_u = 4 * xyz[0] / sum;
44 double var_v = 9 * xyz[1] / sum;
45 if (xyz[1] > 0.008856)
46 luv[0] = 116 * pow(xyz[1], 1 / 3.) - 16;
48 luv[0] = (116 * 7.787) * xyz[1];
49 luv[1] = luv[0] * (13 * (var_u - 4 * REF_WHITE_X / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z)));
50 luv[2] = luv[0] * (13 * (var_v - 9 * REF_WHITE_Y / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z)));
51 /* intervals [0..100], [-134..220], [-140..122] */
56 uns l, u, v; /* average Luv coefficients */
57 uns lh, hl, hh; /* energies in Daubechies wavelet bands */
61 compute_image_area_signature(PixelPacket *pixels, uns width, uns height, struct image_signature *sig)
63 ASSERT(width >= 4 && height >= 4);
67 DBG("Computing signature for image %dx%d... %dx%d blocks", width, height, w, h);
68 uns blocks_count = w * h;
69 struct block *blocks = xmalloc(blocks_count * sizeof(struct block)), *block = blocks; /* FIXME: use mempool */
71 /* Every 4x4 block (FIXME: deal with smaller blocks near the edges) */
72 PixelPacket *p = pixels;
73 for (uns block_y = 0; block_y < h; block_y++, p += width & 3 + width * 3)
74 for (uns block_x = 0; block_x < w; block_x++, p -= 4 * width - 4, block++)
76 int t[16], s[16], *tp = t;
78 /* Convert pixels to Luv color space and compute average coefficients
80 * - could be MUCH faster with precomputed tables and integer arithmetic...
81 * I will propably use interpolation in 3-dim array */
85 for (uns y = 0; y < 4; y++, p += width - 4)
86 for (uns x = 0; x < 4; x++, p++)
88 double rgb[3], luv[3], xyz[3];
89 rgb[0] = (p->red >> (QuantumDepth - 8)) / 255.;
90 rgb[1] = (p->green >> (QuantumDepth - 8)) / 255.;
91 rgb[2] = (p->blue >> (QuantumDepth - 8)) / 255.;
92 srgb_to_xyz_slow(rgb, xyz);
93 xyz_to_luv_slow(xyz, luv);
94 l_sum += *tp++ = luv[0];
95 u_sum += luv[1] + 150;
96 v_sum += luv[2] + 150;
103 /* Apply Daubechies wavelet transformation
105 * - MMX/SSE instructions or tables could be faster
106 * - maybe it would be better to compute Luv and wavelet separately because of processor cache or MMX/SSE
107 * - eliminate slow square roots
108 * - what about Haar transformation? */
110 #define DAUB_0 31651 /* (1 + sqrt 3) / (4 * sqrt 2) */
111 #define DAUB_1 54822 /* (3 + sqrt 3) / (4 * sqrt 2) */
112 #define DAUB_2 14689 /* (3 - sqrt 3) / (4 * sqrt 2) */
113 #define DAUB_3 -8481 /* (1 - sqrt 3) / (4 * sqrt 2) */
115 /* ... to the rows */
117 for (i = 0; i < 16; i += 4)
119 s[i + 0] = (DAUB_0 * t[i + 2] + DAUB_1 * t[i + 3] + DAUB_2 * t[i + 0] + DAUB_3 * t[i + 1]) / 0x10000;
120 s[i + 1] = (DAUB_0 * t[i + 0] + DAUB_1 * t[i + 1] + DAUB_2 * t[i + 2] + DAUB_3 * t[i + 3]) / 0x10000;
121 s[i + 2] = (DAUB_3 * t[i + 2] - DAUB_2 * t[i + 3] + DAUB_1 * t[i + 0] - DAUB_0 * t[i + 1]) / 0x10000;
122 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 /* ... and to the columns... skip LL band */
126 for (i = 0; i < 2; i++)
128 t[i + 8] = (DAUB_3 * s[i + 8] - DAUB_2 * s[i +12] + DAUB_1 * s[i + 0] - DAUB_0 * s[i + 4]) / 0x1000;
129 t[i +12] = (DAUB_3 * s[i + 0] - DAUB_2 * s[i + 4] + DAUB_1 * s[i + 8] - DAUB_0 * s[i +12]) / 0x1000;
133 t[i + 0] = (DAUB_0 * s[i + 8] + DAUB_1 * s[i +12] + DAUB_2 * s[i + 0] + DAUB_3 * s[i + 4]) / 0x1000;
134 t[i + 4] = (DAUB_0 * s[i + 0] + DAUB_1 * s[i + 4] + DAUB_2 * s[i + 8] + DAUB_3 * s[i +12]) / 0x1000;
135 t[i + 8] = (DAUB_3 * s[i + 8] - DAUB_2 * s[i +12] + DAUB_1 * s[i + 0] - DAUB_0 * s[i + 4]) / 0x1000;
136 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 /* Extract energies in LH, HL and HH bands */
140 block->lh = sqrt(t[8] * t[8] + t[9] * t[9] + t[12] * t[12] + t[13] * t[13]);
141 block->hl = sqrt(t[2] * t[2] + t[3] * t[3] + t[6] * t[6] + t[7] * t[7]);
142 block->hh = sqrt(t[10] * t[10] + t[11] * t[11] + t[14] * t[14] + t[15] * t[15]);
145 /* FIXME: simple average is for testing pusposes only */
152 for (uns i = 0; i < blocks_count; i++)
154 l_sum += blocks[i].l;
155 u_sum += blocks[i].u;
156 v_sum += blocks[i].v;
157 lh_sum += blocks[i].lh;
158 hl_sum += blocks[i].hl;
159 hh_sum += blocks[i].hh;
162 sig->vec.f[0] = l_sum / blocks_count;
163 sig->vec.f[1] = u_sum / blocks_count;
164 sig->vec.f[2] = v_sum / blocks_count;
165 sig->vec.f[3] = lh_sum / blocks_count;
166 sig->vec.f[4] = hl_sum / blocks_count;
167 sig->vec.f[5] = hh_sum / blocks_count;
173 DBG("Resulting signature is (%s)", stk_print_image_vector(&sig->vec));
176 static ExceptionInfo exception;
177 static QuantizeInfo quantize_info;
178 static ImageInfo *image_info;
181 compute_image_signature_prepare(void)
183 InitializeMagick(NULL);
184 GetExceptionInfo(&exception);
185 image_info = CloneImageInfo(NULL);
186 image_info->subrange = 1;
187 GetQuantizeInfo(&quantize_info);
188 quantize_info.colorspace = RGBColorspace;
192 compute_image_signature_finish(void)
194 DestroyImageInfo(image_info);
195 DestroyExceptionInfo(&exception);
200 compute_image_signature(void *data, uns len, struct image_signature *sig)
202 Image *image = BlobToImage(image_info, data, len, &exception); /* Damn slow... takes most of CPU time :-/ */
204 die("Invalid image format");
205 if (image->columns < 4 || image->rows < 4)
207 DBG("Image too small (%dx%d)", (int)image->columns, (int)image->rows);
211 QuantizeImage(&quantize_info, image); /* Also slow... and propably not necessary... */
212 PixelPacket *pixels = (PixelPacket *) AcquireImagePixels(image, 0, 0, image->columns, image->rows, &exception);
213 compute_image_area_signature(pixels, image->columns, image->rows, sig);