• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <brianp@vmware.com>
33  * @author Michal Krol <michal@vmware.com>
34  * @author Jose Fonseca <jfonseca@vmware.com>
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 extern const float
44 util_format_srgb_8unorm_to_linear_float_table[256];
45 
46 extern const uint8_t
47 util_format_srgb_to_linear_8unorm_table[256];
48 
49 extern const uint8_t
50 util_format_linear_to_srgb_8unorm_table[256];
51 
52 extern const unsigned
53 util_format_linear_to_srgb_helper_table[104];
54 
55 
56 static inline float
util_format_srgb_to_linear_float(float cs)57 util_format_srgb_to_linear_float(float cs)
58 {
59    if (cs <= 0.0f)
60       return 0.0f;
61    else if (cs <= 0.04045f)
62       return cs / 12.92f;
63    else if (cs < 1.0f)
64       return powf((cs + 0.055) / 1.055f, 2.4f);
65    else
66       return 1.0f;
67 }
68 
69 
70 static inline float
util_format_linear_to_srgb_float(float cl)71 util_format_linear_to_srgb_float(float cl)
72 {
73    if (cl <= 0.0f)
74       return 0.0f;
75    else if (cl < 0.0031308f)
76       return 12.92f * cl;
77    else if (cl < 1.0f)
78       return 1.055f * powf(cl, 0.41666f) - 0.055f;
79    else
80       return 1.0f;
81 }
82 
83 
84 /**
85  * Convert a unclamped linear float to srgb value in the [0,255].
86  */
87 static inline uint8_t
util_format_linear_float_to_srgb_8unorm(float x)88 util_format_linear_float_to_srgb_8unorm(float x)
89 {
90    /*
91     * This is taken from https://gist.github.com/rygorous/2203834
92     * Use LUT and do linear interpolation.
93     */
94    union {
95       uint32_t ui;
96       float f;
97    } almostone, minval, f;
98    unsigned tab, bias, scale, t;
99 
100    almostone.ui = 0x3f7fffff;
101    minval.ui = (127-13) << 23;
102 
103    /*
104     * Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively.
105     * The tests are carefully written so that NaNs map to 0, same as in the
106     * reference implementation.
107     */
108    if (!(x > minval.f))
109       x = minval.f;
110    if (x > almostone.f)
111       x = almostone.f;
112 
113    /* Do the table lookup and unpack bias, scale */
114    f.f = x;
115    tab = util_format_linear_to_srgb_helper_table[(f.ui - minval.ui) >> 20];
116    bias = (tab >> 16) << 9;
117    scale = tab & 0xffff;
118 
119    /* Grab next-highest mantissa bits and perform linear interpolation */
120    t = (f.ui >> 12) & 0xff;
121    return (uint8_t) ((bias + scale*t) >> 16);
122 }
123 
124 
125 /**
126  * Convert an 8-bit sRGB value from non-linear space to a
127  * linear RGB value in [0, 1].
128  * Implemented with a 256-entry lookup table.
129  */
130 static inline float
util_format_srgb_8unorm_to_linear_float(uint8_t x)131 util_format_srgb_8unorm_to_linear_float(uint8_t x)
132 {
133    return util_format_srgb_8unorm_to_linear_float_table[x];
134 }
135 
136 
137 /*
138  * XXX These 2 functions probably don't make a lot of sense (but lots
139  * of potential callers which most likely all don't make sense neither)
140  */
141 
142 /**
143  * Convert a 8bit normalized value from linear to srgb.
144  */
145 static inline uint8_t
util_format_linear_to_srgb_8unorm(uint8_t x)146 util_format_linear_to_srgb_8unorm(uint8_t x)
147 {
148    return util_format_linear_to_srgb_8unorm_table[x];
149 }
150 
151 
152 /**
153  * Convert a 8bit normalized value from srgb to linear.
154  */
155 static inline uint8_t
util_format_srgb_to_linear_8unorm(uint8_t x)156 util_format_srgb_to_linear_8unorm(uint8_t x)
157 {
158    return util_format_srgb_to_linear_8unorm_table[x];
159 }
160 
161 
162 #endif /* U_FORMAT_SRGB_H_ */
163