Add module headers doctoring skeletons.
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / createCanonColorSpaces.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.generateLUT as genlut
11 from aces_ocio.util 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__ = ['createCanonLog',
21            'createColorSpaces']
22
23
24 def createCanonLog(gamut, transferFunction, name, lutDir, lutResolution1d):
25     """
26     Object description.
27
28     Canon-Log to ACES.
29
30     Parameters
31     ----------
32     parameter : type
33         Parameter description.
34
35     Returns
36     -------
37     type
38          Return value description.
39     """
40
41     name = "%s - %s" % (transferFunction, gamut)
42     if transferFunction == "":
43         name = "Linear - %s" % gamut
44     if gamut == "":
45         name = "%s" % transferFunction
46
47     cs = ColorSpace(name)
48     cs.description = name
49     cs.equalityGroup = ''
50     cs.family = 'Canon'
51     cs.isData = False
52
53     def legalToFull(codeValue):
54         return (codeValue - 64.0) / (940.0 - 64.0)
55
56     def canonLogToLinear(codeValue):
57         # log = fullToLegal(c1 * log10(c2*linear + 1) + c3)
58         # linear = (pow(10, (legalToFul(log) - c3)/c1) - 1)/c2
59         c1 = 0.529136
60         c2 = 10.1596
61         c3 = 0.0730597
62
63         linear = (pow(10.0, (legalToFull(codeValue) - c3) / c1) - 1.0) / c2
64         linear = 0.9 * linear
65         # print(codeValue, linear)
66         return linear
67
68     cs.toReferenceTransforms = []
69
70     if transferFunction == "Canon-Log":
71         data = array.array('f', "\0" * lutResolution1d * 4)
72         for c in range(lutResolution1d):
73             data[c] = canonLogToLinear(1023.0 * c / (lutResolution1d - 1))
74
75         lut = "%s_to_linear.spi1d" % transferFunction
76         genlut.writeSPI1D(lutDir + "/" + lut,
77                           0.0,
78                           1.0,
79                           data,
80                           lutResolution1d,
81                           1)
82
83         cs.toReferenceTransforms.append({
84             'type': 'lutFile',
85             'path': lut,
86             'interpolation': 'linear',
87             'direction': 'forward'
88         })
89
90     if gamut == 'Rec. 709 Daylight':
91         cs.toReferenceTransforms.append({
92             'type': 'matrix',
93             'matrix': [0.561538969, 0.402060105, 0.036400926, 0.0,
94                        0.092739623, 0.924121198, -0.016860821, 0.0,
95                        0.084812961, 0.006373835, 0.908813204, 0.0,
96                        0, 0, 0, 1.0],
97             'direction': 'forward'
98         })
99     elif gamut == 'Rec. 709 Tungsten':
100         cs.toReferenceTransforms.append({
101             'type': 'matrix',
102             'matrix': [0.566996399, 0.365079418, 0.067924183, 0.0,
103                        0.070901044, 0.880331008, 0.048767948, 0.0,
104                        0.073013542, -0.066540862, 0.99352732, 0.0,
105                        0, 0, 0, 1.0],
106             'direction': 'forward'
107         })
108     elif gamut == 'DCI-P3 Daylight':
109         cs.toReferenceTransforms.append({
110             'type': 'matrix',
111             'matrix': [0.607160575, 0.299507286, 0.093332140, 0.0,
112                        0.004968120, 1.050982224, -0.055950343, 0.0,
113                        -0.007839939, 0.000809127, 1.007030813, 0.0,
114                        0, 0, 0, 1.0],
115             'direction': 'forward'
116         })
117     elif gamut == 'DCI-P3 Tungsten':
118         cs.toReferenceTransforms.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         })
126     elif gamut == 'Cinema Gamut Daylight':
127         cs.toReferenceTransforms.append({
128             'type': 'matrix',
129             'matrix': [0.763064455, 0.149021161, 0.087914384, 0.0,
130                        0.003657457, 1.10696038, -0.110617837, 0.0,
131                        -0.009407794, -0.218383305, 1.227791099, 0.0,
132                        0, 0, 0, 1.0],
133             'direction': 'forward'
134         })
135     elif gamut == 'Cinema Gamut Tungsten':
136         cs.toReferenceTransforms.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
145     cs.fromReferenceTransforms = []
146     return cs
147
148
149 def createColorSpaces(lutDir, lutResolution1d):
150     """
151     Generates the colorspace conversions.
152
153     Parameters
154     ----------
155     parameter : type
156         Parameter description.
157
158     Returns
159     -------
160     type
161          Return value description.
162     """
163
164     colorspaces = []
165
166     # Full conversion
167     CanonLog1 = createCanonLog(
168         "Rec. 709 Daylight", "Canon-Log", "Canon-Log", lutDir, lutResolution1d)
169     colorspaces.append(CanonLog1)
170
171     CanonLog2 = createCanonLog(
172         "Rec. 709 Tungsten", "Canon-Log", "Canon-Log", lutDir, lutResolution1d)
173     colorspaces.append(CanonLog2)
174
175     CanonLog3 = createCanonLog(
176         "DCI-P3 Daylight", "Canon-Log", "Canon-Log", lutDir, lutResolution1d)
177     colorspaces.append(CanonLog3)
178
179     CanonLog4 = createCanonLog(
180         "DCI-P3 Tungsten", "Canon-Log", "Canon-Log", lutDir, lutResolution1d)
181     colorspaces.append(CanonLog4)
182
183     CanonLog5 = createCanonLog(
184         "Cinema Gamut Daylight", "Canon-Log", "Canon-Log",
185         lutDir, lutResolution1d)
186     colorspaces.append(CanonLog5)
187
188     CanonLog6 = createCanonLog(
189         "Cinema Gamut Tungsten", "Canon-Log", "Canon-Log",
190         lutDir, lutResolution1d)
191     colorspaces.append(CanonLog6)
192
193     # Linearization only
194     CanonLog7 = createCanonLog(
195         '', "Canon-Log", "Canon-Log", lutDir, lutResolution1d)
196     colorspaces.append(CanonLog7)
197
198     # Primaries only
199     CanonLog8 = createCanonLog(
200         "Rec. 709 Daylight", "", "Canon-Log", lutDir, lutResolution1d)
201     colorspaces.append(CanonLog8)
202
203     CanonLog9 = createCanonLog(
204         "Rec. 709 Tungsten", "", "Canon-Log", lutDir, lutResolution1d)
205     colorspaces.append(CanonLog9)
206
207     CanonLog10 = createCanonLog(
208         "DCI-P3 Daylight", "", "Canon-Log", lutDir, lutResolution1d)
209     colorspaces.append(CanonLog10)
210
211     CanonLog11 = createCanonLog("DCI-P3 Tungsten", "", "Canon-Log", lutDir,
212                                 lutResolution1d)
213     colorspaces.append(CanonLog11)
214
215     CanonLog12 = createCanonLog(
216         "Cinema Gamut Daylight", "", "Canon-Log", lutDir, lutResolution1d)
217     colorspaces.append(CanonLog12)
218
219     CanonLog13 = createCanonLog(
220         "Cinema Gamut Tungsten", "", "Canon-Log", lutDir, lutResolution1d)
221     colorspaces.append(CanonLog13)
222
223     return colorspaces