+# -------------------------------------------------------------------------
+# *Matrix Transform*
+# -------------------------------------------------------------------------
+def create_matrix_plus_transfer_colorspace(name='matrix_plus_transfer',
+ transfer_function_name='transfer_function',
+ transfer_function=lambda x: x,
+ lut_directory='/tmp',
+ lut_resolution_1d=1024,
+ from_reference_values=None,
+ to_reference_values=None,
+ aliases=[]):
+ """
+ Object description.
+
+ Parameters
+ ----------
+ parameter : type
+ Parameter description.
+
+ Returns
+ -------
+ type
+ Return value description.
+ """
+
+ if from_reference_values is None:
+ from_reference_values = []
+
+ if to_reference_values is None:
+ to_reference_values = []
+
+ cs = ColorSpace(name)
+ cs.description = 'The %s color space' % name
+ cs.aliases = aliases
+ cs.equality_group = name
+ cs.family = 'Utility'
+ cs.is_data = False
+
+ # A linear space needs allocation variables
+ cs.allocation_type = ocio.Constants.ALLOCATION_LG2
+ cs.allocation_vars = [-8, 5, 0.00390625]
+
+ # Sample the transfer function
+ data = array.array('f', '\0' * lut_resolution_1d * 4)
+ for c in range(lut_resolution_1d):
+ data[c] = transfer_function(c / (lut_resolution_1d - 1))
+
+ # Write the sampled data to a LUT
+ lut = '%s_to_linear.spi1d' % transfer_function_name
+ genlut.write_SPI_1d(
+ os.path.join(lut_directory, lut),
+ 0,
+ 1,
+ data,
+ lut_resolution_1d,
+ 1)
+
+ # Create the 'to_reference' transforms
+ cs.to_reference_transforms = []
+ cs.to_reference_transforms.append({
+ 'type': 'lutFile',
+ 'path': lut,
+ 'interpolation': 'linear',
+ 'direction': 'forward'})
+
+ if to_reference_values:
+ for matrix in to_reference_values:
+ cs.to_reference_transforms.append({
+ 'type': 'matrix',
+ 'matrix': mat44_from_mat33(matrix),
+ 'direction': 'forward'})
+
+ # Create the 'from_reference' transforms
+ cs.from_reference_transforms = []
+ if from_reference_values:
+ for matrix in from_reference_values:
+ cs.from_reference_transforms.append({
+ 'type': 'matrix',
+ 'matrix': mat44_from_mat33(matrix),
+ 'direction': 'forward'})
+
+ cs.from_reference_transforms.append({
+ 'type': 'lutFile',
+ 'path': lut,
+ 'interpolation': 'linear',
+ 'direction': 'inverse'})
+
+ return cs
+
+# Transfer functions for standard color spaces
+def transfer_function_sRGB_to_linear(v):
+ a = 1.055
+ b = 0.04045
+ d = 12.92
+ g = 2.4
+
+ if v < b:
+ return v/d
+ return pow(((v + (a - 1)) / a), g)
+
+def transfer_function_Rec709_to_linear(v):
+ a = 1.099
+ b = 0.018
+ d = 4.5
+ g = (1.0/0.45)
+
+ if v < b:
+ return v/d
+
+ return pow(((v + (a - 1)) / a), g)
+
+def transfer_function_Rec2020_10bit_to_linear(v):
+ a = 1.099
+ b = 0.018
+ d = 4.5
+ g = (1.0/0.45)
+
+ if v < b:
+ return v/d
+
+ return pow(((v + (a - 1)) / a), g)
+
+def transfer_function_Rec2020_12bit_to_linear(v):
+ a = 1.0993
+ b = 0.0181
+ d = 4.5
+ g = (1.0/0.45)
+
+ if v < b:
+ return v/d
+
+ return pow(((v + (a - 1)) / a), g)
+
+def transfer_function_Rec1886_to_linear(v):
+ g = 2.4
+ Lw = 1
+ Lb = 0
+
+ # Ignoring legal to full scaling for now
+ #v = (1023.0*v - 64.0)/876.0
+
+ t = pow(Lw, 1.0/g) - pow(Lb, 1.0/g)
+ a = pow(t, g)
+ b = pow(Lb, 1.0/g)/t
+
+ return a*pow(max((v + b), 0.0), g)