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