Comments pruning session.
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / create_canon_colorspaces.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 import array
9 import os
10
11 import aces_ocio.generate_lut as genlut
12 from aces_ocio.utilities import ColorSpace
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_c_log',
22            'create_colorspaces']
23
24
25 def create_c_log(gamut,
26                  transfer_function,
27                  name,
28                  lut_directory,
29                  lut_resolution_1d):
30     """
31     Object description.
32
33     Canon-Log 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 = 'Canon'
56     cs.is_data = False
57
58     def legal_to_full(codeValue):
59         return (codeValue - 64.0) / (940.0 - 64.0)
60
61     def c_log_to_linear(codeValue):
62         # log = fullToLegal(c1 * log10(c2*linear + 1) + c3)
63         # linear = (pow(10, (legalToFul(log) - c3)/c1) - 1)/c2
64         c1 = 0.529136
65         c2 = 10.1596
66         c3 = 0.0730597
67
68         linear = (pow(10.0, (legal_to_full(codeValue) - c3) / c1) - 1.0) / c2
69         linear *= 0.9
70
71         return linear
72
73     cs.to_reference_transforms = []
74
75     if transfer_function == 'Canon-Log':
76         data = array.array('f', '\0' * lut_resolution_1d * 4)
77         for c in range(lut_resolution_1d):
78             data[c] = c_log_to_linear(1023.0 * c / (lut_resolution_1d - 1))
79
80         lut = '%s_to_linear.spi1d' % transfer_function
81         genlut.write_SPI_1d(
82             os.path.join(lut_directory, lut),
83             0.0,
84             1.0,
85             data,
86             lut_resolution_1d,
87             1)
88
89         cs.to_reference_transforms.append({
90             'type': 'lutFile',
91             'path': lut,
92             'interpolation': 'linear',
93             'direction': 'forward'})
94
95     if gamut == 'Rec. 709 Daylight':
96         cs.to_reference_transforms.append({
97             'type': 'matrix',
98             'matrix': [0.561538969, 0.402060105, 0.036400926, 0.0,
99                        0.092739623, 0.924121198, -0.016860821, 0.0,
100                        0.084812961, 0.006373835, 0.908813204, 0.0,
101                        0, 0, 0, 1.0],
102             'direction': 'forward'})
103     elif gamut == 'Rec. 709 Tungsten':
104         cs.to_reference_transforms.append({
105             'type': 'matrix',
106             'matrix': [0.566996399, 0.365079418, 0.067924183, 0.0,
107                        0.070901044, 0.880331008, 0.048767948, 0.0,
108                        0.073013542, -0.066540862, 0.99352732, 0.0,
109                        0, 0, 0, 1.0],
110             'direction': 'forward'})
111     elif gamut == 'DCI-P3 Daylight':
112         cs.to_reference_transforms.append({
113             'type': 'matrix',
114             'matrix': [0.607160575, 0.299507286, 0.093332140, 0.0,
115                        0.004968120, 1.050982224, -0.055950343, 0.0,
116                        -0.007839939, 0.000809127, 1.007030813, 0.0,
117                        0, 0, 0, 1.0],
118             'direction': 'forward'})
119     elif gamut == 'DCI-P3 Tungsten':
120         cs.to_reference_transforms.append({
121             'type': 'matrix',
122             'matrix': [0.650279125, 0.253880169, 0.095840706, 0.0,
123                        -0.026137986, 1.017900530, 0.008237456, 0.0,
124                        0.007757558, -0.063081669, 1.055324110, 0.0,
125                        0, 0, 0, 1.0],
126             'direction': 'forward'})
127     elif gamut == 'Cinema Gamut Daylight':
128         cs.to_reference_transforms.append({
129             'type': 'matrix',
130             'matrix': [0.763064455, 0.149021161, 0.087914384, 0.0,
131                        0.003657457, 1.10696038, -0.110617837, 0.0,
132                        -0.009407794, -0.218383305, 1.227791099, 0.0,
133                        0, 0, 0, 1.0],
134             'direction': 'forward'})
135     elif gamut == 'Cinema Gamut Tungsten':
136         cs.to_reference_transforms.append({
137             'type': 'matrix',
138             'matrix': [0.817416293, 0.090755698, 0.091828009, 0.0,
139                        -0.035361374, 1.065690585, -0.030329211, 0.0,
140                        0.010390366, -0.299271107, 1.288880741, 0.0,
141                        0, 0, 0, 1.0],
142             'direction': 'forward'})
143
144     cs.from_reference_transforms = []
145     return cs
146
147
148 def create_colorspaces(lut_directory, lut_resolution_1d):
149     """
150     Generates the colorspace conversions.
151
152     Parameters
153     ----------
154     parameter : type
155         Parameter description.
156
157     Returns
158     -------
159     type
160          Return value description.
161     """
162
163     colorspaces = []
164
165     # Full conversion
166     c_log_1 = create_c_log(
167         'Rec. 709 Daylight',
168         'Canon-Log',
169         'Canon-Log',
170         lut_directory,
171         lut_resolution_1d)
172     colorspaces.append(c_log_1)
173
174     c_log_2 = create_c_log(
175         'Rec. 709 Tungsten',
176         'Canon-Log',
177         'Canon-Log',
178         lut_directory,
179         lut_resolution_1d)
180     colorspaces.append(c_log_2)
181
182     c_log_3 = create_c_log(
183         'DCI-P3 Daylight',
184         'Canon-Log',
185         'Canon-Log',
186         lut_directory,
187         lut_resolution_1d)
188     colorspaces.append(c_log_3)
189
190     c_log_4 = create_c_log(
191         'DCI-P3 Tungsten',
192         'Canon-Log',
193         'Canon-Log',
194         lut_directory,
195         lut_resolution_1d)
196     colorspaces.append(c_log_4)
197
198     c_log_5 = create_c_log(
199         'Cinema Gamut Daylight',
200         'Canon-Log',
201         'Canon-Log',
202         lut_directory,
203         lut_resolution_1d)
204     colorspaces.append(c_log_5)
205
206     c_log_6 = create_c_log(
207         'Cinema Gamut Tungsten',
208         'Canon-Log',
209         'Canon-Log',
210         lut_directory,
211         lut_resolution_1d)
212     colorspaces.append(c_log_6)
213
214     # Linearization Only
215     c_log_7 = create_c_log(
216         '',
217         'Canon-Log',
218         'Canon-Log',
219         lut_directory,
220         lut_resolution_1d)
221     colorspaces.append(c_log_7)
222
223     # Primaries Only
224     c_log_8 = create_c_log(
225         'Rec. 709 Daylight',
226         '',
227         'Canon-Log',
228         lut_directory,
229         lut_resolution_1d)
230     colorspaces.append(c_log_8)
231
232     c_log_9 = create_c_log(
233         'Rec. 709 Tungsten',
234         '',
235         'Canon-Log',
236         lut_directory,
237         lut_resolution_1d)
238     colorspaces.append(c_log_9)
239
240     c_log_10 = create_c_log(
241         'DCI-P3 Daylight',
242         '',
243         'Canon-Log',
244         lut_directory,
245         lut_resolution_1d)
246     colorspaces.append(c_log_10)
247
248     c_log_11 = create_c_log(
249         'DCI-P3 Tungsten',
250         '',
251         'Canon-Log',
252         lut_directory,
253         lut_resolution_1d)
254     colorspaces.append(c_log_11)
255
256     c_log_12 = create_c_log(
257         'Cinema Gamut Daylight',
258         '',
259         'Canon-Log',
260         lut_directory,
261         lut_resolution_1d)
262     colorspaces.append(c_log_12)
263
264     c_log_13 = create_c_log(
265         'Cinema Gamut Tungsten',
266         '',
267         'Canon-Log',
268         lut_directory,
269         lut_resolution_1d)
270     colorspaces.append(c_log_13)
271
272     return colorspaces