]> mj.ucw.cz Git - libucw.git/blob - images/color.h
8ec18e92c92e2a38cc04a664a1880289b8cfca21
[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  *      - http://www.tecgraf.puc-rio.br/~mgattass/color/ColorIndex.html
12  *
13  *      FIXME:
14  *      - fix theoretical problems with rounding errors in srgb_to_luv_pixel()
15  *      - SIMD should help to speed up conversion of large arrays
16  *      - maybe try to generate a long switch in color_conv_pixel()
17  *        with optimized entries instead of access to interpolation table
18  *      - most of multiplications in srgb_to_luv_pixels can be replaced
19  *        with tables lookup... tests shows almost the same speed for random
20  *        input and cca 40% gain when input colors fit in CPU chache
21  */
22
23 #ifndef _IMAGES_COLOR_H
24 #define _IMAGES_COLOR_H
25
26 #include "images/images.h"
27
28 static inline uns
29 rgb_to_gray_func(uns r, uns g, uns b)
30 {
31   return (r * 19660 + g * 38666 + b * 7210) >> 16;
32 }
33
34 extern struct color color_black, color_white;
35
36 static inline void
37 color_make_gray(struct color *color, uns gray)
38 {
39   color->c[0] = gray;
40   color->color_space = COLOR_SPACE_GRAYSCALE;
41 }
42
43 static inline void
44 color_make_rgb(struct color *color, uns r, uns g, uns b)
45 {
46   color->c[0] = r;
47   color->c[1] = g;
48   color->c[2] = b;
49   color->color_space = COLOR_SPACE_RGB;
50 }
51
52 void color_put_color_space(byte *dest, struct color *color, enum color_space color_space);
53 void color_put_grayscale(byte *dest, struct color *color);
54 void color_put_rgb(byte *dest, struct color *color);
55
56 /* Exact slow conversion routines */
57 void srgb_to_xyz_slow(double dest[3], double src[3]);
58 void xyz_to_luv_slow(double dest[3], double src[3]);
59
60 /* Reference white */
61 #define REF_WHITE_X 0.96422
62 #define REF_WHITE_Y 1.
63 #define REF_WHITE_Z 0.82521
64
65 /* sRGB -> XYZ matrix */
66 #define SRGB_XYZ_XR 0.412424
67 #define SRGB_XYZ_XG 0.357579
68 #define SRGB_XYZ_XB 0.180464
69 #define SRGB_XYZ_YR 0.212656
70 #define SRGB_XYZ_YG 0.715158
71 #define SRGB_XYZ_YB 0.072186
72 #define SRGB_XYZ_ZR 0.019332
73 #define SRGB_XYZ_ZG 0.119193
74 #define SRGB_XYZ_ZB 0.950444
75
76
77 /*********************** OPTIMIZED CONVERSION ROUTINES **********************/
78
79 /* sRGB -> Luv parameters */
80 #define SRGB_TO_LUV_TAB2_SIZE 9
81 #define SRGB_TO_LUV_TAB2_SCALE 11
82 #define SRGB_TO_LUV_TAB3_SIZE 8
83 #define SRGB_TO_LUV_TAB3_SCALE (39 - SRGB_TO_LUV_TAB2_SCALE - SRGB_TO_LUV_TAB3_SIZE)
84
85 extern u16 srgb_to_luv_tab1[256];
86 extern u16 srgb_to_luv_tab2[9 << SRGB_TO_LUV_TAB2_SIZE];
87 extern u32 srgb_to_luv_tab3[20 << SRGB_TO_LUV_TAB3_SIZE];
88
89 void srgb_to_luv_init(void);
90 void srgb_to_luv_pixels(byte *dest, byte *src, uns count);
91
92 /* L covers the interval [0..255]; u and v are centered to 128 and scaled by 1/4 in respect of L */
93 static inline void
94 srgb_to_luv_pixel(byte *dest, byte *src)
95 {
96   uns r = srgb_to_luv_tab1[src[0]];
97   uns g = srgb_to_luv_tab1[src[1]];
98   uns b = srgb_to_luv_tab1[src[2]];
99   uns x =
100     (uns)(4 * SRGB_XYZ_XR * 0xffff) * r +
101     (uns)(4 * SRGB_XYZ_XG * 0xffff) * g +
102     (uns)(4 * SRGB_XYZ_XB * 0xffff) * b;
103   uns y =
104     (uns)(9 * SRGB_XYZ_YR * 0xffff) * r +
105     (uns)(9 * SRGB_XYZ_YG * 0xffff) * g +
106     (uns)(9 * SRGB_XYZ_YB * 0xffff) * b;
107   uns l = srgb_to_luv_tab2[y >> (28 - SRGB_TO_LUV_TAB2_SIZE)];
108     dest[0] = l >> (SRGB_TO_LUV_TAB2_SCALE - 8);
109   uns sum =
110     (uns)((SRGB_XYZ_XR + 15 * SRGB_XYZ_YR + 3 * SRGB_XYZ_ZR) * 0x7fff) * r +
111     (uns)((SRGB_XYZ_XG + 15 * SRGB_XYZ_YG + 3 * SRGB_XYZ_ZG) * 0x7fff) * g +
112     (uns)((SRGB_XYZ_XB + 15 * SRGB_XYZ_YB + 3 * SRGB_XYZ_ZB) * 0x7fff) * b;
113   uns s = srgb_to_luv_tab3[sum >> (27 - SRGB_TO_LUV_TAB3_SIZE)];
114   int xs = ((u64)x * s) >> 32;
115   int ys = ((u64)y * s) >> 32;
116   int xw = ((4 * 13) << (SRGB_TO_LUV_TAB3_SCALE - 4)) *
117     REF_WHITE_X / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z);
118   int yw = ((9 * 13) << (SRGB_TO_LUV_TAB3_SCALE - 4)) *
119     REF_WHITE_Y / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z);
120   int u = (int)(l) * (xs - xw);
121   int v = (int)(l) * (ys - yw);
122   dest[1] = 128 + (u >> (SRGB_TO_LUV_TAB3_SCALE + SRGB_TO_LUV_TAB2_SCALE - 10));
123   dest[2] = 128 + (v >> (SRGB_TO_LUV_TAB3_SCALE + SRGB_TO_LUV_TAB2_SCALE - 10));
124 }
125
126
127 /****************** GENERAL INTERPOLATION IN 3D GRID ********************/
128
129 #define COLOR_CONV_SIZE 5  /* 128K conversion grid size */
130 #define COLOR_CONV_OFS  3  /* 8K interpolation table size */
131
132 struct color_grid_node {
133   byte val[4];
134 };
135
136 struct color_interpolation_node {
137   u16 ofs[4];
138   u16 mul[4];
139 };
140
141 extern struct color_grid_node *srgb_to_luv_grid;
142 extern struct color_interpolation_node *color_interpolation_table;
143
144 void color_conv_init(void);
145 void color_conv_pixels(byte *dest, byte *src, uns count, struct color_grid_node *grid);
146
147 #define COLOR_CONV_SCALE_CONST (((((1 << COLOR_CONV_SIZE) - 1) << 16) + (1 << (16 - COLOR_CONV_OFS))) / 255)
148
149 static inline void
150 color_conv_pixel(byte *dest, byte *src, struct color_grid_node *grid)
151 {
152   uns s0 = src[0] * COLOR_CONV_SCALE_CONST;
153   uns s1 = src[1] * COLOR_CONV_SCALE_CONST;
154   uns s2 = src[2] * COLOR_CONV_SCALE_CONST;
155   struct color_grid_node *g0, *g1, *g2, *g3, *g = grid +
156     ((s0 >> 16) + ((s1 >> 16) << COLOR_CONV_SIZE) + ((s2 >> 16) << (2 * COLOR_CONV_SIZE)));
157   struct color_interpolation_node *n = color_interpolation_table +
158     (((s0 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - COLOR_CONV_OFS)) +
159     ((s1 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - 2 * COLOR_CONV_OFS)) +
160     ((s2 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - 3 * COLOR_CONV_OFS)));
161   g0 = g + n->ofs[0];
162   g1 = g + n->ofs[1];
163   g2 = g + n->ofs[2];
164   g3 = g + n->ofs[3];
165   dest[0] = (g0->val[0] * n->mul[0] + g1->val[0] * n->mul[1] +
166              g2->val[0] * n->mul[2] + g3->val[0] * n->mul[3] + 128) >> 8;
167   dest[1] = (g0->val[1] * n->mul[0] + g1->val[1] * n->mul[1] +
168              g2->val[1] * n->mul[2] + g3->val[1] * n->mul[3] + 128) >> 8;
169   dest[2] = (g0->val[2] * n->mul[0] + g1->val[2] * n->mul[1] +
170              g2->val[2] * n->mul[2] + g3->val[2] * n->mul[3] + 128) >> 8;
171 }
172
173 #endif