]> mj.ucw.cz Git - libucw.git/blob - images/color.h
Faster sRGB -> Luv colorspace conversion.
[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  *      FIXME:
10  *      - fix theoretical problems with rounding errors in srgb_to_luv_pixel()
11  *      - SIMD should help to speed up conversion of large arrays
12  *      - maybe try to generate a long switch in color_conv_pixel()
13  *        with optimized entries instead of access to interpolation table
14  */
15
16 #ifndef _IMAGES_COLOR_H
17 #define _IMAGES_COLOR_H
18
19 /* Exact slow conversion routines */
20 void srgb_to_xyz_slow(double dest[3], double src[3]);
21 void xyz_to_luv_slow(double dest[3], double src[3]);
22
23 /* Reference white */
24 #define REF_WHITE_X 0.96422
25 #define REF_WHITE_Y 1.
26 #define REF_WHITE_Z 0.82521
27
28 /* sRGB -> XYZ matrix */
29 #define SRGB_XYZ_XR 0.412424
30 #define SRGB_XYZ_XG 0.357579
31 #define SRGB_XYZ_XB 0.180464
32 #define SRGB_XYZ_YR 0.212656
33 #define SRGB_XYZ_YG 0.715158
34 #define SRGB_XYZ_YB 0.072186
35 #define SRGB_XYZ_ZR 0.019332
36 #define SRGB_XYZ_ZG 0.119193
37 #define SRGB_XYZ_ZB 0.950444
38
39
40 /*********************** OPTIMIZED CONVERSION ROUTINES **********************/
41
42 /* sRGB -> Luv parameters */
43 #define SRGB_TO_LUV_TAB2_SIZE 9
44 #define SRGB_TO_LUV_TAB2_SCALE 11
45 #define SRGB_TO_LUV_TAB3_SIZE 8
46 #define SRGB_TO_LUV_TAB3_SCALE (39 - SRGB_TO_LUV_TAB2_SCALE - SRGB_TO_LUV_TAB3_SIZE)
47
48 extern u16 srgb_to_luv_tab1[256];
49 extern u16 srgb_to_luv_tab2[9 << SRGB_TO_LUV_TAB2_SIZE];
50 extern u32 srgb_to_luv_tab3[20 << SRGB_TO_LUV_TAB3_SIZE];
51
52 void srgb_to_luv_init(void);
53 void srgb_to_luv_pixels(byte *dest, byte *src, uns count);
54
55 static inline void
56 srgb_to_luv_pixel(byte *dest, byte *src)
57 {
58   uns r = srgb_to_luv_tab1[src[0]];
59   uns g = srgb_to_luv_tab1[src[1]];
60   uns b = srgb_to_luv_tab1[src[2]];
61   uns x =
62     (uns)(4 * SRGB_XYZ_XR * 0xffff) * r +
63     (uns)(4 * SRGB_XYZ_XG * 0xffff) * g +
64     (uns)(4 * SRGB_XYZ_XB * 0xffff) * b;
65   uns y =
66     (uns)(9 * SRGB_XYZ_YR * 0xffff) * r +
67     (uns)(9 * SRGB_XYZ_YG * 0xffff) * g +
68     (uns)(9 * SRGB_XYZ_YB * 0xffff) * b;
69   uns l = srgb_to_luv_tab2[y >> (28 - SRGB_TO_LUV_TAB2_SIZE)];
70     dest[0] = l >> (SRGB_TO_LUV_TAB2_SCALE - 8);
71   uns sum =
72     (uns)((SRGB_XYZ_XR + 15 * SRGB_XYZ_YR + 3 * SRGB_XYZ_ZR) * 0x7fff) * r +
73     (uns)((SRGB_XYZ_XG + 15 * SRGB_XYZ_YG + 3 * SRGB_XYZ_ZG) * 0x7fff) * g +
74     (uns)((SRGB_XYZ_XB + 15 * SRGB_XYZ_YB + 3 * SRGB_XYZ_ZB) * 0x7fff) * b;
75   uns s = srgb_to_luv_tab3[sum >> (27 - SRGB_TO_LUV_TAB3_SIZE)];
76   int xs = ((u64)x * s) >> 32;
77   int ys = ((u64)y * s) >> 32;
78   int xw = ((4 * 13) << (SRGB_TO_LUV_TAB3_SCALE - 4)) *
79     REF_WHITE_X / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z);
80   int yw = ((9 * 13) << (SRGB_TO_LUV_TAB3_SCALE - 4)) *
81     REF_WHITE_Y / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z);
82   int u = (int)(l) * (xs - xw);
83   int v = (int)(l) * (ys - yw);
84   dest[1] = 128 + (u >> (SRGB_TO_LUV_TAB3_SCALE + SRGB_TO_LUV_TAB2_SCALE - 10));
85   dest[2] = 128 + (v >> (SRGB_TO_LUV_TAB3_SCALE + SRGB_TO_LUV_TAB2_SCALE - 10));
86 }
87
88
89 /****************** GENERAL INTERPOLATION IN 3D GRID ********************/
90
91 #define COLOR_CONV_SIZE 5  /* 128K conversion grid size */
92 #define COLOR_CONV_OFS  3  /* 8K interpolation table size */
93
94 struct color_grid_node {
95   byte val[4];
96 };
97
98 struct color_interpolation_node {
99   u16 ofs[4];
100   u16 mul[4];
101 };
102
103 extern struct color_grid_node *srgb_to_luv_grid;
104 extern struct color_interpolation_node *color_interpolation_table;
105
106 void color_conv_init(void);
107 void color_conv_pixels(byte *dest, byte *src, uns count, struct color_grid_node *grid);
108
109 #define COLOR_CONV_SCALE_CONST (((((1 << COLOR_CONV_SIZE) - 1) << 16) + (1 << (16 - COLOR_CONV_OFS))) / 255)
110
111 static inline void
112 color_conv_pixel(byte *dest, byte *src, struct color_grid_node *grid)
113 {
114   uns s0 = src[0] * COLOR_CONV_SCALE_CONST;
115   uns s1 = src[1] * COLOR_CONV_SCALE_CONST;
116   uns s2 = src[2] * COLOR_CONV_SCALE_CONST;
117   struct color_grid_node *g0, *g1, *g2, *g3, *g = grid +
118     ((s0 >> 16) + ((s1 >> 16) << COLOR_CONV_SIZE) + ((s2 >> 16) << (2 * COLOR_CONV_SIZE)));
119   struct color_interpolation_node *n = color_interpolation_table +
120     (((s0 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - COLOR_CONV_OFS)) +
121     ((s1 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - 2 * COLOR_CONV_OFS)) +
122     ((s2 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - 3 * COLOR_CONV_OFS)));
123   g0 = g + n->ofs[0];
124   g1 = g + n->ofs[1];
125   g2 = g + n->ofs[2];
126   g3 = g + n->ofs[3];
127   dest[0] = (g0->val[0] * n->mul[0] + g1->val[0] * n->mul[1] + 
128              g2->val[0] * n->mul[2] + g3->val[0] * n->mul[3] + 128) >> 8;
129   dest[1] = (g0->val[1] * n->mul[0] + g1->val[1] * n->mul[1] + 
130              g2->val[1] * n->mul[2] + g3->val[1] * n->mul[3] + 128) >> 8;
131   dest[2] = (g0->val[2] * n->mul[0] + g1->val[2] * n->mul[1] + 
132              g2->val[2] * n->mul[2] + g3->val[2] * n->mul[3] + 128) >> 8;
133 }
134
135 #endif