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