Fix "PEP8" coding style violations.
[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                  name,
31                  lut_directory,
32                  lut_resolution_1d,
33                  aliases):
34     """
35     Object description.
36
37     Panasonic V-Log 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     name = '%s - %s' % (transfer_function, gamut)
51     if transfer_function == '':
52         name = 'Linear - %s' % gamut
53     if gamut == '':
54         name = 'Curve - %s' % transfer_function
55
56     cs = ColorSpace(name)
57     cs.description = name
58     cs.aliases = aliases
59     cs.equality_group = ''
60     cs.family = 'Input/Panasonic'
61     cs.is_data = False
62
63     # A linear space needs allocation variables
64     if transfer_function == '':
65         cs.allocation_type = ocio.Constants.ALLOCATION_LG2
66         cs.allocation_vars = [-8, 5, 0.00390625]
67
68     def v_log_to_linear(x):
69         cutInv = 0.181
70         b = 0.00873
71         c = 0.241514
72         d = 0.598206
73
74         if (x <= cutInv):
75             return (x - 0.125) / 5.6
76         else:
77             return pow(10, (x - d) / c) - b
78
79     cs.to_reference_transforms = []
80
81     if transfer_function == 'V-Log':
82         data = array.array('f', '\0' * lut_resolution_1d * 4)
83         for c in range(lut_resolution_1d):
84             data[c] = v_log_to_linear(float(c) / (lut_resolution_1d - 1))
85
86         lut = '%s_to_linear.spi1d' % transfer_function
87         genlut.write_SPI_1d(
88             os.path.join(lut_directory, lut),
89             0.0,
90             1.0,
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 == 'V-Gamut':
102         cs.to_reference_transforms.append({
103             'type': 'matrix',
104             'matrix': [0.724382758, 0.166748484, 0.108497411, 0.0,
105                        0.021354009, 0.985138372, -0.006319092, 0.0,
106                        -0.009234278, -0.00104295, 1.010272625, 0.0,
107                        0, 0, 0, 1.0],
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     v_log_1 = create_v_log(
133         'V-Gamut',
134         'V-Log',
135         'V-Log',
136         lut_directory,
137         lut_resolution_1d,
138         ['vlog_vgamut'])
139     colorspaces.append(v_log_1)
140
141     # Linearization Only
142     v_log_2 = create_v_log(
143         '',
144         'V-Log',
145         'V-Log',
146         lut_directory,
147         lut_resolution_1d,
148         ['crv_vlog'])
149     colorspaces.append(v_log_2)
150
151     # Primaries Only
152     v_log_3 = create_v_log(
153         'V-Gamut',
154         '',
155         'V-Log',
156         lut_directory,
157         lut_resolution_1d,
158         ['lin_vgamut'])
159     colorspaces.append(v_log_3)
160
161     return colorspaces