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