1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * SRGB translation.
31 *
32 * @author Brian Paul <[email protected]>
33 * @author Michal Krol <[email protected]>
34 * @author Jose Fonseca <[email protected]>
35 */
36
37 #ifndef U_FORMAT_SRGB_H_
38 #define U_FORMAT_SRGB_H_
39
40 #include <stdint.h>
41 #include <math.h>
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 extern const float
48 util_format_srgb_8unorm_to_linear_float_table[256];
49
50 extern const uint8_t
51 util_format_srgb_to_linear_8unorm_table[256];
52
53 extern const uint8_t
54 util_format_linear_to_srgb_8unorm_table[256];
55
56 extern const unsigned
57 util_format_linear_to_srgb_helper_table[104];
58
59
60 static inline float
util_format_srgb_to_linear_float(float cs)61 util_format_srgb_to_linear_float(float cs)
62 {
63 if (cs <= 0.0f)
64 return 0.0f;
65 else if (cs <= 0.04045f)
66 return cs / 12.92f;
67 else if (cs < 1.0f)
68 return powf((cs + 0.055) / 1.055f, 2.4f);
69 else
70 return 1.0f;
71 }
72
73
74 static inline float
util_format_linear_to_srgb_float(float cl)75 util_format_linear_to_srgb_float(float cl)
76 {
77 if (cl <= 0.0f)
78 return 0.0f;
79 else if (cl < 0.0031308f)
80 return 12.92f * cl;
81 else if (cl < 1.0f)
82 return 1.055f * powf(cl, 0.41666f) - 0.055f;
83 else
84 return 1.0f;
85 }
86
87
88 /**
89 * Convert a unclamped linear float to srgb value in the [0,255].
90 */
91 static inline uint8_t
util_format_linear_float_to_srgb_8unorm(float x)92 util_format_linear_float_to_srgb_8unorm(float x)
93 {
94 /*
95 * This is taken from https://gist.github.com/rygorous/2203834
96 * Use LUT and do linear interpolation.
97 */
98 union {
99 uint32_t ui;
100 float f;
101 } almostone, minval, f;
102 unsigned tab, bias, scale, t;
103
104 almostone.ui = 0x3f7fffff;
105 minval.ui = (127-13) << 23;
106
107 /*
108 * Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively.
109 * The tests are carefully written so that NaNs map to 0, same as in the
110 * reference implementation.
111 */
112 if (!(x > minval.f))
113 x = minval.f;
114 if (x > almostone.f)
115 x = almostone.f;
116
117 /* Do the table lookup and unpack bias, scale */
118 f.f = x;
119 tab = util_format_linear_to_srgb_helper_table[(f.ui - minval.ui) >> 20];
120 bias = (tab >> 16) << 9;
121 scale = tab & 0xffff;
122
123 /* Grab next-highest mantissa bits and perform linear interpolation */
124 t = (f.ui >> 12) & 0xff;
125 return (uint8_t) ((bias + scale*t) >> 16);
126 }
127
128
129 /**
130 * Convert an 8-bit sRGB value from non-linear space to a
131 * linear RGB value in [0, 1].
132 * Implemented with a 256-entry lookup table.
133 */
134 static inline float
util_format_srgb_8unorm_to_linear_float(uint8_t x)135 util_format_srgb_8unorm_to_linear_float(uint8_t x)
136 {
137 return util_format_srgb_8unorm_to_linear_float_table[x];
138 }
139
140
141 /*
142 * XXX These 2 functions probably don't make a lot of sense (but lots
143 * of potential callers which most likely all don't make sense neither)
144 */
145
146 /**
147 * Convert a 8bit normalized value from linear to srgb.
148 */
149 static inline uint8_t
util_format_linear_to_srgb_8unorm(uint8_t x)150 util_format_linear_to_srgb_8unorm(uint8_t x)
151 {
152 return util_format_linear_to_srgb_8unorm_table[x];
153 }
154
155
156 /**
157 * Convert a 8bit normalized value from srgb to linear.
158 */
159 static inline uint8_t
util_format_srgb_to_linear_8unorm(uint8_t x)160 util_format_srgb_to_linear_8unorm(uint8_t x)
161 {
162 return util_format_srgb_to_linear_8unorm_table[x];
163 }
164
165 #ifdef __cplusplus
166 }
167 #endif
168
169 #endif /* U_FORMAT_SRGB_H_ */
170