1 from __future__
import print_function
4 /**************************************************************************
6 * Copyright 2010 VMware, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sub license, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice (including the
18 * next paragraph) shall be included in all copies or substantial portions
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
25 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 **************************************************************************/
35 * @author Brian Paul <brianp@vmware.com>
36 * @author Michal Krol <michal@vmware.com>
37 * @author Jose Fonseca <jfonseca@vmware.com>
46 def srgb_to_linear(x
):
50 return math
.pow((x
+ 0.055) / 1.055, 2.4)
53 def linear_to_srgb(x
):
55 return 1.055 * math
.pow(x
, 0.41666666) - 0.055
60 def generate_srgb_tables():
62 print('util_format_srgb_8unorm_to_linear_float_table[256] = {')
63 for j
in range(0, 256, 4):
65 print(' '.join(['%.7e,' % srgb_to_linear(i
/ 255.0) for i
in range(j
, j
+ 4)]))
68 print('const uint8_t')
69 print('util_format_srgb_to_linear_8unorm_table[256] = {')
70 for j
in range(0, 256, 16):
72 print(' '.join(['%3u,' % int(srgb_to_linear(i
/ 255.0) * 255.0 + 0.5) for i
in range(j
, j
+ 16)]))
75 print('const uint8_t')
76 print('util_format_linear_to_srgb_8unorm_table[256] = {')
77 for j
in range(0, 256, 16):
79 print(' '.join(['%3u,' % int(linear_to_srgb(i
/ 255.0) * 255.0 + 0.5) for i
in range(j
, j
+ 16)]))
83 # calculate the table interpolation values used in float linear to unorm8 srgb
86 # stepshift is just used to only use every x-th float to make things faster,
87 # 5 is largest value which still gives exact same table as 0
89 nbuckets
= numexp
<< mantissa_msb
90 bucketsize
= (1 << (23 - mantissa_msb
)) >> stepshift
93 sum_aa
= float(bucketsize
)
96 for i
in range(0, bucketsize
):
97 j
= (i
<< stepshift
) >> mantshift
100 inv_det
= 1.0 / (sum_aa
* sum_bb
- sum_ab
* sum_ab
)
102 for bucket
in range(0, nbuckets
):
103 start
= ((127 - numexp
) << 23) + bucket
*(bucketsize
<< stepshift
)
107 for i
in range(0, bucketsize
):
108 j
= (i
<< stepshift
) >> mantshift
109 fint
= start
+ (i
<< stepshift
)
110 ffloat
= struct
.unpack('f', struct
.pack('I', fint
))[0]
111 val
= linear_to_srgb(ffloat
) * 255.0 + 0.5
115 solved_a
= inv_det
* (sum_bb
*sum_a
- sum_ab
*sum_b
)
116 solved_b
= inv_det
* (sum_aa
*sum_b
- sum_ab
*sum_a
)
118 scaled_a
= solved_a
* 65536.0 / 512.0
119 scaled_b
= solved_b
* 65536.0
121 int_a
= int(scaled_a
+ 0.5)
122 int_b
= int(scaled_b
+ 0.5)
124 valtable
.append((int_a
<< 16) + int_b
)
126 print('const unsigned')
127 print('util_format_linear_to_srgb_helper_table[104] = {')
129 for j
in range(0, nbuckets
, 4):
131 print(' '.join(['0x%08x,' % valtable
[i
] for i
in range(j
, j
+ 4)]))
136 print('/* This file is autogenerated by u_format_srgb.py. Do not edit directly. */')
138 # This will print the copyright message on the top of this file
139 print(CopyRight
.strip())
141 print('#include "format_srgb.h"')
143 generate_srgb_tables()
146 if __name__
== '__main__':