Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / auxiliary / util / u_format_srgb.py
1 #!/usr/bin/env python
2
3 CopyRight = '''
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 math
43
44
45 def srgb_to_linear(x):
46     if x <= 0.04045:
47         return x / 12.92
48     else:
49         return math.pow((x + 0.055) / 1.055, 2.4)
50
51
52 def linear_to_srgb(x):
53     if x >= 0.0031308:
54         return 1.055 * math.pow(x, 0.41666) - 0.055
55     else:
56         return 12.92 * x
57
58 def generate_srgb_tables():
59     print 'const float'
60     print 'util_format_srgb_8unorm_to_linear_float_table[256] = {'
61     for j in range(0, 256, 4):
62         print '   ',
63         for i in range(j, j + 4):
64             print '%.7e,' % (srgb_to_linear(i / 255.0),),
65         print
66     print '};'
67     print
68     print 'const uint8_t'
69     print 'util_format_srgb_to_linear_8unorm_table[256] = {'
70     for j in range(0, 256, 16):
71         print '   ',
72         for i in range(j, j + 16):
73             print '%3u,' % (int(srgb_to_linear(i / 255.0) * 255.0 + 0.5),),
74         print
75     print '};'
76     print
77     print 'const uint8_t'
78     print 'util_format_linear_to_srgb_8unorm_table[256] = {'
79     for j in range(0, 256, 16):
80         print '   ',
81         for i in range(j, j + 16):
82             print '%3u,' % (int(linear_to_srgb(i / 255.0) * 255.0 + 0.5),),
83         print
84     print '};'
85     print
86
87
88 def main():
89     print '/* This file is autogenerated by u_format_srgb.py. Do not edit directly. */'
90     print
91     # This will print the copyright message on the top of this file
92     print CopyRight.strip()
93     print
94     print '#include "u_format_srgb.h"'
95     print
96     generate_srgb_tables()    
97
98
99 if __name__ == '__main__':
100     main()