2 # -*- coding: utf-8 -*-
5 Implements support for general colorspaces conversions and transfer functions.
8 from __future__ import division
13 import PyOpenColorIO as ocio
15 import aces_ocio.generate_lut as genlut
16 from aces_ocio.colorspaces import aces
17 from aces_ocio.utilities import ColorSpace, mat44_from_mat33
20 __author__ = 'ACES Developers'
21 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
23 __maintainer__ = 'ACES Developers'
24 __email__ = 'aces@oscars.org'
25 __status__ = 'Production'
27 __all__ = ['create_matrix_colorspace',
30 # -------------------------------------------------------------------------
32 # -------------------------------------------------------------------------
33 def create_matrix_colorspace(name='matrix',
34 from_reference_values=None,
35 to_reference_values=None,
43 Parameter description.
48 Return value description.
51 if from_reference_values is None:
52 from_reference_values = []
54 if to_reference_values is None:
55 to_reference_values = []
58 cs.description = 'The %s color space' % name
60 cs.equality_group = name
64 # A linear space needs allocation variables
65 cs.allocation_type = ocio.Constants.ALLOCATION_UNIFORM
66 cs.allocation_vars = [0, 1]
68 cs.to_reference_transforms = []
69 if to_reference_values:
70 for matrix in to_reference_values:
71 cs.to_reference_transforms.append({
73 'matrix': mat44_from_mat33(matrix),
74 'direction': 'forward'})
76 cs.from_reference_transforms = []
77 if from_reference_values:
78 for matrix in from_reference_values:
79 cs.from_reference_transforms.append({
81 'matrix': mat44_from_mat33(matrix),
82 'direction': 'forward'})
86 # -------------------------------------------------------------------------
87 # *Transfer Function Transform*
88 # -------------------------------------------------------------------------
89 def create_transfer_colorspace(name='transfer',
90 transfer_function_name='transfer_function',
91 transfer_function=lambda x: x,
93 lut_resolution_1d=1024,
101 Parameter description.
106 Return value description.
109 cs = ColorSpace(name)
110 cs.description = 'The %s color space' % name
112 cs.equality_group = name
113 cs.family = 'Utility'
116 # A linear space needs allocation variables
117 cs.allocation_type = ocio.Constants.ALLOCATION_UNIFORM
118 cs.allocation_vars = [0, 1]
120 # Sample the transfer function
121 data = array.array('f', '\0' * lut_resolution_1d * 4)
122 for c in range(lut_resolution_1d):
123 data[c] = transfer_function(c / (lut_resolution_1d - 1))
125 # Write the sampled data to a LUT
126 lut = '%s_to_linear.spi1d' % transfer_function_name
128 os.path.join(lut_directory, lut),
135 # Create the 'to_reference' transforms
136 cs.to_reference_transforms = []
137 cs.to_reference_transforms.append({
140 'interpolation': 'linear',
141 'direction': 'forward'})
143 # Create the 'from_reference' transforms
144 cs.from_reference_transforms = []
147 # create_transfer_colorspace
149 # -------------------------------------------------------------------------
150 # *Transfer Function + Matrix Transform*
151 # -------------------------------------------------------------------------
152 def create_matrix_plus_transfer_colorspace(name='matrix_plus_transfer',
153 transfer_function_name='transfer_function',
154 transfer_function=lambda x: x,
155 lut_directory='/tmp',
156 lut_resolution_1d=1024,
157 from_reference_values=None,
158 to_reference_values=None,
166 Parameter description.
171 Return value description.
174 if from_reference_values is None:
175 from_reference_values = []
177 if to_reference_values is None:
178 to_reference_values = []
180 cs = ColorSpace(name)
181 cs.description = 'The %s color space' % name
183 cs.equality_group = name
184 cs.family = 'Utility'
187 # A linear space needs allocation variables
188 cs.allocation_type = ocio.Constants.ALLOCATION_UNIFORM
189 cs.allocation_vars = [0, 1]
191 # Sample the transfer function
192 data = array.array('f', '\0' * lut_resolution_1d * 4)
193 for c in range(lut_resolution_1d):
194 data[c] = transfer_function(c / (lut_resolution_1d - 1))
196 # Write the sampled data to a LUT
197 lut = '%s_to_linear.spi1d' % transfer_function_name
199 os.path.join(lut_directory, lut),
206 # Create the 'to_reference' transforms
207 cs.to_reference_transforms = []
208 if to_reference_values:
209 cs.to_reference_transforms.append({
212 'interpolation': 'linear',
213 'direction': 'forward'})
215 for matrix in to_reference_values:
216 cs.to_reference_transforms.append({
218 'matrix': mat44_from_mat33(matrix),
219 'direction': 'forward'})
221 # Create the 'from_reference' transforms
222 cs.from_reference_transforms = []
223 if from_reference_values:
224 for matrix in from_reference_values:
225 cs.from_reference_transforms.append({
227 'matrix': mat44_from_mat33(matrix),
228 'direction': 'forward'})
230 cs.from_reference_transforms.append({
233 'interpolation': 'linear',
234 'direction': 'inverse'})
237 # create_matrix_plus_transfer_colorspace
239 # Transfer functions for standard color spaces
240 def transfer_function_sRGB_to_linear(v):
248 return pow(((v + (a - 1)) / a), g)
250 def transfer_function_Rec709_to_linear(v):
259 return pow(((v + (a - 1)) / a), g)
261 def transfer_function_Rec2020_10bit_to_linear(v):
270 return pow(((v + (a - 1)) / a), g)
272 def transfer_function_Rec2020_12bit_to_linear(v):
281 return pow(((v + (a - 1)) / a), g)
283 def transfer_function_Rec1886_to_linear(v):
288 # Ignoring legal to full scaling for now
289 #v = (1023.0*v - 64.0)/876.0
291 t = pow(Lw, 1.0/g) - pow(Lb, 1.0/g)
295 return a*pow(max((v + b), 0.0), g)
297 def create_colorspaces(lut_directory,
301 Generates the colorspace conversions.
306 Parameter description.
311 Return value description.
319 cs = create_matrix_colorspace('XYZ-D60',
320 to_reference_values=[aces.ACES_XYZ_TO_AP0],
321 from_reference_values=[aces.ACES_AP0_TO_XYZ],
322 aliases=["lin_xyz_d60"])
323 colorspaces.append(cs)
328 # *ACES* to *Linear*, *P3D60* primaries.
329 XYZ_to_P3D60 = [2.4027414142, -0.8974841639, -0.3880533700,
330 -0.8325796487, 1.7692317536, 0.0237127115,
331 0.0388233815, -0.0824996856, 1.0363685997]
333 cs = create_matrix_colorspace(
335 from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_P3D60],
336 aliases=["lin_p3d60"])
337 colorspaces.append(cs)
342 # *ACES* to *Linear*, *P3DCI* primaries.
343 XYZ_to_P3DCI = [2.7253940305, -1.0180030062, -0.4401631952,
344 -0.7951680258, 1.6897320548, 0.0226471906,
345 0.0412418914, -0.0876390192, 1.1009293786]
347 cs = create_matrix_colorspace(
349 from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_P3DCI],
350 aliases=["lin_p3dci"])
351 colorspaces.append(cs)
356 # *ACES* to *Linear*, *Rec. 709* primaries.
357 XYZ_to_Rec709 = [3.2409699419, -1.5373831776, -0.4986107603,
358 -0.9692436363, 1.8759675015, 0.0415550574,
359 0.0556300797, -0.2039769589, 1.0569715142]
361 cs = create_matrix_colorspace(
363 from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
364 aliases=["lin_rec709"])
365 colorspaces.append(cs)
367 # *Linear* to *Rec. 709* Transfer Function*
368 cs = create_transfer_colorspace(
371 transfer_function_Rec709_to_linear,
374 aliases=["crv_rec709"])
375 colorspaces.append(cs)
377 # *ACES* to *Rec. 709* Primaries + Transfer Function*
378 cs = create_matrix_plus_transfer_colorspace(
381 transfer_function_Rec709_to_linear,
384 from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
386 colorspaces.append(cs)
391 # *ACES* to *Linear*, *Rec. 709* primaries.
392 # sRGB and Rec 709 use the same gamut
393 XYZ_to_Rec709 = [3.2409699419, -1.5373831776, -0.4986107603,
394 -0.9692436363, 1.8759675015, 0.0415550574,
395 0.0556300797, -0.2039769589, 1.0569715142]
397 cs = create_matrix_colorspace(
399 from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
400 aliases=["lin_srgb"])
401 colorspaces.append(cs)
403 # *Linear* to *sRGB* Transfer Function*
404 cs = create_transfer_colorspace(
407 transfer_function_sRGB_to_linear,
410 aliases=["crv_srgb"])
411 colorspaces.append(cs)
413 # *ACES* to *sRGB* Primaries + Transfer Function*
414 cs = create_matrix_plus_transfer_colorspace(
417 transfer_function_sRGB_to_linear,
420 from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
422 colorspaces.append(cs)
428 # *Linear* to *Rec.2020* Transfer Function*
429 cs = create_transfer_colorspace(
432 transfer_function_Rec1886_to_linear,
435 aliases=["crv_rec1886"])
436 colorspaces.append(cs)
441 # *ACES* to *Linear*, *Rec. 2020* primaries.
442 XYZ_to_Rec2020 = [1.7166511880, -0.3556707838, -0.2533662814,
443 -0.6666843518, 1.6164812366, 0.0157685458,
444 0.0176398574, -0.0427706133, 0.9421031212]
446 cs = create_matrix_colorspace(
448 from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec2020],
449 aliases=["lin_rec2020"])
450 colorspaces.append(cs)
452 # *Linear* to *Rec. 2020 10 bit* Transfer Function*
453 cs = create_transfer_colorspace(
456 transfer_function_Rec2020_10bit_to_linear,
459 aliases=["crv_rec2020"])
460 colorspaces.append(cs)
462 # *ACES* to *Rec. 2020 10 bit* Primaries + Transfer Function*
463 cs = create_matrix_plus_transfer_colorspace(
466 transfer_function_Rec2020_10bit_to_linear,
469 from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec2020],
471 colorspaces.append(cs)
476 # *ACES* to *Linear*, *Pro Photo* primaries.
477 AP0_to_RIMM = [1.2412367771, -0.1685692287, -0.0726675484,
478 0.0061203066, 1.083151174, -0.0892714806,
479 -0.0032853314, 0.0099796402, 0.9933056912]
481 cs = create_matrix_colorspace(
482 'Linear - RIMM ROMM (ProPhoto)',
483 from_reference_values=[AP0_to_RIMM],
484 aliases=["lin_prophoto", "lin_rimm"])
485 colorspaces.append(cs)
490 # *ACES* to *Linear*, *Adobe RGB* primaries.
491 AP0_to_ADOBERGB = [1.7245603168, -0.4199935942, -0.3045667227,
492 -0.2764799142, 1.3727190877, -0.0962391734,
493 -0.0261255258, -0.0901747807, 1.1163003065]
495 cs = create_matrix_colorspace(
496 'Linear - Adobe RGB',
497 from_reference_values=[AP0_to_ADOBERGB],
498 aliases=["lin_adobergb"])
499 colorspaces.append(cs)
502 # Adobe Wide Gamut RGB
505 # *ACES* to *Linear*, *Adobe Wide Gamut RGB* primaries.
506 AP0_to_ADOBERGB = [1.3809814778, -0.1158594573, -0.2651220205,
507 0.0057015535, 1.0402949043, -0.0459964578,
508 -0.0038908746, -0.0597091815, 1.0636000561]
510 cs = create_matrix_colorspace(
511 'Linear - Adobe Wide Gamut RGB',
512 from_reference_values=[AP0_to_ADOBERGB],
513 aliases=["lin_adobewidegamutrgb"])
514 colorspaces.append(cs)
520 # *Raw* utility space
522 raw = ColorSpace(name)
523 raw.description = 'The %s color space' % name
525 raw.equality_group = name
526 raw.family = 'Utility'