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