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