2 # -*- coding: utf-8 -*-
5 Implements support for *ARRI* colorspaces conversions and transfer functions.
8 from __future__ import division
14 import PyOpenColorIO as ocio
16 import aces_ocio.generate_lut as genlut
17 from aces_ocio.utilities import ColorSpace, mat44_from_mat33, sanitize
19 __author__ = 'ACES Developers'
20 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
22 __maintainer__ = 'ACES Developers'
23 __email__ = 'aces@oscars.org'
24 __status__ = 'Production'
26 __all__ = ['create_log_c',
30 def create_log_c(gamut,
37 Creates colorspace covering the conversion from LogC to ACES, with various transfer
38 functions and encoding gamuts covered
43 The name of the encoding gamut to use.
44 transfer_function : str
45 The name of the transfer function to use
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
53 Aliases for this colorspace
58 A ColorSpace container class referencing the LUTs, matrices and identifying
59 information for the requested colorspace.
62 name = '%s (EI%s) - %s' % (transfer_function, exposure_index, gamut)
63 if transfer_function == '':
64 name = 'Linear - ARRI %s' % gamut
66 name = 'Curve - %s (EI%s)' % (transfer_function, exposure_index)
71 cs.equality_group = ''
72 cs.family = 'Input/ARRI'
75 if gamut and transfer_function:
76 cs.aces_transform_id = (
77 'IDT.ARRI.Alexa-v3-logC-EI%s.a1.v1' % exposure_index)
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]
84 IDT_maker_version = '0.08'
87 black_signal = 0.003907
88 mid_gray_signal = 0.01
89 encoding_gain = 0.256598
90 encoding_offset = 0.391007
93 return (math.log(EI / nominal_EI) / math.log(2) * (
94 0.89 - 1) / 3 + 1) * encoding_gain
96 def log_c_inverse_parameters_for_EI(EI):
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
110 b = nz - black_signal / gray
111 e = slope * a * enc_gain
112 f = enc_gain * (slope * b + offset) + enc_offset
114 # Ensuring we can return relative exposure.
124 'cut': (cut - b) / a,
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']) -
137 linear = (code_value - p['f']) / p['e']
140 cs.to_reference_transforms = []
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),
148 lut = '%s_to_linear.spi1d' % (
149 '%s_%s' % (transfer_function, exposure_index))
154 os.path.join(lut_directory, lut),
161 cs.to_reference_transforms.append({
164 'interpolation': 'linear',
165 'direction': 'forward'})
167 if gamut == 'Wide Gamut':
168 cs.to_reference_transforms.append({
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'})
175 cs.from_reference_transforms = []
179 def create_colorspaces(lut_directory, lut_resolution_1d):
181 Generates the colorspace conversions.
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
193 A list of colorspaces for ARRI cameras and encodings
198 transfer_function = 'V3 LogC'
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]
209 log_c_EI_full = create_log_c(
215 ['%sei%s_%s' % ('logc3', str(EI), 'arriwide')])
216 colorspaces.append(log_c_EI_full)
220 log_c_EI_linearization = create_log_c(
226 ['crv_%sei%s' % ('logc3', str(EI))])
227 colorspaces.append(log_c_EI_linearization)
230 log_c_EI_primaries = create_log_c(
236 ['%s_%s' % ('lin', 'arriwide')])
237 colorspaces.append(log_c_EI_primaries)