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