]> mj.ucw.cz Git - libucw.git/blob - images/color.h
reverted invalid usage of GET*/PUT* macros
[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_srgb_slow(double dest[3], double src[3]);
59 void xyz_to_luv_slow(double dest[3], double src[3]);
60 void luv_to_xyz_slow(double dest[3], double src[3]);
61
62 /* Reference white */
63 #define REF_WHITE_X 0.96422
64 #define REF_WHITE_Y 1.
65 #define REF_WHITE_Z 0.82521
66
67 /* sRGB -> XYZ matrix */
68 #define SRGB_XYZ_XR 0.412424
69 #define SRGB_XYZ_XG 0.357579
70 #define SRGB_XYZ_XB 0.180464
71 #define SRGB_XYZ_YR 0.212656
72 #define SRGB_XYZ_YG 0.715158
73 #define SRGB_XYZ_YB 0.072186
74 #define SRGB_XYZ_ZR 0.019332
75 #define SRGB_XYZ_ZG 0.119193
76 #define SRGB_XYZ_ZB 0.950444
77
78
79 /*********************** OPTIMIZED CONVERSION ROUTINES **********************/
80
81 /* sRGB -> Luv parameters */
82 #define SRGB_TO_LUV_TAB2_SIZE 9
83 #define SRGB_TO_LUV_TAB2_SCALE 11
84 #define SRGB_TO_LUV_TAB3_SIZE 8
85 #define SRGB_TO_LUV_TAB3_SCALE (39 - SRGB_TO_LUV_TAB2_SCALE - SRGB_TO_LUV_TAB3_SIZE)
86
87 extern u16 srgb_to_luv_tab1[256];
88 extern u16 srgb_to_luv_tab2[9 << SRGB_TO_LUV_TAB2_SIZE];
89 extern u32 srgb_to_luv_tab3[20 << SRGB_TO_LUV_TAB3_SIZE];
90
91 void srgb_to_luv_init(void);
92 void srgb_to_luv_pixels(byte *dest, byte *src, uns count);
93
94 /* L covers the interval [0..255]; u and v are centered to 128 and scaled by 1/4 in respect of L */
95 static inline void
96 srgb_to_luv_pixel(byte *dest, byte *src)
97 {
98   uns r = srgb_to_luv_tab1[src[0]];
99   uns g = srgb_to_luv_tab1[src[1]];
100   uns b = srgb_to_luv_tab1[src[2]];
101   uns x =
102     (uns)(4 * SRGB_XYZ_XR * 0xffff) * r +
103     (uns)(4 * SRGB_XYZ_XG * 0xffff) * g +
104     (uns)(4 * SRGB_XYZ_XB * 0xffff) * b;
105   uns y =
106     (uns)(9 * SRGB_XYZ_YR * 0xffff) * r +
107     (uns)(9 * SRGB_XYZ_YG * 0xffff) * g +
108     (uns)(9 * SRGB_XYZ_YB * 0xffff) * b;
109   uns l = srgb_to_luv_tab2[y >> (28 - SRGB_TO_LUV_TAB2_SIZE)];
110     dest[0] = l >> (SRGB_TO_LUV_TAB2_SCALE - 8);
111   uns sum =
112     (uns)((SRGB_XYZ_XR + 15 * SRGB_XYZ_YR + 3 * SRGB_XYZ_ZR) * 0x7fff) * r +
113     (uns)((SRGB_XYZ_XG + 15 * SRGB_XYZ_YG + 3 * SRGB_XYZ_ZG) * 0x7fff) * g +
114     (uns)((SRGB_XYZ_XB + 15 * SRGB_XYZ_YB + 3 * SRGB_XYZ_ZB) * 0x7fff) * b;
115   uns s = srgb_to_luv_tab3[sum >> (27 - SRGB_TO_LUV_TAB3_SIZE)];
116   int xs = ((u64)x * s) >> 32;
117   int ys = ((u64)y * s) >> 32;
118   int xw = ((4 * 13) << (SRGB_TO_LUV_TAB3_SCALE - 4)) *
119     REF_WHITE_X / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z);
120   int yw = ((9 * 13) << (SRGB_TO_LUV_TAB3_SCALE - 4)) *
121     REF_WHITE_Y / (REF_WHITE_X + 15 * REF_WHITE_Y + 3 * REF_WHITE_Z);
122   int u = (int)(l) * (xs - xw);
123   int v = (int)(l) * (ys - yw);
124   dest[1] = 128 + (u >> (SRGB_TO_LUV_TAB3_SCALE + SRGB_TO_LUV_TAB2_SCALE - 10));
125   dest[2] = 128 + (v >> (SRGB_TO_LUV_TAB3_SCALE + SRGB_TO_LUV_TAB2_SCALE - 10));
126 }
127
128
129 /****************** GENERAL INTERPOLATION IN 3D GRID ********************/
130
131 #define COLOR_CONV_SIZE 5  /* 128K conversion grid size */
132 #define COLOR_CONV_OFS  3  /* 8K interpolation table size */
133
134 struct color_grid_node {
135   byte val[4];
136 };
137
138 struct color_interpolation_node {
139   u16 ofs[4];
140   u16 mul[4];
141 };
142
143 extern struct color_grid_node *srgb_to_luv_grid;
144 extern struct color_interpolation_node *color_interpolation_table;
145
146 void color_conv_init(void);
147 void color_conv_pixels(byte *dest, byte *src, uns count, struct color_grid_node *grid);
148
149 #define COLOR_CONV_SCALE_CONST (((((1 << COLOR_CONV_SIZE) - 1) << 16) + (1 << (16 - COLOR_CONV_OFS))) / 255)
150
151 static inline void
152 color_conv_pixel(byte *dest, byte *src, struct color_grid_node *grid)
153 {
154   uns s0 = src[0] * COLOR_CONV_SCALE_CONST;
155   uns s1 = src[1] * COLOR_CONV_SCALE_CONST;
156   uns s2 = src[2] * COLOR_CONV_SCALE_CONST;
157   struct color_grid_node *g0, *g1, *g2, *g3, *g = grid +
158     ((s0 >> 16) + ((s1 >> 16) << COLOR_CONV_SIZE) + ((s2 >> 16) << (2 * COLOR_CONV_SIZE)));
159   struct color_interpolation_node *n = color_interpolation_table +
160     (((s0 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - COLOR_CONV_OFS)) +
161     ((s1 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - 2 * COLOR_CONV_OFS)) +
162     ((s2 & (0x10000 - (0x10000 >> COLOR_CONV_OFS))) >> (16 - 3 * COLOR_CONV_OFS)));
163   g0 = g + n->ofs[0];
164   g1 = g + n->ofs[1];
165   g2 = g + n->ofs[2];
166   g3 = g + n->ofs[3];
167   dest[0] = (g0->val[0] * n->mul[0] + g1->val[0] * n->mul[1] +
168              g2->val[0] * n->mul[2] + g3->val[0] * n->mul[3] + 128) >> 8;
169   dest[1] = (g0->val[1] * n->mul[0] + g1->val[1] * n->mul[1] +
170              g2->val[1] * n->mul[2] + g3->val[1] * n->mul[3] + 128) >> 8;
171   dest[2] = (g0->val[2] * n->mul[0] + g1->val[2] * n->mul[1] +
172              g2->val[2] * n->mul[2] + g3->val[2] * n->mul[3] + 128) >> 8;
173 }
174
175 #endif