Code formatting.
[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 = "IDT.ARRI.Alexa-v3-logC-EI%s.a1.v1" % exposure_index
68
69     # A linear space needs allocation variables
70     if transfer_function == '':
71         cs.allocation_type = ocio.Constants.ALLOCATION_LG2
72         cs.allocation_vars = [-8, 5, 0.00390625]
73
74     # Globals.
75     IDT_maker_version = '0.08'
76
77     nominal_EI = 400
78     black_signal = 0.003907
79     mid_gray_signal = 0.01
80     encoding_gain = 0.256598
81     encoding_offset = 0.391007
82
83     def gain_for_EI(EI):
84         return (math.log(EI / nominal_EI) / math.log(2) * (
85             0.89 - 1) / 3 + 1) * encoding_gain
86
87     def log_c_inverse_parameters_for_EI(EI):
88         cut = 1 / 9
89         slope = 1 / (cut * math.log(10))
90         offset = math.log10(cut) - slope * cut
91         gain = EI / nominal_EI
92         gray = mid_gray_signal / gain
93         # The higher the EI, the lower the gamma.
94         enc_gain = gain_for_EI(EI)
95         enc_offset = encoding_offset
96         for i in range(0, 3):
97             nz = ((95 / 1023 - enc_offset) / enc_gain - offset) / slope
98             enc_offset = encoding_offset - math.log10(1 + nz) * enc_gain
99
100         a = 1 / gray
101         b = nz - black_signal / gray
102         e = slope * a * enc_gain
103         f = enc_gain * (slope * b + offset) + enc_offset
104
105         # Ensuring we can return relative exposure.
106         s = 4 / (0.18 * EI)
107         t = black_signal
108         b += a * t
109         a *= s
110         f += e * t
111         e *= s
112
113         return {'a': a,
114                 'b': b,
115                 'cut': (cut - b) / a,
116                 'c': enc_gain,
117                 'd': enc_offset,
118                 'e': e,
119                 'f': f}
120
121     def normalized_log_c_to_linear(code_value, exposure_index):
122         p = log_c_inverse_parameters_for_EI(exposure_index)
123         breakpoint = p['e'] * p['cut'] + p['f']
124         if code_value > breakpoint:
125             linear = ((pow(10, (code_value - p['d']) / p['c']) -
126                        p['b']) / p['a'])
127         else:
128             linear = (code_value - p['f']) / p['e']
129         return linear
130
131     cs.to_reference_transforms = []
132
133     if transfer_function == 'V3 LogC':
134         data = array.array('f', '\0' * lut_resolution_1d * 4)
135         for c in range(lut_resolution_1d):
136             data[c] = normalized_log_c_to_linear(c / (lut_resolution_1d - 1),
137                                                  int(exposure_index))
138
139         lut = '%s_to_linear.spi1d' % (
140             '%s_%s' % (transfer_function, exposure_index))
141
142         lut = sanitize(lut)
143
144         genlut.write_SPI_1d(
145             os.path.join(lut_directory, lut),
146             0,
147             1,
148             data,
149             lut_resolution_1d,
150             1)
151
152         cs.to_reference_transforms.append({
153             'type': 'lutFile',
154             'path': lut,
155             'interpolation': 'linear',
156             'direction': 'forward'
157         })
158
159     if gamut == 'Wide Gamut':
160         cs.to_reference_transforms.append({
161             'type': 'matrix',
162             'matrix': mat44_from_mat33([0.680206, 0.236137, 0.083658,
163                                         0.085415, 1.017471, -0.102886,
164                                         0.002057, -0.062563, 1.060506]),
165             'direction': 'forward'
166         })
167
168     cs.from_reference_transforms = []
169     return cs
170
171
172 def create_colorspaces(lut_directory, lut_resolution_1d):
173     """
174     Generates the colorspace conversions.
175
176     Parameters
177     ----------
178     parameter : type
179         Parameter description.
180
181     Returns
182     -------
183     type
184          Return value description.
185     """
186
187     colorspaces = []
188
189     transfer_function = 'V3 LogC'
190     gamut = 'Wide Gamut'
191
192     # EIs = [160, 200, 250, 320, 400, 500, 640, 800,
193     # 1000, 1280, 1600, 2000, 2560, 3200]
194     EIs = [160, 200, 250, 320, 400, 500, 640, 800,
195            1000, 1280, 1600, 2000, 2560, 3200]
196     default_EI = 800
197
198     # Full Conversion
199     for EI in EIs:
200         log_c_EI_full = create_log_c(
201             gamut,
202             transfer_function,
203             EI,
204             'LogC',
205             lut_directory,
206             lut_resolution_1d,
207             ["%sei%s_%s" % ("logc3", str(EI), "arriwide")])
208         colorspaces.append(log_c_EI_full)
209
210     # Linearization Only
211     for EI in [800]:
212         log_c_EI_linearization = create_log_c(
213             '',
214             transfer_function,
215             EI,
216             'LogC',
217             lut_directory,
218             lut_resolution_1d,
219             ["crv_%sei%s" % ("logc3", str(EI))])
220         colorspaces.append(log_c_EI_linearization)
221
222     # Primaries Only
223     log_c_EI_primaries = create_log_c(
224         gamut,
225         '',
226         default_EI,
227         'LogC',
228         lut_directory,
229         lut_resolution_1d,
230         ["%s_%s" % ('lin', "arriwide")])
231     colorspaces.append(log_c_EI_primaries)
232
233     return colorspaces