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