Remove unused locals.
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / colorspaces / gopro.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 Implements support for *GoPro* colorspaces conversions and transfer functions.
6 """
7
8 from __future__ import division
9
10 import array
11 import os
12
13 import PyOpenColorIO as ocio
14
15 import aces_ocio.generate_lut as genlut
16 from aces_ocio.utilities import (ColorSpace, sanitize)
17
18 __author__ = 'ACES Developers'
19 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
20 __license__ = ''
21 __maintainer__ = 'ACES Developers'
22 __email__ = 'aces@oscars.org'
23 __status__ = 'Production'
24
25 __all__ = ['create_protune',
26            'create_colorspaces']
27
28
29 def create_protune(gamut,
30                    transfer_function,
31                    lut_directory,
32                    lut_resolution_1d,
33                    aliases):
34     """
35     Object description.
36
37     Protune to ACES.
38
39     Parameters
40     ----------
41     parameter : type
42         Parameter description.
43
44     Returns
45     -------
46     type
47          Return value description.
48     """
49
50     # The gamut should be marked as experimental until
51     # matrices are fully verified
52     name = '%s - %s - Experimental' % (transfer_function, gamut)
53     if transfer_function == '':
54         name = 'Linear - %s - Experimental' % gamut
55     if gamut == '':
56         name = 'Curve - %s' % transfer_function
57
58     cs = ColorSpace(name)
59     cs.description = name
60     cs.aliases = aliases
61     cs.equality_group = ''
62     cs.family = 'Input/GoPro'
63     cs.is_data = False
64
65     # A linear space needs allocation variables
66     if transfer_function == '':
67         cs.allocation_type = ocio.Constants.ALLOCATION_LG2
68         cs.allocation_vars = [-8, 5, 0.00390625]
69
70     def protune_to_linear(normalized_code_value):
71         c1 = 113.0
72         c2 = 1.0
73         c3 = 112.0
74         linear = ((pow(c1, normalized_code_value) - c2) / c3)
75
76         return linear
77
78     cs.to_reference_transforms = []
79
80     if transfer_function == 'Protune Flat':
81         data = array.array('f', '\0' * lut_resolution_1d * 4)
82         for c in range(lut_resolution_1d):
83             data[c] = protune_to_linear(float(c) / (lut_resolution_1d - 1))
84
85         lut = '%s_to_linear.spi1d' % transfer_function
86         lut = sanitize(lut)
87         genlut.write_SPI_1d(
88             os.path.join(lut_directory, lut),
89             0,
90             1,
91             data,
92             lut_resolution_1d,
93             1)
94
95         cs.to_reference_transforms.append({
96             'type': 'lutFile',
97             'path': lut,
98             'interpolation': 'linear',
99             'direction': 'forward'})
100
101     if gamut == 'Protune Native':
102         cs.to_reference_transforms.append({
103             'type': 'matrix',
104             'matrix': [0.533448429, 0.32413911, 0.142412421, 0,
105                        -0.050729924, 1.07572006, -0.024990416, 0,
106                        0.071419661, -0.290521962, 1.219102381, 0,
107                        0, 0, 0, 1],
108             'direction': 'forward'})
109
110     cs.from_reference_transforms = []
111     return cs
112
113
114 def create_colorspaces(lut_directory, lut_resolution_1d):
115     """
116     Generates the colorspace conversions.
117
118     Parameters
119     ----------
120     parameter : type
121         Parameter description.
122
123     Returns
124     -------
125     type
126          Return value description.
127     """
128
129     colorspaces = []
130
131     # Full conversion
132     protune_1 = create_protune(
133         'Protune Native',
134         'Protune Flat',
135         lut_directory,
136         lut_resolution_1d,
137         ['protuneflat_protunegamutexp'])
138     colorspaces.append(protune_1)
139
140     # Linearization Only
141     protune_2 = create_protune(
142         '',
143         'Protune Flat',
144         lut_directory,
145         lut_resolution_1d,
146         ['crv_protuneflat'])
147     colorspaces.append(protune_2)
148
149     # Primaries Only
150     protune_3 = create_protune(
151         'Protune Native',
152         '',
153         lut_directory,
154         lut_resolution_1d,
155         ['lin_protunegamutexp'])
156     colorspaces.append(protune_3)
157
158     return colorspaces