Merge branch '7.8'
[mesa.git] / src / gallium / auxiliary / util / u_format_srgb.py
1 #!/usr/bin/env python
2
3 '''
4 /**************************************************************************
5 *
6 * Copyright 2010 VMware, Inc.
7 * All Rights Reserved.
8 *
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:
16 *
17 * The above copyright notice and this permission notice (including the
18 * next paragraph) shall be included in all copies or substantial portions
19 * of the Software.
20 *
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.
28 *
29 **************************************************************************/
30
31 /**
32 * @file
33 * SRGB translation.
34 *
35 * @author Brian Paul <brianp@vmware.com>
36 * @author Michal Krol <michal@vmware.com>
37 * @author Jose Fonseca <jfonseca@vmware.com>
38 */
39 '''
40
41
42 import sys
43 import math
44
45
46 def srgb_to_linear(x):
47 if x <= 0.04045:
48 return x / 12.92
49 else:
50 return math.pow((x + 0.055) / 1.055, 2.4)
51
52
53 def linear_to_srgb(x):
54 if x >= 0.0031308:
55 return 1.055 * math.pow(x, 0.41666) - 0.055
56 else:
57 return 12.92 * x
58
59 def generate_srgb_tables():
60 print 'const float'
61 print 'util_format_srgb_8unorm_to_linear_float_table[256] = {'
62 for j in range(0, 256, 4):
63 print ' ',
64 for i in range(j, j + 4):
65 print '%.7e,' % (srgb_to_linear(i / 255.0),),
66 print
67 print '};'
68 print
69 print 'const uint8_t'
70 print 'util_format_srgb_to_linear_8unorm_table[256] = {'
71 for j in range(0, 256, 16):
72 print ' ',
73 for i in range(j, j + 16):
74 print '%3u,' % (int(srgb_to_linear(i / 255.0) * 255.0 + 0.5),),
75 print
76 print '};'
77 print
78 print 'const uint8_t'
79 print 'util_format_linear_to_srgb_8unorm_table[256] = {'
80 for j in range(0, 256, 16):
81 print ' ',
82 for i in range(j, j + 16):
83 print '%3u,' % (int(linear_to_srgb(i / 255.0) * 255.0 + 0.5),),
84 print
85 print '};'
86 print
87
88
89 def main():
90 print '/* This file is autogenerated by u_format_srgb.py. Do not edit directly. */'
91 print
92 # This will print the copyright message on the top of this file
93 print __doc__.strip()
94 print
95 print '#include "u_format_srgb.h"'
96 print
97 generate_srgb_tables()
98
99
100 if __name__ == '__main__':
101 main()