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