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