Grouped camera colorspaces as 'Input' for applications that group colorspaces based...
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / create_gopro_colorspaces.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     name = '%s - %s' % (transfer_function, gamut)
52     if transfer_function == '':
53         name = 'Linear - %s' % gamut
54     if gamut == '':
55         name = '%s' % transfer_function
56
57     cs = ColorSpace(name)
58     cs.description = name
59     cs.aliases = aliases
60     cs.equality_group = ''
61     cs.family = 'Input/GoPro'
62     cs.is_data = False
63
64     # A linear space needs allocation variables
65     if transfer_function == '':
66         cs.allocation_type = ocio.Constants.ALLOCATION_LG2
67         cs.allocation_vars = [-8, 5, 0.00390625]
68
69     def protune_to_linear(normalized_code_value):
70         c1 = 113.0
71         c2 = 1.0
72         c3 = 112.0
73         linear = ((pow(c1, (normalized_code_value)) - c2) / c3)
74
75         return linear
76
77     cs.to_reference_transforms = []
78
79     if transfer_function == 'Protune Flat':
80         data = array.array('f', '\0' * lut_resolution_1d * 4)
81         for c in range(lut_resolution_1d):
82             data[c] = protune_to_linear(float(c) / (lut_resolution_1d - 1))
83
84         lut = '%s_to_linear.spi1d' % transfer_function
85         lut = sanitize(lut)
86         genlut.write_SPI_1d(
87             os.path.join(lut_directory, lut),
88             0,
89             1,
90             data,
91             lut_resolution_1d,
92             1)
93
94         cs.to_reference_transforms.append({
95             'type': 'lutFile',
96             'path': lut,
97             'interpolation': 'linear',
98             'direction': 'forward'})
99
100     if gamut == 'Protune Gamut':
101         cs.to_reference_transforms.append({
102             'type': 'matrix',
103             'matrix': [ 0.533448429,  0.32413911,  0.142412421, 0,
104                        -0.050729924,  1.07572006, -0.024990416, 0,
105                         0.071419661, -0.290521962, 1.219102381, 0,
106                        0, 0, 0, 1],
107             'direction': 'forward'})
108
109     cs.from_reference_transforms = []
110     return cs
111
112
113 def create_colorspaces(lut_directory, lut_resolution_1d):
114     """
115     Generates the colorspace conversions.
116
117     Parameters
118     ----------
119     parameter : type
120         Parameter description.
121
122     Returns
123     -------
124     type
125          Return value description.
126     """
127
128     colorspaces = []
129
130     # Full conversion
131     protune_1 = create_protune(
132         'Protune Native',
133         'Protune Flat',
134         'Protune',
135         lut_directory,
136         lut_resolution_1d,
137         ["protuneflat_protunegamut"])
138     colorspaces.append(protune_1)
139
140     # Linearization Only
141     protune_2 = create_protune(
142         '',
143         'Protune Flat',
144         'Protune',
145         lut_directory,
146         lut_resolution_1d,
147         ["crv_protuneflat"])
148     colorspaces.append(protune_2)
149
150     # Primaries Only
151     protune_3 = create_protune(
152         'Protune Native',
153         '',
154         'Protune',
155         lut_directory,
156         lut_resolution_1d,
157         ["lin_protunegamut"])
158     colorspaces.append(protune_3)
159
160     return colorspaces