9345d09c0da5028b1638f5e1f73313dff2cdd6e2
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / createSonyColorSpaces.py
1 import array
2
3 import aces_ocio.generateLUT as genlut
4 from aces_ocio.util import ColorSpace, mat44FromMat33
5
6 #
7 # SLog to ACES
8 #
9 def createSlog(gamut, transferFunction, name, lutDir, lutResolution1d):
10     name = "%s - %s" % (transferFunction, gamut)
11     if transferFunction == "":
12         name = "Linear - %s" % gamut
13     if gamut == "":
14         name = "%s" % transferFunction
15
16     cs = ColorSpace(name)
17     cs.description = name
18     cs.equalityGroup = ''
19     cs.family = 'Sony'
20     cs.isData=False
21
22     def sLog1ToLinear(SLog):
23         b = 64.
24         ab = 90.
25         w = 940.
26
27         if (SLog >= ab):
28             lin = ( pow(10., ( ( ( SLog - b) / ( w - b) - 0.616596 - 0.03) / 0.432699)) - 0.037584) * 0.9
29         else:
30             lin = ( ( ( SLog - b) / ( w - b) - 0.030001222851889303) / 5.) * 0.9 
31         return lin
32
33     def sLog2ToLinear(SLog):
34         b = 64.
35         ab = 90.
36         w = 940.
37
38         if (SLog >= ab):
39             lin = ( 219. * ( pow(10., ( ( ( SLog - b) / ( w - b) - 0.616596 - 0.03) / 0.432699)) - 0.037584) / 155.) * 0.9
40         else:
41             lin = ( ( ( SLog - b) / ( w - b) - 0.030001222851889303) / 3.53881278538813) * 0.9
42         return lin
43
44     def sLog3ToLinear(codeValue):
45         if codeValue >= (171.2102946929):
46             linear = pow(10.0, ((codeValue - 420.0) / 261.5)) * (0.18 + 0.01) - 0.01
47         else:
48             linear = (codeValue - 95.0)*0.01125000/(171.2102946929 - 95.0)
49         #print( codeValue, linear )
50         return linear
51
52     cs.toReferenceTransforms = []
53
54     if transferFunction == "S-Log1":
55         data = array.array('f', "\0" * lutResolution1d * 4)
56         for c in range(lutResolution1d):
57             data[c] = sLog1ToLinear(1023.0*c/(lutResolution1d-1))
58
59         lut = "%s_to_linear.spi1d" % transferFunction
60         genlut.writeSPI1D(lutDir + "/" + lut, 0.0, 1.0, data, lutResolution1d, 1)
61
62         #print( "Writing %s" % lut)
63
64         cs.toReferenceTransforms.append( {
65             'type':'lutFile', 
66             'path':lut, 
67             'interpolation':'linear', 
68             'direction':'forward'
69         } )
70     elif transferFunction == "S-Log2":
71         data = array.array('f', "\0" * lutResolution1d * 4)
72         for c in range(lutResolution1d):
73             data[c] = sLog2ToLinear(1023.0*c/(lutResolution1d-1))
74
75         lut = "%s_to_linear.spi1d" % transferFunction
76         genlut.writeSPI1D(lutDir + "/" + lut, 0.0, 1.0, data, lutResolution1d, 1)
77
78         #print( "Writing %s" % lut)
79
80         cs.toReferenceTransforms.append( {
81             'type':'lutFile', 
82             'path':lut, 
83             'interpolation':'linear', 
84             'direction':'forward'
85         } )
86     elif transferFunction == "S-Log3":
87         data = array.array('f', "\0" * lutResolution1d * 4)
88         for c in range(lutResolution1d):
89             data[c] = sLog3ToLinear(1023.0*c/(lutResolution1d-1))
90
91         lut = "%s_to_linear.spi1d" % transferFunction
92         genlut.writeSPI1D(lutDir + "/" + lut, 0.0, 1.0, data, lutResolution1d, 1)
93
94         #print( "Writing %s" % lut)
95
96         cs.toReferenceTransforms.append( {
97             'type':'lutFile', 
98             'path':lut, 
99             'interpolation':'linear', 
100             'direction':'forward'
101         } )
102
103     if gamut == 'S-Gamut':
104         cs.toReferenceTransforms.append( {
105             'type':'matrix',
106             'matrix':mat44FromMat33([0.754338638, 0.133697046, 0.111968437,
107                                     0.021198141, 1.005410934, -0.026610548, 
108                                     -0.009756991, 0.004508563, 1.005253201]),
109             'direction':'forward'
110         })
111     elif gamut == 'S-Gamut Daylight':
112         cs.toReferenceTransforms.append( {
113             'type':'matrix',
114             'matrix':mat44FromMat33([0.8764457030, 0.0145411681, 0.1090131290,
115                                     0.0774075345, 0.9529571767, -0.0303647111, 
116                                     0.0573564351, -0.1151066335, 1.0577501984]),
117             'direction':'forward'
118         })
119     elif gamut == 'S-Gamut Tungsten':
120         cs.toReferenceTransforms.append( {
121             'type':'matrix',
122             'matrix':mat44FromMat33([1.0110238740, -0.1362526051, 0.1252287310, 
123                         0.1011994504, 0.9562196265, -0.0574190769,
124                         0.0600766530, -0.1010185315, 1.0409418785]),
125             'direction':'forward'
126         })
127     elif gamut == 'S-Gamut3.Cine':
128         cs.toReferenceTransforms.append( {
129             'type':'matrix',
130             'matrix':mat44FromMat33([0.6387886672, 0.2723514337, 0.0888598992, 
131                                     -0.0039159061, 1.0880732308, -0.0841573249, 
132                                     -0.0299072021, -0.0264325799, 1.0563397820]),
133             'direction':'forward'
134         })
135     elif gamut == 'S-Gamut3':
136         cs.toReferenceTransforms.append( {
137             'type':'matrix',
138             'matrix':mat44FromMat33([0.7529825954, 0.1433702162, 0.1036471884, 
139                         0.0217076974, 1.0153188355, -0.0370265329, 
140                         -0.0094160528, 0.0033704179, 1.0060456349]),
141             'direction':'forward'
142         })
143
144     cs.fromReferenceTransforms = []
145     return cs
146
147 def createColorSpaces(lutDir, lutResolution1d):
148     colorspaces = []
149
150     # SLog1
151     SLog1SGamut = createSlog("S-Gamut", "S-Log1", "S-Log", lutDir, lutResolution1d)
152     colorspaces.append(SLog1SGamut)
153
154     # SLog2
155     SLog2SGamut = createSlog("S-Gamut", "S-Log2", "S-Log2", lutDir, lutResolution1d)
156     colorspaces.append(SLog2SGamut)
157
158     SLog2SGamutDaylight = createSlog("S-Gamut Daylight", "S-Log2", "S-Log2", lutDir, lutResolution1d)
159     colorspaces.append(SLog2SGamutDaylight)
160
161     SLog2SGamutTungsten = createSlog("S-Gamut Tungsten", "S-Log2", "S-Log2", lutDir, lutResolution1d)
162     colorspaces.append(SLog2SGamutTungsten)
163
164     # SLog3
165     SLog3SGamut3Cine = createSlog("S-Gamut3.Cine", "S-Log3", "S-Log3", lutDir, lutResolution1d)
166     colorspaces.append(SLog3SGamut3Cine)
167
168     SLog3SGamut3 = createSlog("S-Gamut3", "S-Log3", "S-Log3", lutDir, lutResolution1d)
169     colorspaces.append(SLog3SGamut3)
170
171     # Linearization only
172     SLog1 = createSlog("", "S-Log1", "S-Log", lutDir, lutResolution1d)
173     colorspaces.append(SLog1)
174
175     SLog2 = createSlog("", "S-Log2", "S-Log2", lutDir, lutResolution1d)
176     colorspaces.append(SLog2)
177
178     SLog3 = createSlog("", "S-Log3", "S-Log3", lutDir, lutResolution1d)
179     colorspaces.append(SLog3)
180
181     # Primaries only
182     SGamut = createSlog("S-Gamut", "", "S-Log", lutDir, lutResolution1d)
183     colorspaces.append(SGamut)
184
185     SGamutDaylight = createSlog("S-Gamut Daylight", "", "S-Log2", lutDir, lutResolution1d)
186     colorspaces.append(SGamutDaylight)
187
188     SGamutTungsten = createSlog("S-Gamut Tungsten", "", "S-Log2", lutDir, lutResolution1d)
189     colorspaces.append(SGamutTungsten)
190
191     SGamut3Cine = createSlog("S-Gamut3.Cine", "", "S-Log3", lutDir, lutResolution1d)
192     colorspaces.append(SGamut3Cine)
193
194     SGamut3 = createSlog("S-Gamut3", "", "S-Log3", lutDir, lutResolution1d)
195     colorspaces.append(SGamut3)
196
197     return colorspaces
198