Added documentation for individual functions
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / colorspaces / panasonic.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 Implements support for *Panasonic* colorspaces conversions and transfer
6 functions.
7 """
8
9 import array
10 import os
11
12 import PyOpenColorIO as ocio
13
14 import aces_ocio.generate_lut as genlut
15 from aces_ocio.utilities import ColorSpace
16
17 __author__ = 'ACES Developers'
18 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
19 __license__ = ''
20 __maintainer__ = 'ACES Developers'
21 __email__ = 'aces@oscars.org'
22 __status__ = 'Production'
23
24 __all__ = ['create_v_log',
25            'create_colorspaces']
26
27
28 def create_v_log(gamut,
29                  transfer_function,
30                  lut_directory,
31                  lut_resolution_1d,
32                  aliases):
33     """
34     Creates colorspace covering the conversion from VLog to ACES, with various transfer 
35     functions and encoding gamuts covered
36
37     Parameters
38     ----------
39     gamut : str
40         The name of the encoding gamut to use.
41     transfer_function : str
42         The name of the transfer function to use
43     lut_directory : str or unicode 
44         The directory to use when generating LUTs
45     lut_resolution_1d : int
46         The resolution of generated 1D LUTs
47     aliases : list of str
48         Aliases for this colorspace
49
50     Returns
51     -------
52     ColorSpace
53          A ColorSpace container class referencing the LUTs, matrices and identifying
54          information for the requested colorspace.
55     """
56
57     name = '%s - %s' % (transfer_function, gamut)
58     if transfer_function == '':
59         name = 'Linear - %s' % gamut
60     if gamut == '':
61         name = 'Curve - %s' % transfer_function
62
63     cs = ColorSpace(name)
64     cs.description = name
65     cs.aliases = aliases
66     cs.equality_group = ''
67     cs.family = 'Input/Panasonic'
68     cs.is_data = False
69
70     # A linear space needs allocation variables
71     if transfer_function == '':
72         cs.allocation_type = ocio.Constants.ALLOCATION_LG2
73         cs.allocation_vars = [-8, 5, 0.00390625]
74
75     def v_log_to_linear(x):
76         cut_inv = 0.181
77         b = 0.00873
78         c = 0.241514
79         d = 0.598206
80
81         if x <= cut_inv:
82             return (x - 0.125) / 5.6
83         else:
84             return pow(10, (x - d) / c) - b
85
86     cs.to_reference_transforms = []
87
88     if transfer_function == 'V-Log':
89         data = array.array('f', '\0' * lut_resolution_1d * 4)
90         for c in range(lut_resolution_1d):
91             data[c] = v_log_to_linear(float(c) / (lut_resolution_1d - 1))
92
93         lut = '%s_to_linear.spi1d' % transfer_function
94         genlut.write_SPI_1d(
95             os.path.join(lut_directory, lut),
96             0.0,
97             1.0,
98             data,
99             lut_resolution_1d,
100             1)
101
102         cs.to_reference_transforms.append({
103             'type': 'lutFile',
104             'path': lut,
105             'interpolation': 'linear',
106             'direction': 'forward'})
107
108     if gamut == 'V-Gamut':
109         cs.to_reference_transforms.append({
110             'type': 'matrix',
111             'matrix': [0.724382758, 0.166748484, 0.108497411, 0.0,
112                        0.021354009, 0.985138372, -0.006319092, 0.0,
113                        -0.009234278, -0.00104295, 1.010272625, 0.0,
114                        0, 0, 0, 1.0],
115             'direction': 'forward'})
116
117     cs.from_reference_transforms = []
118     return cs
119
120
121 def create_colorspaces(lut_directory, lut_resolution_1d):
122     """
123     Generates the colorspace conversions.
124
125     Parameters
126     ----------
127     lut_directory : str or unicode 
128         The directory to use when generating LUTs
129     lut_resolution_1d : int
130         The resolution of generated 1D LUTs
131
132     Returns
133     -------
134     list
135          A list of colorspaces for Panasonic cameras and encodings 
136     """
137
138     colorspaces = []
139
140     # Full conversion
141     v_log_1 = create_v_log(
142         'V-Gamut',
143         'V-Log',
144         lut_directory,
145         lut_resolution_1d,
146         ['vlog_vgamut'])
147     colorspaces.append(v_log_1)
148
149     # Linearization Only
150     v_log_2 = create_v_log(
151         '',
152         'V-Log',
153         lut_directory,
154         lut_resolution_1d,
155         ['crv_vlog'])
156     colorspaces.append(v_log_2)
157
158     # Primaries Only
159     v_log_3 = create_v_log(
160         'V-Gamut',
161         '',
162         lut_directory,
163         lut_resolution_1d,
164         ['lin_vgamut'])
165     colorspaces.append(v_log_3)
166
167     return colorspaces