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