Fix default mutable arguments.
[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(name='matrix_plus_transfer',
162                                            transfer_function_name='transfer_function',
163                                            transfer_function=lambda x: x,
164                                            lut_directory='/tmp',
165                                            lut_resolution_1d=1024,
166                                            from_reference_values=None,
167                                            to_reference_values=None,
168                                            aliases=None):
169     """
170     Object description.
171
172     Parameters
173     ----------
174     parameter : type
175         Parameter description.
176
177     Returns
178     -------
179     type
180          Return value description.
181     """
182
183     if from_reference_values is None:
184         from_reference_values = []
185
186     if to_reference_values is None:
187         to_reference_values = []
188
189     if aliases is None:
190         aliases = []
191
192     cs = ColorSpace(name)
193     cs.description = 'The %s color space' % name
194     cs.aliases = aliases
195     cs.equality_group = name
196     cs.family = 'Utility'
197     cs.is_data = False
198
199     # A linear space needs allocation variables
200     cs.allocation_type = ocio.Constants.ALLOCATION_UNIFORM
201     cs.allocation_vars = [0, 1]
202
203     # Sample the transfer function
204     data = array.array('f', '\0' * lut_resolution_1d * 4)
205     for c in range(lut_resolution_1d):
206         data[c] = transfer_function(c / (lut_resolution_1d - 1))
207
208     # Write the sampled data to a LUT
209     lut = '%s_to_linear.spi1d' % transfer_function_name
210     genlut.write_SPI_1d(
211         os.path.join(lut_directory, lut),
212         0,
213         1,
214         data,
215         lut_resolution_1d,
216         1)
217
218     # Create the 'to_reference' transforms
219     cs.to_reference_transforms = []
220     if to_reference_values:
221         cs.to_reference_transforms.append({
222             'type': 'lutFile',
223             'path': lut,
224             'interpolation': 'linear',
225             'direction': 'forward'})
226
227         for matrix in to_reference_values:
228             cs.to_reference_transforms.append({
229                 'type': 'matrix',
230                 'matrix': mat44_from_mat33(matrix),
231                 'direction': 'forward'})
232
233     # Create the 'from_reference' transforms
234     cs.from_reference_transforms = []
235     if from_reference_values:
236         for matrix in from_reference_values:
237             cs.from_reference_transforms.append({
238                 'type': 'matrix',
239                 'matrix': mat44_from_mat33(matrix),
240                 'direction': 'forward'})
241
242         cs.from_reference_transforms.append({
243             'type': 'lutFile',
244             'path': lut,
245             'interpolation': 'linear',
246             'direction': 'inverse'})
247
248     return cs
249
250
251 # create_matrix_plus_transfer_colorspace
252
253 # Transfer functions for standard color spaces
254 def transfer_function_sRGB_to_linear(v):
255     a = 1.055
256     b = 0.04045
257     d = 12.92
258     g = 2.4
259
260     if v < b:
261         return v / d
262     return pow(((v + (a - 1)) / a), g)
263
264
265 def transfer_function_Rec709_to_linear(v):
266     a = 1.099
267     b = 0.018
268     d = 4.5
269     g = (1.0 / 0.45)
270
271     if v < b * d:
272         return v / d
273
274     return pow(((v + (a - 1)) / a), g)
275
276
277 def transfer_function_Rec2020_10bit_to_linear(v):
278     a = 1.099
279     b = 0.018
280     d = 4.5
281     g = (1.0 / 0.45)
282
283     if v < b * d:
284         return v / d
285
286     return pow(((v + (a - 1)) / a), g)
287
288
289 def transfer_function_Rec2020_12bit_to_linear(v):
290     a = 1.0993
291     b = 0.0181
292     d = 4.5
293     g = (1.0 / 0.45)
294
295     if v < b * d:
296         return v / d
297
298     return pow(((v + (a - 1)) / a), g)
299
300
301 def transfer_function_Rec1886_to_linear(v):
302     g = 2.4
303     Lw = 1
304     Lb = 0
305
306     # Ignoring legal to full scaling for now
307     # v = (1023.0*v - 64.0)/876.0
308
309     t = pow(Lw, 1.0 / g) - pow(Lb, 1.0 / g)
310     a = pow(t, g)
311     b = pow(Lb, 1.0 / g) / t
312
313     return a * pow(max((v + b), 0.0), g)
314
315
316 def create_colorspaces(lut_directory,
317                        lut_resolution_1d,
318                        lut_resolution_3d):
319     """
320     Generates the colorspace conversions.
321
322     Parameters
323     ----------
324     parameter : type
325         Parameter description.
326
327     Returns
328     -------
329     type
330          Return value description.
331     """
332
333     colorspaces = []
334
335     #
336     # XYZ
337     #
338     cs = create_matrix_colorspace('XYZ-D60',
339                                   to_reference_values=[aces.ACES_XYZ_TO_AP0],
340                                   from_reference_values=[aces.ACES_AP0_TO_XYZ],
341                                   aliases=['lin_xyz_d60'])
342     colorspaces.append(cs)
343
344     #
345     # P3-D60
346     #
347     # *ACES* to *Linear*, *P3D60* primaries.
348     XYZ_to_P3D60 = [2.4027414142, -0.8974841639, -0.3880533700,
349                     -0.8325796487, 1.7692317536, 0.0237127115,
350                     0.0388233815, -0.0824996856, 1.0363685997]
351
352     cs = create_matrix_colorspace(
353         'Linear - P3-D60',
354         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_P3D60],
355         aliases=['lin_p3d60'])
356     colorspaces.append(cs)
357
358     #
359     # P3-DCI
360     #
361     # *ACES* to *Linear*, *P3DCI* primaries.
362     XYZ_to_P3DCI = [2.7253940305, -1.0180030062, -0.4401631952,
363                     -0.7951680258, 1.6897320548, 0.0226471906,
364                     0.0412418914, -0.0876390192, 1.1009293786]
365
366     cs = create_matrix_colorspace(
367         'Linear - P3-DCI',
368         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_P3DCI],
369         aliases=['lin_p3dci'])
370     colorspaces.append(cs)
371
372     #
373     # sRGB
374     #
375     # *ACES* to *Linear*, *Rec. 709* primaries.
376     # sRGB and Rec 709 use the same gamut
377     XYZ_to_Rec709 = [3.2409699419, -1.5373831776, -0.4986107603,
378                      -0.9692436363, 1.8759675015, 0.0415550574,
379                      0.0556300797, -0.2039769589, 1.0569715142]
380
381     cs = create_matrix_colorspace(
382         'Linear - sRGB',
383         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
384         aliases=['lin_srgb'])
385     colorspaces.append(cs)
386
387     # *Linear* to *sRGB* Transfer Function*
388     cs = create_transfer_colorspace(
389         'Curve - sRGB',
390         'sRGB',
391         transfer_function_sRGB_to_linear,
392         lut_directory,
393         lut_resolution_1d,
394         aliases=['crv_srgb'])
395     colorspaces.append(cs)
396
397     # *ACES* to *sRGB* Primaries + Transfer Function*
398     cs = create_matrix_plus_transfer_colorspace(
399         'sRGB',
400         'sRGB',
401         transfer_function_sRGB_to_linear,
402         lut_directory,
403         lut_resolution_1d,
404         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
405         aliases=['srgb'])
406     colorspaces.append(cs)
407
408     #
409     # Rec 709
410     #
411     # *ACES* to *Linear*, *Rec. 709* primaries.
412     XYZ_to_Rec709 = [3.2409699419, -1.5373831776, -0.4986107603,
413                      -0.9692436363, 1.8759675015, 0.0415550574,
414                      0.0556300797, -0.2039769589, 1.0569715142]
415
416     cs = create_matrix_colorspace(
417         'Linear - Rec.709',
418         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
419         aliases=['lin_rec709'])
420     colorspaces.append(cs)
421
422     # *Linear* to *Rec. 709* Transfer Function*
423     cs = create_transfer_colorspace(
424         'Curve - Rec.709',
425         'rec709',
426         transfer_function_Rec709_to_linear,
427         lut_directory,
428         lut_resolution_1d,
429         aliases=['crv_rec709'])
430     colorspaces.append(cs)
431
432     # *ACES* to *Rec. 709* Primaries + Transfer Function*
433     cs = create_matrix_plus_transfer_colorspace(
434         'Rec.709 - Camera',
435         'rec709',
436         transfer_function_Rec709_to_linear,
437         lut_directory,
438         lut_resolution_1d,
439         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
440         aliases=['rec709_camera'])
441     colorspaces.append(cs)
442
443     #
444     # Rec 2020
445     #
446     # *ACES* to *Linear*, *Rec. 2020* primaries.
447     XYZ_to_Rec2020 = [1.7166511880, -0.3556707838, -0.2533662814,
448                       -0.6666843518, 1.6164812366, 0.0157685458,
449                       0.0176398574, -0.0427706133, 0.9421031212]
450
451     cs = create_matrix_colorspace(
452         'Linear - Rec.2020',
453         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec2020],
454         aliases=['lin_rec2020'])
455     colorspaces.append(cs)
456
457     # *Linear* to *Rec. 2020 10 bit* Transfer Function*
458     cs = create_transfer_colorspace(
459         'Curve - Rec.2020',
460         'rec2020',
461         transfer_function_Rec2020_10bit_to_linear,
462         lut_directory,
463         lut_resolution_1d,
464         aliases=['crv_rec2020'])
465     colorspaces.append(cs)
466
467     # *ACES* to *Rec. 2020 10 bit* Primaries + Transfer Function*
468     cs = create_matrix_plus_transfer_colorspace(
469         'Rec.2020 - Camera',
470         'rec2020',
471         transfer_function_Rec2020_10bit_to_linear,
472         lut_directory,
473         lut_resolution_1d,
474         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec2020],
475         aliases=['rec2020_camera'])
476     colorspaces.append(cs)
477
478     #
479     # Rec 1886
480     #
481
482     # *Linear* to *Rec.1886* Transfer Function*
483     cs = create_transfer_colorspace(
484         'Curve - Rec.1886',
485         'rec1886',
486         transfer_function_Rec1886_to_linear,
487         lut_directory,
488         lut_resolution_1d,
489         aliases=['crv_rec1886'])
490     colorspaces.append(cs)
491
492     # *ACES* to *sRGB* Primaries + Transfer Function*
493     cs = create_matrix_plus_transfer_colorspace(
494         'Rec.709 - Display',
495         'rec1886',
496         transfer_function_Rec1886_to_linear,
497         lut_directory,
498         lut_resolution_1d,
499         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
500         aliases=['rec709_display'])
501     colorspaces.append(cs)
502
503     # *ACES* to *sRGB* Primaries + Transfer Function*
504     cs = create_matrix_plus_transfer_colorspace(
505         'Rec.2020 - Display',
506         'rec1886',
507         transfer_function_Rec1886_to_linear,
508         lut_directory,
509         lut_resolution_1d,
510         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec2020],
511         aliases=['rec2020_display'])
512     colorspaces.append(cs)
513
514     #
515     # ProPhoto
516     #
517     # *ACES* to *Linear*, *Pro Photo* primaries.
518     AP0_to_RIMM = [1.2412367771, -0.1685692287, -0.0726675484,
519                    0.0061203066, 1.083151174, -0.0892714806,
520                    -0.0032853314, 0.0099796402, 0.9933056912]
521
522     cs = create_matrix_colorspace(
523         'Linear - RIMM ROMM (ProPhoto)',
524         from_reference_values=[AP0_to_RIMM],
525         aliases=['lin_prophoto', 'lin_rimm'])
526     colorspaces.append(cs)
527
528     #
529     # Adobe RGB
530     #
531     # *ACES* to *Linear*, *Adobe RGB* primaries.
532     AP0_to_ADOBERGB = [1.7245603168, -0.4199935942, -0.3045667227,
533                        -0.2764799142, 1.3727190877, -0.0962391734,
534                        -0.0261255258, -0.0901747807, 1.1163003065]
535
536     cs = create_matrix_colorspace(
537         'Linear - Adobe RGB',
538         from_reference_values=[AP0_to_ADOBERGB],
539         aliases=['lin_adobergb'])
540     colorspaces.append(cs)
541
542     #
543     # Adobe Wide Gamut RGB
544     #
545
546     # *ACES* to *Linear*, *Adobe Wide Gamut RGB* primaries.
547     AP0_to_ADOBERGB = [1.3809814778, -0.1158594573, -0.2651220205,
548                        0.0057015535, 1.0402949043, -0.0459964578,
549                        -0.0038908746, -0.0597091815, 1.0636000561]
550
551     cs = create_matrix_colorspace(
552         'Linear - Adobe Wide Gamut RGB',
553         from_reference_values=[AP0_to_ADOBERGB],
554         aliases=['lin_adobewidegamutrgb'])
555     colorspaces.append(cs)
556
557     return colorspaces
558
559
560 def create_raw():
561     # *Raw* utility space
562     name = 'Raw'
563     raw = ColorSpace(name)
564     raw.description = 'The %s color space' % name
565     raw.aliases = ['raw']
566     raw.equality_group = name
567     raw.family = 'Utility'
568     raw.is_data = True
569
570     return raw