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