f1e0bf241b16b8411dbe6a62a318126712c52cdd
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / colorspaces / aces.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 Implements support for *ACES* colorspaces conversions and transfer functions.
6 """
7
8 from __future__ import division
9
10 import math
11 import numpy
12 import os
13 import pprint
14 import string
15 import shutil
16
17 import PyOpenColorIO as ocio
18
19 from aces_ocio.generate_lut import (
20     generate_1d_LUT_from_CTL,
21     generate_3d_LUT_from_CTL,
22     write_SPI_1d)
23 from aces_ocio.utilities import (
24     ColorSpace,
25     mat44_from_mat33,
26     sanitize,
27     compact)
28
29 __author__ = 'ACES Developers'
30 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
31 __license__ = ''
32 __maintainer__ = 'ACES Developers'
33 __email__ = 'aces@oscars.org'
34 __status__ = 'Production'
35
36 __all__ = ['ACES_AP1_TO_AP0',
37            'ACES_AP0_TO_AP1',
38            'ACES_AP0_TO_XYZ',
39            'ACES_XYZ_TO_AP0',
40            'create_ACES',
41            'create_ACEScc',
42            'create_ACESproxy',
43            'create_ACEScg',
44            'create_ADX',
45            'create_generic_log',
46            'create_Dolby_PQ',
47            'create_Dolby_PQ_scaled',
48            'create_ACES_LMT',
49            'create_LMTs',
50            'create_ACES_RRT_plus_ODT',
51            'create_ODTs',
52            'get_transform_info',
53            'get_ODTs_info',
54            'get_LMTs_info',
55            'create_colorspaces']
56
57 # Matrix converting *ACES AP1* primaries to *ACES AP0*.
58 ACES_AP1_TO_AP0 = [0.6954522414, 0.1406786965, 0.1638690622,
59                    0.0447945634, 0.8596711185, 0.0955343182,
60                    -0.0055258826, 0.0040252103, 1.0015006723]
61
62 # Matrix converting *ACES AP0* primaries to *ACES AP1*.
63 ACES_AP0_TO_AP1 = [1.4514393161, -0.2365107469, -0.2149285693,
64                    -0.0765537734, 1.1762296998, -0.0996759264,
65                    0.0083161484, -0.0060324498, 0.9977163014]
66
67 # Matrix converting *ACES AP0* primaries to *XYZ*.
68 ACES_AP0_TO_XYZ = [0.9525523959, 0.0000000000, 0.0000936786,
69                    0.3439664498, 0.7281660966, -0.0721325464,
70                    0.0000000000, 0.0000000000, 1.0088251844]
71
72 # Matrix converting *ACES AP0* primaries to *XYZ*.
73 ACES_XYZ_TO_AP0 = [1.0498110175, 0.0000000000, -0.0000974845,
74                    -0.4959030231, 1.3733130458, 0.0982400361,
75                    0.0000000000, 0.0000000000, 0.9912520182]
76
77
78 def create_ACES():
79     """
80     Object description.
81
82     Parameters
83     ----------
84     parameter : type
85         Parameter description.
86
87     Returns
88     -------
89     type
90          Return value description.
91     """
92
93     # Defining the reference colorspace.
94     aces2065_1 = ColorSpace('ACES2065-1')
95     aces2065_1.description = (
96         'The Academy Color Encoding System reference color space')
97     aces2065_1.equality_group = ''
98     aces2065_1.aliases = ['lin_ap0', 'aces']
99     aces2065_1.family = 'ACES'
100     aces2065_1.is_data = False
101     aces2065_1.allocation_type = ocio.Constants.ALLOCATION_LG2
102     aces2065_1.allocation_vars = [-8, 5, 0.00390625]
103
104     return aces2065_1
105
106
107 def create_ACEScc(aces_ctl_directory,
108                   lut_directory,
109                   lut_resolution_1d,
110                   cleanup,
111                   name='ACEScc',
112                   min_value=0,
113                   max_value=1,
114                   input_scale=1):
115     """
116     Creates the *ACEScc* colorspace.
117
118     Parameters
119     ----------
120     parameter : type
121         Parameter description.
122
123     Returns
124     -------
125     Colorspace
126          *ACEScc* colorspace.
127     """
128
129     cs = ColorSpace(name)
130     cs.description = 'The %s color space' % name
131     cs.aliases = ['acescc', 'acescc_ap1']
132     cs.equality_group = ''
133     cs.family = 'ACES'
134     cs.is_data = False
135     cs.allocation_type = ocio.Constants.ALLOCATION_UNIFORM
136     cs.allocation_vars = [min_value, max_value]
137     cs.aces_transform_id = 'ACEScsc.ACEScc_to_ACES.a1.0.0'
138
139     ctls = [os.path.join(aces_ctl_directory,
140                          'ACEScc',
141                          'ACEScsc.ACEScc_to_ACES.a1.0.0.ctl'),
142             # This transform gets back to the *AP1* primaries.
143             # Useful as the 1d LUT is only covering the transfer function.
144             # The primaries switch is covered by the matrix below:
145             os.path.join(aces_ctl_directory,
146                          'ACEScg',
147                          'ACEScsc.ACES_to_ACEScg.a1.0.0.ctl')]
148     lut = '%s_to_linear.spi1d' % name
149
150     lut = sanitize(lut)
151
152     generate_1d_LUT_from_CTL(
153         os.path.join(lut_directory, lut),
154         ctls,
155         lut_resolution_1d,
156         'float',
157         input_scale,
158         1,
159         {},
160         cleanup,
161         aces_ctl_directory,
162         min_value,
163         max_value,
164         1)
165
166     cs.to_reference_transforms = []
167     cs.to_reference_transforms.append({
168         'type': 'lutFile',
169         'path': lut,
170         'interpolation': 'linear',
171         'direction': 'forward'})
172
173     # *AP1* primaries to *AP0* primaries
174     cs.to_reference_transforms.append({
175         'type': 'matrix',
176         'matrix': mat44_from_mat33(ACES_AP1_TO_AP0),
177         'direction': 'forward'})
178
179     cs.from_reference_transforms = []
180     return cs
181
182
183 def create_ACESproxy(aces_ctl_directory,
184                      lut_directory,
185                      lut_resolution_1d,
186                      cleanup,
187                      name='ACESproxy'):
188     """
189     Creates the *ACESproxy* colorspace.
190
191     Parameters
192     ----------
193     parameter : type
194         Parameter description.
195
196     Returns
197     -------
198     Colorspace
199          *ACESproxy* colorspace.
200     """
201
202     cs = ColorSpace(name)
203     cs.description = 'The %s color space' % name
204     cs.aliases = ['acesproxy', 'acesproxy_ap1']
205     cs.equality_group = ''
206     cs.family = 'ACES'
207     cs.is_data = False
208
209     cs.aces_transform_id = 'ACEScsc.ACESproxy10i_to_ACES.a1.0.0'
210
211     ctls = [os.path.join(aces_ctl_directory,
212                          'ACESproxy',
213                          'ACEScsc.ACESproxy10i_to_ACES.a1.0.0.ctl'),
214             # This transform gets back to the *AP1* primaries.
215             # Useful as the 1d LUT is only covering the transfer function.
216             # The primaries switch is covered by the matrix below:
217             os.path.join(aces_ctl_directory,
218                          'ACEScg',
219                          'ACEScsc.ACES_to_ACEScg.a1.0.0.ctl')]
220     lut = '%s_to_linear.spi1d' % name
221
222     lut = sanitize(lut)
223
224     generate_1d_LUT_from_CTL(
225         os.path.join(lut_directory, lut),
226         ctls,
227         lut_resolution_1d,
228         'float',
229         1,
230         1,
231         {},
232         cleanup,
233         aces_ctl_directory,
234         0,
235         1,
236         1)
237
238     cs.to_reference_transforms = []
239     cs.to_reference_transforms.append({
240         'type': 'lutFile',
241         'path': lut,
242         'interpolation': 'linear',
243         'direction': 'forward'})
244
245     # *AP1* primaries to *AP0* primaries
246     cs.to_reference_transforms.append({
247         'type': 'matrix',
248         'matrix': mat44_from_mat33(ACES_AP1_TO_AP0),
249         'direction': 'forward'})
250
251     cs.from_reference_transforms = []
252     return cs
253
254
255 # -------------------------------------------------------------------------
256 # *ACEScg*
257 # -------------------------------------------------------------------------
258 def create_ACEScg():
259     """
260     Creates the *ACEScg* colorspace.
261
262     Parameters
263     ----------
264     parameter : type
265         Parameter description.
266
267     Returns
268     -------
269     Colorspace
270          *ACEScg* colorspace.
271     """
272
273     name = 'ACEScg'
274
275     cs = ColorSpace(name)
276     cs.description = 'The %s color space' % name
277     cs.aliases = ['acescg', 'lin_ap1']
278     cs.equality_group = ''
279     cs.family = 'ACES'
280     cs.is_data = False
281     cs.allocation_type = ocio.Constants.ALLOCATION_LG2
282     cs.allocation_vars = [-8, 5, 0.00390625]
283
284     cs.aces_transform_id = 'ACEScsc.ACEScg_to_ACES.a1.0.0'
285
286     cs.to_reference_transforms = []
287
288     # *AP1* primaries to *AP0* primaries
289     cs.to_reference_transforms.append({
290         'type': 'matrix',
291         'matrix': mat44_from_mat33(ACES_AP1_TO_AP0),
292         'direction': 'forward'})
293
294     cs.from_reference_transforms = []
295
296     # *AP1* primaries to *AP0* primaries
297     cs.from_reference_transforms.append({
298         'type': 'matrix',
299         'matrix': mat44_from_mat33(ACES_AP0_TO_AP1),
300         'direction': 'forward'})
301
302     return cs
303
304
305 # -------------------------------------------------------------------------
306 # *ADX*
307 # -------------------------------------------------------------------------
308 def create_ADX(lut_directory,
309                bit_depth=10,
310                name='ADX'):
311     """
312     Creates the *ADX* colorspace.
313
314     Parameters
315     ----------
316     parameter : type
317         Parameter description.
318
319     Returns
320     -------
321     Colorspace
322          *ADX* colorspace.
323     """
324
325     name = '%s%s' % (name, bit_depth)
326     cs = ColorSpace(name)
327     cs.description = '%s color space - used for film scans' % name
328     cs.aliases = ['adx%s' % str(bit_depth)]
329     cs.equality_group = ''
330     cs.family = 'ADX'
331     cs.is_data = False
332
333     if bit_depth == 10:
334         cs.aces_transform_id = 'ACEScsc.ADX10_to_ACES.a1.0.0'
335
336         cs.bit_depth = ocio.Constants.BIT_DEPTH_UINT10
337         ADX_to_CDD = [1023 / 500, 0, 0, 0,
338                       0, 1023 / 500, 0, 0,
339                       0, 0, 1023 / 500, 0,
340                       0, 0, 0, 1]
341         offset = [-95 / 500, -95 / 500, -95 / 500, 0]
342     elif bit_depth == 16:
343         cs.aces_transform_id = 'ACEScsc.ADX16_to_ACES.a1.0.0'
344
345         cs.bit_depth = ocio.Constants.BIT_DEPTH_UINT16
346         ADX_to_CDD = [65535 / 8000, 0, 0, 0,
347                       0, 65535 / 8000, 0, 0,
348                       0, 0, 65535 / 8000, 0,
349                       0, 0, 0, 1]
350         offset = [-1520 / 8000, -1520 / 8000, -1520 / 8000, 0]
351
352     cs.to_reference_transforms = []
353
354     # Converting from *ADX* to *Channel-Dependent Density*.
355     cs.to_reference_transforms.append({
356         'type': 'matrix',
357         'matrix': ADX_to_CDD,
358         'offset': offset,
359         'direction': 'forward'})
360
361     # Converting from *Channel-Dependent Density* to
362     # *Channel-Independent Density*.
363     cs.to_reference_transforms.append({
364         'type': 'matrix',
365         'matrix': [0.75573, 0.22197, 0.02230, 0,
366                    0.05901, 0.96928, -0.02829, 0,
367                    0.16134, 0.07406, 0.76460, 0,
368                    0, 0, 0, 1],
369         'direction': 'forward'})
370
371     # Copied from *Alex Fry*'s *adx_cid_to_rle.py*
372     def create_CID_to_RLE_LUT():
373
374         def interpolate_1d(x, xp, fp):
375             return numpy.interp(x, xp, fp)
376
377         LUT_1D_XP = [-0.190000000000000,
378                      0.010000000000000,
379                      0.028000000000000,
380                      0.054000000000000,
381                      0.095000000000000,
382                      0.145000000000000,
383                      0.220000000000000,
384                      0.300000000000000,
385                      0.400000000000000,
386                      0.500000000000000,
387                      0.600000000000000]
388
389         LUT_1D_FP = [-6.000000000000000,
390                      -2.721718645000000,
391                      -2.521718645000000,
392                      -2.321718645000000,
393                      -2.121718645000000,
394                      -1.921718645000000,
395                      -1.721718645000000,
396                      -1.521718645000000,
397                      -1.321718645000000,
398                      -1.121718645000000,
399                      -0.926545676714876]
400
401         REF_PT = ((7120 - 1520) / 8000 * (100 / 55) -
402                   math.log(0.18, 10))
403
404         def cid_to_rle(x):
405             if x <= 0.6:
406                 return interpolate_1d(x, LUT_1D_XP, LUT_1D_FP)
407             return (100 / 55) * x - REF_PT
408
409         def fit(value, from_min, from_max, to_min, to_max):
410             if from_min == from_max:
411                 raise ValueError('from_min == from_max')
412             return (value - from_min) / (from_max - from_min) * (
413                 to_max - to_min) + to_min
414
415         num_samples = 2 ** 12
416         domain = (-0.19, 3)
417         data = []
418         for i in xrange(num_samples):
419             x = i / (num_samples - 1)
420             x = fit(x, 0, 1, domain[0], domain[1])
421             data.append(cid_to_rle(x))
422
423         lut = 'ADX_CID_to_RLE.spi1d'
424         write_SPI_1d(os.path.join(lut_directory, lut),
425                      domain[0],
426                      domain[1],
427                      data,
428                      num_samples, 1)
429
430         return lut
431
432     # Converting *Channel Independent Density* values to
433     # *Relative Log Exposure* values.
434     lut = create_CID_to_RLE_LUT()
435     cs.to_reference_transforms.append({
436         'type': 'lutFile',
437         'path': lut,
438         'interpolation': 'linear',
439         'direction': 'forward'})
440
441     # Converting *Relative Log Exposure* values to
442     # *Relative Exposure* values.
443     cs.to_reference_transforms.append({
444         'type': 'log',
445         'base': 10,
446         'direction': 'inverse'})
447
448     # Convert *Relative Exposure* values to *ACES* values.
449     cs.to_reference_transforms.append({
450         'type': 'matrix',
451         'matrix': [0.72286, 0.12630, 0.15084, 0,
452                    0.11923, 0.76418, 0.11659, 0,
453                    0.01427, 0.08213, 0.90359, 0,
454                    0, 0, 0, 1],
455         'direction': 'forward'})
456
457     cs.from_reference_transforms = []
458     return cs
459
460
461 # -------------------------------------------------------------------------
462 # Generic *Log* Transform
463 # -------------------------------------------------------------------------
464 def create_generic_log(aces_ctl_directory,
465                        lut_directory,
466                        lut_resolution_1d,
467                        cleanup,
468                        name='log',
469                        aliases=None,
470                        min_value=0,
471                        max_value=1,
472                        input_scale=1,
473                        middle_grey=0.18,
474                        min_exposure=-6,
475                        max_exposure=6.5):
476     """
477     Creates the *Generic Log* colorspace.
478
479     Parameters
480     ----------
481     parameter : type
482         Parameter description.
483
484     Returns
485     -------
486     Colorspace
487          *Generic Log* colorspace.
488     """
489
490     if aliases is None:
491         aliases = []
492
493     cs = ColorSpace(name)
494     cs.description = 'The %s color space' % name
495     cs.aliases = aliases
496     cs.equality_group = name
497     cs.family = 'Utility'
498     cs.is_data = False
499
500     ctls = [os.path.join(
501         aces_ctl_directory,
502         'utilities',
503         'ACESlib.Log2_to_Lin_param.a1.0.0.ctl')]
504     lut = '%s_to_linear.spi1d' % name
505
506     lut = sanitize(lut)
507
508     generate_1d_LUT_from_CTL(
509         os.path.join(lut_directory, lut),
510         ctls,
511         lut_resolution_1d,
512         'float',
513         input_scale,
514         1,
515         {'middleGrey': middle_grey,
516          'minExposure': min_exposure,
517          'maxExposure': max_exposure},
518         cleanup,
519         aces_ctl_directory,
520         min_value,
521         max_value,
522         1)
523
524     cs.to_reference_transforms = []
525     cs.to_reference_transforms.append({
526         'type': 'lutFile',
527         'path': lut,
528         'interpolation': 'linear',
529         'direction': 'forward'})
530
531     cs.from_reference_transforms = []
532     return cs
533
534
535 # -------------------------------------------------------------------------
536 # Base *Dolby PQ* Transform
537 # -------------------------------------------------------------------------
538 def create_Dolby_PQ(aces_ctl_directory,
539                     lut_directory,
540                     lut_resolution_1d,
541                     cleanup,
542                     name='pq',
543                     aliases=None,
544                     min_value=0.0,
545                     max_value=1.0,
546                     input_scale=1.0):
547     if aliases is None:
548         aliases = []
549
550     cs = ColorSpace(name)
551     cs.description = 'The %s color space' % name
552     cs.aliases = aliases
553     cs.equality_group = name
554     cs.family = 'Utility'
555     cs.is_data = False
556
557     ctls = [os.path.join(
558         aces_ctl_directory,
559         'utilities',
560         'ACESlib.DolbyPQ_to_Lin.a1.0.0.ctl')]
561     lut = '%s_to_linear.spi1d' % name
562
563     lut = sanitize(lut)
564
565     generate_1d_LUT_from_CTL(
566         os.path.join(lut_directory, lut),
567         ctls,
568         lut_resolution_1d,
569         'float',
570         input_scale,
571         1.0,
572         {},
573         cleanup,
574         aces_ctl_directory,
575         min_value,
576         max_value)
577
578     cs.to_reference_transforms = []
579     cs.to_reference_transforms.append({
580         'type': 'lutFile',
581         'path': lut,
582         'interpolation': 'linear',
583         'direction': 'forward'})
584
585     cs.from_reference_transforms = []
586     return cs
587
588
589 # -------------------------------------------------------------------------
590 # *Dolby PQ* Transform - Fixed Linear Range
591 # -------------------------------------------------------------------------
592 def create_Dolby_PQ_scaled(aces_ctl_directory,
593                            lut_directory,
594                            lut_resolution_1d,
595                            cleanup,
596                            name='pq',
597                            aliases=None,
598                            min_value=0.0,
599                            max_value=1.0,
600                            input_scale=1.0,
601                            middle_grey=0.18,
602                            min_exposure=-6.0,
603                            max_exposure=6.5):
604     if aliases is None:
605         aliases = []
606
607     cs = ColorSpace(name)
608     cs.description = 'The %s color space' % name
609     cs.aliases = aliases
610     cs.equality_group = name
611     cs.family = 'Utility'
612     cs.is_data = False
613
614     ctls = [os.path.join(
615         aces_ctl_directory,
616         'utilities',
617         'ACESlib.OCIOShaper_to_lin_param.a1.0.0.ctl')]
618     lut = '%s_to_linear.spi1d' % name
619
620     lut = sanitize(lut)
621
622     generate_1d_LUT_from_CTL(
623         os.path.join(lut_directory, lut),
624         ctls,
625         lut_resolution_1d,
626         'float',
627         input_scale,
628         1.0,
629         {'middleGrey': middle_grey,
630          'minExposure': min_exposure,
631          'maxExposure': max_exposure},
632         cleanup,
633         aces_ctl_directory,
634         min_value,
635         max_value)
636
637     cs.to_reference_transforms = []
638     cs.to_reference_transforms.append({
639         'type': 'lutFile',
640         'path': lut,
641         'interpolation': 'linear',
642         'direction': 'forward'})
643
644     cs.from_reference_transforms = []
645     return cs
646
647
648 # -------------------------------------------------------------------------
649 # Individual *LMT*
650 # -------------------------------------------------------------------------
651 def create_ACES_LMT(lmt_name,
652                     lmt_values,
653                     shaper_info,
654                     aces_ctl_directory,
655                     lut_directory,
656                     lut_resolution_3d=64,
657                     cleanup=True,
658                     aliases=None):
659     """
660     Creates the *ACES LMT* colorspace.
661
662     Parameters
663     ----------
664     parameter : type
665         Parameter description.
666
667     Returns
668     -------
669     Colorspace
670          *ACES LMT* colorspace.
671     """
672
673     if aliases is None:
674         aliases = []
675
676     cs = ColorSpace('%s' % lmt_name)
677     cs.description = 'The ACES Look Transform: %s' % lmt_name
678     cs.aliases = aliases
679     cs.equality_group = ''
680     cs.family = 'Look'
681     cs.is_data = False
682     cs.allocation_type = ocio.Constants.ALLOCATION_LG2
683     cs.allocation_vars = [-8, 5, 0.00390625]
684     cs.aces_transform_id = lmt_values['transformID']
685
686     pprint.pprint(lmt_values)
687
688     # Generating the *shaper* transform.
689     (shaper_name,
690      shaper_to_aces_ctl,
691      shaper_from_aces_ctl,
692      shaper_input_scale,
693      shaper_params) = shaper_info
694
695     shaper_lut = '%s_to_linear.spi1d' % shaper_name
696     shaper_lut = sanitize(shaper_lut)
697
698     shaper_ocio_transform = {
699         'type': 'lutFile',
700         'path': shaper_lut,
701         'interpolation': 'linear',
702         'direction': 'inverse'}
703
704     # Generating the forward transform.
705     cs.from_reference_transforms = []
706
707     if 'transformCTL' in lmt_values:
708         ctls = [shaper_to_aces_ctl % aces_ctl_directory,
709                 os.path.join(aces_ctl_directory,
710                              lmt_values['transformCTL'])]
711         lut = '%s.%s.spi3d' % (shaper_name, lmt_name)
712
713         lut = sanitize(lut)
714
715         generate_3d_LUT_from_CTL(
716             os.path.join(lut_directory, lut),
717             ctls,
718             lut_resolution_3d,
719             'float',
720             1 / shaper_input_scale,
721             1,
722             shaper_params,
723             cleanup,
724             aces_ctl_directory)
725
726         cs.from_reference_transforms.append(shaper_ocio_transform)
727         cs.from_reference_transforms.append({
728             'type': 'lutFile',
729             'path': lut,
730             'interpolation': 'tetrahedral',
731             'direction': 'forward'})
732
733     # Generating the inverse transform.
734     cs.to_reference_transforms = []
735
736     if 'transformCTLInverse' in lmt_values:
737         ctls = [os.path.join(aces_ctl_directory,
738                              lmt_values['transformCTLInverse']),
739                 shaper_from_aces_ctl % aces_ctl_directory]
740         lut = 'Inverse.%s.%s.spi3d' % (lmt_name, shaper_name)
741
742         lut = sanitize(lut)
743
744         generate_3d_LUT_from_CTL(
745             os.path.join(lut_directory, lut),
746             ctls,
747             lut_resolution_3d,
748             'half',
749             1,
750             shaper_input_scale,
751             shaper_params,
752             cleanup,
753             aces_ctl_directory,
754             0)
755
756         cs.to_reference_transforms.append({
757             'type': 'lutFile',
758             'path': lut,
759             'interpolation': 'tetrahedral',
760             'direction': 'forward'})
761
762         shaper_inverse = shaper_ocio_transform.copy()
763         shaper_inverse['direction'] = 'forward'
764         cs.to_reference_transforms.append(shaper_inverse)
765
766     return cs
767
768
769 # -------------------------------------------------------------------------
770 # *LMTs*
771 # -------------------------------------------------------------------------
772 def create_LMTs(aces_ctl_directory,
773                 lut_directory,
774                 lut_resolution_1d,
775                 lut_resolution_3d,
776                 lmt_info,
777                 cleanup):
778     """
779     Object description.
780
781     Parameters
782     ----------
783     parameter : type
784         Parameter description.
785
786     Returns
787     -------
788     type
789          Return value description.
790     """
791
792     colorspaces = []
793
794     # -------------------------------------------------------------------------
795     # *LMT Shaper*
796     # -------------------------------------------------------------------------
797     lmt_lut_resolution_1d = max(4096, lut_resolution_1d)
798     lmt_lut_resolution_3d = max(65, lut_resolution_3d)
799
800     # Defining the *Log 2* shaper.
801     lmt_shaper_name = 'LMT Shaper'
802     lmt_shaper_name_aliases = ['crv_lmtshaper']
803     lmt_params = {
804         'middleGrey': 0.18,
805         'minExposure': -10,
806         'maxExposure': 6.5}
807
808     lmt_shaper = create_generic_log(aces_ctl_directory,
809                                     lut_directory,
810                                     lmt_lut_resolution_1d,
811                                     cleanup,
812                                     name=lmt_shaper_name,
813                                     middle_grey=lmt_params['middleGrey'],
814                                     min_exposure=lmt_params['minExposure'],
815                                     max_exposure=lmt_params['maxExposure'],
816                                     aliases=lmt_shaper_name_aliases)
817     colorspaces.append(lmt_shaper)
818
819     shaper_input_scale_generic_log2 = 1
820
821     # *Log 2* shaper name and *CTL* transforms bundled up.
822     lmt_shaper_data = [
823         lmt_shaper_name,
824         os.path.join('%s',
825                      'utilities',
826                      'ACESlib.Log2_to_Lin_param.a1.0.0.ctl'),
827         os.path.join('%s',
828                      'utilities',
829                      'ACESlib.Lin_to_Log2_param.a1.0.0.ctl'),
830         shaper_input_scale_generic_log2,
831         lmt_params]
832
833     sorted_lmts = sorted(lmt_info.iteritems(), key=lambda x: x[1])
834     print(sorted_lmts)
835     for lmt in sorted_lmts:
836         lmt_name, lmt_values = lmt
837         lmt_aliases = ['look_%s' % compact(lmt_values['transformUserName'])]
838         cs = create_ACES_LMT(
839             lmt_values['transformUserName'],
840             lmt_values,
841             lmt_shaper_data,
842             aces_ctl_directory,
843             lut_directory,
844             lmt_lut_resolution_3d,
845             cleanup,
846             lmt_aliases)
847         colorspaces.append(cs)
848
849     return colorspaces
850
851
852 # -------------------------------------------------------------------------
853 # *ACES RRT* with supplied *ODT*.
854 # -------------------------------------------------------------------------
855 def create_ACES_RRT_plus_ODT(odt_name,
856                              odt_values,
857                              shaper_info,
858                              aces_ctl_directory,
859                              lut_directory,
860                              lut_resolution_3d=64,
861                              cleanup=True,
862                              aliases=None):
863     """
864     Object description.
865
866     Parameters
867     ----------
868     parameter : type
869         Parameter description.
870
871     Returns
872     -------
873     type
874          Return value description.
875     """
876
877     if aliases is None:
878         aliases = []
879
880     cs = ColorSpace('%s' % odt_name)
881     cs.description = '%s - %s Output Transform' % (
882         odt_values['transformUserNamePrefix'], odt_name)
883     cs.aliases = aliases
884     cs.equality_group = ''
885     cs.family = 'Output'
886     cs.is_data = False
887
888     cs.aces_transform_id = odt_values['transformID']
889
890     pprint.pprint(odt_values)
891
892     # Generating the *shaper* transform.
893     (shaper_name,
894      shaper_to_aces_ctl,
895      shaper_from_aces_ctl,
896      shaper_input_scale,
897      shaper_params) = shaper_info
898
899     if 'legalRange' in odt_values:
900         shaper_params['legalRange'] = odt_values['legalRange']
901     else:
902         shaper_params['legalRange'] = 0
903
904     shaper_lut = '%s_to_linear.spi1d' % shaper_name
905     shaper_lut = sanitize(shaper_lut)
906
907     shaper_ocio_transform = {
908         'type': 'lutFile',
909         'path': shaper_lut,
910         'interpolation': 'linear',
911         'direction': 'inverse'}
912
913     # Generating the *forward* transform.
914     cs.from_reference_transforms = []
915
916     if 'transformLUT' in odt_values:
917         transform_lut_file_name = os.path.basename(
918             odt_values['transformLUT'])
919         lut = os.path.join(lut_directory, transform_lut_file_name)
920         shutil.copy(odt_values['transformLUT'], lut)
921
922         cs.from_reference_transforms.append(shaper_ocio_transform)
923         cs.from_reference_transforms.append({
924             'type': 'lutFile',
925             'path': transform_lut_file_name,
926             'interpolation': 'tetrahedral',
927             'direction': 'forward'})
928     elif 'transformCTL' in odt_values:
929         ctls = [
930             shaper_to_aces_ctl % aces_ctl_directory,
931             os.path.join(aces_ctl_directory,
932                          'rrt',
933                          'RRT.a1.0.0.ctl'),
934             os.path.join(aces_ctl_directory,
935                          'odt',
936                          odt_values['transformCTL'])]
937         lut = '%s.RRT.a1.0.0.%s.spi3d' % (shaper_name, odt_name)
938
939         lut = sanitize(lut)
940
941         generate_3d_LUT_from_CTL(
942             os.path.join(lut_directory, lut),
943             ctls,
944             lut_resolution_3d,
945             'float',
946             1 / shaper_input_scale,
947             1,
948             shaper_params,
949             cleanup,
950             aces_ctl_directory)
951
952         cs.from_reference_transforms.append(shaper_ocio_transform)
953         cs.from_reference_transforms.append({
954             'type': 'lutFile',
955             'path': lut,
956             'interpolation': 'tetrahedral',
957             'direction': 'forward'})
958
959     # Generating the *inverse* transform.
960     cs.to_reference_transforms = []
961
962     if 'transformLUTInverse' in odt_values:
963         transform_lut_inverse_file_name = os.path.basename(
964             odt_values['transformLUTInverse'])
965         lut = os.path.join(lut_directory, transform_lut_inverse_file_name)
966         shutil.copy(odt_values['transformLUTInverse'], lut)
967
968         cs.to_reference_transforms.append({
969             'type': 'lutFile',
970             'path': transform_lut_inverse_file_name,
971             'interpolation': 'tetrahedral',
972             'direction': 'forward'})
973
974         shaper_inverse = shaper_ocio_transform.copy()
975         shaper_inverse['direction'] = 'forward'
976         cs.to_reference_transforms.append(shaper_inverse)
977     elif 'transformCTLInverse' in odt_values:
978         ctls = [os.path.join(aces_ctl_directory,
979                              'odt',
980                              odt_values['transformCTLInverse']),
981                 os.path.join(aces_ctl_directory,
982                              'rrt',
983                              'InvRRT.a1.0.0.ctl'),
984                 shaper_from_aces_ctl % aces_ctl_directory]
985         lut = 'InvRRT.a1.0.0.%s.%s.spi3d' % (odt_name, shaper_name)
986
987         lut = sanitize(lut)
988
989         generate_3d_LUT_from_CTL(
990             os.path.join(lut_directory, lut),
991             ctls,
992             lut_resolution_3d,
993             'half',
994             1,
995             shaper_input_scale,
996             shaper_params,
997             cleanup,
998             aces_ctl_directory)
999
1000         cs.to_reference_transforms.append({
1001             'type': 'lutFile',
1002             'path': lut,
1003             'interpolation': 'tetrahedral',
1004             'direction': 'forward'})
1005
1006         shaper_inverse = shaper_ocio_transform.copy()
1007         shaper_inverse['direction'] = 'forward'
1008         cs.to_reference_transforms.append(shaper_inverse)
1009
1010     return cs
1011
1012
1013 # -------------------------------------------------------------------------
1014 # *ODTs*
1015 # -------------------------------------------------------------------------
1016 def create_ODTs(aces_ctl_directory,
1017                 lut_directory,
1018                 lut_resolution_1d,
1019                 lut_resolution_3d,
1020                 odt_info,
1021                 shaper_name,
1022                 cleanup,
1023                 linear_display_space,
1024                 log_display_space):
1025     """
1026     Object description.
1027
1028     Parameters
1029     ----------
1030     parameter : type
1031         Parameter description.
1032
1033     Returns
1034     -------
1035     type
1036          Return value description.
1037     """
1038
1039     colorspaces = []
1040     displays = {}
1041
1042     # -------------------------------------------------------------------------
1043     # *RRT / ODT* Shaper Options
1044     # -------------------------------------------------------------------------
1045     shaper_data = {}
1046
1047     # Defining the *Log 2* shaper.
1048     log2_shaper_name = shaper_name
1049     log2_shaper_name_aliases = ['crv_%s' % compact(log2_shaper_name)]
1050     log2_params = {
1051         'middleGrey': 0.18,
1052         'minExposure': -6,
1053         'maxExposure': 6.5}
1054
1055     log2_shaper_colorspace = create_generic_log(
1056         aces_ctl_directory,
1057         lut_directory,
1058         lut_resolution_1d,
1059         cleanup,
1060         name=log2_shaper_name,
1061         middle_grey=log2_params['middleGrey'],
1062         min_exposure=log2_params['minExposure'],
1063         max_exposure=log2_params['maxExposure'],
1064         aliases=log2_shaper_name_aliases)
1065     colorspaces.append(log2_shaper_colorspace)
1066
1067     shaper_input_scale_generic_log2 = 1
1068
1069     # *Log 2* shaper name and *CTL* transforms bundled up.
1070     log2_shaper_data = [
1071         log2_shaper_name,
1072         os.path.join('%s',
1073                      'utilities',
1074                      'ACESlib.Log2_to_Lin_param.a1.0.0.ctl'),
1075         os.path.join('%s',
1076                      'utilities',
1077                      'ACESlib.Lin_to_Log2_param.a1.0.0.ctl'),
1078         shaper_input_scale_generic_log2,
1079         log2_params]
1080
1081     shaper_data[log2_shaper_name] = log2_shaper_data
1082
1083     # Space with a more user-friendly name. Direct copy otherwise.
1084     log2_shaper_copy_name = 'Log2 Shaper'
1085     log2_shaper_copy_colorspace = ColorSpace(log2_shaper_copy_name)
1086     log2_shaper_copy_colorspace.description = (
1087         'The %s color space' % log2_shaper_copy_name)
1088     log2_shaper_copy_colorspace.aliases = [
1089         'crv_%s' % compact(log2_shaper_copy_name)]
1090     log2_shaper_copy_colorspace.equality_group = log2_shaper_copy_name
1091     log2_shaper_copy_colorspace.family = log2_shaper_colorspace.family
1092     log2_shaper_copy_colorspace.is_data = log2_shaper_colorspace.is_data
1093     log2_shaper_copy_colorspace.to_reference_transforms = list(
1094         log2_shaper_colorspace.to_reference_transforms)
1095     log2_shaper_copy_colorspace.from_reference_transforms = list(
1096         log2_shaper_colorspace.from_reference_transforms)
1097     colorspaces.append(log2_shaper_copy_colorspace)
1098
1099     # Defining the *Log2 shaper that includes the AP1* primaries.
1100     log2_shaper_api1_name = '%s - AP1' % 'Log2 Shaper'
1101     log2_shaper_api1_colorspace = ColorSpace(log2_shaper_api1_name)
1102     log2_shaper_api1_colorspace.description = (
1103         'The %s color space' % log2_shaper_api1_name)
1104     log2_shaper_api1_colorspace.aliases = [
1105         '%s_ap1' % compact(log2_shaper_copy_name)]
1106     log2_shaper_api1_colorspace.equality_group = log2_shaper_api1_name
1107     log2_shaper_api1_colorspace.family = log2_shaper_colorspace.family
1108     log2_shaper_api1_colorspace.is_data = log2_shaper_colorspace.is_data
1109     log2_shaper_api1_colorspace.to_reference_transforms = list(
1110         log2_shaper_colorspace.to_reference_transforms)
1111     log2_shaper_api1_colorspace.from_reference_transforms = list(
1112         log2_shaper_colorspace.from_reference_transforms)
1113
1114     # *AP1* primaries to *AP0* primaries
1115     log2_shaper_api1_colorspace.to_reference_transforms.append({
1116         'type': 'matrix',
1117         'matrix': mat44_from_mat33(ACES_AP1_TO_AP0),
1118         'direction': 'forward'
1119     })
1120     colorspaces.append(log2_shaper_api1_colorspace)
1121
1122     # Defining the *Log2* shaper that includes the *AP1* primaries.
1123     # Named with `shaper_name` variable and needed for some *LUT* baking steps.
1124     shaper_api1_name = '%s - AP1' % shaper_name
1125     shaper_api1_colorspace = ColorSpace(shaper_api1_name)
1126     shaper_api1_colorspace.description = (
1127         'The %s color space' % shaper_api1_name)
1128     shaper_api1_colorspace.aliases = ['%s_ap1' % compact(shaper_name)]
1129     shaper_api1_colorspace.equality_group = shaper_api1_name
1130     shaper_api1_colorspace.family = log2_shaper_colorspace.family
1131     shaper_api1_colorspace.is_data = log2_shaper_colorspace.is_data
1132     shaper_api1_colorspace.to_reference_transforms = list(
1133         log2_shaper_api1_colorspace.to_reference_transforms)
1134     shaper_api1_colorspace.from_reference_transforms = list(
1135         log2_shaper_api1_colorspace.from_reference_transforms)
1136     colorspaces.append(shaper_api1_colorspace)
1137
1138     # Define the base *Dolby PQ Shaper*
1139     #
1140     dolby_pq_shaper_name = 'Dolby PQ 10000'
1141     dolby_pq_shaper_name_aliases = ['crv_%s' % 'dolbypq_10000']
1142
1143     dolby_pq_shaper_colorspace = create_Dolby_PQ(
1144         aces_ctl_directory,
1145         lut_directory,
1146         lut_resolution_1d,
1147         cleanup,
1148         name=dolby_pq_shaper_name,
1149         aliases=dolby_pq_shaper_name_aliases)
1150     colorspaces.append(dolby_pq_shaper_colorspace)
1151
1152     # *Dolby PQ* shaper name and *CTL* transforms bundled up.
1153     dolby_pq_shaper_data = [
1154         dolby_pq_shaper_name,
1155         os.path.join('%s',
1156                      'utilities',
1157                      'ACESlib.DolbyPQ_to_Lin.a1.0.0.ctl'),
1158         os.path.join('%s',
1159                      'utilities',
1160                      'ACESlib.Lin_to_DolbyPQ.a1.0.0.ctl'),
1161         1.0,
1162         {}]
1163
1164     shaper_data[dolby_pq_shaper_name] = dolby_pq_shaper_data
1165
1166     # Define the *Dolby PQ Shaper that considers a fixed linear range*
1167     dolby_pq_scaled_shaper_name = 'Dolby PQ Scaled'
1168     dolby_pq_scaled_shaper_name_aliases = ['crv_%s' % 'dolbypq_scaled']
1169
1170     dolby_pq_scaled_shaper_colorspace = create_Dolby_PQ_scaled(
1171         aces_ctl_directory,
1172         lut_directory,
1173         lut_resolution_1d,
1174         cleanup,
1175         name=dolby_pq_scaled_shaper_name,
1176         aliases=dolby_pq_scaled_shaper_name_aliases)
1177     colorspaces.append(dolby_pq_scaled_shaper_colorspace)
1178
1179     # *Dolby PQ* shaper name and *CTL* transforms bundled up.
1180     dolby_pq_scaled_shaper_data = [
1181         dolby_pq_scaled_shaper_name,
1182         os.path.join('%s',
1183                      'utilities',
1184                      'ACESlib.OCIOShaper_to_Lin_param.a1.0.0.ctl'),
1185         os.path.join('%s',
1186                      'utilities',
1187                      'ACESlib.Lin_to_OCIOShaper_param.a1.0.0.ctl'),
1188         1.0,
1189         log2_params]
1190
1191     shaper_data[dolby_pq_scaled_shaper_name] = dolby_pq_scaled_shaper_data
1192
1193     rrt_shaper = log2_shaper_data
1194     # rrt_shaper = dolby_pq_scaled_shaper_data
1195
1196     # *RRT + ODT* combinations.
1197     sorted_odts = sorted(odt_info.iteritems(), key=lambda x: x[1])
1198     print(sorted_odts)
1199     for odt in sorted_odts:
1200         (odt_name, odt_values) = odt
1201
1202         # Defining full range transform for *ODTs* that can generate either
1203         # *legal* or *full* output.
1204
1205         # Uncomment these lines and the lower section and
1206         # flip the `legalRange` value to 1 to restore the old behavior,
1207         # where both *legal* or *full* range *LUTs* were generated.
1208         if odt_values['transformHasFullLegalSwitch']:
1209             # odt_name_legal = '%s - Legal' % odt_values['transformUserName']
1210             odt_legal['legalRange'] = 0
1211         # else:
1212         #    odt_name_legal = odt_values['transformUserName']
1213
1214         odt_name_legal = odt_values['transformUserName']
1215
1216         odt_legal = odt_values.copy()
1217
1218         odt_aliases = ['out_%s' % compact(odt_name_legal)]
1219
1220         cs = create_ACES_RRT_plus_ODT(
1221             odt_name_legal,
1222             odt_legal,
1223             rrt_shaper,
1224             aces_ctl_directory,
1225             lut_directory,
1226             lut_resolution_3d,
1227             cleanup,
1228             odt_aliases)
1229         colorspaces.append(cs)
1230
1231         displays[odt_name_legal] = {
1232             'Raw': linear_display_space,
1233             'Log': log_display_space,
1234             'Output Transform': cs}
1235
1236         """
1237         # Generating full range transform for *ODTs* that can generate 
1238         # either *legal* or *full* output.
1239         if odt_values['transformHasFullLegalSwitch']:
1240             print('Generating full range ODT for %s' % odt_name)
1241
1242             odt_name_full = '%s - Full' % odt_values['transformUserName']
1243             odt_full = odt_values.copy()
1244             odt_full['legalRange'] = 0
1245
1246             odt_full_aliases = ['out_%s' % compact(odt_name_full)]
1247
1248             cs_full = create_ACES_RRT_plus_ODT(
1249                 odt_name_full,
1250                 odt_full,
1251                 rrt_shaper,
1252                 aces_ctl_directory,
1253                 lut_directory,
1254                 lut_resolution_1d,
1255                 lut_resolution_3d,
1256                 cleanup,
1257                 odt_full_aliases)
1258             colorspaces.append(cs_full)
1259
1260             displays[odt_name_full] = {
1261                 'Raw': linear_display_space,
1262                 'Log': log_display_space,
1263                 'Output Transform': cs_full}
1264         """
1265
1266     return colorspaces, displays
1267
1268
1269 def get_transform_info(ctl_transform):
1270     """
1271     Object description.
1272
1273     Parameters
1274     ----------
1275     parameter : type
1276         Parameter description.
1277
1278     Returns
1279     -------
1280     type
1281          Return value description.
1282     """
1283
1284     with open(ctl_transform, 'rb') as fp:
1285         lines = fp.readlines()
1286
1287     # Retrieving the *transform ID* and *User Name*.
1288     transform_id = lines[1][3:].split('<')[1].split('>')[1].strip()
1289     transform_user_name = '-'.join(
1290         lines[2][3:].split('<')[1].split('>')[1].split('-')[1:]).strip()
1291     transform_user_name_prefix = (
1292         lines[2][3:].split('<')[1].split('>')[1].split('-')[0].strip())
1293
1294     # Figuring out if this transform has options for processing *full* and
1295     # *legal* ranges.
1296     transform_full_legal_switch = False
1297     for line in lines:
1298         if line.strip() == 'input varying int legalRange = 0':
1299             # print( '%s has legal range flag' % transform_user_name)
1300             transform_full_legal_switch = True
1301             break
1302
1303     return (transform_id,
1304             transform_user_name,
1305             transform_user_name_prefix,
1306             transform_full_legal_switch)
1307
1308
1309 def get_ODTs_info(aces_ctl_directory):
1310     """
1311     Object description.
1312
1313     For versions after WGR9.
1314
1315     Parameters
1316     ----------
1317     parameter : type
1318         Parameter description.
1319
1320     Returns
1321     -------
1322     type
1323          Return value description.
1324     """
1325
1326     # TODO: Investigate usage of *files_walker* definition here.
1327     # Credit to *Alex Fry* for the original approach here.
1328     odt_dir = os.path.join(aces_ctl_directory, 'odt')
1329     all_odt = []
1330     for dir_name, subdir_list, file_list in os.walk(odt_dir):
1331         for fname in file_list:
1332             all_odt.append((os.path.join(dir_name, fname)))
1333
1334     odt_ctls = [x for x in all_odt if
1335                 ('InvODT' not in x) and (os.path.split(x)[-1][0] != '.')]
1336
1337     odts = {}
1338
1339     for odt_ctl in odt_ctls:
1340         odt_tokens = os.path.split(odt_ctl)
1341
1342         # Handling nested directories.
1343         odt_path_tokens = os.path.split(odt_tokens[-2])
1344         odt_dir = odt_path_tokens[-1]
1345         while odt_path_tokens[-2][-3:] != 'odt':
1346             odt_path_tokens = os.path.split(odt_path_tokens[-2])
1347             odt_dir = os.path.join(odt_path_tokens[-1], odt_dir)
1348
1349         # Building full name.
1350         transform_ctl = odt_tokens[-1]
1351         odt_name = string.join(transform_ctl.split('.')[1:-1], '.')
1352
1353         # Finding id, user name and user name prefix.
1354         (transform_id,
1355          transform_user_name,
1356          transform_user_name_prefix,
1357          transform_full_legal_switch) = get_transform_info(
1358             os.path.join(aces_ctl_directory, 'odt', odt_dir, transform_ctl))
1359
1360         # Finding inverse.
1361         transform_ctl_inverse = 'InvODT.%s.ctl' % odt_name
1362         if not os.path.exists(
1363                 os.path.join(odt_tokens[-2], transform_ctl_inverse)):
1364             transform_ctl_inverse = None
1365
1366         # Adding to list of *ODTs*.
1367         odts[odt_name] = {}
1368         odts[odt_name]['transformCTL'] = os.path.join(odt_dir, transform_ctl)
1369         if transform_ctl_inverse is not None:
1370             odts[odt_name]['transformCTLInverse'] = os.path.join(
1371                 odt_dir, transform_ctl_inverse)
1372
1373         odts[odt_name]['transformID'] = transform_id
1374         odts[odt_name]['transformUserNamePrefix'] = transform_user_name_prefix
1375         odts[odt_name]['transformUserName'] = transform_user_name
1376         odts[odt_name][
1377             'transformHasFullLegalSwitch'] = transform_full_legal_switch
1378
1379         forward_ctl = odts[odt_name]['transformCTL']
1380
1381         print('ODT : %s' % odt_name)
1382         print('\tTransform ID               : %s' % transform_id)
1383         print('\tTransform User Name Prefix : %s' % transform_user_name_prefix)
1384         print('\tTransform User Name        : %s' % transform_user_name)
1385         print(
1386             '\tHas Full / Legal Switch    : %s' % transform_full_legal_switch)
1387         print('\tForward ctl                : %s' % forward_ctl)
1388         if 'transformCTLInverse' in odts[odt_name]:
1389             inverse_ctl = odts[odt_name]['transformCTLInverse']
1390             print('\tInverse ctl                : %s' % inverse_ctl)
1391         else:
1392             print('\tInverse ctl                : %s' % 'None')
1393
1394     print('\n')
1395
1396     return odts
1397
1398
1399 def get_LMTs_info(aces_ctl_directory):
1400     """
1401     Object description.
1402
1403     For versions after WGR9.
1404
1405     Parameters
1406     ----------
1407     parameter : type
1408         Parameter description.
1409
1410     Returns
1411     -------
1412     type
1413          Return value description.
1414     """
1415
1416     # TODO: Investigate refactoring with previous definition.
1417
1418     # Credit to Alex Fry for the original approach here
1419     lmt_dir = os.path.join(aces_ctl_directory, 'lmt')
1420     all_lmt = []
1421     for dir_name, subdir_list, file_list in os.walk(lmt_dir):
1422         for fname in file_list:
1423             all_lmt.append((os.path.join(dir_name, fname)))
1424
1425     lmt_ctls = [x for x in all_lmt if
1426                 ('InvLMT' not in x) and ('README' not in x) and (
1427                     os.path.split(x)[-1][0] != '.')]
1428
1429     lmts = {}
1430
1431     for lmt_ctl in lmt_ctls:
1432         lmt_tokens = os.path.split(lmt_ctl)
1433
1434         # Handlimg nested directories.
1435         lmt_path_tokens = os.path.split(lmt_tokens[-2])
1436         lmt_dir = lmt_path_tokens[-1]
1437         while lmt_path_tokens[-2][-3:] != 'ctl':
1438             lmt_path_tokens = os.path.split(lmt_path_tokens[-2])
1439             lmt_dir = os.path.join(lmt_path_tokens[-1], lmt_dir)
1440
1441         # Building full name.
1442         transform_ctl = lmt_tokens[-1]
1443         lmt_name = string.join(transform_ctl.split('.')[1:-1], '.')
1444
1445         # Finding id, user name and user name prefix.
1446         (transform_id,
1447          transform_user_name,
1448          transform_user_name_prefix,
1449          transform_full_legal_switch) = get_transform_info(
1450             os.path.join(aces_ctl_directory, lmt_dir, transform_ctl))
1451
1452         # Finding inverse.
1453         transform_ctl_inverse = 'InvLMT.%s.ctl' % lmt_name
1454         if not os.path.exists(
1455                 os.path.join(lmt_tokens[-2], transform_ctl_inverse)):
1456             transform_ctl_inverse = None
1457
1458         lmts[lmt_name] = {}
1459         lmts[lmt_name]['transformCTL'] = os.path.join(lmt_dir, transform_ctl)
1460         if transform_ctl_inverse is not None:
1461             lmts[lmt_name]['transformCTLInverse'] = os.path.join(
1462                 lmt_dir, transform_ctl_inverse)
1463
1464         lmts[lmt_name]['transformID'] = transform_id
1465         lmts[lmt_name]['transformUserNamePrefix'] = transform_user_name_prefix
1466         lmts[lmt_name]['transformUserName'] = transform_user_name
1467
1468         forward_ctl = lmts[lmt_name]['transformCTL']
1469
1470         print('LMT : %s' % lmt_name)
1471         print('\tTransform ID               : %s' % transform_id)
1472         print('\tTransform User Name Prefix : %s' % transform_user_name_prefix)
1473         print('\tTransform User Name        : %s' % transform_user_name)
1474         print('\t Forward ctl               : %s' % forward_ctl)
1475         if 'transformCTLInverse' in lmts[lmt_name]:
1476             inverse_ctl = lmts[lmt_name]['transformCTLInverse']
1477             print('\t Inverse ctl                : %s' % inverse_ctl)
1478         else:
1479             print('\t Inverse ctl                : %s' % 'None')
1480
1481     print('\n')
1482
1483     return lmts
1484
1485
1486 def create_colorspaces(aces_ctl_directory,
1487                        lut_directory,
1488                        lut_resolution_1d,
1489                        lut_resolution_3d,
1490                        lmt_info,
1491                        odt_info,
1492                        shaper_name,
1493                        cleanup):
1494     """
1495     Generates the colorspace conversions.
1496
1497     Parameters
1498     ----------
1499     parameter : type
1500         Parameter description.
1501
1502     Returns
1503     -------
1504     type
1505          Return value description.
1506     """
1507
1508     colorspaces = []
1509
1510     ACES = create_ACES()
1511
1512     ACEScc = create_ACEScc(aces_ctl_directory, lut_directory,
1513                            lut_resolution_1d, cleanup,
1514                            min_value=-0.35840, max_value=1.468)
1515     colorspaces.append(ACEScc)
1516
1517     ACESproxy = create_ACESproxy(aces_ctl_directory, lut_directory,
1518                                  lut_resolution_1d, cleanup)
1519     colorspaces.append(ACESproxy)
1520
1521     ACEScg = create_ACEScg()
1522     colorspaces.append(ACEScg)
1523
1524     ADX10 = create_ADX(lut_directory, bit_depth=10)
1525     colorspaces.append(ADX10)
1526
1527     ADX16 = create_ADX(lut_directory, bit_depth=16)
1528     colorspaces.append(ADX16)
1529
1530     lmts = create_LMTs(aces_ctl_directory,
1531                        lut_directory,
1532                        lut_resolution_1d,
1533                        lut_resolution_3d,
1534                        lmt_info,
1535                        cleanup)
1536     colorspaces.extend(lmts)
1537
1538     odts, displays = create_ODTs(aces_ctl_directory,
1539                                  lut_directory,
1540                                  lut_resolution_1d,
1541                                  lut_resolution_3d,
1542                                  odt_info,
1543                                  shaper_name,
1544                                  cleanup,
1545                                  ACES,
1546                                  ACEScc)
1547     colorspaces.extend(odts)
1548
1549     # TODO: Investigate if there is a way to retrieve these values from *CTL*.
1550     default_display = 'sRGB (D60 sim.)'
1551     color_picking = 'Rec.709'
1552
1553     roles = {'color_picking': color_picking,
1554              'color_timing': ACEScc.name,
1555              'compositing_log': ACEScc.name,
1556              'data': '',
1557              'default': ACES.name,
1558              'matte_paint': ACEScc.name,
1559              'reference': '',
1560              'scene_linear': ACEScg.name,
1561              'texture_paint': '',
1562              'compositing_linear': ACEScg.name,
1563              'rendering': ACEScg.name}
1564
1565     return ACES, colorspaces, displays, ACEScc, roles, default_display