• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1CopyRight = '''
2/**************************************************************************
3 *
4 * Copyright 2010 VMware, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29/**
30 * @file
31 * SRGB translation.
32 *
33 * @author Brian Paul <brianp@vmware.com>
34 * @author Michal Krol <michal@vmware.com>
35 * @author Jose Fonseca <jfonseca@vmware.com>
36 */
37'''
38
39
40import math
41import struct
42
43
44def srgb_to_linear(x):
45    if x <= 0.04045:
46        return x / 12.92
47    else:
48        return math.pow((x + 0.055) / 1.055, 2.4)
49
50
51def linear_to_srgb(x):
52    if x >= 0.0031308:
53        return 1.055 * math.pow(x, 0.41666666) - 0.055
54    else:
55        return 12.92 * x
56
57
58def 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('   ', end=' ')
63        print(' '.join(['%.7ef,' % srgb_to_linear(i / 255.0) for i in range(j, j + 4)]))
64    print('};')
65    print()
66    print('const uint8_t')
67    print('util_format_srgb_to_linear_8unorm_table[256] = {')
68    for j in range(0, 256, 16):
69        print('   ', end=' ')
70        print(' '.join(['%3u,' % int(srgb_to_linear(i / 255.0) * 255.0 + 0.5) for i in range(j, j + 16)]))
71    print('};')
72    print()
73    print('const uint8_t')
74    print('util_format_linear_to_srgb_8unorm_table[256] = {')
75    for j in range(0, 256, 16):
76        print('   ', end=' ')
77        print(' '.join(['%3u,' % int(linear_to_srgb(i / 255.0) * 255.0 + 0.5) for i in range(j, j + 16)]))
78    print('};')
79    print()
80
81# calculate the table interpolation values used in float linear to unorm8 srgb
82    numexp = 13
83    mantissa_msb = 3
84# stepshift is just used to only use every x-th float to make things faster,
85# 5 is largest value which still gives exact same table as 0
86    stepshift = 5
87    nbuckets = numexp << mantissa_msb
88    bucketsize = (1 << (23 - mantissa_msb)) >> stepshift
89    mantshift = 12
90    valtable = []
91    sum_aa = float(bucketsize)
92    sum_ab = 0.0
93    sum_bb = 0.0
94    for i in range(0, bucketsize):
95        j = (i << stepshift) >> mantshift
96        sum_ab += j
97        sum_bb += j*j
98    inv_det = 1.0 / (sum_aa * sum_bb - sum_ab * sum_ab)
99
100    for bucket in range(0, nbuckets):
101        start = ((127 - numexp) << 23) + bucket*(bucketsize << stepshift)
102        sum_a = 0.0
103        sum_b = 0.0
104
105        for i in range(0, bucketsize):
106            j = (i << stepshift) >> mantshift
107            fint = start + (i << stepshift)
108            ffloat = struct.unpack('f', struct.pack('I', fint))[0]
109            val = linear_to_srgb(ffloat) * 255.0 + 0.5
110            sum_a += val
111            sum_b += j*val
112
113        solved_a = inv_det * (sum_bb*sum_a - sum_ab*sum_b)
114        solved_b = inv_det * (sum_aa*sum_b - sum_ab*sum_a)
115
116        scaled_a = solved_a * 65536.0 / 512.0
117        scaled_b = solved_b * 65536.0
118
119        int_a = int(scaled_a + 0.5)
120        int_b = int(scaled_b + 0.5)
121
122        valtable.append((int_a << 16) + int_b)
123
124    print('const unsigned')
125    print('util_format_linear_to_srgb_helper_table[104] = {')
126
127    for j in range(0, nbuckets, 4):
128        print('   ', end=' ')
129        print(' '.join(['0x%08x,' % valtable[i] for i in range(j, j + 4)]))
130    print('};')
131    print()
132
133def main():
134    print('/* This file is autogenerated by u_format_srgb.py. Do not edit directly. */')
135    print()
136    # This will print the copyright message on the top of this file
137    print(CopyRight.strip())
138    print()
139    print('#include "format_srgb.h"')
140    print()
141    generate_srgb_tables()
142
143
144if __name__ == '__main__':
145    main()
146