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