Added proper allocatoin for all linear color spaces
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / create_panasonic_colorspaces.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 Implements support for *Panasonic* colorspaces conversions and transfer functions.
6 """
7
8 import array
9 import os
10
11 import PyOpenColorIO as ocio
12
13 import aces_ocio.generate_lut as genlut
14 from aces_ocio.utilities import ColorSpace
15
16 __author__ = 'ACES Developers'
17 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
18 __license__ = ''
19 __maintainer__ = 'ACES Developers'
20 __email__ = 'aces@oscars.org'
21 __status__ = 'Production'
22
23 __all__ = ['create_v_log',
24            'create_colorspaces']
25
26
27 def create_v_log(gamut,
28                  transfer_function,
29                  name,
30                  lut_directory,
31                  lut_resolution_1d,
32                  aliases):
33     """
34     Object description.
35
36     Panasonic V-Log to ACES.
37
38     Parameters
39     ----------
40     parameter : type
41         Parameter description.
42
43     Returns
44     -------
45     type
46          Return value description.
47     """
48
49     name = '%s - %s' % (transfer_function, gamut)
50     if transfer_function == '':
51         name = 'Linear - %s' % gamut
52     if gamut == '':
53         name = '%s' % transfer_function
54
55     cs = ColorSpace(name)
56     cs.description = name
57     cs.aliases = aliases
58     cs.equality_group = ''
59     cs.family = 'Panasonic'
60     cs.is_data = False
61
62     # A linear space needs allocation variables
63     if transfer_function == '':
64         cs.allocation_type = ocio.Constants.ALLOCATION_LG2
65         cs.allocation_vars = [-8, 5, 0.00390625]
66
67     def v_log_to_linear(x):
68         cutInv = 0.181
69         b = 0.00873
70         c = 0.241514
71         d = 0.598206
72
73         if (x <= cutInv):
74             return (x - 0.125) / 5.6
75         else:
76             return pow(10, (x-d)/c) - b
77
78     cs.to_reference_transforms = []
79
80     if transfer_function == 'V-Log':
81         data = array.array('f', '\0' * lut_resolution_1d * 4)
82         for c in range(lut_resolution_1d):
83             data[c] = v_log_to_linear(float(c) / (lut_resolution_1d - 1))
84
85         lut = '%s_to_linear.spi1d' % transfer_function
86         genlut.write_SPI_1d(
87             os.path.join(lut_directory, lut),
88             0.0,
89             1.0,
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 == 'V-Gamut':
101         cs.to_reference_transforms.append({
102             'type': 'matrix',
103             'matrix': [ 0.724382758,  0.166748484,  0.108497411, 0.0,
104                         0.021354009,  0.985138372, -0.006319092, 0.0,
105                        -0.009234278, -0.00104295,   1.010272625, 0.0,
106                         0, 0, 0, 1.0],
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     v_log_1 = create_v_log(
132         'V-Gamut',
133         'V-Log',
134         'V-Log',
135         lut_directory,
136         lut_resolution_1d,
137         ["vlog_vgamut"])
138     colorspaces.append(v_log_1)
139
140     # Linearization Only
141     v_log_2 = create_v_log(
142         '',
143         'V-Log',
144         'V-Log',
145         lut_directory,
146         lut_resolution_1d,
147         ["crv_vlog"])
148     colorspaces.append(v_log_2)
149
150     # Primaries Only
151     v_log_3 = create_v_log(
152         'V-Gamut',
153         '',
154         'V-Log',
155         lut_directory,
156         lut_resolution_1d,
157         ["lin_vgamut"])
158     colorspaces.append(v_log_3)
159
160     return colorspaces