• 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 
41 #include "pipe/p_compiler.h"
42 #include "u_math.h"
43 
44 
45 extern const float
46 util_format_srgb_8unorm_to_linear_float_table[256];
47 
48 extern const uint8_t
49 util_format_srgb_to_linear_8unorm_table[256];
50 
51 extern const uint8_t
52 util_format_linear_to_srgb_8unorm_table[256];
53 
54 
55 /**
56  * Convert a unclamped linear float to srgb value in the [0,255].
57  * XXX this hasn't been tested (render to srgb surface).
58  * XXX this needs optimization.
59  */
60 static INLINE uint8_t
util_format_linear_float_to_srgb_8unorm(float x)61 util_format_linear_float_to_srgb_8unorm(float x)
62 {
63    if (x >= 1.0f)
64       return 255;
65    else if (x >= 0.0031308f)
66       return float_to_ubyte(1.055f * powf(x, 0.41666f) - 0.055f);
67    else if (x > 0.0f)
68       return float_to_ubyte(12.92f * x);
69    else
70       return 0;
71 }
72 
73 
74 /**
75  * Convert an 8-bit sRGB value from non-linear space to a
76  * linear RGB value in [0, 1].
77  * Implemented with a 256-entry lookup table.
78  */
79 static INLINE float
util_format_srgb_8unorm_to_linear_float(uint8_t x)80 util_format_srgb_8unorm_to_linear_float(uint8_t x)
81 {
82    return util_format_srgb_8unorm_to_linear_float_table[x];
83 }
84 
85 
86 /**
87  * Convert a 8bit normalized value from linear to srgb.
88  */
89 static INLINE uint8_t
util_format_linear_to_srgb_8unorm(uint8_t x)90 util_format_linear_to_srgb_8unorm(uint8_t x)
91 {
92    return util_format_linear_to_srgb_8unorm_table[x];
93 }
94 
95 
96 /**
97  * Convert a 8bit normalized value from srgb to linear.
98  */
99 static INLINE uint8_t
util_format_srgb_to_linear_8unorm(uint8_t x)100 util_format_srgb_to_linear_8unorm(uint8_t x)
101 {
102    return util_format_srgb_to_linear_8unorm_table[x];
103 }
104 
105 
106 #endif /* U_FORMAT_SRGB_H_ */
107