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.
12 #include "sherlock/sherlock.h"
14 #include "images/color.h"
17 /********************* EXACT CONVERSION ROUTINES **********************/
21 srgb_to_xyz_slow(double xyz[3], double srgb[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] = SRGB_XYZ_XR * a[0] + SRGB_XYZ_XG * a[1] + SRGB_XYZ_XB * a[2];
30 xyz[1] = SRGB_XYZ_YR * a[0] + SRGB_XYZ_YG * a[1] + SRGB_XYZ_YB * a[2];
31 xyz[2] = SRGB_XYZ_ZR * a[0] + SRGB_XYZ_ZG * a[1] + SRGB_XYZ_ZB * a[2];
36 xyz_to_luv_slow(double luv[3], double xyz[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 /***************** OPTIMIZED SRGB -> LUV CONVERSION *********************/
58 u16 srgb_to_luv_tab1[256];
59 u16 srgb_to_luv_tab2[9 << SRGB_TO_LUV_TAB2_SIZE];
60 u32 srgb_to_luv_tab3[20 << SRGB_TO_LUV_TAB3_SIZE];
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);
104 /************************ GRID INTERPOLATION ALGORITHM ************************/
106 struct color_grid_node *srgb_to_luv_grid;
107 struct color_interpolation_node *color_interpolation_table;
109 /* Returns volume of a given tetrahedron multiplied by 6 */
111 tetrahedron_volume(uns *v1, uns *v2, uns *v3, uns *v4)
113 int a[3], b[3], c[3];
114 for (uns i = 0; i < 3; i++)
116 a[i] = v2[i] - v1[i];
117 b[i] = v3[i] - v1[i];
118 c[i] = v4[i] - v1[i];
121 a[0] * (b[1] * c[2] - b[2] * c[1]) -
122 a[1] * (b[0] * c[2] - b[2] * c[0]) +
123 a[2] * (b[0] * c[1] - b[1] * c[0]);
124 return (result > 0) ? result : -result;
128 interpolate_tetrahedron(struct color_interpolation_node *n, uns *p, const uns *c)
131 for (uns i = 0; i < 4; i++)
133 v[i][0] = (c[i] & 0001) ? (1 << COLOR_CONV_OFS) : 0;
134 v[i][1] = (c[i] & 0010) ? (1 << COLOR_CONV_OFS) : 0;
135 v[i][2] = (c[i] & 0100) ? (1 << COLOR_CONV_OFS) : 0;
137 ((c[i] & 0001) ? 1 : 0) +
138 ((c[i] & 0010) ? (1 << COLOR_CONV_SIZE) : 0) +
139 ((c[i] & 0100) ? (1 << (COLOR_CONV_SIZE * 2)) : 0);
141 uns vol = tetrahedron_volume(v[0], v[1], v[2], v[3]);
142 n->mul[0] = ((tetrahedron_volume(p, v[1], v[2], v[3]) << 8) + (vol >> 1)) / vol;
143 n->mul[1] = ((tetrahedron_volume(v[0], p, v[2], v[3]) << 8) + (vol >> 1)) / vol;
144 n->mul[2] = ((tetrahedron_volume(v[0], v[1], p, v[3]) << 8) + (vol >> 1)) / vol;
145 n->mul[3] = ((tetrahedron_volume(v[0], v[1], v[2], p) << 8) + (vol >> 1)) / vol;
147 for (j = 0; j < 4; j++)
150 for (uns i = 0; i < 4; i++)
152 n->ofs[i] = n->ofs[j];
156 interpolation_table_init(void)
158 DBG("Initializing color interpolation table");
159 struct color_interpolation_node *n = color_interpolation_table =
160 xmalloc(sizeof(struct color_interpolation_node) << (COLOR_CONV_OFS * 3));
162 for (p[2] = 0; p[2] < (1 << COLOR_CONV_OFS); p[2]++)
163 for (p[1] = 0; p[1] < (1 << COLOR_CONV_OFS); p[1]++)
164 for (p[0] = 0; p[0] < (1 << COLOR_CONV_OFS); p[0]++)
167 static const uns tetrahedrons[5][4] = {
168 {0000, 0001, 0010, 0100},
169 {0110, 0111, 0100, 0010},
170 {0101, 0100, 0111, 0001},
171 {0011, 0010, 0001, 0111},
172 {0111, 0001, 0010, 0100}};
173 if (p[0] + p[1] + p[2] <= (1 << COLOR_CONV_OFS))
175 else if ((1 << COLOR_CONV_OFS) + p[0] <= p[1] + p[2])
177 else if ((1 << COLOR_CONV_OFS) + p[1] <= p[0] + p[2])
179 else if ((1 << COLOR_CONV_OFS) + p[2] <= p[0] + p[1])
183 interpolate_tetrahedron(n, p, tetrahedrons[index]);
188 typedef void color_conv_func(double dest[3], double src[3]);
191 conv_grid_init(struct color_grid_node **grid, color_conv_func func)
195 struct color_grid_node *g = *grid = xmalloc((sizeof(struct color_grid_node)) << (COLOR_CONV_SIZE * 3));
196 double src[3], dest[3];
197 for (uns k = 0; k < (1 << COLOR_CONV_SIZE); k++)
199 src[2] = k * (255 / (double)((1 << COLOR_CONV_SIZE) - 1));
200 for (uns j = 0; j < (1 << COLOR_CONV_SIZE); j++)
202 src[1] = j * (255/ (double)((1 << COLOR_CONV_SIZE) - 1));
203 for (uns i = 0; i < (1 << COLOR_CONV_SIZE); i++)
205 src[0] = i * (255 / (double)((1 << COLOR_CONV_SIZE) - 1));
207 g->val[0] = CLAMP(dest[0] + 0.5, 0, 255);
208 g->val[1] = CLAMP(dest[1] + 0.5, 0, 255);
209 g->val[2] = CLAMP(dest[2] + 0.5, 0, 255);
217 srgb_to_luv_func(double dest[3], double src[3])
219 double srgb[3], xyz[3], luv[3];
220 srgb[0] = src[0] / 255.;
221 srgb[1] = src[1] / 255.;
222 srgb[2] = src[2] / 255.;
223 srgb_to_xyz_slow(xyz, srgb);
224 xyz_to_luv_slow(luv, xyz);
225 dest[0] = luv[0] * 2.55;
226 dest[1] = luv[1] * (2.55 / 4) + 128;
227 dest[2] = luv[2] * (2.55 / 4) + 128;
231 color_conv_init(void)
233 interpolation_table_init();
234 conv_grid_init(&srgb_to_luv_grid, srgb_to_luv_func);
238 color_conv_pixels(byte *dest, byte *src, uns count, struct color_grid_node *grid)
242 color_conv_pixel(dest, src, grid);
249 /**************************** TESTS *******************************/
255 conv_error(u32 color, struct color_grid_node *grid, color_conv_func func)
257 byte src[3], dest[3];
258 src[0] = color & 255;
259 src[1] = (color >> 8) & 255;
260 src[2] = (color >> 16) & 255;
261 color_conv_pixel(dest, src, grid);
262 double src2[3], dest2[3];
263 for (uns i = 0; i < 3; i++)
267 for (uns i = 0; i < 3; i++)
268 err += (dest[i] - dest2[i]) * (dest[i] - dest2[i]);
272 typedef void test_fn(byte *dest, byte *src);
275 func_error(u32 color, test_fn test, color_conv_func func)
277 byte src[3], dest[3];
278 src[0] = color & 255;
279 src[1] = (color >> 8) & 255;
280 src[2] = (color >> 16) & 255;
282 double src2[3], dest2[3];
283 for (uns i = 0; i < 3; i++)
287 for (uns i = 0; i < 3; i++)
288 err += (dest[i] - dest2[i]) * (dest[i] - dest2[i]);
293 test_grid(byte *name, struct color_grid_node *grid, color_conv_func func)
295 double max_err = 0, sum_err = 0;
297 for (uns i = 0; i < count; i++)
299 double err = conv_error(random_max(0x1000000), grid, func);
300 max_err = MAX(err, max_err);
303 DBG("%s: error max=%f avg=%f", name, max_err, sum_err / count);
305 die("Too large error in %s conversion", name);
309 test_func(byte *name, test_fn test, color_conv_func func)
311 double max_err = 0, sum_err = 0;
313 for (uns i = 0; i < count; i++)
315 double err = func_error(random_max(0x1000000), test, func);
316 max_err = MAX(err, max_err);
319 DBG("%s: error max=%f avg=%f", name, max_err, sum_err / count);
321 die("Too large error in %s conversion", name);
328 test_func("func sRGB -> Luv", srgb_to_luv_pixel, srgb_to_luv_func);
330 test_grid("grid sRGB -> Luv", srgb_to_luv_grid, srgb_to_luv_func);
334 byte *a = xmalloc(3 * CNT), *b = xmalloc(3 * CNT);
335 for (uns i = 0; i < 3 * CNT; i++)
336 a[i] = random_max(256);
338 for (uns i = 0; i < TESTS; i++)
339 memcpy(b, a, CNT * 3);
340 DBG("memcpy time=%d", (uns)get_timer());
342 for (uns i = 0; i < TESTS; i++)
343 srgb_to_luv_pixels(b, a, CNT);
344 DBG("direct time=%d", (uns)get_timer());
346 for (uns i = 0; i < TESTS; i++)
347 color_conv_pixels(b, a, CNT, srgb_to_luv_grid);
348 DBG("grid time=%d", (uns)get_timer());