2 * Image Library -- Color Spaces
4 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
10 * - http://www.tecgraf.puc-rio.br/~mgattass/color/ColorIndex.html
15 #include "sherlock/sherlock.h"
17 #include "images/color.h"
19 u16 srgb_to_luv_tab1[256];
20 u16 srgb_to_luv_tab2[9 << SRGB_TO_LUV_TAB2_SIZE];
21 u32 srgb_to_luv_tab3[20 << SRGB_TO_LUV_TAB3_SIZE];
23 struct color_grid_node *srgb_to_luv_grid;
24 struct color_interpolation_node *color_interpolation_table;
28 srgb_to_xyz_slow(double xyz[3], double srgb[3])
31 for (uns i = 0; i < 3; i++)
32 if (srgb[i] > 0.04045)
33 a[i] = pow((srgb[i] + 0.055) * (1 / 1.055), 2.4);
35 a[i] = srgb[i] * (1 / 12.92);
36 xyz[0] = SRGB_XYZ_XR * a[0] + SRGB_XYZ_XG * a[1] + SRGB_XYZ_XB * a[2];
37 xyz[1] = SRGB_XYZ_YR * a[0] + SRGB_XYZ_YG * a[1] + SRGB_XYZ_YB * a[2];
38 xyz[2] = SRGB_XYZ_ZR * a[0] + SRGB_XYZ_ZG * a[1] + SRGB_XYZ_ZB * a[2];
43 xyz_to_luv_slow(double luv[3], double xyz[3])
45 double sum = xyz[0] + 15 * xyz[1] + 3 * xyz[2];
47 luv[0] = luv[1] = luv[2] = 0;
50 double var_u = 4 * xyz[0] / sum;
51 double var_v = 9 * xyz[1] / sum;
52 if (xyz[1] > 0.008856)
53 luv[0] = 116 * pow(xyz[1], 1 / 3.) - 16;
55 luv[0] = (116 * 7.787) * xyz[1];
56 luv[1] = luv[0] * (13 * (var_u - 4 * REF_WHITE_X / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z)));
57 luv[2] = luv[0] * (13 * (var_v - 9 * REF_WHITE_Y / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z)));
58 /* intervals [0..100], [-134..220], [-140..122] */
63 srgb_to_luv_init(void)
65 DBG("Initializing sRGB -> Luv table");
66 for (uns i = 0; i < 256; i++)
70 t = pow((t + 0.055) * (1 / 1.055), 2.4);
73 srgb_to_luv_tab1[i] = CLAMP(t * 0xfff + 0.5, 0, 0xfff);
75 for (uns i = 0; i < (9 << SRGB_TO_LUV_TAB2_SIZE); i++)
77 double t = i / (double)((9 << SRGB_TO_LUV_TAB2_SIZE) - 1);
79 t = 1.16 * pow(t, 1 / 3.) - 0.16;
81 t = (1.16 * 7.787) * t;
83 CLAMP(t * ((1 << SRGB_TO_LUV_TAB2_SCALE) - 1) + 0.5,
84 0, (1 << SRGB_TO_LUV_TAB2_SCALE) - 1);
86 for (uns i = 0; i < (20 << SRGB_TO_LUV_TAB3_SIZE); i++)
88 srgb_to_luv_tab3[i] = i ? (13 << (SRGB_TO_LUV_TAB3_SCALE + SRGB_TO_LUV_TAB3_SIZE)) / i : 0;
93 srgb_to_luv_pixels(byte *dest, byte *src, uns count)
97 srgb_to_luv_pixel(dest, src);
103 /* Returns volume of a given tetrahedron multiplied by 6 */
105 tetrahedron_volume(uns *v1, uns *v2, uns *v3, uns *v4)
107 int a[3], b[3], c[3];
108 for (uns i = 0; i < 3; i++)
110 a[i] = v2[i] - v1[i];
111 b[i] = v3[i] - v1[i];
112 c[i] = v4[i] - v1[i];
115 a[0] * (b[1] * c[2] - b[2] * c[1]) -
116 a[1] * (b[0] * c[2] - b[2] * c[0]) +
117 a[2] * (b[0] * c[1] - b[1] * c[0]);
118 return (result > 0) ? result : -result;
122 interpolate_tetrahedron(struct color_interpolation_node *n, uns *p, const uns *c)
125 for (uns i = 0; i < 4; i++)
127 v[i][0] = (c[i] & 0001) ? (1 << COLOR_CONV_OFS) : 0;
128 v[i][1] = (c[i] & 0010) ? (1 << COLOR_CONV_OFS) : 0;
129 v[i][2] = (c[i] & 0100) ? (1 << COLOR_CONV_OFS) : 0;
131 ((c[i] & 0001) ? 1 : 0) +
132 ((c[i] & 0010) ? (1 << COLOR_CONV_SIZE) : 0) +
133 ((c[i] & 0100) ? (1 << (COLOR_CONV_SIZE * 2)) : 0);
135 uns vol = tetrahedron_volume(v[0], v[1], v[2], v[3]);
136 n->mul[0] = ((tetrahedron_volume(p, v[1], v[2], v[3]) << 8) + (vol >> 1)) / vol;
137 n->mul[1] = ((tetrahedron_volume(v[0], p, v[2], v[3]) << 8) + (vol >> 1)) / vol;
138 n->mul[2] = ((tetrahedron_volume(v[0], v[1], p, v[3]) << 8) + (vol >> 1)) / vol;
139 n->mul[3] = ((tetrahedron_volume(v[0], v[1], v[2], p) << 8) + (vol >> 1)) / vol;
141 for (j = 0; j < 4; j++)
144 for (uns i = 0; i < 4; i++)
146 n->ofs[i] = n->ofs[j];
150 interpolation_table_init(void)
152 DBG("Initializing color interpolation table");
153 struct color_interpolation_node *n = color_interpolation_table =
154 xmalloc(sizeof(struct color_interpolation_node) << (COLOR_CONV_OFS * 3));
156 for (p[2] = 0; p[2] < (1 << COLOR_CONV_OFS); p[2]++)
157 for (p[1] = 0; p[1] < (1 << COLOR_CONV_OFS); p[1]++)
158 for (p[0] = 0; p[0] < (1 << COLOR_CONV_OFS); p[0]++)
161 static const uns tetrahedrons[5][4] = {
162 {0000, 0001, 0010, 0100},
163 {0110, 0111, 0100, 0010},
164 {0101, 0100, 0111, 0001},
165 {0011, 0010, 0001, 0111},
166 {0111, 0001, 0010, 0100}};
167 if (p[0] + p[1] + p[2] <= (1 << COLOR_CONV_OFS))
169 else if ((1 << COLOR_CONV_OFS) + p[0] <= p[1] + p[2])
171 else if ((1 << COLOR_CONV_OFS) + p[1] <= p[0] + p[2])
173 else if ((1 << COLOR_CONV_OFS) + p[2] <= p[0] + p[1])
177 interpolate_tetrahedron(n, p, tetrahedrons[index]);
182 typedef void color_conv_func(double dest[3], double src[3]);
185 conv_grid_init(struct color_grid_node **grid, color_conv_func func)
189 struct color_grid_node *g = *grid = xmalloc((sizeof(struct color_grid_node)) << (COLOR_CONV_SIZE * 3));
190 double src[3], dest[3];
191 for (uns k = 0; k < (1 << COLOR_CONV_SIZE); k++)
193 src[2] = k * (255 / (double)((1 << COLOR_CONV_SIZE) - 1));
194 for (uns j = 0; j < (1 << COLOR_CONV_SIZE); j++)
196 src[1] = j * (255/ (double)((1 << COLOR_CONV_SIZE) - 1));
197 for (uns i = 0; i < (1 << COLOR_CONV_SIZE); i++)
199 src[0] = i * (255 / (double)((1 << COLOR_CONV_SIZE) - 1));
201 g->val[0] = CLAMP(dest[0] + 0.5, 0, 255);
202 g->val[1] = CLAMP(dest[1] + 0.5, 0, 255);
203 g->val[2] = CLAMP(dest[2] + 0.5, 0, 255);
211 srgb_to_luv_func(double dest[3], double src[3])
213 double srgb[3], xyz[3], luv[3];
214 srgb[0] = src[0] / 255.;
215 srgb[1] = src[1] / 255.;
216 srgb[2] = src[2] / 255.;
217 srgb_to_xyz_slow(xyz, srgb);
218 xyz_to_luv_slow(luv, xyz);
219 dest[0] = luv[0] * 2.55;
220 dest[1] = luv[1] * (2.55 / 4) + 128;
221 dest[2] = luv[2] * (2.55 / 4) + 128;
225 color_conv_init(void)
227 interpolation_table_init();
228 conv_grid_init(&srgb_to_luv_grid, srgb_to_luv_func);
232 color_conv_pixels(byte *dest, byte *src, uns count, struct color_grid_node *grid)
236 color_conv_pixel(dest, src, grid);
246 conv_error(u32 color, struct color_grid_node *grid, color_conv_func func)
248 byte src[3], dest[3];
249 src[0] = color & 255;
250 src[1] = (color >> 8) & 255;
251 src[2] = (color >> 16) & 255;
252 color_conv_pixel(dest, src, grid);
253 double src2[3], dest2[3];
254 for (uns i = 0; i < 3; i++)
258 for (uns i = 0; i < 3; i++)
259 err += (dest[i] - dest2[i]) * (dest[i] - dest2[i]);
263 typedef void test_fn(byte *dest, byte *src);
266 func_error(u32 color, test_fn test, color_conv_func func)
268 byte src[3], dest[3];
269 src[0] = color & 255;
270 src[1] = (color >> 8) & 255;
271 src[2] = (color >> 16) & 255;
273 double src2[3], dest2[3];
274 for (uns i = 0; i < 3; i++)
278 for (uns i = 0; i < 3; i++)
279 err += (dest[i] - dest2[i]) * (dest[i] - dest2[i]);
284 test_grid(byte *name, struct color_grid_node *grid, color_conv_func func)
286 double max_err = 0, sum_err = 0;
288 for (uns i = 0; i < count; i++)
290 double err = conv_error(random_max(0x1000000), grid, func);
291 max_err = MAX(err, max_err);
294 DBG("%s: error max=%f avg=%f", name, max_err, sum_err / count);
296 die("Too large error in %s conversion", name);
300 test_func(byte *name, test_fn test, color_conv_func func)
302 double max_err = 0, sum_err = 0;
304 for (uns i = 0; i < count; i++)
306 double err = func_error(random_max(0x1000000), test, func);
307 max_err = MAX(err, max_err);
310 DBG("%s: error max=%f avg=%f", name, max_err, sum_err / count);
312 die("Too large error in %s conversion", name);
319 test_func("func sRGB -> Luv", srgb_to_luv_pixel, srgb_to_luv_func);
321 test_grid("grid sRGB -> Luv", srgb_to_luv_grid, srgb_to_luv_func);
324 byte *a = xmalloc(3 * CNT), *b = xmalloc(3 * CNT);
326 for (uns i = 0; i < 20; i++)
327 memcpy(b, a, CNT * 3);
328 DBG("memcpy time=%d", (uns)get_timer());
329 for (uns i = 0; i < 3 * CNT; i++)
330 a[i] = random_max(256);
332 for (uns i = 0; i < 20; i++)
333 srgb_to_luv_pixels(b, a, CNT);
334 DBG("direct time=%d", (uns)get_timer());
336 for (uns i = 0; i < 20; i++)
337 color_conv_pixels(b, a, CNT, srgb_to_luv_grid);
338 DBG("grid time=%d", (uns)get_timer());