Implement code review notes.
[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                  lut_directory,
34                  lut_resolution_1d,
35                  aliases):
36     """
37     Object description.
38
39     LogC to ACES.
40
41     Parameters
42     ----------
43     parameter : type
44         Parameter description.
45
46     Returns
47     -------
48     type
49          Return value description.
50     """
51
52     name = '%s (EI%s) - %s' % (transfer_function, exposure_index, gamut)
53     if transfer_function == '':
54         name = 'Linear - ARRI %s' % gamut
55     if gamut == '':
56         name = 'Curve - %s (EI%s)' % (transfer_function, exposure_index)
57
58     cs = ColorSpace(name)
59     cs.description = name
60     cs.aliases = aliases
61     cs.equality_group = ''
62     cs.family = 'Input/ARRI'
63     cs.is_data = False
64
65     if gamut and transfer_function:
66         cs.aces_transform_id = (
67             '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     IDT_maker_version = '0.08'
75
76     nominal_EI = 400
77     black_signal = 0.003907
78     mid_gray_signal = 0.01
79     encoding_gain = 0.256598
80     encoding_offset = 0.391007
81
82     def gain_for_EI(EI):
83         return (math.log(EI / nominal_EI) / math.log(2) * (
84             0.89 - 1) / 3 + 1) * encoding_gain
85
86     def log_c_inverse_parameters_for_EI(EI):
87         cut = 1 / 9
88         slope = 1 / (cut * math.log(10))
89         offset = math.log10(cut) - slope * cut
90         gain = EI / nominal_EI
91         gray = mid_gray_signal / gain
92         # The higher the EI, the lower the gamma.
93         enc_gain = gain_for_EI(EI)
94         enc_offset = encoding_offset
95         for i in range(0, 3):
96             nz = ((95 / 1023 - enc_offset) / enc_gain - offset) / slope
97             enc_offset = encoding_offset - math.log10(1 + nz) * enc_gain
98
99         a = 1 / gray
100         b = nz - black_signal / gray
101         e = slope * a * enc_gain
102         f = enc_gain * (slope * b + offset) + enc_offset
103
104         # Ensuring we can return relative exposure.
105         s = 4 / (0.18 * EI)
106         t = black_signal
107         b += a * t
108         a *= s
109         f += e * t
110         e *= s
111
112         return {'a': a,
113                 'b': b,
114                 'cut': (cut - b) / a,
115                 'c': enc_gain,
116                 'd': enc_offset,
117                 'e': e,
118                 'f': f}
119
120     def normalized_log_c_to_linear(code_value, exposure_index):
121         p = log_c_inverse_parameters_for_EI(exposure_index)
122         breakpoint = p['e'] * p['cut'] + p['f']
123         if code_value > breakpoint:
124             linear = ((pow(10, (code_value - p['d']) / p['c']) -
125                        p['b']) / p['a'])
126         else:
127             linear = (code_value - p['f']) / p['e']
128         return linear
129
130     cs.to_reference_transforms = []
131
132     if transfer_function == 'V3 LogC':
133         data = array.array('f', '\0' * lut_resolution_1d * 4)
134         for c in range(lut_resolution_1d):
135             data[c] = normalized_log_c_to_linear(c / (lut_resolution_1d - 1),
136                                                  int(exposure_index))
137
138         lut = '%s_to_linear.spi1d' % (
139             '%s_%s' % (transfer_function, exposure_index))
140
141         lut = sanitize(lut)
142
143         genlut.write_SPI_1d(
144             os.path.join(lut_directory, lut),
145             0,
146             1,
147             data,
148             lut_resolution_1d,
149             1)
150
151         cs.to_reference_transforms.append({
152             'type': 'lutFile',
153             'path': lut,
154             'interpolation': 'linear',
155             'direction': 'forward'})
156
157     if gamut == 'Wide Gamut':
158         cs.to_reference_transforms.append({
159             'type': 'matrix',
160             'matrix': mat44_from_mat33([0.680206, 0.236137, 0.083658,
161                                         0.085415, 1.017471, -0.102886,
162                                         0.002057, -0.062563, 1.060506]),
163             'direction': 'forward'})
164
165     cs.from_reference_transforms = []
166     return cs
167
168
169 def create_colorspaces(lut_directory, lut_resolution_1d):
170     """
171     Generates the colorspace conversions.
172
173     Parameters
174     ----------
175     parameter : type
176         Parameter description.
177
178     Returns
179     -------
180     type
181          Return value description.
182     """
183
184     colorspaces = []
185
186     transfer_function = 'V3 LogC'
187     gamut = 'Wide Gamut'
188
189     # EIs = [160, 200, 250, 320, 400, 500, 640, 800,
190     # 1000, 1280, 1600, 2000, 2560, 3200]
191     EIs = [160, 200, 250, 320, 400, 500, 640, 800,
192            1000, 1280, 1600, 2000, 2560, 3200]
193     default_EI = 800
194
195     # Full Conversion
196     for EI in EIs:
197         log_c_EI_full = create_log_c(
198             gamut,
199             transfer_function,
200             EI,
201             lut_directory,
202             lut_resolution_1d,
203             ['%sei%s_%s' % ('logc3', str(EI), 'arriwide')])
204         colorspaces.append(log_c_EI_full)
205
206     # Linearization Only
207     for EI in [800]:
208         log_c_EI_linearization = create_log_c(
209             '',
210             transfer_function,
211             EI,
212             lut_directory,
213             lut_resolution_1d,
214             ['crv_%sei%s' % ('logc3', str(EI))])
215         colorspaces.append(log_c_EI_linearization)
216
217     # Primaries Only
218     log_c_EI_primaries = create_log_c(
219         gamut,
220         '',
221         default_EI,
222         lut_directory,
223         lut_resolution_1d,
224         ['%s_%s' % ('lin', 'arriwide')])
225     colorspaces.append(log_c_EI_primaries)
226
227     return colorspaces