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