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