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.
11 * - A Review of RGB Color Spaces, Danny Pascale (2003)
12 * - http://www.adobe.com/digitalimag/pdfs/AdobeRGB1998.pdf
13 * - http://www.tecgraf.puc-rio.br/~mgattass/color/ColorIndex.html
16 * - fix theoretical problems with rounding errors in srgb_to_luv_pixel()
17 * - SIMD should help to speed up conversion of large arrays
18 * - maybe try to generate a long switch in color_conv_pixel()
19 * with optimized entries instead of access to interpolation table
20 * - most of multiplications in srgb_to_luv_pixels can be replaced
21 * with tables lookup... tests shows almost the same speed for random
22 * input and cca 40% gain when input colors fit in CPU chache
25 #ifndef _IMAGES_COLOR_H
26 #define _IMAGES_COLOR_H
28 #include "images/images.h"
31 COLOR_SPACE_UNKNOWN = 0,
32 COLOR_SPACE_UNKNOWN_1 = 1,
33 COLOR_SPACE_UNKNOWN_2 = 2,
34 COLOR_SPACE_UNKNOWN_3 = 3,
35 COLOR_SPACE_UNKNOWN_4 = 4,
36 COLOR_SPACE_UNKNOWN_MAX = 4,
37 COLOR_SPACE_GRAYSCALE,
48 extern uns color_space_channels[COLOR_SPACE_MAX];
49 extern byte *color_space_name[COLOR_SPACE_MAX];
51 byte *color_space_id_to_name(uns id);
52 uns color_space_name_to_id(byte *name);
54 /* Color spaces in the CIE 1931 chromacity diagram */
56 struct color_space_chromacity_info {
63 struct color_space_gamma_info {
65 double detailed_gamma;
71 struct color_space_info {
73 struct color_space_chromacity_info chromacity;
74 struct color_space_gamma_info gamma;
78 color_illuminant_d50[2],
79 color_illuminant_d65[2],
80 color_illuminant_e[2];
82 extern const struct color_space_info
83 color_adobe_rgb_info, /* Adobe RGB (1998) */
84 color_apple_rgb_info, /* Apple RGB */
85 color_cie_rgb_info, /* CIE RGB */
86 color_color_match_rgb_info, /* ColorMatch RGB */
87 color_srgb_info; /* sRGB */
89 /* These routines do not check numeric errors! */
90 void color_compute_color_space_to_xyz_matrix(double matrix[9], const struct color_space_chromacity_info *space);
91 void color_compute_bradford_matrix(double matrix[9], const double src[2], const double dest[2]);
92 void color_compute_color_spaces_conversion_matrix(double matrix[9], const struct color_space_chromacity_info *src, const struct color_space_chromacity_info *dest);
93 void color_invert_matrix(double dest[9], double matrix[9]);
96 rgb_to_gray_func(uns r, uns g, uns b)
98 return (r * 19660 + g * 38666 + b * 7210) >> 16;
101 extern struct color color_black, color_white;
104 color_make_gray(struct color *color, uns gray)
107 color->color_space = COLOR_SPACE_GRAYSCALE;
111 color_make_rgb(struct color *color, uns r, uns g, uns b)
116 color->color_space = COLOR_SPACE_RGB;
119 void color_put_color_space(byte *dest, struct color *color, uns color_space);
120 void color_put_grayscale(byte *dest, struct color *color);
121 void color_put_rgb(byte *dest, struct color *color);
123 /* Exact slow conversion routines */
124 void srgb_to_xyz_exact(double dest[3], double src[3]);
125 void xyz_to_srgb_exact(double dest[3], double src[3]);
126 void xyz_to_luv_exact(double dest[3], double src[3]);
127 void luv_to_xyz_exact(double dest[3], double src[3]);
129 /* Reference white */
130 #define REF_WHITE_X 0.96422
131 #define REF_WHITE_Y 1.
132 #define REF_WHITE_Z 0.82521
134 /* sRGB -> XYZ matrix */
135 #define SRGB_XYZ_XR 0.412424
136 #define SRGB_XYZ_XG 0.357579
137 #define SRGB_XYZ_XB 0.180464
138 #define SRGB_XYZ_YR 0.212656
139 #define SRGB_XYZ_YG 0.715158
140 #define SRGB_XYZ_YB 0.072186
141 #define SRGB_XYZ_ZR 0.019332
142 #define SRGB_XYZ_ZG 0.119193
143 #define SRGB_XYZ_ZB 0.950444
146 /*********************** OPTIMIZED CONVERSION ROUTINES **********************/
148 /* sRGB -> Luv parameters */
149 #define SRGB_TO_LUV_TAB2_SIZE 9
150 #define SRGB_TO_LUV_TAB2_SCALE 11
151 #define SRGB_TO_LUV_TAB3_SIZE 8
152 #define SRGB_TO_LUV_TAB3_SCALE (39 - SRGB_TO_LUV_TAB2_SCALE - SRGB_TO_LUV_TAB3_SIZE)
154 extern u16 srgb_to_luv_tab1[256];
155 extern u16 srgb_to_luv_tab2[9 << SRGB_TO_LUV_TAB2_SIZE];
156 extern u32 srgb_to_luv_tab3[20 << SRGB_TO_LUV_TAB3_SIZE];
158 void srgb_to_luv_init(void);
159 void srgb_to_luv_pixels(byte *dest, byte *src, uns count);
161 /* L covers the interval [0..255]; u and v are centered to 128 and scaled by 1/4 in respect of L */
163 srgb_to_luv_pixel(byte *dest, byte *src)
165 uns r = srgb_to_luv_tab1[src[0]];
166 uns g = srgb_to_luv_tab1[src[1]];
167 uns b = srgb_to_luv_tab1[src[2]];
169 (uns)(4 * SRGB_XYZ_XR * 0xffff) * r +
170 (uns)(4 * SRGB_XYZ_XG * 0xffff) * g +
171 (uns)(4 * SRGB_XYZ_XB * 0xffff) * b;
173 (uns)(9 * SRGB_XYZ_YR * 0xffff) * r +
174 (uns)(9 * SRGB_XYZ_YG * 0xffff) * g +
175 (uns)(9 * SRGB_XYZ_YB * 0xffff) * b;
176 uns l = srgb_to_luv_tab2[y >> (28 - SRGB_TO_LUV_TAB2_SIZE)];
177 dest[0] = l >> (SRGB_TO_LUV_TAB2_SCALE - 8);
179 (uns)((SRGB_XYZ_XR + 15 * SRGB_XYZ_YR + 3 * SRGB_XYZ_ZR) * 0x7fff) * r +
180 (uns)((SRGB_XYZ_XG + 15 * SRGB_XYZ_YG + 3 * SRGB_XYZ_ZG) * 0x7fff) * g +
181 (uns)((SRGB_XYZ_XB + 15 * SRGB_XYZ_YB + 3 * SRGB_XYZ_ZB) * 0x7fff) * b;
182 uns s = srgb_to_luv_tab3[sum >> (27 - SRGB_TO_LUV_TAB3_SIZE)];
183 int xs = ((u64)x * s) >> 32;
184 int ys = ((u64)y * s) >> 32;
185 int xw = ((4 * 13) << (SRGB_TO_LUV_TAB3_SCALE - 4)) *
186 REF_WHITE_X / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z);
187 int yw = ((9 * 13) << (SRGB_TO_LUV_TAB3_SCALE - 4)) *
188 REF_WHITE_Y / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z);
189 int u = (int)(l) * (xs - xw);
190 int v = (int)(l) * (ys - yw);
191 dest[1] = 128 + (u >> (SRGB_TO_LUV_TAB3_SCALE + SRGB_TO_LUV_TAB2_SCALE - 10));
192 dest[2] = 128 + (v >> (SRGB_TO_LUV_TAB3_SCALE + SRGB_TO_LUV_TAB2_SCALE - 10));
196 /****************** GENERAL INTERPOLATION IN 3D GRID ********************/
198 #define COLOR_CONV_SIZE 5 /* 128K conversion grid size */
199 #define COLOR_CONV_OFS 3 /* 8K interpolation table size */
201 struct color_grid_node {
205 struct color_interpolation_node {
210 extern struct color_grid_node *srgb_to_luv_grid;
211 extern struct color_interpolation_node *color_interpolation_table;
213 void color_conv_init(void);
214 void color_conv_pixels(byte *dest, byte *src, uns count, struct color_grid_node *grid);
216 #define COLOR_CONV_SCALE_CONST (((((1 << COLOR_CONV_SIZE) - 1) << 16) + (1 << (16 - COLOR_CONV_OFS))) / 255)
219 color_conv_pixel(byte *dest, byte *src, struct color_grid_node *grid)
221 uns s0 = src[0] * COLOR_CONV_SCALE_CONST;
222 uns s1 = src[1] * COLOR_CONV_SCALE_CONST;
223 uns s2 = src[2] * COLOR_CONV_SCALE_CONST;
224 struct color_grid_node *g0, *g1, *g2, *g3, *g = grid +
225 ((s0 >> 16) + ((s1 >> 16) << COLOR_CONV_SIZE) + ((s2 >> 16) << (2 * COLOR_CONV_SIZE)));
226 struct color_interpolation_node *n = color_interpolation_table +
227 (((s0 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - COLOR_CONV_OFS)) +
228 ((s1 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - 2 * COLOR_CONV_OFS)) +
229 ((s2 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - 3 * COLOR_CONV_OFS)));
234 dest[0] = (g0->val[0] * n->mul[0] + g1->val[0] * n->mul[1] +
235 g2->val[0] * n->mul[2] + g3->val[0] * n->mul[3] + 128) >> 8;
236 dest[1] = (g0->val[1] * n->mul[0] + g1->val[1] * n->mul[1] +
237 g2->val[1] * n->mul[2] + g3->val[1] * n->mul[3] + 128) >> 8;
238 dest[2] = (g0->val[2] * n->mul[0] + g1->val[2] * n->mul[1] +
239 g2->val[2] * n->mul[2] + g3->val[2] * n->mul[3] + 128) >> 8;