Enforce quotes consistency.
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / create_arri_colorspaces.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 Implements support for *ARRI* colorspaces conversions and transfer functions.
6 """
7
8 import array
9 import math
10
11 import aces_ocio.generate_lut as genlut
12 from aces_ocio.utilities import ColorSpace, mat44_from_mat33
13
14
15 __author__ = 'ACES Developers'
16 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
17 __license__ = ''
18 __maintainer__ = 'ACES Developers'
19 __email__ = 'aces@oscars.org'
20 __status__ = 'Production'
21
22 __all__ = ['create_log_c',
23            'create_colorspaces']
24
25
26 def create_log_c(gamut,
27                  transfer_function,
28                  exposure_index,
29                  name,
30                  lut_directory,
31                  lut_resolution_1d):
32     """
33     Object description.
34
35     LogC to ACES.
36
37     Parameters
38     ----------
39     parameter : type
40         Parameter description.
41
42     Returns
43     -------
44     type
45          Return value description.
46     """
47
48     name = '%s (EI%s) - %s' % (transfer_function, exposure_index, gamut)
49     if transfer_function == '':
50         name = 'Linear - %s' % gamut
51     if gamut == '':
52         name = '%s (EI%s)' % (transfer_function, exposure_index)
53
54     cs = ColorSpace(name)
55     cs.description = name
56     cs.equality_group = ''
57     cs.family = 'ARRI'
58     cs.is_data = False
59
60     # Globals
61     IDT_maker_version = '0.08'
62
63     nominal_EI = 400.0
64     black_signal = 0.003907
65     mid_gray_signal = 0.01
66     encoding_gain = 0.256598
67     encoding_offset = 0.391007
68
69     def gain_for_EI(EI):
70         return (math.log(EI / nominal_EI) / math.log(2) * (
71             0.89 - 1) / 3 + 1) * encoding_gain
72
73     def log_c_inverse_parameters_for_EI(EI):
74         cut = 1.0 / 9.0
75         slope = 1.0 / (cut * math.log(10))
76         offset = math.log10(cut) - slope * cut
77         gain = EI / nominal_EI
78         gray = mid_gray_signal / gain
79         # The higher the EI, the lower the gamma
80         enc_gain = gain_for_EI(EI)
81         enc_offset = encoding_offset
82         for i in range(0, 3):
83             nz = ((95.0 / 1023.0 - enc_offset) / enc_gain - offset) / slope
84             enc_offset = encoding_offset - math.log10(1 + nz) * enc_gain
85         # Calculate some intermediate values
86         a = 1.0 / gray
87         b = nz - black_signal / gray
88         e = slope * a * enc_gain
89         f = enc_gain * (slope * b + offset) + enc_offset
90         # Manipulations so we can return relative exposure
91         s = 4 / (0.18 * EI)
92         t = black_signal
93         b += a * t
94         a *= s
95         f += e * t
96         e *= s
97
98         return {'a': a,
99                 'b': b,
100                 'cut': (cut - b) / a,
101                 'c': enc_gain,
102                 'd': enc_offset,
103                 'e': e,
104                 'f': f}
105
106     def log_c_to_linear(code_value, exposure_index):
107         p = log_c_inverse_parameters_for_EI(exposure_index)
108         breakpoint = p['e'] * p['cut'] + p['f']
109         if (code_value > breakpoint):
110             linear = ((pow(10, (code_value / 1023.0 - p['d']) / p['c']) -
111                        p['b']) / p['a'])
112         else:
113             linear = (code_value / 1023.0 - p['f']) / p['e']
114
115         # print(codeValue, linear)
116         return linear
117
118
119     cs.to_reference_transforms = []
120
121     if transfer_function == 'V3 LogC':
122         data = array.array('f', '\0' * lut_resolution_1d * 4)
123         for c in range(lut_resolution_1d):
124             data[c] = log_c_to_linear(1023.0 * c / (lut_resolution_1d - 1),
125                                       int(exposure_index))
126
127         lut = '%s_to_linear.spi1d' % (
128             '%s_%s' % (transfer_function, exposure_index))
129
130         # Remove spaces and parentheses
131         lut = lut.replace(' ', '_').replace(')', '_').replace('(', '_')
132
133         genlut.write_SPI_1d(lut_directory + '/' + lut,
134                             0.0,
135                             1.0,
136                             data,
137                             lut_resolution_1d,
138                             1)
139
140         # print('Writing %s' % lut)
141         cs.to_reference_transforms.append({
142             'type': 'lutFile',
143             'path': lut,
144             'interpolation': 'linear',
145             'direction': 'forward'
146         })
147
148     if gamut == 'Wide Gamut':
149         cs.to_reference_transforms.append({
150             'type': 'matrix',
151             'matrix': mat44_from_mat33([0.680206, 0.236137, 0.083658,
152                                       0.085415, 1.017471, -0.102886,
153                                       0.002057, -0.062563, 1.060506]),
154             'direction': 'forward'
155         })
156
157     cs.from_reference_transforms = []
158     return cs
159
160
161 def create_colorspaces(lut_directory, lut_resolution_1d):
162     """
163     Generates the colorspace conversions.
164
165     Parameters
166     ----------
167     parameter : type
168         Parameter description.
169
170     Returns
171     -------
172     type
173          Return value description.
174     """
175
176     colorspaces = []
177
178     transfer_function = 'V3 LogC'
179     gamut = 'Wide Gamut'
180
181     # EIs = [160.0, 200.0, 250.0, 320.0, 400.0, 500.0, 640.0, 800.0,
182     # 1000.0, 1280.0, 1600.0, 2000.0, 2560.0, 3200.0]
183     EIs = [160, 200, 250, 320, 400, 500, 640, 800,
184            1000, 1280, 1600, 2000, 2560, 3200]
185     default_EI = 800
186
187     # Full conversion
188     for EI in EIs:
189         log_c_EI_full = create_log_c(
190             gamut,
191             transfer_function,
192             EI,
193             'LogC',
194             lut_directory,
195             lut_resolution_1d)
196         colorspaces.append(log_c_EI_full)
197
198     # Linearization only
199     for EI in [800]:
200         log_c_EI_linearization = create_log_c(
201             '',
202             transfer_function,
203             EI,
204             'LogC',
205             lut_directory,
206             lut_resolution_1d)
207         colorspaces.append(log_c_EI_linearization)
208
209     # Primaries
210     log_c_EI_primaries = create_log_c(
211         gamut,
212         '',
213         default_EI,
214         'LogC',
215         lut_directory,
216         lut_resolution_1d)
217     colorspaces.append(log_c_EI_primaries)
218
219     return colorspaces