]> mj.ucw.cz Git - libucw.git/blob - images/color.h
8c87e87c5414500d89a77bdb603e9a21dd5943c3
[libucw.git] / images / color.h
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  *      References:
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
14  *
15  *      FIXME:
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
23  */
24
25 #ifndef _IMAGES_COLOR_H
26 #define _IMAGES_COLOR_H
27
28 #include "images/images.h"
29
30 // A comparison of four multimedia RGB spaces, Danny Pascale
31
32 enum {
33   COLOR_SPACE_UNKNOWN = 0,
34   COLOR_SPACE_GRAYSCALE,
35   COLOR_SPACE_RGB,
36   COLOR_SPACE_XYZ,
37   COLOR_SPACE_LAB,
38   COLOR_SPACE_LUV,
39   COLOR_SPACE_YCBCR,
40   COLOR_SPACE_MAX
41 };
42
43 /* Color spaces in the CIE 1931 chromacity diagram */
44
45 struct color_space_chromacity_info {
46   double prim1[2];
47   double prim2[2];
48   double prim3[2];
49   double white[2];
50 };
51
52 struct color_space_gamma_info {
53   double simple_gamma;
54   double detailed_gamma;
55   double offset;
56   double transition;
57   double slope;
58 };
59
60 struct color_space_info {
61   byte *name;
62   struct color_space_chromacity_info chromacity;
63   struct color_space_gamma_info gamma;
64 };
65
66 extern const double
67   color_illuminant_d50[2],
68   color_illuminant_d65[2],
69   color_illuminant_e[2];
70
71 extern const struct color_space_info
72   color_adobe_rgb_info,         /* Adobe RGB (1998) */
73   color_apple_rgb_info,         /* Apple RGB */
74   color_cie_rgb_info,           /* CIE RGB */
75   color_color_match_rgb_info,   /* ColorMatch RGB */
76   color_srgb_info;              /* sRGB */
77
78 /* These routines do not check numeric errors! */
79 void color_compute_color_space_to_xyz_matrix(double matrix[9], const struct color_space_chromacity_info *space);
80 void color_compute_bradford_matrix(double matrix[9], const double src[2], const double dest[2]);
81 void color_compute_color_spaces_conversion_matrix(double matrix[9], const struct color_space_chromacity_info *src, const struct color_space_chromacity_info *dest);
82 void color_invert_matrix(double dest[9], double matrix[9]);
83
84 static inline uns
85 rgb_to_gray_func(uns r, uns g, uns b)
86 {
87   return (r * 19660 + g * 38666 + b * 7210) >> 16;
88 }
89
90 extern struct color color_black, color_white;
91
92 static inline void
93 color_make_gray(struct color *color, uns gray)
94 {
95   color->c[0] = gray;
96   color->color_space = COLOR_SPACE_GRAYSCALE;
97 }
98
99 static inline void
100 color_make_rgb(struct color *color, uns r, uns g, uns b)
101 {
102   color->c[0] = r;
103   color->c[1] = g;
104   color->c[2] = b;
105   color->color_space = COLOR_SPACE_RGB;
106 }
107
108 void color_put_color_space(byte *dest, struct color *color, uns color_space);
109 void color_put_grayscale(byte *dest, struct color *color);
110 void color_put_rgb(byte *dest, struct color *color);
111
112 /* Exact slow conversion routines */
113 void srgb_to_xyz_exact(double dest[3], double src[3]);
114 void xyz_to_srgb_exact(double dest[3], double src[3]);
115 void xyz_to_luv_exact(double dest[3], double src[3]);
116 void luv_to_xyz_exact(double dest[3], double src[3]);
117
118 /* Reference white */
119 #define REF_WHITE_X 0.96422
120 #define REF_WHITE_Y 1.
121 #define REF_WHITE_Z 0.82521
122
123 /* sRGB -> XYZ matrix */
124 #define SRGB_XYZ_XR 0.412424
125 #define SRGB_XYZ_XG 0.357579
126 #define SRGB_XYZ_XB 0.180464
127 #define SRGB_XYZ_YR 0.212656
128 #define SRGB_XYZ_YG 0.715158
129 #define SRGB_XYZ_YB 0.072186
130 #define SRGB_XYZ_ZR 0.019332
131 #define SRGB_XYZ_ZG 0.119193
132 #define SRGB_XYZ_ZB 0.950444
133
134
135 /*********************** OPTIMIZED CONVERSION ROUTINES **********************/
136
137 /* sRGB -> Luv parameters */
138 #define SRGB_TO_LUV_TAB2_SIZE 9
139 #define SRGB_TO_LUV_TAB2_SCALE 11
140 #define SRGB_TO_LUV_TAB3_SIZE 8
141 #define SRGB_TO_LUV_TAB3_SCALE (39 - SRGB_TO_LUV_TAB2_SCALE - SRGB_TO_LUV_TAB3_SIZE)
142
143 extern u16 srgb_to_luv_tab1[256];
144 extern u16 srgb_to_luv_tab2[9 << SRGB_TO_LUV_TAB2_SIZE];
145 extern u32 srgb_to_luv_tab3[20 << SRGB_TO_LUV_TAB3_SIZE];
146
147 void srgb_to_luv_init(void);
148 void srgb_to_luv_pixels(byte *dest, byte *src, uns count);
149
150 /* L covers the interval [0..255]; u and v are centered to 128 and scaled by 1/4 in respect of L */
151 static inline void
152 srgb_to_luv_pixel(byte *dest, byte *src)
153 {
154   uns r = srgb_to_luv_tab1[src[0]];
155   uns g = srgb_to_luv_tab1[src[1]];
156   uns b = srgb_to_luv_tab1[src[2]];
157   uns x =
158     (uns)(4 * SRGB_XYZ_XR * 0xffff) * r +
159     (uns)(4 * SRGB_XYZ_XG * 0xffff) * g +
160     (uns)(4 * SRGB_XYZ_XB * 0xffff) * b;
161   uns y =
162     (uns)(9 * SRGB_XYZ_YR * 0xffff) * r +
163     (uns)(9 * SRGB_XYZ_YG * 0xffff) * g +
164     (uns)(9 * SRGB_XYZ_YB * 0xffff) * b;
165   uns l = srgb_to_luv_tab2[y >> (28 - SRGB_TO_LUV_TAB2_SIZE)];
166     dest[0] = l >> (SRGB_TO_LUV_TAB2_SCALE - 8);
167   uns sum =
168     (uns)((SRGB_XYZ_XR + 15 * SRGB_XYZ_YR + 3 * SRGB_XYZ_ZR) * 0x7fff) * r +
169     (uns)((SRGB_XYZ_XG + 15 * SRGB_XYZ_YG + 3 * SRGB_XYZ_ZG) * 0x7fff) * g +
170     (uns)((SRGB_XYZ_XB + 15 * SRGB_XYZ_YB + 3 * SRGB_XYZ_ZB) * 0x7fff) * b;
171   uns s = srgb_to_luv_tab3[sum >> (27 - SRGB_TO_LUV_TAB3_SIZE)];
172   int xs = ((u64)x * s) >> 32;
173   int ys = ((u64)y * s) >> 32;
174   int xw = ((4 * 13) << (SRGB_TO_LUV_TAB3_SCALE - 4)) *
175     REF_WHITE_X / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z);
176   int yw = ((9 * 13) << (SRGB_TO_LUV_TAB3_SCALE - 4)) *
177     REF_WHITE_Y / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z);
178   int u = (int)(l) * (xs - xw);
179   int v = (int)(l) * (ys - yw);
180   dest[1] = 128 + (u >> (SRGB_TO_LUV_TAB3_SCALE + SRGB_TO_LUV_TAB2_SCALE - 10));
181   dest[2] = 128 + (v >> (SRGB_TO_LUV_TAB3_SCALE + SRGB_TO_LUV_TAB2_SCALE - 10));
182 }
183
184
185 /****************** GENERAL INTERPOLATION IN 3D GRID ********************/
186
187 #define COLOR_CONV_SIZE 5  /* 128K conversion grid size */
188 #define COLOR_CONV_OFS  3  /* 8K interpolation table size */
189
190 struct color_grid_node {
191   byte val[4];
192 };
193
194 struct color_interpolation_node {
195   u16 ofs[4];
196   u16 mul[4];
197 };
198
199 extern struct color_grid_node *srgb_to_luv_grid;
200 extern struct color_interpolation_node *color_interpolation_table;
201
202 void color_conv_init(void);
203 void color_conv_pixels(byte *dest, byte *src, uns count, struct color_grid_node *grid);
204
205 #define COLOR_CONV_SCALE_CONST (((((1 << COLOR_CONV_SIZE) - 1) << 16) + (1 << (16 - COLOR_CONV_OFS))) / 255)
206
207 static inline void
208 color_conv_pixel(byte *dest, byte *src, struct color_grid_node *grid)
209 {
210   uns s0 = src[0] * COLOR_CONV_SCALE_CONST;
211   uns s1 = src[1] * COLOR_CONV_SCALE_CONST;
212   uns s2 = src[2] * COLOR_CONV_SCALE_CONST;
213   struct color_grid_node *g0, *g1, *g2, *g3, *g = grid +
214     ((s0 >> 16) + ((s1 >> 16) << COLOR_CONV_SIZE) + ((s2 >> 16) << (2 * COLOR_CONV_SIZE)));
215   struct color_interpolation_node *n = color_interpolation_table +
216     (((s0 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - COLOR_CONV_OFS)) +
217     ((s1 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - 2 * COLOR_CONV_OFS)) +
218     ((s2 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - 3 * COLOR_CONV_OFS)));
219   g0 = g + n->ofs[0];
220   g1 = g + n->ofs[1];
221   g2 = g + n->ofs[2];
222   g3 = g + n->ofs[3];
223   dest[0] = (g0->val[0] * n->mul[0] + g1->val[0] * n->mul[1] +
224              g2->val[0] * n->mul[2] + g3->val[0] * n->mul[3] + 128) >> 8;
225   dest[1] = (g0->val[1] * n->mul[0] + g1->val[1] * n->mul[1] +
226              g2->val[1] * n->mul[2] + g3->val[1] * n->mul[3] + 128) >> 8;
227   dest[2] = (g0->val[2] * n->mul[0] + g1->val[2] * n->mul[1] +
228              g2->val[2] * n->mul[2] + g3->val[2] * n->mul[3] + 128) >> 8;
229 }
230
231 #endif