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