Improve paths handling and strings concatenations.
[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 import array
9 import math
10 import os
11
12 import aces_ocio.generate_lut as genlut
13 from aces_ocio.utilities import ColorSpace, mat44_from_mat33
14
15
16 __author__ = 'ACES Developers'
17 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
18 __license__ = ''
19 __maintainer__ = 'ACES Developers'
20 __email__ = 'aces@oscars.org'
21 __status__ = 'Production'
22
23 __all__ = ['create_log_c',
24            'create_colorspaces']
25
26
27 def create_log_c(gamut,
28                  transfer_function,
29                  exposure_index,
30                  name,
31                  lut_directory,
32                  lut_resolution_1d):
33     """
34     Object description.
35
36     LogC to ACES.
37
38     Parameters
39     ----------
40     parameter : type
41         Parameter description.
42
43     Returns
44     -------
45     type
46          Return value description.
47     """
48
49     name = '%s (EI%s) - %s' % (transfer_function, exposure_index, gamut)
50     if transfer_function == '':
51         name = 'Linear - %s' % gamut
52     if gamut == '':
53         name = '%s (EI%s)' % (transfer_function, exposure_index)
54
55     cs = ColorSpace(name)
56     cs.description = name
57     cs.equality_group = ''
58     cs.family = 'ARRI'
59     cs.is_data = False
60
61     # Globals
62     IDT_maker_version = '0.08'
63
64     nominal_EI = 400.0
65     black_signal = 0.003907
66     mid_gray_signal = 0.01
67     encoding_gain = 0.256598
68     encoding_offset = 0.391007
69
70     def gain_for_EI(EI):
71         return (math.log(EI / nominal_EI) / math.log(2) * (
72             0.89 - 1) / 3 + 1) * encoding_gain
73
74     def log_c_inverse_parameters_for_EI(EI):
75         cut = 1.0 / 9.0
76         slope = 1.0 / (cut * math.log(10))
77         offset = math.log10(cut) - slope * cut
78         gain = EI / nominal_EI
79         gray = mid_gray_signal / gain
80         # The higher the EI, the lower the gamma
81         enc_gain = gain_for_EI(EI)
82         enc_offset = encoding_offset
83         for i in range(0, 3):
84             nz = ((95.0 / 1023.0 - enc_offset) / enc_gain - offset) / slope
85             enc_offset = encoding_offset - math.log10(1 + nz) * enc_gain
86         # Calculate some intermediate values
87         a = 1.0 / gray
88         b = nz - black_signal / gray
89         e = slope * a * enc_gain
90         f = enc_gain * (slope * b + offset) + enc_offset
91         # Manipulations so we can return relative exposure
92         s = 4 / (0.18 * EI)
93         t = black_signal
94         b += a * t
95         a *= s
96         f += e * t
97         e *= s
98
99         return {'a': a,
100                 'b': b,
101                 'cut': (cut - b) / a,
102                 'c': enc_gain,
103                 'd': enc_offset,
104                 'e': e,
105                 'f': f}
106
107     def log_c_to_linear(code_value, exposure_index):
108         p = log_c_inverse_parameters_for_EI(exposure_index)
109         breakpoint = p['e'] * p['cut'] + p['f']
110         if (code_value > breakpoint):
111             linear = ((pow(10, (code_value / 1023.0 - p['d']) / p['c']) -
112                        p['b']) / p['a'])
113         else:
114             linear = (code_value / 1023.0 - p['f']) / p['e']
115
116         # print(codeValue, linear)
117         return linear
118
119
120     cs.to_reference_transforms = []
121
122     if transfer_function == 'V3 LogC':
123         data = array.array('f', '\0' * lut_resolution_1d * 4)
124         for c in range(lut_resolution_1d):
125             data[c] = log_c_to_linear(1023.0 * c / (lut_resolution_1d - 1),
126                                       int(exposure_index))
127
128         lut = '%s_to_linear.spi1d' % (
129             '%s_%s' % (transfer_function, exposure_index))
130
131         # Remove spaces and parentheses
132         lut = lut.replace(' ', '_').replace(')', '_').replace('(', '_')
133
134         genlut.write_SPI_1d(
135             os.path.join(lut_directory, lut),
136             0.0,
137             1.0,
138             data,
139             lut_resolution_1d,
140             1)
141
142         # print('Writing %s' % lut)
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.0, 200.0, 250.0, 320.0, 400.0, 500.0, 640.0, 800.0,
184     # 1000.0, 1280.0, 1600.0, 2000.0, 2560.0, 3200.0]
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         colorspaces.append(log_c_EI_full)
199
200     # Linearization only
201     for EI in [800]:
202         log_c_EI_linearization = create_log_c(
203             '',
204             transfer_function,
205             EI,
206             'LogC',
207             lut_directory,
208             lut_resolution_1d)
209         colorspaces.append(log_c_EI_linearization)
210
211     # Primaries
212     log_c_EI_primaries = create_log_c(
213         gamut,
214         '',
215         default_EI,
216         'LogC',
217         lut_directory,
218         lut_resolution_1d)
219     colorspaces.append(log_c_EI_primaries)
220
221     return colorspaces