ACES 0.7.1
[OpenColorIO-Configs.git] / aces / luts / sony / slog2.py
1 #!/usr/bin/env python
2
3 import math
4
5 # IT's annoying that the 1023,4 and 4095,16 almost, but dont exactly, cancel. UGH
6 # The intent is clearly to have the same mapping, but it's not done very well.
7 # Sony engineers and/or the Academy should pick one of these mappings for both.
8
9 def SLog_to_lin(x):
10     return (math.pow(10.0,(((((x*1023.0)/4.0-16.0)/219.0)-0.616596-0.03)/0.432699))-0.037584)*0.9
11
12 def Fit(value, fromMin, fromMax, toMin, toMax):
13     if fromMin == fromMax:
14         raise ValueError("fromMin == fromMax")
15     return (value - fromMin) / (fromMax - fromMin) * (toMax - toMin) + toMin
16
17 def SLog2_to_lin(x):
18     x = Fit(x, 64.0/1023.0, 940.0/1023.0, 0.0, 1.0)
19     if x < 0.030001222851889303:
20         y = ((x-0.030001222851889303 ) * 0.28258064516129)
21     else:
22         y = (219.0*(math.pow(10.0, ((x-0.616596-0.03)/0.432699)) - 0.037584) /155.0)
23     return y*0.9
24     
25 """
26 steps = 1024
27 for i in xrange(steps):
28     x = i/(steps-1.0)
29     print x, SLog2_to_lin(x)
30 """
31 """
32 print SLog2_to_lin_copy(90.0/1023.0) / 0.9
33 print SLog2_to_lin_copy(91.0/1023.0) / 0.9
34 print SLog2_to_lin_copy(582.0/1023.0) / 0.9
35 print SLog2_to_lin_copy(940.0/1023.0) / 0.9
36 print SLog2_to_lin_copy(998.0/1023.0) / 0.9
37 """
38
39 def WriteSPI1D(filename, fromMin, fromMax, data):
40     f = file(filename,'w')
41     f.write("Version 1\n")
42     f.write("From %s %s\n" % (fromMin, fromMax))
43     f.write("Length %d\n" % len(data))
44     f.write("Components 1\n")
45     f.write("{\n")
46     for value in data:
47         f.write("        %s\n" % value)
48     f.write("}\n")
49     f.close()
50
51 NUM_SAMPLES = 2**14
52 RANGE = (-0.125, 1.125)
53 data = []
54 for i in xrange(NUM_SAMPLES):
55     x = i/(NUM_SAMPLES-1.0)
56     x = Fit(x, 0.0, 1.0, RANGE[0], RANGE[1])
57     data.append(SLog2_to_lin(x))
58 WriteSPI1D('sony_slog2_10.spi1d', RANGE[0], RANGE[1], data)