]> mj.ucw.cz Git - libucw.git/blob - images/color.c
do not ignore incomplete blocks near the edges
[libucw.git] / images / color.c
1 /*
2  *      Image Library -- Color Spaces
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/math.h"
14 #include "images/images.h"
15 #include "images/color.h"
16
17 struct color color_black = { .color_space = COLOR_SPACE_GRAYSCALE };
18 struct color color_white = { .c = { 255 }, .color_space = COLOR_SPACE_GRAYSCALE };
19
20 inline void
21 color_put_grayscale(byte *dest, struct color *color)
22 {
23   switch (color->color_space)
24     {
25       case COLOR_SPACE_GRAYSCALE:
26         dest[0] = color->c[0];
27         break;
28       case COLOR_SPACE_RGB:
29         dest[0] = rgb_to_gray_func(color->c[0], color->c[1], color->c[2]);
30         break;
31       default:
32         ASSERT(0);      
33     }
34 }
35
36 inline void
37 color_put_rgb(byte *dest, struct color *color)
38 {
39   switch (color->color_space)
40     {
41       case COLOR_SPACE_GRAYSCALE:
42         dest[0] = dest[1] = dest[2] = color->c[0];
43         break;
44       case COLOR_SPACE_RGB:
45         dest[0] = color->c[0];
46         dest[1] = color->c[1];
47         dest[2] = color->c[2];
48         break;
49       default:
50         ASSERT(0);      
51     }
52 }
53
54 void
55 color_put_color_space(byte *dest, struct color *color, enum color_space color_space)
56 {
57   switch (color_space)
58     {
59       case COLOR_SPACE_GRAYSCALE:
60         color_put_grayscale(dest, color);
61         break;  
62       case COLOR_SPACE_RGB:
63         color_put_rgb(dest, color);     
64         break;
65       default:
66         ASSERT(0);      
67     }
68 }
69
70 /********************* EXACT CONVERSION ROUTINES **********************/
71
72 /* sRGB to XYZ */
73 void
74 srgb_to_xyz_slow(double xyz[3], double srgb[3])
75 {
76   double a[3];
77   for (uns i = 0; i < 3; i++)
78     if (srgb[i] > 0.04045)
79       a[i] = pow((srgb[i] + 0.055) * (1 / 1.055), 2.4);
80     else
81       a[i] = srgb[i] * (1 / 12.92);
82   xyz[0] = SRGB_XYZ_XR * a[0] + SRGB_XYZ_XG * a[1] + SRGB_XYZ_XB * a[2];
83   xyz[1] = SRGB_XYZ_YR * a[0] + SRGB_XYZ_YG * a[1] + SRGB_XYZ_YB * a[2];
84   xyz[2] = SRGB_XYZ_ZR * a[0] + SRGB_XYZ_ZG * a[1] + SRGB_XYZ_ZB * a[2];
85 }
86
87 /* XYZ to CIE-Luv */
88 void
89 xyz_to_luv_slow(double luv[3], double xyz[3])
90 {
91   double sum = xyz[0] + 15 * xyz[1] + 3 * xyz[2];
92   if (sum < 0.000001)
93     luv[0] = luv[1] = luv[2] = 0;
94   else
95     {
96       double var_u = 4 * xyz[0] / sum;
97       double var_v = 9 * xyz[1] / sum;
98       if (xyz[1] > 0.008856)
99         luv[0] = 116 * pow(xyz[1], 1 / 3.) - 16;
100       else
101         luv[0] = (116 * 7.787) * xyz[1];
102      luv[1] = luv[0] * (13 * (var_u - 4 * REF_WHITE_X / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z)));
103      luv[2] = luv[0] * (13 * (var_v - 9 * REF_WHITE_Y / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z)));
104      /* intervals [0..100], [-134..220], [-140..122] */
105    }
106 }
107
108
109 /***************** OPTIMIZED SRGB -> LUV CONVERSION *********************/
110
111 u16 srgb_to_luv_tab1[256];
112 u16 srgb_to_luv_tab2[9 << SRGB_TO_LUV_TAB2_SIZE];
113 u32 srgb_to_luv_tab3[20 << SRGB_TO_LUV_TAB3_SIZE];
114
115 void
116 srgb_to_luv_init(void)
117 {
118   DBG("Initializing sRGB -> Luv table");
119   for (uns i = 0; i < 256; i++)
120     {
121       double t = i / 255.;
122       if (t > 0.04045)
123         t = pow((t + 0.055) * (1 / 1.055), 2.4);
124       else
125         t = t * (1 / 12.92);
126       srgb_to_luv_tab1[i] = CLAMP(t * 0xfff + 0.5, 0, 0xfff);
127     }
128   for (uns i = 0; i < (9 << SRGB_TO_LUV_TAB2_SIZE); i++)
129     {
130       double t = i / (double)((9 << SRGB_TO_LUV_TAB2_SIZE) - 1);
131       if (t > 0.008856)
132         t = 1.16 * pow(t, 1 / 3.) - 0.16;
133       else
134         t = (1.16 * 7.787) * t;
135       srgb_to_luv_tab2[i] =
136         CLAMP(t * ((1 << SRGB_TO_LUV_TAB2_SCALE) - 1) + 0.5,
137           0, (1 << SRGB_TO_LUV_TAB2_SCALE) - 1);
138     }
139   for (uns i = 0; i < (20 << SRGB_TO_LUV_TAB3_SIZE); i++)
140     {
141       srgb_to_luv_tab3[i] = i ? (13 << (SRGB_TO_LUV_TAB3_SCALE + SRGB_TO_LUV_TAB3_SIZE)) / i : 0;
142     }
143 }
144
145 void
146 srgb_to_luv_pixels(byte *dest, byte *src, uns count)
147 {
148   while (count--)
149     {
150       srgb_to_luv_pixel(dest, src);
151       dest += 3;
152       src += 3;
153     }
154 }
155
156
157 /************************ GRID INTERPOLATION ALGORITHM ************************/
158
159 struct color_grid_node *srgb_to_luv_grid;
160 struct color_interpolation_node *color_interpolation_table;
161
162 /* Returns volume of a given tetrahedron multiplied by 6 */
163 static inline uns
164 tetrahedron_volume(uns *v1, uns *v2, uns *v3, uns *v4)
165 {
166   int a[3], b[3], c[3];
167   for (uns i = 0; i < 3; i++)
168     {
169       a[i] = v2[i] - v1[i];
170       b[i] = v3[i] - v1[i];
171       c[i] = v4[i] - v1[i];
172     }
173   int result =
174     a[0] * (b[1] * c[2] - b[2] * c[1]) -
175     a[1] * (b[0] * c[2] - b[2] * c[0]) +
176     a[2] * (b[0] * c[1] - b[1] * c[0]);
177   return (result > 0) ? result : -result;
178 }
179
180 static void
181 interpolate_tetrahedron(struct color_interpolation_node *n, uns *p, const uns *c)
182 {
183   uns v[4][3];
184   for (uns i = 0; i < 4; i++)
185     {
186       v[i][0] = (c[i] & 0001) ? (1 << COLOR_CONV_OFS) : 0;
187       v[i][1] = (c[i] & 0010) ? (1 << COLOR_CONV_OFS) : 0;
188       v[i][2] = (c[i] & 0100) ? (1 << COLOR_CONV_OFS) : 0;
189       n->ofs[i] =
190         ((c[i] & 0001) ? 1 : 0) +
191         ((c[i] & 0010) ? (1 << COLOR_CONV_SIZE) : 0) +
192         ((c[i] & 0100) ? (1 << (COLOR_CONV_SIZE * 2)) : 0);
193     }
194   uns vol = tetrahedron_volume(v[0], v[1], v[2], v[3]);
195   n->mul[0] = ((tetrahedron_volume(p, v[1], v[2], v[3]) << 8) + (vol >> 1)) / vol;
196   n->mul[1] = ((tetrahedron_volume(v[0], p, v[2], v[3]) << 8) + (vol >> 1)) / vol;
197   n->mul[2] = ((tetrahedron_volume(v[0], v[1], p, v[3]) << 8) + (vol >> 1)) / vol;
198   n->mul[3] = ((tetrahedron_volume(v[0], v[1], v[2], p) << 8) + (vol >> 1)) / vol;
199   uns j;
200   for (j = 0; j < 4; j++)
201     if (n->mul[j])
202       break;
203   for (uns i = 0; i < 4; i++)
204     if (n->mul[i] == 0)
205       n->ofs[i] = n->ofs[j];
206 }
207
208 static void
209 interpolation_table_init(void)
210 {
211   DBG("Initializing color interpolation table");
212   struct color_interpolation_node *n = color_interpolation_table =
213     xmalloc(sizeof(struct color_interpolation_node) << (COLOR_CONV_OFS * 3));
214   uns p[3];
215   for (p[2] = 0; p[2] < (1 << COLOR_CONV_OFS); p[2]++)
216     for (p[1] = 0; p[1] < (1 << COLOR_CONV_OFS); p[1]++)
217       for (p[0] = 0; p[0] < (1 << COLOR_CONV_OFS); p[0]++)
218         {
219           uns index;
220           static const uns tetrahedrons[5][4] = {
221             {0000, 0001, 0010, 0100},
222             {0110, 0111, 0100, 0010},
223             {0101, 0100, 0111, 0001},
224             {0011, 0010, 0001, 0111},
225             {0111, 0001, 0010, 0100}};
226           if (p[0] + p[1] + p[2] <= (1 << COLOR_CONV_OFS))
227             index = 0;
228           else if ((1 << COLOR_CONV_OFS) + p[0] <= p[1] + p[2])
229             index = 1;
230           else if ((1 << COLOR_CONV_OFS) + p[1] <= p[0] + p[2])
231             index = 2;
232           else if ((1 << COLOR_CONV_OFS) + p[2] <= p[0] + p[1])
233             index = 3;
234           else
235             index = 4;
236           interpolate_tetrahedron(n, p, tetrahedrons[index]);
237           n++;
238         }
239 }
240
241 typedef void color_conv_func(double dest[3], double src[3]);
242
243 static void
244 conv_grid_init(struct color_grid_node **grid, color_conv_func func)
245 {
246   if (*grid)
247     return;
248   struct color_grid_node *g = *grid = xmalloc((sizeof(struct color_grid_node)) << (COLOR_CONV_SIZE * 3));
249   double src[3], dest[3];
250   for (uns k = 0; k < (1 << COLOR_CONV_SIZE); k++)
251     {
252       src[2] = k * (255 / (double)((1 << COLOR_CONV_SIZE) - 1));
253       for (uns j = 0; j < (1 << COLOR_CONV_SIZE); j++)
254         {
255           src[1] = j * (255/ (double)((1 << COLOR_CONV_SIZE) - 1));
256           for (uns i = 0; i < (1 << COLOR_CONV_SIZE); i++)
257             {
258               src[0] = i * (255 / (double)((1 << COLOR_CONV_SIZE) - 1));
259               func(dest, src);
260               g->val[0] = CLAMP(dest[0] + 0.5, 0, 255);
261               g->val[1] = CLAMP(dest[1] + 0.5, 0, 255);
262               g->val[2] = CLAMP(dest[2] + 0.5, 0, 255);
263               g++;
264             }
265         }
266     }
267 }
268
269 static void
270 srgb_to_luv_func(double dest[3], double src[3])
271 {
272   double srgb[3], xyz[3], luv[3];
273   srgb[0] = src[0] / 255.;
274   srgb[1] = src[1] / 255.;
275   srgb[2] = src[2] / 255.;
276   srgb_to_xyz_slow(xyz, srgb);
277   xyz_to_luv_slow(luv, xyz);
278   dest[0] = luv[0] * 2.55;
279   dest[1] = luv[1] * (2.55 / 4) + 128;
280   dest[2] = luv[2] * (2.55 / 4) + 128;
281 }
282
283 void
284 color_conv_init(void)
285 {
286   interpolation_table_init();
287   conv_grid_init(&srgb_to_luv_grid, srgb_to_luv_func);
288 }
289
290 void
291 color_conv_pixels(byte *dest, byte *src, uns count, struct color_grid_node *grid)
292 {
293   while (count--)
294     {
295       color_conv_pixel(dest, src, grid);
296       dest += 3;
297       src += 3;
298     }
299 }
300
301
302 /**************************** TESTS *******************************/
303
304 #ifdef TEST
305 #include <string.h>
306
307 static double
308 conv_error(u32 color, struct color_grid_node *grid, color_conv_func func)
309 {
310   byte src[3], dest[3];
311   src[0] = color & 255;
312   src[1] = (color >> 8) & 255;
313   src[2] = (color >> 16) & 255;
314   color_conv_pixel(dest, src, grid);
315   double src2[3], dest2[3];
316   for (uns i = 0; i < 3; i++)
317     src2[i] = src[i];
318   func(dest2, src2);
319   double err = 0;
320   for (uns i = 0; i < 3; i++)
321     err += (dest[i] - dest2[i]) * (dest[i] - dest2[i]);
322   return err;
323 }
324
325 typedef void test_fn(byte *dest, byte *src);
326
327 static double
328 func_error(u32 color, test_fn test, color_conv_func func)
329 {
330   byte src[3], dest[3];
331   src[0] = color & 255;
332   src[1] = (color >> 8) & 255;
333   src[2] = (color >> 16) & 255;
334   test(dest, src);
335   double src2[3], dest2[3];
336   for (uns i = 0; i < 3; i++)
337     src2[i] = src[i];
338   func(dest2, src2);
339   double err = 0;
340   for (uns i = 0; i < 3; i++)
341     err += (dest[i] - dest2[i]) * (dest[i] - dest2[i]);
342   return err;
343 }
344
345 static void
346 test_grid(byte *name, struct color_grid_node *grid, color_conv_func func)
347 {
348   double max_err = 0, sum_err = 0;
349   uns count = 100000;
350   for (uns i = 0; i < count; i++)
351     {
352       double err = conv_error(random_max(0x1000000), grid, func);
353       max_err = MAX(err, max_err);
354       sum_err += err;
355     }
356   DBG("%s: error max=%f avg=%f", name, max_err, sum_err / count);
357   if (max_err > 12)
358     die("Too large error in %s conversion", name);
359 }
360
361 static void
362 test_func(byte *name, test_fn test, color_conv_func func)
363 {
364   double max_err = 0, sum_err = 0;
365   uns count = 100000;
366   for (uns i = 0; i < count; i++)
367     {
368       double err = func_error(random_max(0x1000000), test, func);
369       max_err = MAX(err, max_err);
370       sum_err += err;
371     }
372   DBG("%s: error max=%f avg=%f", name, max_err, sum_err / count);
373   if (max_err > 12)
374     die("Too large error in %s conversion", name);
375 }
376
377 int
378 main(void)
379 {
380   srgb_to_luv_init();
381   test_func("func sRGB -> Luv", srgb_to_luv_pixel, srgb_to_luv_func);
382   color_conv_init();
383   test_grid("grid sRGB -> Luv", srgb_to_luv_grid, srgb_to_luv_func);
384 #ifdef LOCAL_DEBUG
385 #define CNT 1000000
386 #define TESTS 10
387   byte *a = xmalloc(3 * CNT), *b = xmalloc(3 * CNT);
388   for (uns i = 0; i < 3 * CNT; i++)
389     a[i] = random_max(256);
390   init_timer();
391   for (uns i = 0; i < TESTS; i++)
392     memcpy(b, a, CNT * 3);
393   DBG("memcpy time=%d", (uns)get_timer());
394   init_timer();
395   for (uns i = 0; i < TESTS; i++)
396     srgb_to_luv_pixels(b, a, CNT);
397   DBG("direct time=%d", (uns)get_timer());
398   init_timer();
399   for (uns i = 0; i < TESTS; i++)
400     color_conv_pixels(b, a, CNT, srgb_to_luv_grid);
401   DBG("grid time=%d", (uns)get_timer());
402 #endif
403   return 0;
404 }
405 #endif
406