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