Fix "PEP8" coding style violations.
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / colorspaces / general.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 Implements support for general 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.colorspaces import aces
17 from aces_ocio.utilities import ColorSpace, mat44_from_mat33
18
19 __author__ = 'ACES Developers'
20 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
21 __license__ = ''
22 __maintainer__ = 'ACES Developers'
23 __email__ = 'aces@oscars.org'
24 __status__ = 'Production'
25
26 __all__ = ['create_matrix_colorspace',
27            'create_colorspaces']
28
29
30 # -------------------------------------------------------------------------
31 # *Matrix Transform*
32 # -------------------------------------------------------------------------
33 def create_matrix_colorspace(name='matrix',
34                              from_reference_values=None,
35                              to_reference_values=None,
36                              aliases=None):
37     """
38     Object description.
39
40     Parameters
41     ----------
42     parameter : type
43         Parameter description.
44
45     Returns
46     -------
47     type
48          Return value description.
49     """
50
51     if from_reference_values is None:
52         from_reference_values = []
53
54     if to_reference_values is None:
55         to_reference_values = []
56
57     if aliases is None:
58         aliases = []
59
60     cs = ColorSpace(name)
61     cs.description = 'The %s color space' % name
62     cs.aliases = aliases
63     cs.equality_group = name
64     cs.family = 'Utility'
65     cs.is_data = False
66
67     # A linear space needs allocation variables
68     cs.allocation_type = ocio.Constants.ALLOCATION_UNIFORM
69     cs.allocation_vars = [0, 1]
70
71     cs.to_reference_transforms = []
72     if to_reference_values:
73         for matrix in to_reference_values:
74             cs.to_reference_transforms.append({
75                 'type': 'matrix',
76                 'matrix': mat44_from_mat33(matrix),
77                 'direction': 'forward'})
78
79     cs.from_reference_transforms = []
80     if from_reference_values:
81         for matrix in from_reference_values:
82             cs.from_reference_transforms.append({
83                 'type': 'matrix',
84                 'matrix': mat44_from_mat33(matrix),
85                 'direction': 'forward'})
86
87     return cs
88
89
90 # -------------------------------------------------------------------------
91 # *Transfer Function Transform*
92 # -------------------------------------------------------------------------
93 def create_transfer_colorspace(name='transfer',
94                                transfer_function_name='transfer_function',
95                                transfer_function=lambda x: x,
96                                lut_directory='/tmp',
97                                lut_resolution_1d=1024,
98                                aliases=None):
99     """
100     Object description.
101
102     Parameters
103     ----------
104     parameter : type
105         Parameter description.
106
107     Returns
108     -------
109     type
110          Return value description.
111     """
112
113     if aliases is None:
114         aliases = []
115
116     cs = ColorSpace(name)
117     cs.description = 'The %s color space' % name
118     cs.aliases = aliases
119     cs.equality_group = name
120     cs.family = 'Utility'
121     cs.is_data = False
122
123     # A linear space needs allocation variables
124     cs.allocation_type = ocio.Constants.ALLOCATION_UNIFORM
125     cs.allocation_vars = [0, 1]
126
127     # Sample the transfer function
128     data = array.array('f', '\0' * lut_resolution_1d * 4)
129     for c in range(lut_resolution_1d):
130         data[c] = transfer_function(c / (lut_resolution_1d - 1))
131
132     # Write the sampled data to a LUT
133     lut = '%s_to_linear.spi1d' % transfer_function_name
134     genlut.write_SPI_1d(
135         os.path.join(lut_directory, lut),
136         0,
137         1,
138         data,
139         lut_resolution_1d,
140         1)
141
142     # Create the 'to_reference' transforms
143     cs.to_reference_transforms = []
144     cs.to_reference_transforms.append({
145         'type': 'lutFile',
146         'path': lut,
147         'interpolation': 'linear',
148         'direction': 'forward'})
149
150     # Create the 'from_reference' transforms
151     cs.from_reference_transforms = []
152
153     return cs
154
155
156 # create_transfer_colorspace
157
158 # -------------------------------------------------------------------------
159 # *Transfer Function + Matrix Transform*
160 # -------------------------------------------------------------------------
161 def create_matrix_plus_transfer_colorspace(
162         name='matrix_plus_transfer',
163         transfer_function_name='transfer_function',
164         transfer_function=lambda x: x,
165         lut_directory='/tmp',
166         lut_resolution_1d=1024,
167         from_reference_values=None,
168         to_reference_values=None,
169         aliases=None):
170     """
171     Object description.
172
173     Parameters
174     ----------
175     parameter : type
176         Parameter description.
177
178     Returns
179     -------
180     type
181          Return value description.
182     """
183
184     if from_reference_values is None:
185         from_reference_values = []
186
187     if to_reference_values is None:
188         to_reference_values = []
189
190     if aliases is None:
191         aliases = []
192
193     cs = ColorSpace(name)
194     cs.description = 'The %s color space' % name
195     cs.aliases = aliases
196     cs.equality_group = name
197     cs.family = 'Utility'
198     cs.is_data = False
199
200     # A linear space needs allocation variables
201     cs.allocation_type = ocio.Constants.ALLOCATION_UNIFORM
202     cs.allocation_vars = [0, 1]
203
204     # Sample the transfer function
205     data = array.array('f', '\0' * lut_resolution_1d * 4)
206     for c in range(lut_resolution_1d):
207         data[c] = transfer_function(c / (lut_resolution_1d - 1))
208
209     # Write the sampled data to a LUT
210     lut = '%s_to_linear.spi1d' % transfer_function_name
211     genlut.write_SPI_1d(
212         os.path.join(lut_directory, lut),
213         0,
214         1,
215         data,
216         lut_resolution_1d,
217         1)
218
219     # Create the 'to_reference' transforms
220     cs.to_reference_transforms = []
221     if to_reference_values:
222         cs.to_reference_transforms.append({
223             'type': 'lutFile',
224             'path': lut,
225             'interpolation': 'linear',
226             'direction': 'forward'})
227
228         for matrix in to_reference_values:
229             cs.to_reference_transforms.append({
230                 'type': 'matrix',
231                 'matrix': mat44_from_mat33(matrix),
232                 'direction': 'forward'})
233
234     # Create the 'from_reference' transforms
235     cs.from_reference_transforms = []
236     if from_reference_values:
237         for matrix in from_reference_values:
238             cs.from_reference_transforms.append({
239                 'type': 'matrix',
240                 'matrix': mat44_from_mat33(matrix),
241                 'direction': 'forward'})
242
243         cs.from_reference_transforms.append({
244             'type': 'lutFile',
245             'path': lut,
246             'interpolation': 'linear',
247             'direction': 'inverse'})
248
249     return cs
250
251
252 # create_matrix_plus_transfer_colorspace
253
254 # Transfer functions for standard color spaces
255 def transfer_function_sRGB_to_linear(v):
256     a = 1.055
257     b = 0.04045
258     d = 12.92
259     g = 2.4
260
261     if v < b:
262         return v / d
263     return pow(((v + (a - 1)) / a), g)
264
265
266 def transfer_function_Rec709_to_linear(v):
267     a = 1.099
268     b = 0.018
269     d = 4.5
270     g = (1.0 / 0.45)
271
272     if v < b * d:
273         return v / d
274
275     return pow(((v + (a - 1)) / a), g)
276
277
278 def transfer_function_Rec2020_10bit_to_linear(v):
279     a = 1.099
280     b = 0.018
281     d = 4.5
282     g = (1.0 / 0.45)
283
284     if v < b * d:
285         return v / d
286
287     return pow(((v + (a - 1)) / a), g)
288
289
290 def transfer_function_Rec2020_12bit_to_linear(v):
291     a = 1.0993
292     b = 0.0181
293     d = 4.5
294     g = (1.0 / 0.45)
295
296     if v < b * d:
297         return v / d
298
299     return pow(((v + (a - 1)) / a), g)
300
301
302 def transfer_function_Rec1886_to_linear(v):
303     g = 2.4
304     Lw = 1
305     Lb = 0
306
307     # Ignoring legal to full scaling for now
308     # v = (1023.0*v - 64.0)/876.0
309
310     t = pow(Lw, 1.0 / g) - pow(Lb, 1.0 / g)
311     a = pow(t, g)
312     b = pow(Lb, 1.0 / g) / t
313
314     return a * pow(max((v + b), 0.0), g)
315
316
317 def create_colorspaces(lut_directory,
318                        lut_resolution_1d,
319                        lut_resolution_3d):
320     """
321     Generates the colorspace conversions.
322
323     Parameters
324     ----------
325     parameter : type
326         Parameter description.
327
328     Returns
329     -------
330     type
331          Return value description.
332     """
333
334     colorspaces = []
335
336     #
337     # XYZ
338     #
339     cs = create_matrix_colorspace('XYZ-D60',
340                                   to_reference_values=[aces.ACES_XYZ_TO_AP0],
341                                   from_reference_values=[aces.ACES_AP0_TO_XYZ],
342                                   aliases=['lin_xyz_d60'])
343     colorspaces.append(cs)
344
345     #
346     # P3-D60
347     #
348     # *ACES* to *Linear*, *P3D60* primaries.
349     XYZ_to_P3D60 = [2.4027414142, -0.8974841639, -0.3880533700,
350                     -0.8325796487, 1.7692317536, 0.0237127115,
351                     0.0388233815, -0.0824996856, 1.0363685997]
352
353     cs = create_matrix_colorspace(
354         'Linear - P3-D60',
355         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_P3D60],
356         aliases=['lin_p3d60'])
357     colorspaces.append(cs)
358
359     #
360     # P3-DCI
361     #
362     # *ACES* to *Linear*, *P3DCI* primaries.
363     XYZ_to_P3DCI = [2.7253940305, -1.0180030062, -0.4401631952,
364                     -0.7951680258, 1.6897320548, 0.0226471906,
365                     0.0412418914, -0.0876390192, 1.1009293786]
366
367     cs = create_matrix_colorspace(
368         'Linear - P3-DCI',
369         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_P3DCI],
370         aliases=['lin_p3dci'])
371     colorspaces.append(cs)
372
373     #
374     # sRGB
375     #
376     # *ACES* to *Linear*, *Rec. 709* primaries.
377     # sRGB and Rec 709 use the same gamut
378     XYZ_to_Rec709 = [3.2409699419, -1.5373831776, -0.4986107603,
379                      -0.9692436363, 1.8759675015, 0.0415550574,
380                      0.0556300797, -0.2039769589, 1.0569715142]
381
382     cs = create_matrix_colorspace(
383         'Linear - sRGB',
384         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
385         aliases=['lin_srgb'])
386     colorspaces.append(cs)
387
388     # *Linear* to *sRGB* Transfer Function*
389     cs = create_transfer_colorspace(
390         'Curve - sRGB',
391         'sRGB',
392         transfer_function_sRGB_to_linear,
393         lut_directory,
394         lut_resolution_1d,
395         aliases=['crv_srgb'])
396     colorspaces.append(cs)
397
398     # *ACES* to *sRGB* Primaries + Transfer Function*
399     cs = create_matrix_plus_transfer_colorspace(
400         'sRGB',
401         'sRGB',
402         transfer_function_sRGB_to_linear,
403         lut_directory,
404         lut_resolution_1d,
405         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
406         aliases=['srgb'])
407     colorspaces.append(cs)
408
409     #
410     # Rec 709
411     #
412     # *ACES* to *Linear*, *Rec. 709* primaries.
413     XYZ_to_Rec709 = [3.2409699419, -1.5373831776, -0.4986107603,
414                      -0.9692436363, 1.8759675015, 0.0415550574,
415                      0.0556300797, -0.2039769589, 1.0569715142]
416
417     cs = create_matrix_colorspace(
418         'Linear - Rec.709',
419         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
420         aliases=['lin_rec709'])
421     colorspaces.append(cs)
422
423     # *Linear* to *Rec. 709* Transfer Function*
424     cs = create_transfer_colorspace(
425         'Curve - Rec.709',
426         'rec709',
427         transfer_function_Rec709_to_linear,
428         lut_directory,
429         lut_resolution_1d,
430         aliases=['crv_rec709'])
431     colorspaces.append(cs)
432
433     # *ACES* to *Rec. 709* Primaries + Transfer Function*
434     cs = create_matrix_plus_transfer_colorspace(
435         'Rec.709 - Camera',
436         'rec709',
437         transfer_function_Rec709_to_linear,
438         lut_directory,
439         lut_resolution_1d,
440         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
441         aliases=['rec709_camera'])
442     colorspaces.append(cs)
443
444     #
445     # Rec 2020
446     #
447     # *ACES* to *Linear*, *Rec. 2020* primaries.
448     XYZ_to_Rec2020 = [1.7166511880, -0.3556707838, -0.2533662814,
449                       -0.6666843518, 1.6164812366, 0.0157685458,
450                       0.0176398574, -0.0427706133, 0.9421031212]
451
452     cs = create_matrix_colorspace(
453         'Linear - Rec.2020',
454         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec2020],
455         aliases=['lin_rec2020'])
456     colorspaces.append(cs)
457
458     # *Linear* to *Rec. 2020 10 bit* Transfer Function*
459     cs = create_transfer_colorspace(
460         'Curve - Rec.2020',
461         'rec2020',
462         transfer_function_Rec2020_10bit_to_linear,
463         lut_directory,
464         lut_resolution_1d,
465         aliases=['crv_rec2020'])
466     colorspaces.append(cs)
467
468     # *ACES* to *Rec. 2020 10 bit* Primaries + Transfer Function*
469     cs = create_matrix_plus_transfer_colorspace(
470         'Rec.2020 - Camera',
471         'rec2020',
472         transfer_function_Rec2020_10bit_to_linear,
473         lut_directory,
474         lut_resolution_1d,
475         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec2020],
476         aliases=['rec2020_camera'])
477     colorspaces.append(cs)
478
479     #
480     # Rec 1886
481     #
482
483     # *Linear* to *Rec.1886* Transfer Function*
484     cs = create_transfer_colorspace(
485         'Curve - Rec.1886',
486         'rec1886',
487         transfer_function_Rec1886_to_linear,
488         lut_directory,
489         lut_resolution_1d,
490         aliases=['crv_rec1886'])
491     colorspaces.append(cs)
492
493     # *ACES* to *sRGB* Primaries + Transfer Function*
494     cs = create_matrix_plus_transfer_colorspace(
495         'Rec.709 - Display',
496         'rec1886',
497         transfer_function_Rec1886_to_linear,
498         lut_directory,
499         lut_resolution_1d,
500         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
501         aliases=['rec709_display'])
502     colorspaces.append(cs)
503
504     # *ACES* to *sRGB* Primaries + Transfer Function*
505     cs = create_matrix_plus_transfer_colorspace(
506         'Rec.2020 - Display',
507         'rec1886',
508         transfer_function_Rec1886_to_linear,
509         lut_directory,
510         lut_resolution_1d,
511         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec2020],
512         aliases=['rec2020_display'])
513     colorspaces.append(cs)
514
515     #
516     # ProPhoto
517     #
518     # *ACES* to *Linear*, *Pro Photo* primaries.
519     AP0_to_RIMM = [1.2412367771, -0.1685692287, -0.0726675484,
520                    0.0061203066, 1.083151174, -0.0892714806,
521                    -0.0032853314, 0.0099796402, 0.9933056912]
522
523     cs = create_matrix_colorspace(
524         'Linear - RIMM ROMM (ProPhoto)',
525         from_reference_values=[AP0_to_RIMM],
526         aliases=['lin_prophoto', 'lin_rimm'])
527     colorspaces.append(cs)
528
529     #
530     # Adobe RGB
531     #
532     # *ACES* to *Linear*, *Adobe RGB* primaries.
533     AP0_to_ADOBERGB = [1.7245603168, -0.4199935942, -0.3045667227,
534                        -0.2764799142, 1.3727190877, -0.0962391734,
535                        -0.0261255258, -0.0901747807, 1.1163003065]
536
537     cs = create_matrix_colorspace(
538         'Linear - Adobe RGB',
539         from_reference_values=[AP0_to_ADOBERGB],
540         aliases=['lin_adobergb'])
541     colorspaces.append(cs)
542
543     #
544     # Adobe Wide Gamut RGB
545     #
546
547     # *ACES* to *Linear*, *Adobe Wide Gamut RGB* primaries.
548     AP0_to_ADOBERGB = [1.3809814778, -0.1158594573, -0.2651220205,
549                        0.0057015535, 1.0402949043, -0.0459964578,
550                        -0.0038908746, -0.0597091815, 1.0636000561]
551
552     cs = create_matrix_colorspace(
553         'Linear - Adobe Wide Gamut RGB',
554         from_reference_values=[AP0_to_ADOBERGB],
555         aliases=['lin_adobewidegamutrgb'])
556     colorspaces.append(cs)
557
558     return colorspaces
559
560
561 def create_raw():
562     # *Raw* utility space
563     name = 'Raw'
564     raw = ColorSpace(name)
565     raw.description = 'The %s color space' % name
566     raw.aliases = ['raw']
567     raw.equality_group = name
568     raw.family = 'Utility'
569     raw.is_data = True
570
571     return raw