1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
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:
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.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 **************************************************************************/
32 * @author Brian Paul <brianp@vmware.com>
33 * @author Michal Krol <michal@vmware.com>
34 * @author Jose Fonseca <jfonseca@vmware.com>
37 #ifndef U_FORMAT_SRGB_H_
38 #define U_FORMAT_SRGB_H_
42 #include "c99_compat.h"
45 util_format_srgb_8unorm_to_linear_float_table
[256];
48 util_format_srgb_to_linear_8unorm_table
[256];
51 util_format_linear_to_srgb_8unorm_table
[256];
54 util_format_linear_to_srgb_helper_table
[104];
58 util_format_srgb_to_linear_float(float cs
)
62 else if (cs
<= 0.04045f
)
65 return powf((cs
+ 0.055) / 1.055f
, 2.4f
);
72 util_format_linear_to_srgb_float(float cl
)
76 else if (cl
< 0.0031308f
)
79 return 1.055f
* powf(cl
, 0.41666f
) - 0.055f
;
86 * Convert a unclamped linear float to srgb value in the [0,255].
89 util_format_linear_float_to_srgb_8unorm(float x
)
92 * This is taken from https://gist.github.com/rygorous/2203834
93 * Use LUT and do linear interpolation.
98 } almostone
, minval
, f
;
99 unsigned tab
, bias
, scale
, t
;
101 almostone
.ui
= 0x3f7fffff;
102 minval
.ui
= (127-13) << 23;
105 * Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively.
106 * The tests are carefully written so that NaNs map to 0, same as in the
107 * reference implementation.
114 /* Do the table lookup and unpack bias, scale */
116 tab
= util_format_linear_to_srgb_helper_table
[(f
.ui
- minval
.ui
) >> 20];
117 bias
= (tab
>> 16) << 9;
118 scale
= tab
& 0xffff;
120 /* Grab next-highest mantissa bits and perform linear interpolation */
121 t
= (f
.ui
>> 12) & 0xff;
122 return (uint8_t) ((bias
+ scale
*t
) >> 16);
127 * Convert an 8-bit sRGB value from non-linear space to a
128 * linear RGB value in [0, 1].
129 * Implemented with a 256-entry lookup table.
132 util_format_srgb_8unorm_to_linear_float(uint8_t x
)
134 return util_format_srgb_8unorm_to_linear_float_table
[x
];
139 * XXX These 2 functions probably don't make a lot of sense (but lots
140 * of potential callers which most likely all don't make sense neither)
144 * Convert a 8bit normalized value from linear to srgb.
146 static inline uint8_t
147 util_format_linear_to_srgb_8unorm(uint8_t x
)
149 return util_format_linear_to_srgb_8unorm_table
[x
];
154 * Convert a 8bit normalized value from srgb to linear.
156 static inline uint8_t
157 util_format_srgb_to_linear_8unorm(uint8_t x
)
159 return util_format_srgb_to_linear_8unorm_table
[x
];
163 #endif /* U_FORMAT_SRGB_H_ */