Added 'Curve - ' prefix to colorspaces that only included a transfer function/curve.
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / colorspaces / canon.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 Implements support for *Canon* 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
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_c_log',
26            'create_colorspaces']
27
28
29 def create_c_log(gamut,
30                  transfer_function,
31                  name,
32                  lut_directory,
33                  lut_resolution_1d,
34                  aliases):
35     """
36     Object description.
37
38     Canon-Log 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 - Canon %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/Canon'
62     cs.is_data = False
63
64     # A linear space needs allocation variables
65     if transfer_function == '':
66         cs.allocation_type = ocio.Constants.ALLOCATION_LG2
67         cs.allocation_vars = [-8, 5, 0.00390625]
68
69     def legal_to_full(code_value):
70         return (code_value - 64) / (940 - 64)
71
72     def c_log_to_linear(code_value):
73         # log = fullToLegal(c1 * log10(c2*linear + 1) + c3)
74         # linear = (pow(10, (legalToFul(log) - c3)/c1) - 1)/c2
75         c1 = 0.529136
76         c2 = 10.1596
77         c3 = 0.0730597
78
79         linear = (pow(10, (legal_to_full(code_value) - c3) / c1) - 1) / c2
80         linear *= 0.9
81
82         return linear
83
84     cs.to_reference_transforms = []
85
86     if transfer_function == 'Canon-Log':
87         data = array.array('f', '\0' * lut_resolution_1d * 4)
88         for c in range(lut_resolution_1d):
89             data[c] = c_log_to_linear(1023 * c / (lut_resolution_1d - 1))
90
91         lut = '%s_to_linear.spi1d' % transfer_function
92         genlut.write_SPI_1d(
93             os.path.join(lut_directory, lut),
94             0,
95             1,
96             data,
97             lut_resolution_1d,
98             1)
99
100         cs.to_reference_transforms.append({
101             'type': 'lutFile',
102             'path': lut,
103             'interpolation': 'linear',
104             'direction': 'forward'})
105
106     if gamut == 'Rec. 709 Daylight':
107         cs.to_reference_transforms.append({
108             'type': 'matrix',
109             'matrix': [0.561538969, 0.402060105, 0.036400926, 0,
110                        0.092739623, 0.924121198, -0.016860821, 0,
111                        0.084812961, 0.006373835, 0.908813204, 0,
112                        0, 0, 0, 1],
113             'direction': 'forward'})
114     elif gamut == 'Rec. 709 Tungsten':
115         cs.to_reference_transforms.append({
116             'type': 'matrix',
117             'matrix': [0.566996399, 0.365079418, 0.067924183, 0,
118                        0.070901044, 0.880331008, 0.048767948, 0,
119                        0.073013542, -0.066540862, 0.99352732, 0,
120                        0, 0, 0, 1],
121             'direction': 'forward'})
122     elif gamut == 'DCI-P3 Daylight':
123         cs.to_reference_transforms.append({
124             'type': 'matrix',
125             'matrix': [0.607160575, 0.299507286, 0.093332140, 0,
126                        0.004968120, 1.050982224, -0.055950343, 0,
127                        -0.007839939, 0.000809127, 1.007030813, 0,
128                        0, 0, 0, 1],
129             'direction': 'forward'})
130     elif gamut == 'DCI-P3 Tungsten':
131         cs.to_reference_transforms.append({
132             'type': 'matrix',
133             'matrix': [0.650279125, 0.253880169, 0.095840706, 0,
134                        -0.026137986, 1.017900530, 0.008237456, 0,
135                        0.007757558, -0.063081669, 1.055324110, 0,
136                        0, 0, 0, 1],
137             'direction': 'forward'})
138     elif gamut == 'Cinema Gamut Daylight':
139         cs.to_reference_transforms.append({
140             'type': 'matrix',
141             'matrix': [0.763064455, 0.149021161, 0.087914384, 0,
142                        0.003657457, 1.10696038, -0.110617837, 0,
143                        -0.009407794, -0.218383305, 1.227791099, 0,
144                        0, 0, 0, 1],
145             'direction': 'forward'})
146     elif gamut == 'Cinema Gamut Tungsten':
147         cs.to_reference_transforms.append({
148             'type': 'matrix',
149             'matrix': [0.817416293, 0.090755698, 0.091828009, 0,
150                        -0.035361374, 1.065690585, -0.030329211, 0,
151                        0.010390366, -0.299271107, 1.288880741, 0,
152                        0, 0, 0, 1],
153             'direction': 'forward'})
154
155     cs.from_reference_transforms = []
156     return cs
157
158
159 def create_colorspaces(lut_directory, lut_resolution_1d):
160     """
161     Generates the colorspace conversions.
162
163     Parameters
164     ----------
165     parameter : type
166         Parameter description.
167
168     Returns
169     -------
170     type
171          Return value description.
172     """
173
174     colorspaces = []
175
176     # Full conversion
177     c_log_1 = create_c_log(
178         'Rec. 709 Daylight',
179         'Canon-Log',
180         'Canon-Log',
181         lut_directory,
182         lut_resolution_1d,
183         ["canonlog_rec709day"])
184     colorspaces.append(c_log_1)
185
186     c_log_2 = create_c_log(
187         'Rec. 709 Tungsten',
188         'Canon-Log',
189         'Canon-Log',
190         lut_directory,
191         lut_resolution_1d,
192         ["canonlog_rec709tung"])
193     colorspaces.append(c_log_2)
194
195     c_log_3 = create_c_log(
196         'DCI-P3 Daylight',
197         'Canon-Log',
198         'Canon-Log',
199         lut_directory,
200         lut_resolution_1d,
201         ["canonlog_dcip3day"])
202     colorspaces.append(c_log_3)
203
204     c_log_4 = create_c_log(
205         'DCI-P3 Tungsten',
206         'Canon-Log',
207         'Canon-Log',
208         lut_directory,
209         lut_resolution_1d,
210         ["canonlog_dcip3tung"])
211     colorspaces.append(c_log_4)
212
213     c_log_5 = create_c_log(
214         'Cinema Gamut Daylight',
215         'Canon-Log',
216         'Canon-Log',
217         lut_directory,
218         lut_resolution_1d,
219         ["canonlog_cgamutday"])
220     colorspaces.append(c_log_5)
221
222     c_log_6 = create_c_log(
223         'Cinema Gamut Tungsten',
224         'Canon-Log',
225         'Canon-Log',
226         lut_directory,
227         lut_resolution_1d,
228         ["canonlog_cgamuttung"])
229     colorspaces.append(c_log_6)
230
231     # Linearization Only
232     c_log_7 = create_c_log(
233         '',
234         'Canon-Log',
235         'Canon-Log',
236         lut_directory,
237         lut_resolution_1d,
238         ["crv_canonlog"])
239     colorspaces.append(c_log_7)
240
241     # Primaries Only
242     c_log_8 = create_c_log(
243         'Rec. 709 Daylight',
244         '',
245         'Canon-Log',
246         lut_directory,
247         lut_resolution_1d,
248         ["lin_canonrec709day"])
249     colorspaces.append(c_log_8)
250
251     c_log_9 = create_c_log(
252         'Rec. 709 Tungsten',
253         '',
254         'Canon-Log',
255         lut_directory,
256         lut_resolution_1d,
257         ["lin_canonrec709tung"])
258     colorspaces.append(c_log_9)
259
260     c_log_10 = create_c_log(
261         'DCI-P3 Daylight',
262         '',
263         'Canon-Log',
264         lut_directory,
265         lut_resolution_1d,
266         ["lin_canondcip3day"])
267     colorspaces.append(c_log_10)
268
269     c_log_11 = create_c_log(
270         'DCI-P3 Tungsten',
271         '',
272         'Canon-Log',
273         lut_directory,
274         lut_resolution_1d,
275         ["lin_canondcip3tung"])
276     colorspaces.append(c_log_11)
277
278     c_log_12 = create_c_log(
279         'Cinema Gamut Daylight',
280         '',
281         'Canon-Log',
282         lut_directory,
283         lut_resolution_1d,
284         ["lin_canoncgamutday"])
285     colorspaces.append(c_log_12)
286
287     c_log_13 = create_c_log(
288         'Cinema Gamut Tungsten',
289         '',
290         'Canon-Log',
291         lut_directory,
292         lut_resolution_1d,
293         ["lin_canoncgamuttung"])
294     colorspaces.append(c_log_13)
295
296     return colorspaces