Fix "PEP8" coding style violations.
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / colorspaces / arri.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 from __future__ import division
9
10 import array
11 import math
12 import os
13
14 import PyOpenColorIO as ocio
15
16 import aces_ocio.generate_lut as genlut
17 from aces_ocio.utilities import ColorSpace, mat44_from_mat33, sanitize
18
19 __author__ = 'ACES Developers'
20 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
21 __license__ = ''
22 __maintainer__ = 'ACES Developers'
23 __email__ = 'aces@oscars.org'
24 __status__ = 'Production'
25
26 __all__ = ['create_log_c',
27            'create_colorspaces']
28
29
30 def create_log_c(gamut,
31                  transfer_function,
32                  exposure_index,
33                  name,
34                  lut_directory,
35                  lut_resolution_1d,
36                  aliases):
37     """
38     Object description.
39
40     LogC to ACES.
41
42     Parameters
43     ----------
44     parameter : type
45         Parameter description.
46
47     Returns
48     -------
49     type
50          Return value description.
51     """
52
53     name = '%s (EI%s) - %s' % (transfer_function, exposure_index, gamut)
54     if transfer_function == '':
55         name = 'Linear - ARRI %s' % gamut
56     if gamut == '':
57         name = 'Curve - %s (EI%s)' % (transfer_function, exposure_index)
58
59     cs = ColorSpace(name)
60     cs.description = name
61     cs.aliases = aliases
62     cs.equality_group = ''
63     cs.family = 'Input/ARRI'
64     cs.is_data = False
65
66     if gamut and transfer_function:
67         cs.aces_transform_id = (
68             'IDT.ARRI.Alexa-v3-logC-EI%s.a1.v1' % exposure_index)
69
70     # A linear space needs allocation variables
71     if transfer_function == '':
72         cs.allocation_type = ocio.Constants.ALLOCATION_LG2
73         cs.allocation_vars = [-8, 5, 0.00390625]
74
75     # Globals.
76     IDT_maker_version = '0.08'
77
78     nominal_EI = 400
79     black_signal = 0.003907
80     mid_gray_signal = 0.01
81     encoding_gain = 0.256598
82     encoding_offset = 0.391007
83
84     def gain_for_EI(EI):
85         return (math.log(EI / nominal_EI) / math.log(2) * (
86             0.89 - 1) / 3 + 1) * encoding_gain
87
88     def log_c_inverse_parameters_for_EI(EI):
89         cut = 1 / 9
90         slope = 1 / (cut * math.log(10))
91         offset = math.log10(cut) - slope * cut
92         gain = EI / nominal_EI
93         gray = mid_gray_signal / gain
94         # The higher the EI, the lower the gamma.
95         enc_gain = gain_for_EI(EI)
96         enc_offset = encoding_offset
97         for i in range(0, 3):
98             nz = ((95 / 1023 - enc_offset) / enc_gain - offset) / slope
99             enc_offset = encoding_offset - math.log10(1 + nz) * enc_gain
100
101         a = 1 / gray
102         b = nz - black_signal / gray
103         e = slope * a * enc_gain
104         f = enc_gain * (slope * b + offset) + enc_offset
105
106         # Ensuring we can return relative exposure.
107         s = 4 / (0.18 * EI)
108         t = black_signal
109         b += a * t
110         a *= s
111         f += e * t
112         e *= s
113
114         return {'a': a,
115                 'b': b,
116                 'cut': (cut - b) / a,
117                 'c': enc_gain,
118                 'd': enc_offset,
119                 'e': e,
120                 'f': f}
121
122     def normalized_log_c_to_linear(code_value, exposure_index):
123         p = log_c_inverse_parameters_for_EI(exposure_index)
124         breakpoint = p['e'] * p['cut'] + p['f']
125         if code_value > breakpoint:
126             linear = ((pow(10, (code_value - p['d']) / p['c']) -
127                        p['b']) / p['a'])
128         else:
129             linear = (code_value - p['f']) / p['e']
130         return linear
131
132     cs.to_reference_transforms = []
133
134     if transfer_function == 'V3 LogC':
135         data = array.array('f', '\0' * lut_resolution_1d * 4)
136         for c in range(lut_resolution_1d):
137             data[c] = normalized_log_c_to_linear(c / (lut_resolution_1d - 1),
138                                                  int(exposure_index))
139
140         lut = '%s_to_linear.spi1d' % (
141             '%s_%s' % (transfer_function, exposure_index))
142
143         lut = sanitize(lut)
144
145         genlut.write_SPI_1d(
146             os.path.join(lut_directory, lut),
147             0,
148             1,
149             data,
150             lut_resolution_1d,
151             1)
152
153         cs.to_reference_transforms.append({
154             'type': 'lutFile',
155             'path': lut,
156             'interpolation': 'linear',
157             'direction': 'forward'
158         })
159
160     if gamut == 'Wide Gamut':
161         cs.to_reference_transforms.append({
162             'type': 'matrix',
163             'matrix': mat44_from_mat33([0.680206, 0.236137, 0.083658,
164                                         0.085415, 1.017471, -0.102886,
165                                         0.002057, -0.062563, 1.060506]),
166             'direction': 'forward'
167         })
168
169     cs.from_reference_transforms = []
170     return cs
171
172
173 def create_colorspaces(lut_directory, lut_resolution_1d):
174     """
175     Generates the colorspace conversions.
176
177     Parameters
178     ----------
179     parameter : type
180         Parameter description.
181
182     Returns
183     -------
184     type
185          Return value description.
186     """
187
188     colorspaces = []
189
190     transfer_function = 'V3 LogC'
191     gamut = 'Wide Gamut'
192
193     # EIs = [160, 200, 250, 320, 400, 500, 640, 800,
194     # 1000, 1280, 1600, 2000, 2560, 3200]
195     EIs = [160, 200, 250, 320, 400, 500, 640, 800,
196            1000, 1280, 1600, 2000, 2560, 3200]
197     default_EI = 800
198
199     # Full Conversion
200     for EI in EIs:
201         log_c_EI_full = create_log_c(
202             gamut,
203             transfer_function,
204             EI,
205             'LogC',
206             lut_directory,
207             lut_resolution_1d,
208             ['%sei%s_%s' % ('logc3', str(EI), 'arriwide')])
209         colorspaces.append(log_c_EI_full)
210
211     # Linearization Only
212     for EI in [800]:
213         log_c_EI_linearization = create_log_c(
214             '',
215             transfer_function,
216             EI,
217             'LogC',
218             lut_directory,
219             lut_resolution_1d,
220             ['crv_%sei%s' % ('logc3', str(EI))])
221         colorspaces.append(log_c_EI_linearization)
222
223     # Primaries Only
224     log_c_EI_primaries = create_log_c(
225         gamut,
226         '',
227         default_EI,
228         'LogC',
229         lut_directory,
230         lut_resolution_1d,
231         ['%s_%s' % ('lin', 'arriwide')])
232     colorspaces.append(log_c_EI_primaries)
233
234     return colorspaces