2d221f9ffaa84fb02f90eca2af90c989cd8f670f
[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('-', ''), 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         'S-Log',
239         lut_directory,
240         lut_resolution_1d,
241         ["slog1_sgamut"])
242     colorspaces.append(s_log1_s_gamut)
243
244     # *S-Log2*
245     s_log2_s_gamut = create_s_log(
246         'S-Gamut',
247         'S-Log2',
248         'S-Log2',
249         lut_directory,
250         lut_resolution_1d,
251         ["slog2_sgamut"])
252     colorspaces.append(s_log2_s_gamut)
253
254     s_log2_s_gamut_daylight = create_s_log(
255         'S-Gamut Daylight',
256         'S-Log2',
257         'S-Log2',
258         lut_directory,
259         lut_resolution_1d,
260         ["slog2_sgamutday"])
261     colorspaces.append(s_log2_s_gamut_daylight)
262
263     s_log2_s_gamut_tungsten = create_s_log(
264         'S-Gamut Tungsten',
265         'S-Log2',
266         'S-Log2',
267         lut_directory,
268         lut_resolution_1d,
269         ["slog2_sgamuttung"])
270     colorspaces.append(s_log2_s_gamut_tungsten)
271
272     # *S-Log3*
273     s_log3_s_gamut3Cine = create_s_log(
274         'S-Gamut3.Cine',
275         'S-Log3',
276         'S-Log3',
277         lut_directory,
278         lut_resolution_1d,
279         ["slog3_sgamutcine"])
280     colorspaces.append(s_log3_s_gamut3Cine)
281
282     s_log3_s_gamut3 = create_s_log(
283         'S-Gamut3',
284         'S-Log3',
285         'S-Log3',
286         lut_directory,
287         lut_resolution_1d,
288         ["slog3_sgamut3"])
289     colorspaces.append(s_log3_s_gamut3)
290
291     # Linearization Only
292     s_log1 = create_s_log(
293         '',
294         'S-Log1',
295         'S-Log',
296         lut_directory,
297         lut_resolution_1d,
298         ["crv_slog1"])
299     colorspaces.append(s_log1)
300
301     s_log2 = create_s_log(
302         '',
303         'S-Log2',
304         'S-Log2',
305         lut_directory,
306         lut_resolution_1d,
307         ["crv_slog2"])
308     colorspaces.append(s_log2)
309
310     s_log3 = create_s_log(
311         '',
312         'S-Log3',
313         'S-Log3',
314         lut_directory,
315         lut_resolution_1d,
316         ["crv_slog3"])
317     colorspaces.append(s_log3)
318
319     # Primaries Only
320     s_gamut = create_s_log(
321         'S-Gamut',
322         '',
323         'S-Log',
324         lut_directory,
325         lut_resolution_1d,
326         ["lin_sgamut"])
327     colorspaces.append(s_gamut)
328
329     s_gamut_daylight = create_s_log(
330         'S-Gamut Daylight',
331         '',
332         'S-Log2',
333         lut_directory,
334         lut_resolution_1d,
335         ["lin_sgamutday"])
336     colorspaces.append(s_gamut_daylight)
337
338     s_gamut_tungsten = create_s_log(
339         'S-Gamut Tungsten',
340         '',
341         'S-Log2',
342         lut_directory,
343         lut_resolution_1d,
344         ["lin_sgamuttung"])
345     colorspaces.append(s_gamut_tungsten)
346
347     s_gamut3Cine = create_s_log(
348         'S-Gamut3.Cine',
349         '',
350         'S-Log3',
351         lut_directory,
352         lut_resolution_1d,
353         ["lin_sgamut3cine"])
354     colorspaces.append(s_gamut3Cine)
355
356     s_gamut3 = create_s_log(
357         'S-Gamut3',
358         '',
359         'S-Log3',
360         lut_directory,
361         lut_resolution_1d,
362         ["lin_sgamut3"])
363     colorspaces.append(s_gamut3)
364
365     return colorspaces