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