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