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