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>
30 #ifdef CONFIG_UCW_CLEAN_ABI
31 #define cmyk_to_rgb_exact ucw_cmyk_to_rgb_exact
32 #define color_adobe_rgb_info ucw_color_adobe_rgb_info
33 #define color_apple_rgb_info ucw_color_apple_rgb_info
34 #define color_black ucw_color_black
35 #define color_cie_rgb_info ucw_color_cie_rgb_info
36 #define color_color_match_rgb_info ucw_color_color_match_rgb_info
37 #define color_compute_bradford_matrix ucw_color_compute_bradford_matrix
38 #define color_compute_color_space_to_xyz_matrix ucw_color_compute_color_space_to_xyz_matrix
39 #define color_compute_color_spaces_conversion_matrix ucw_color_compute_color_spaces_conversion_matrix
40 #define color_conv_init ucw_color_conv_init
41 #define color_conv_pixels ucw_color_conv_pixels
42 #define color_get ucw_color_get
43 #define color_illuminant_d50 ucw_color_illuminant_d50
44 #define color_illuminant_d65 ucw_color_illuminant_d65
45 #define color_illuminant_e ucw_color_illuminant_e
46 #define color_interpolation_table ucw_color_interpolation_table
47 #define color_invert_matrix ucw_color_invert_matrix
48 #define color_put ucw_color_put
49 #define color_space_channels ucw_color_space_channels
50 #define color_space_id_to_name ucw_color_space_id_to_name
51 #define color_space_name ucw_color_space_name
52 #define color_space_name_to_id ucw_color_space_name_to_id
53 #define color_srgb_info ucw_color_srgb_info
54 #define color_white ucw_color_white
55 #define image_conv ucw_image_conv
56 #define image_conv_defaults ucw_image_conv_defaults
57 #define luv_to_xyz_exact ucw_luv_to_xyz_exact
58 #define rgb_to_cmyk_exact ucw_rgb_to_cmyk_exact
59 #define srgb_to_luv_grid ucw_srgb_to_luv_grid
60 #define srgb_to_luv_init ucw_srgb_to_luv_init
61 #define srgb_to_luv_pixels ucw_srgb_to_luv_pixels
62 #define srgb_to_luv_tab1 ucw_srgb_to_luv_tab1
63 #define srgb_to_luv_tab2 ucw_srgb_to_luv_tab2
64 #define srgb_to_luv_tab3 ucw_srgb_to_luv_tab3
65 #define srgb_to_xyz_exact ucw_srgb_to_xyz_exact
66 #define xyz_to_luv_exact ucw_xyz_to_luv_exact
67 #define xyz_to_srgb_exact ucw_xyz_to_srgb_exact
70 /* Basic color spaces */
72 COLOR_SPACE_UNKNOWN = 0,
73 COLOR_SPACE_UNKNOWN_1 = 1, /* unknown 1-channel color space */
74 COLOR_SPACE_UNKNOWN_2 = 2, /* unknown 2-channels color space */
75 COLOR_SPACE_UNKNOWN_3 = 3, /* unknown 3-channels color space */
76 COLOR_SPACE_UNKNOWN_4 = 4, /* unknown 4-channels color space */
77 COLOR_SPACE_UNKNOWN_MAX = 4,
78 COLOR_SPACE_GRAYSCALE,
89 extern uint color_space_channels[COLOR_SPACE_MAX];
90 extern byte *color_space_name[COLOR_SPACE_MAX];
92 /* Color space ID <-> name conversions */
93 byte *color_space_id_to_name(uint id);
94 uint color_space_name_to_id(byte *name);
96 /* Struct color manipulation */
97 int color_get(struct color *color, byte *src, uint src_space);
98 int color_put(struct image_context *ctx, struct color *color, byte *dest, uint dest_space);
100 static inline void color_make_gray(struct color *color, uint gray)
103 color->color_space = COLOR_SPACE_GRAYSCALE;
106 static inline void color_make_rgb(struct color *color, uint r, uint g, uint b)
111 color->color_space = COLOR_SPACE_RGB;
114 extern struct color color_black, color_white;
116 /* Conversion between various pixel formats */
119 IMAGE_CONV_FILL_ALPHA = 1,
120 IMAGE_CONV_COPY_ALPHA = 2,
121 IMAGE_CONV_APPLY_ALPHA = 4,
124 struct image_conv_options {
126 struct color background;
129 extern struct image_conv_options image_conv_defaults;
131 int image_conv(struct image_context *ctx, struct image *dest, struct image *src, struct image_conv_options *opt);
133 /* Color spaces in the CIE 1931 chromacity diagram */
135 struct color_space_chromacity_info {
142 struct color_space_gamma_info {
144 double detailed_gamma;
150 struct color_space_info {
152 struct color_space_chromacity_info chromacity;
153 struct color_space_gamma_info gamma;
157 color_illuminant_d50[2],
158 color_illuminant_d65[2],
159 color_illuminant_e[2];
161 extern const struct color_space_info
162 color_adobe_rgb_info, /* Adobe RGB (1998) */
163 color_apple_rgb_info, /* Apple RGB */
164 color_cie_rgb_info, /* CIE RGB */
165 color_color_match_rgb_info, /* ColorMatch RGB */
166 color_srgb_info; /* sRGB */
168 /* These routines do not check numeric errors! */
169 void color_compute_color_space_to_xyz_matrix(double matrix[9], const struct color_space_chromacity_info *space);
170 void color_compute_bradford_matrix(double matrix[9], const double src[2], const double dest[2]);
171 void color_compute_color_spaces_conversion_matrix(double matrix[9], const struct color_space_chromacity_info *src, const struct color_space_chromacity_info *dest);
172 void color_invert_matrix(double dest[9], double matrix[9]);
174 static inline uint rgb_to_gray_func(uint r, uint g, uint b)
176 return (r * 19660 + g * 38666 + b * 7210) >> 16;
179 /* Exact slow conversion routines */
180 void srgb_to_xyz_exact(double dest[3], double src[3]);
181 void xyz_to_srgb_exact(double dest[3], double src[3]);
182 void xyz_to_luv_exact(double dest[3], double src[3]);
183 void luv_to_xyz_exact(double dest[3], double src[3]);
184 void rgb_to_cmyk_exact(double dest[4], double src[3]);
185 void cmyk_to_rgb_exact(double dest[3], double src[4]);
187 /* Reference white */
188 #define REF_WHITE_X 0.96422
189 #define REF_WHITE_Y 1.
190 #define REF_WHITE_Z 0.82521
192 /* sRGB -> XYZ matrix */
193 #define SRGB_XYZ_XR 0.412424
194 #define SRGB_XYZ_XG 0.357579
195 #define SRGB_XYZ_XB 0.180464
196 #define SRGB_XYZ_YR 0.212656
197 #define SRGB_XYZ_YG 0.715158
198 #define SRGB_XYZ_YB 0.072186
199 #define SRGB_XYZ_ZR 0.019332
200 #define SRGB_XYZ_ZG 0.119193
201 #define SRGB_XYZ_ZB 0.950444
204 /*********************** OPTIMIZED CONVERSION ROUTINES **********************/
206 /* sRGB -> Luv parameters */
207 #define SRGB_TO_LUV_TAB2_SIZE 9
208 #define SRGB_TO_LUV_TAB2_SCALE 11
209 #define SRGB_TO_LUV_TAB3_SIZE 8
210 #define SRGB_TO_LUV_TAB3_SCALE (39 - SRGB_TO_LUV_TAB2_SCALE - SRGB_TO_LUV_TAB3_SIZE)
212 extern u16 srgb_to_luv_tab1[256];
213 extern u16 srgb_to_luv_tab2[9 << SRGB_TO_LUV_TAB2_SIZE];
214 extern u32 srgb_to_luv_tab3[20 << SRGB_TO_LUV_TAB3_SIZE];
216 void srgb_to_luv_init(void);
217 void srgb_to_luv_pixels(byte *dest, byte *src, uint count);
219 /* L covers the interval [0..255]; u and v are centered to 128 and scaled by 1/4 in respect of L */
220 static inline void srgb_to_luv_pixel(byte *dest, byte *src)
222 uint r = srgb_to_luv_tab1[src[0]];
223 uint g = srgb_to_luv_tab1[src[1]];
224 uint b = srgb_to_luv_tab1[src[2]];
226 (uint)(4 * SRGB_XYZ_XR * 0xffff) * r +
227 (uint)(4 * SRGB_XYZ_XG * 0xffff) * g +
228 (uint)(4 * SRGB_XYZ_XB * 0xffff) * b;
230 (uint)(9 * SRGB_XYZ_YR * 0xffff) * r +
231 (uint)(9 * SRGB_XYZ_YG * 0xffff) * g +
232 (uint)(9 * SRGB_XYZ_YB * 0xffff) * b;
233 uint l = srgb_to_luv_tab2[y >> (28 - SRGB_TO_LUV_TAB2_SIZE)];
234 dest[0] = l >> (SRGB_TO_LUV_TAB2_SCALE - 8);
236 (uint)((SRGB_XYZ_XR + 15 * SRGB_XYZ_YR + 3 * SRGB_XYZ_ZR) * 0x7fff) * r +
237 (uint)((SRGB_XYZ_XG + 15 * SRGB_XYZ_YG + 3 * SRGB_XYZ_ZG) * 0x7fff) * g +
238 (uint)((SRGB_XYZ_XB + 15 * SRGB_XYZ_YB + 3 * SRGB_XYZ_ZB) * 0x7fff) * b;
239 uint s = srgb_to_luv_tab3[sum >> (27 - SRGB_TO_LUV_TAB3_SIZE)];
240 int xs = ((u64)x * s) >> 32;
241 int ys = ((u64)y * s) >> 32;
242 int xw = ((4 * 13) << (SRGB_TO_LUV_TAB3_SCALE - 4)) *
243 REF_WHITE_X / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z);
244 int yw = ((9 * 13) << (SRGB_TO_LUV_TAB3_SCALE - 4)) *
245 REF_WHITE_Y / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z);
246 int u = (int)(l) * (xs - xw);
247 int v = (int)(l) * (ys - yw);
248 dest[1] = 128 + (u >> (SRGB_TO_LUV_TAB3_SCALE + SRGB_TO_LUV_TAB2_SCALE - 10));
249 dest[2] = 128 + (v >> (SRGB_TO_LUV_TAB3_SCALE + SRGB_TO_LUV_TAB2_SCALE - 10));
253 /****************** GENERAL INTERPOLATION IN 3D GRID ********************/
255 #define COLOR_CONV_SIZE 5 /* 128K conversion grid size */
256 #define COLOR_CONV_OFS 3 /* 8K interpolation table size */
258 struct color_grid_node {
262 struct color_interpolation_node {
267 extern struct color_grid_node *srgb_to_luv_grid;
268 extern struct color_interpolation_node *color_interpolation_table;
270 void color_conv_init(void);
271 void color_conv_pixels(byte *dest, byte *src, uint count, struct color_grid_node *grid);
273 #define COLOR_CONV_SCALE_CONST (((((1 << COLOR_CONV_SIZE) - 1) << 16) + (1 << (16 - COLOR_CONV_OFS))) / 255)
275 static inline void color_conv_pixel(byte *dest, byte *src, struct color_grid_node *grid)
277 uint s0 = src[0] * COLOR_CONV_SCALE_CONST;
278 uint s1 = src[1] * COLOR_CONV_SCALE_CONST;
279 uint s2 = src[2] * COLOR_CONV_SCALE_CONST;
280 struct color_grid_node *g0, *g1, *g2, *g3, *g = grid +
281 ((s0 >> 16) + ((s1 >> 16) << COLOR_CONV_SIZE) + ((s2 >> 16) << (2 * COLOR_CONV_SIZE)));
282 struct color_interpolation_node *n = color_interpolation_table +
283 (((s0 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - COLOR_CONV_OFS)) +
284 ((s1 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - 2 * COLOR_CONV_OFS)) +
285 ((s2 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - 3 * COLOR_CONV_OFS)));
290 dest[0] = (g0->val[0] * n->mul[0] + g1->val[0] * n->mul[1] +
291 g2->val[0] * n->mul[2] + g3->val[0] * n->mul[3] + 128) >> 8;
292 dest[1] = (g0->val[1] * n->mul[0] + g1->val[1] * n->mul[1] +
293 g2->val[1] * n->mul[2] + g3->val[1] * n->mul[3] + 128) >> 8;
294 dest[2] = (g0->val[2] * n->mul[0] + g1->val[2] * n->mul[1] +
295 g2->val[2] * n->mul[2] + g3->val[2] * n->mul[3] + 128) >> 8;