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