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 for i
in range(j
, j
+ 4):
66 print '%.7e,' % (srgb_to_linear(i
/ 255.0),),
71 print 'util_format_srgb_to_linear_8unorm_table[256] = {'
72 for j
in range(0, 256, 16):
74 for i
in range(j
, j
+ 16):
75 print '%3u,' % (int(srgb_to_linear(i
/ 255.0) * 255.0 + 0.5),),
80 print 'util_format_linear_to_srgb_8unorm_table[256] = {'
81 for j
in range(0, 256, 16):
83 for i
in range(j
, j
+ 16):
84 print '%3u,' % (int(linear_to_srgb(i
/ 255.0) * 255.0 + 0.5),),
89 # calculate the table interpolation values used in float linear to unorm8 srgb
92 # stepshift is just used to only use every x-th float to make things faster,
93 # 5 is largest value which still gives exact same table as 0
95 nbuckets
= numexp
<< mantissa_msb
96 bucketsize
= (1 << (23 - mantissa_msb
)) >> stepshift
99 sum_aa
= float(bucketsize
)
102 for i
in range(0, bucketsize
):
103 j
= (i
<< stepshift
) >> mantshift
106 inv_det
= 1.0 / (sum_aa
* sum_bb
- sum_ab
* sum_ab
)
108 for bucket
in range(0, nbuckets
):
109 start
= ((127 - numexp
) << 23) + bucket
*(bucketsize
<< stepshift
)
113 for i
in range(0, bucketsize
):
114 j
= (i
<< stepshift
) >> mantshift
115 fint
= start
+ (i
<< stepshift
)
116 ffloat
= struct
.unpack('f', struct
.pack('I', fint
))[0]
117 val
= linear_to_srgb(ffloat
) * 255.0 + 0.5
121 solved_a
= inv_det
* (sum_bb
*sum_a
- sum_ab
*sum_b
)
122 solved_b
= inv_det
* (sum_aa
*sum_b
- sum_ab
*sum_a
)
124 scaled_a
= solved_a
* 65536.0 / 512.0
125 scaled_b
= solved_b
* 65536.0
127 int_a
= int(scaled_a
+ 0.5)
128 int_b
= int(scaled_b
+ 0.5)
130 valtable
.append((int_a
<< 16) + int_b
)
132 print 'const unsigned'
133 print 'util_format_linear_to_srgb_helper_table[104] = {'
135 for j
in range(0, nbuckets
, 4):
137 for i
in range(j
, j
+ 4):
138 print '0x%08x,' % (valtable
[i
],),
144 print '/* This file is autogenerated by u_format_srgb.py. Do not edit directly. */'
146 # This will print the copyright message on the top of this file
147 print CopyRight
.strip()
149 print '#include "format_srgb.h"'
151 generate_srgb_tables()
154 if __name__
== '__main__':