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