Added 'Curve - ' prefix to colorspaces that only included a transfer function/curve.
[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
20 __author__ = 'ACES Developers'
21 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
22 __license__ = ''
23 __maintainer__ = 'ACES Developers'
24 __email__ = 'aces@oscars.org'
25 __status__ = 'Production'
26
27 __all__ = ['create_log_c',
28            'create_colorspaces']
29
30
31 def create_log_c(gamut,
32                  transfer_function,
33                  exposure_index,
34                  name,
35                  lut_directory,
36                  lut_resolution_1d,
37                  aliases):
38     """
39     Object description.
40
41     LogC to ACES.
42
43     Parameters
44     ----------
45     parameter : type
46         Parameter description.
47
48     Returns
49     -------
50     type
51          Return value description.
52     """
53
54     name = '%s (EI%s) - %s' % (transfer_function, exposure_index, gamut)
55     if transfer_function == '':
56         name = 'Linear - ARRI %s' % gamut
57     if gamut == '':
58         name = 'Curve - %s (EI%s)' % (transfer_function, exposure_index)
59
60     cs = ColorSpace(name)
61     cs.description = name
62     cs.aliases = aliases
63     cs.equality_group = ''
64     cs.family = 'Input/ARRI'
65     cs.is_data = False
66
67     # A linear space needs allocation variables
68     if transfer_function == '':
69         cs.allocation_type = ocio.Constants.ALLOCATION_LG2
70         cs.allocation_vars = [-8, 5, 0.00390625]
71
72     # Globals.
73     IDT_maker_version = '0.08'
74
75     nominal_EI = 400
76     black_signal = 0.003907
77     mid_gray_signal = 0.01
78     encoding_gain = 0.256598
79     encoding_offset = 0.391007
80
81     def gain_for_EI(EI):
82         return (math.log(EI / nominal_EI) / math.log(2) * (
83             0.89 - 1) / 3 + 1) * encoding_gain
84
85     def log_c_inverse_parameters_for_EI(EI):
86         cut = 1 / 9
87         slope = 1 / (cut * math.log(10))
88         offset = math.log10(cut) - slope * cut
89         gain = EI / nominal_EI
90         gray = mid_gray_signal / gain
91         # The higher the EI, the lower the gamma.
92         enc_gain = gain_for_EI(EI)
93         enc_offset = encoding_offset
94         for i in range(0, 3):
95             nz = ((95 / 1023 - enc_offset) / enc_gain - offset) / slope
96             enc_offset = encoding_offset - math.log10(1 + nz) * enc_gain
97
98         a = 1 / gray
99         b = nz - black_signal / gray
100         e = slope * a * enc_gain
101         f = enc_gain * (slope * b + offset) + enc_offset
102
103         # Ensuring we can return relative exposure.
104         s = 4 / (0.18 * EI)
105         t = black_signal
106         b += a * t
107         a *= s
108         f += e * t
109         e *= s
110
111         return {'a': a,
112                 'b': b,
113                 'cut': (cut - b) / a,
114                 'c': enc_gain,
115                 'd': enc_offset,
116                 'e': e,
117                 'f': f}
118
119     def normalized_log_c_to_linear(code_value, exposure_index):
120         p = log_c_inverse_parameters_for_EI(exposure_index)
121         breakpoint = p['e'] * p['cut'] + p['f']
122         if code_value > breakpoint:
123             linear = ((pow(10, (code_value - p['d']) / p['c']) -
124                        p['b']) / p['a'])
125         else:
126             linear = (code_value - p['f']) / p['e']
127         return linear
128
129     cs.to_reference_transforms = []
130
131     if transfer_function == 'V3 LogC':
132         data = array.array('f', '\0' * lut_resolution_1d * 4)
133         for c in range(lut_resolution_1d):
134             data[c] = normalized_log_c_to_linear(c / (lut_resolution_1d - 1),
135                                                  int(exposure_index))
136
137         lut = '%s_to_linear.spi1d' % (
138             '%s_%s' % (transfer_function, exposure_index))
139
140         lut = sanitize(lut)
141
142         genlut.write_SPI_1d(
143             os.path.join(lut_directory, lut),
144             0,
145             1,
146             data,
147             lut_resolution_1d,
148             1)
149
150         cs.to_reference_transforms.append({
151             'type': 'lutFile',
152             'path': lut,
153             'interpolation': 'linear',
154             'direction': 'forward'
155         })
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
166     cs.from_reference_transforms = []
167     return cs
168
169
170 def create_colorspaces(lut_directory, lut_resolution_1d):
171     """
172     Generates the colorspace conversions.
173
174     Parameters
175     ----------
176     parameter : type
177         Parameter description.
178
179     Returns
180     -------
181     type
182          Return value description.
183     """
184
185     colorspaces = []
186
187     transfer_function = 'V3 LogC'
188     gamut = 'Wide Gamut'
189
190     # EIs = [160, 200, 250, 320, 400, 500, 640, 800,
191     # 1000, 1280, 1600, 2000, 2560, 3200]
192     EIs = [160, 200, 250, 320, 400, 500, 640, 800,
193            1000, 1280, 1600, 2000, 2560, 3200]
194     default_EI = 800
195
196     # Full Conversion
197     for EI in EIs:
198         log_c_EI_full = create_log_c(
199             gamut,
200             transfer_function,
201             EI,
202             'LogC',
203             lut_directory,
204             lut_resolution_1d,
205             ["%sei%s_%s" % ("logc3", str(EI), "arriwide")])
206         colorspaces.append(log_c_EI_full)
207
208     # Linearization Only
209     for EI in [800]:
210         log_c_EI_linearization = create_log_c(
211             '',
212             transfer_function,
213             EI,
214             'LogC',
215             lut_directory,
216             lut_resolution_1d,
217             ["crv_%sei%s" % ("logc3", str(EI))])
218         colorspaces.append(log_c_EI_linearization)
219
220     # Primaries Only
221     log_c_EI_primaries = create_log_c(
222         gamut,
223         '',
224         default_EI,
225         'LogC',
226         lut_directory,
227         lut_resolution_1d,
228         ["%s_%s" % ('lin', "arriwide")])
229     colorspaces.append(log_c_EI_primaries)
230
231     return colorspaces