Enforce quotes consistency.
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / create_sony_colorspaces.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.generate_lut as genlut
11 from aces_ocio.utilities import ColorSpace, mat44_from_mat33
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__ = ['create_s_log',
21            'create_colorspaces']
22
23
24 def create_s_log(gamut,
25                  transfer_function,
26                  name,
27                  lut_directory,
28                  lut_resolution_1d):
29     """
30     Object description.
31
32     SLog to ACES.
33
34     Parameters
35     ----------
36     parameter : type
37         Parameter description.
38
39     Returns
40     -------
41     type
42          Return value description.
43     """
44
45     name = '%s - %s' % (transfer_function, gamut)
46     if transfer_function == '':
47         name = 'Linear - %s' % gamut
48     if gamut == '':
49         name = '%s' % transfer_function
50
51     cs = ColorSpace(name)
52     cs.description = name
53     cs.equality_group = ''
54     cs.family = 'Sony'
55     cs.is_data = False
56
57     def s_log1_to_linear(s_log):
58         b = 64.
59         ab = 90.
60         w = 940.
61
62         if (s_log >= ab):
63             linear = ((pow(10.,
64                            ( ((s_log - b) /
65                               (w - b) - 0.616596 - 0.03) / 0.432699)) -
66                        0.037584) * 0.9)
67         else:
68             linear = (
69                          ((s_log - b) / (
70                              w - b) - 0.030001222851889303) / 5.) * 0.9
71         return linear
72
73     def s_log2_to_linear(s_log):
74         b = 64.
75         ab = 90.
76         w = 940.
77
78         if (s_log >= ab):
79             linear = ((219. * (pow(10.,
80                                    (((s_log - b) /
81                                      (w - b) - 0.616596 - 0.03) / 0.432699)) -
82                                0.037584) / 155.) * 0.9)
83         else:
84             linear = (((s_log - b) / (
85                 w - b) - 0.030001222851889303) / 3.53881278538813) * 0.9
86         return linear
87
88     def s_log3_to_linear(code_value):
89         if code_value >= (171.2102946929):
90             linear = (pow(10.0, ((code_value - 420.0) / 261.5)) *
91                       (0.18 + 0.01) - 0.01)
92         else:
93             linear = (code_value - 95.0) * 0.01125000 / (171.2102946929 - 95.0)
94         # print(codeValue, linear)
95         return linear
96
97     cs.to_reference_transforms = []
98
99     if transfer_function == 'S-Log1':
100         data = array.array('f', '\0' * lut_resolution_1d * 4)
101         for c in range(lut_resolution_1d):
102             data[c] = s_log1_to_linear(1023.0 * c / (lut_resolution_1d - 1))
103
104         lut = '%s_to_linear.spi1d' % transfer_function
105         genlut.write_SPI_1d(lut_directory + '/' + lut,
106                             0.0,
107                             1.0,
108                             data,
109                             lut_resolution_1d,
110                             1)
111
112         # print('Writing %s' % lut)
113
114         cs.to_reference_transforms.append({
115             'type': 'lutFile',
116             'path': lut,
117             'interpolation': 'linear',
118             'direction': 'forward'
119         })
120     elif transfer_function == 'S-Log2':
121         data = array.array('f', '\0' * lut_resolution_1d * 4)
122         for c in range(lut_resolution_1d):
123             data[c] = s_log2_to_linear(1023.0 * c / (lut_resolution_1d - 1))
124
125         lut = '%s_to_linear.spi1d' % transfer_function
126         genlut.write_SPI_1d(lut_directory + '/' + lut,
127                             0.0,
128                             1.0,
129                             data,
130                             lut_resolution_1d,
131                             1)
132
133         # print('Writing %s' % lut)
134
135         cs.to_reference_transforms.append({
136             'type': 'lutFile',
137             'path': lut,
138             'interpolation': 'linear',
139             'direction': 'forward'
140         })
141     elif transfer_function == 'S-Log3':
142         data = array.array('f', '\0' * lut_resolution_1d * 4)
143         for c in range(lut_resolution_1d):
144             data[c] = s_log3_to_linear(1023.0 * c / (lut_resolution_1d - 1))
145
146         lut = '%s_to_linear.spi1d' % transfer_function
147         genlut.write_SPI_1d(lut_directory + '/' + lut,
148                             0.0,
149                             1.0,
150                             data,
151                             lut_resolution_1d,
152                             1)
153
154         # print('Writing %s' % lut)
155
156         cs.to_reference_transforms.append({
157             'type': 'lutFile',
158             'path': lut,
159             'interpolation': 'linear',
160             'direction': 'forward'
161         })
162
163     if gamut == 'S-Gamut':
164         cs.to_reference_transforms.append({
165             'type': 'matrix',
166             'matrix': mat44_from_mat33(
167                 [0.754338638, 0.133697046, 0.111968437,
168                  0.021198141, 1.005410934, -0.026610548,
169                  -0.009756991, 0.004508563, 1.005253201]),
170             'direction': 'forward'})
171     elif gamut == 'S-Gamut Daylight':
172         cs.to_reference_transforms.append({
173             'type': 'matrix',
174             'matrix': mat44_from_mat33(
175                 [0.8764457030, 0.0145411681, 0.1090131290,
176                  0.0774075345, 0.9529571767, -0.0303647111,
177                  0.0573564351, -0.1151066335, 1.0577501984]),
178             'direction': 'forward'})
179     elif gamut == 'S-Gamut Tungsten':
180         cs.to_reference_transforms.append({
181             'type': 'matrix',
182             'matrix': mat44_from_mat33(
183                 [1.0110238740, -0.1362526051, 0.1252287310,
184                  0.1011994504, 0.9562196265, -0.0574190769,
185                  0.0600766530, -0.1010185315, 1.0409418785]),
186             'direction': 'forward'})
187     elif gamut == 'S-Gamut3.Cine':
188         cs.to_reference_transforms.append({
189             'type': 'matrix',
190             'matrix': mat44_from_mat33(
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     elif gamut == 'S-Gamut3':
196         cs.to_reference_transforms.append({
197             'type': 'matrix',
198             'matrix': mat44_from_mat33(
199                 [0.7529825954, 0.1433702162, 0.1036471884,
200                  0.0217076974, 1.0153188355, -0.0370265329,
201                  -0.0094160528, 0.0033704179, 1.0060456349]),
202             'direction': 'forward'})
203
204     cs.from_reference_transforms = []
205     return cs
206
207
208 def create_colorspaces(lut_directory, lut_resolution_1d):
209     """
210     Generates the colorspace conversions.
211
212     Parameters
213     ----------
214     parameter : type
215         Parameter description.
216
217     Returns
218     -------
219     type
220          Return value description.
221     """
222
223     colorspaces = []
224
225     # S-Log1
226     s_log1_s_gamut = create_s_log(
227         'S-Gamut',
228         'S-Log1',
229         'S-Log',
230         lut_directory,
231         lut_resolution_1d)
232     colorspaces.append(s_log1_s_gamut)
233
234     # S-Log2
235     s_log2_s_gamut = create_s_log(
236         'S-Gamut',
237         'S-Log2',
238         'S-Log2',
239         lut_directory,
240         lut_resolution_1d)
241     colorspaces.append(s_log2_s_gamut)
242
243     s_log2_s_gamut_daylight = create_s_log(
244         'S-Gamut Daylight',
245         'S-Log2',
246         'S-Log2',
247         lut_directory,
248         lut_resolution_1d)
249     colorspaces.append(s_log2_s_gamut_daylight)
250
251     s_log2_s_gamut_tungsten = create_s_log(
252         'S-Gamut Tungsten',
253         'S-Log2',
254         'S-Log2',
255         lut_directory,
256         lut_resolution_1d)
257     colorspaces.append(s_log2_s_gamut_tungsten)
258
259     # S-Log3
260     s_log3_s_gamut3Cine = create_s_log(
261         'S-Gamut3.Cine',
262         'S-Log3',
263         'S-Log3',
264         lut_directory,
265         lut_resolution_1d)
266     colorspaces.append(s_log3_s_gamut3Cine)
267
268     s_log3_s_gamut3 = create_s_log(
269         'S-Gamut3',
270         'S-Log3',
271         'S-Log3',
272         lut_directory,
273         lut_resolution_1d)
274     colorspaces.append(s_log3_s_gamut3)
275
276     # Linearization only
277     s_log1 = create_s_log(
278         '',
279         'S-Log1',
280         'S-Log',
281         lut_directory,
282         lut_resolution_1d)
283     colorspaces.append(s_log1)
284
285     s_log2 = create_s_log(
286         '',
287         'S-Log2',
288         'S-Log2',
289         lut_directory,
290         lut_resolution_1d)
291     colorspaces.append(s_log2)
292
293     s_log3 = create_s_log(
294         '',
295         'S-Log3',
296         'S-Log3',
297         lut_directory,
298         lut_resolution_1d)
299     colorspaces.append(s_log3)
300
301     # Primaries only
302     s_gamut = create_s_log(
303         'S-Gamut',
304         '',
305         'S-Log',
306         lut_directory,
307         lut_resolution_1d)
308     colorspaces.append(s_gamut)
309
310     s_gamut_daylight = create_s_log(
311         'S-Gamut Daylight',
312         '',
313         'S-Log2',
314         lut_directory,
315         lut_resolution_1d)
316     colorspaces.append(s_gamut_daylight)
317
318     s_gamut_tungsten = create_s_log(
319         'S-Gamut Tungsten',
320         '',
321         'S-Log2',
322         lut_directory,
323         lut_resolution_1d)
324     colorspaces.append(s_gamut_tungsten)
325
326     s_gamut3Cine = create_s_log(
327         'S-Gamut3.Cine',
328         '',
329         'S-Log3',
330         lut_directory,
331         lut_resolution_1d)
332     colorspaces.append(s_gamut3Cine)
333
334     s_gamut3 = create_s_log(
335         'S-Gamut3',
336         '',
337         'S-Log3',
338         lut_directory,
339         lut_resolution_1d)
340     colorspaces.append(s_gamut3)
341
342     return colorspaces