Fixed issue with mismatched gamut name in different functions
[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                    name,
32                    lut_directory,
33                    lut_resolution_1d,
34                    aliases):
35     """
36     Object description.
37
38     Protune to ACES.
39
40     Parameters
41     ----------
42     parameter : type
43         Parameter description.
44
45     Returns
46     -------
47     type
48          Return value description.
49     """
50
51     # The gamut should be marked as experimental until
52     # matrices are fully verified
53     name = '%s - %s - Experimental' % (transfer_function, gamut)
54     if transfer_function == '':
55         name = 'Linear - %s - Experimental' % gamut
56     if gamut == '':
57         name = 'Curve - %s' % transfer_function
58
59     cs = ColorSpace(name)
60     cs.description = name
61     cs.aliases = aliases
62     cs.equality_group = ''
63     cs.family = 'Input/GoPro'
64     cs.is_data = False
65
66     # A linear space needs allocation variables
67     if transfer_function == '':
68         cs.allocation_type = ocio.Constants.ALLOCATION_LG2
69         cs.allocation_vars = [-8, 5, 0.00390625]
70
71     def protune_to_linear(normalized_code_value):
72         c1 = 113.0
73         c2 = 1.0
74         c3 = 112.0
75         linear = ((pow(c1, (normalized_code_value)) - c2) / c3)
76
77         return linear
78
79     cs.to_reference_transforms = []
80
81     if transfer_function == 'Protune Flat':
82         data = array.array('f', '\0' * lut_resolution_1d * 4)
83         for c in range(lut_resolution_1d):
84             data[c] = protune_to_linear(float(c) / (lut_resolution_1d - 1))
85
86         lut = '%s_to_linear.spi1d' % transfer_function
87         lut = sanitize(lut)
88         genlut.write_SPI_1d(
89             os.path.join(lut_directory, lut),
90             0,
91             1,
92             data,
93             lut_resolution_1d,
94             1)
95
96         cs.to_reference_transforms.append({
97             'type': 'lutFile',
98             'path': lut,
99             'interpolation': 'linear',
100             'direction': 'forward'})
101
102     if gamut == 'Protune Native':
103         cs.to_reference_transforms.append({
104             'type': 'matrix',
105             'matrix': [0.533448429, 0.32413911, 0.142412421, 0,
106                        -0.050729924, 1.07572006, -0.024990416, 0,
107                        0.071419661, -0.290521962, 1.219102381, 0,
108                        0, 0, 0, 1],
109             'direction': 'forward'})
110
111     cs.from_reference_transforms = []
112     return cs
113
114
115 def create_colorspaces(lut_directory, lut_resolution_1d):
116     """
117     Generates the colorspace conversions.
118
119     Parameters
120     ----------
121     parameter : type
122         Parameter description.
123
124     Returns
125     -------
126     type
127          Return value description.
128     """
129
130     colorspaces = []
131
132     # Full conversion
133     protune_1 = create_protune(
134         'Protune Native',
135         'Protune Flat',
136         'Protune',
137         lut_directory,
138         lut_resolution_1d,
139         ["protuneflat_protunegamutexp"])
140     colorspaces.append(protune_1)
141
142     # Linearization Only
143     protune_2 = create_protune(
144         '',
145         'Protune Flat',
146         'Protune',
147         lut_directory,
148         lut_resolution_1d,
149         ["crv_protuneflat"])
150     colorspaces.append(protune_2)
151
152     # Primaries Only
153     protune_3 = create_protune(
154         'Protune Native',
155         '',
156         'Protune',
157         lut_directory,
158         lut_resolution_1d,
159         ["lin_protunegamutexp"])
160     colorspaces.append(protune_3)
161
162     return colorspaces