Code formatting.
[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     if gamut and transfer_function:
65         cs.aces_transform_id = "IDT.Sony.%s_%s_10i.a1.v1" % (
66             transfer_function.replace('-', ''),
67             gamut.replace('-', '').replace(' ', '_'))
68
69     # A linear space needs allocation variables
70     if transfer_function == '':
71         cs.allocation_type = ocio.Constants.ALLOCATION_LG2
72         cs.allocation_vars = [-8, 5, 0.00390625]
73
74     def s_log1_to_linear(s_log):
75         b = 64.
76         ab = 90.
77         w = 940.
78
79         if s_log >= ab:
80             linear = ((pow(10.,
81                            (((s_log - b) /
82                              (w - b) - 0.616596 - 0.03) / 0.432699)) -
83                        0.037584) * 0.9)
84         else:
85             linear = (((s_log - b) / (
86                 w - b) - 0.030001222851889303) / 5.) * 0.9
87         return linear
88
89     def s_log2_to_linear(s_log):
90         b = 64.
91         ab = 90.
92         w = 940.
93
94         if s_log >= ab:
95             linear = ((219. * (pow(10.,
96                                    (((s_log - b) /
97                                      (w - b) - 0.616596 - 0.03) / 0.432699)) -
98                                0.037584) / 155.) * 0.9)
99         else:
100             linear = (((s_log - b) / (
101                 w - b) - 0.030001222851889303) / 3.53881278538813) * 0.9
102         return linear
103
104     def s_log3_to_linear(code_value):
105         if code_value >= 171.2102946929:
106             linear = (pow(10, ((code_value - 420) / 261.5)) *
107                       (0.18 + 0.01) - 0.01)
108         else:
109             linear = (code_value - 95) * 0.01125000 / (171.2102946929 - 95)
110
111         return linear
112
113     cs.to_reference_transforms = []
114
115     if transfer_function == 'S-Log1':
116         data = array.array('f', '\0' * lut_resolution_1d * 4)
117         for c in range(lut_resolution_1d):
118             data[c] = s_log1_to_linear(1023 * c / (lut_resolution_1d - 1))
119
120         lut = '%s_to_linear.spi1d' % transfer_function
121         genlut.write_SPI_1d(
122             os.path.join(lut_directory, lut),
123             0,
124             1,
125             data,
126             lut_resolution_1d,
127             1)
128
129         cs.to_reference_transforms.append({
130             'type': 'lutFile',
131             'path': lut,
132             'interpolation': 'linear',
133             'direction': 'forward'})
134     elif transfer_function == 'S-Log2':
135         data = array.array('f', '\0' * lut_resolution_1d * 4)
136         for c in range(lut_resolution_1d):
137             data[c] = s_log2_to_linear(1023 * c / (lut_resolution_1d - 1))
138
139         lut = '%s_to_linear.spi1d' % transfer_function
140         genlut.write_SPI_1d(
141             os.path.join(lut_directory, lut),
142             0,
143             1,
144             data,
145             lut_resolution_1d,
146             1)
147
148         cs.to_reference_transforms.append({
149             'type': 'lutFile',
150             'path': lut,
151             'interpolation': 'linear',
152             'direction': 'forward'})
153     elif transfer_function == 'S-Log3':
154         data = array.array('f', '\0' * lut_resolution_1d * 4)
155         for c in range(lut_resolution_1d):
156             data[c] = s_log3_to_linear(1023 * c / (lut_resolution_1d - 1))
157
158         lut = '%s_to_linear.spi1d' % transfer_function
159         genlut.write_SPI_1d(
160             os.path.join(lut_directory, lut),
161             0,
162             1,
163             data,
164             lut_resolution_1d,
165             1)
166
167         cs.to_reference_transforms.append({
168             'type': 'lutFile',
169             'path': lut,
170             'interpolation': 'linear',
171             'direction': 'forward'})
172
173     if gamut == 'S-Gamut':
174         cs.to_reference_transforms.append({
175             'type': 'matrix',
176             'matrix': mat44_from_mat33(
177                 [0.754338638, 0.133697046, 0.111968437,
178                  0.021198141, 1.005410934, -0.026610548,
179                  -0.009756991, 0.004508563, 1.005253201]),
180             'direction': 'forward'})
181     elif gamut == 'S-Gamut Daylight':
182         cs.to_reference_transforms.append({
183             'type': 'matrix',
184             'matrix': mat44_from_mat33(
185                 [0.8764457030, 0.0145411681, 0.1090131290,
186                  0.0774075345, 0.9529571767, -0.0303647111,
187                  0.0573564351, -0.1151066335, 1.0577501984]),
188             'direction': 'forward'})
189     elif gamut == 'S-Gamut Tungsten':
190         cs.to_reference_transforms.append({
191             'type': 'matrix',
192             'matrix': mat44_from_mat33(
193                 [1.0110238740, -0.1362526051, 0.1252287310,
194                  0.1011994504, 0.9562196265, -0.0574190769,
195                  0.0600766530, -0.1010185315, 1.0409418785]),
196             'direction': 'forward'})
197     elif gamut == 'S-Gamut3.Cine':
198         cs.to_reference_transforms.append({
199             'type': 'matrix',
200             'matrix': mat44_from_mat33(
201                 [0.6387886672, 0.2723514337, 0.0888598992,
202                  -0.0039159061, 1.0880732308, -0.0841573249,
203                  -0.0299072021, -0.0264325799, 1.0563397820]),
204             'direction': 'forward'})
205     elif gamut == 'S-Gamut3':
206         cs.to_reference_transforms.append({
207             'type': 'matrix',
208             'matrix': mat44_from_mat33(
209                 [0.7529825954, 0.1433702162, 0.1036471884,
210                  0.0217076974, 1.0153188355, -0.0370265329,
211                  -0.0094160528, 0.0033704179, 1.0060456349]),
212             'direction': 'forward'})
213
214     cs.from_reference_transforms = []
215     return cs
216
217
218 def create_colorspaces(lut_directory, lut_resolution_1d):
219     """
220     Generates the colorspace conversions.
221
222     Parameters
223     ----------
224     parameter : type
225         Parameter description.
226
227     Returns
228     -------
229     type
230          Return value description.
231     """
232
233     colorspaces = []
234
235     # *S-Log1*
236     s_log1_s_gamut = create_s_log(
237         'S-Gamut',
238         'S-Log1',
239         'S-Log',
240         lut_directory,
241         lut_resolution_1d,
242         ["slog1_sgamut"])
243     colorspaces.append(s_log1_s_gamut)
244
245     # *S-Log2*
246     s_log2_s_gamut = create_s_log(
247         'S-Gamut',
248         'S-Log2',
249         'S-Log2',
250         lut_directory,
251         lut_resolution_1d,
252         ["slog2_sgamut"])
253     colorspaces.append(s_log2_s_gamut)
254
255     s_log2_s_gamut_daylight = create_s_log(
256         'S-Gamut Daylight',
257         'S-Log2',
258         'S-Log2',
259         lut_directory,
260         lut_resolution_1d,
261         ["slog2_sgamutday"])
262     colorspaces.append(s_log2_s_gamut_daylight)
263
264     s_log2_s_gamut_tungsten = create_s_log(
265         'S-Gamut Tungsten',
266         'S-Log2',
267         'S-Log2',
268         lut_directory,
269         lut_resolution_1d,
270         ["slog2_sgamuttung"])
271     colorspaces.append(s_log2_s_gamut_tungsten)
272
273     # *S-Log3*
274     s_log3_s_gamut3Cine = create_s_log(
275         'S-Gamut3.Cine',
276         'S-Log3',
277         'S-Log3',
278         lut_directory,
279         lut_resolution_1d,
280         ["slog3_sgamutcine"])
281     colorspaces.append(s_log3_s_gamut3Cine)
282
283     s_log3_s_gamut3 = create_s_log(
284         'S-Gamut3',
285         'S-Log3',
286         'S-Log3',
287         lut_directory,
288         lut_resolution_1d,
289         ["slog3_sgamut3"])
290     colorspaces.append(s_log3_s_gamut3)
291
292     # Linearization Only
293     s_log1 = create_s_log(
294         '',
295         'S-Log1',
296         'S-Log',
297         lut_directory,
298         lut_resolution_1d,
299         ["crv_slog1"])
300     colorspaces.append(s_log1)
301
302     s_log2 = create_s_log(
303         '',
304         'S-Log2',
305         'S-Log2',
306         lut_directory,
307         lut_resolution_1d,
308         ["crv_slog2"])
309     colorspaces.append(s_log2)
310
311     s_log3 = create_s_log(
312         '',
313         'S-Log3',
314         'S-Log3',
315         lut_directory,
316         lut_resolution_1d,
317         ["crv_slog3"])
318     colorspaces.append(s_log3)
319
320     # Primaries Only
321     s_gamut = create_s_log(
322         'S-Gamut',
323         '',
324         'S-Log',
325         lut_directory,
326         lut_resolution_1d,
327         ["lin_sgamut"])
328     colorspaces.append(s_gamut)
329
330     s_gamut_daylight = create_s_log(
331         'S-Gamut Daylight',
332         '',
333         'S-Log2',
334         lut_directory,
335         lut_resolution_1d,
336         ["lin_sgamutday"])
337     colorspaces.append(s_gamut_daylight)
338
339     s_gamut_tungsten = create_s_log(
340         'S-Gamut Tungsten',
341         '',
342         'S-Log2',
343         lut_directory,
344         lut_resolution_1d,
345         ["lin_sgamuttung"])
346     colorspaces.append(s_gamut_tungsten)
347
348     s_gamut3Cine = create_s_log(
349         'S-Gamut3.Cine',
350         '',
351         'S-Log3',
352         lut_directory,
353         lut_resolution_1d,
354         ["lin_sgamut3cine"])
355     colorspaces.append(s_gamut3Cine)
356
357     s_gamut3 = create_s_log(
358         'S-Gamut3',
359         '',
360         'S-Log3',
361         lut_directory,
362         lut_resolution_1d,
363         ["lin_sgamut3"])
364     colorspaces.append(s_gamut3)
365
366     return colorspaces