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