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