6e7c738fb7cec24e43bf0a0c44adb6bfc3fab1c9
[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     # Rec 709
355     #
356     # *ACES* to *Linear*, *Rec. 709* primaries.
357     XYZ_to_Rec709 = [3.2409699419, -1.5373831776, -0.4986107603,
358                      -0.9692436363, 1.8759675015, 0.0415550574,
359                      0.0556300797, -0.2039769589, 1.0569715142]
360
361     cs = create_matrix_colorspace(
362         'Linear - Rec.709',
363         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
364         aliases=["lin_rec709"])
365     colorspaces.append(cs)
366
367     # *Linear* to *Rec. 709* Transfer Function*
368     cs = create_transfer_colorspace(
369         'Curve - Rec.709',
370         'rec709',
371         transfer_function_Rec709_to_linear,
372         lut_directory,
373         lut_resolution_1d,
374         aliases=["crv_rec709"])
375     colorspaces.append(cs)
376
377     # *ACES* to *Rec. 709* Primaries + Transfer Function*
378     cs = create_matrix_plus_transfer_colorspace(
379         'Rec.709',
380         'rec709',
381         transfer_function_Rec709_to_linear,
382         lut_directory,
383         lut_resolution_1d,
384         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
385         aliases=["rec709"])
386     colorspaces.append(cs)
387
388     #
389     # sRGB
390     #
391     # *ACES* to *Linear*, *Rec. 709* primaries.
392     # sRGB and Rec 709 use the same gamut
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 - sRGB',
399         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
400         aliases=["lin_srgb"])
401     colorspaces.append(cs)
402
403     # *Linear* to *sRGB* Transfer Function*
404     cs = create_transfer_colorspace(
405         'Curve - sRGB',
406         'sRGB',
407         transfer_function_sRGB_to_linear,
408         lut_directory,
409         lut_resolution_1d,
410         aliases=["crv_srgb"])
411     colorspaces.append(cs)
412
413     # *ACES* to *sRGB* Primaries + Transfer Function*
414     cs = create_matrix_plus_transfer_colorspace(
415         'sRGB',
416         'sRGB',
417         transfer_function_sRGB_to_linear,
418         lut_directory,
419         lut_resolution_1d,
420         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec709],
421         aliases=["srgb"])
422     colorspaces.append(cs)
423
424     #
425     # Rec 1886
426     #
427
428     # *Linear* to *Rec.2020* Transfer Function*
429     cs = create_transfer_colorspace(
430         'Curve - Rec.1886',
431         'rec1886',
432         transfer_function_Rec1886_to_linear,
433         lut_directory,
434         lut_resolution_1d,
435         aliases=["crv_rec1886"])
436     colorspaces.append(cs)
437
438     #
439     # Rec 2020
440     #
441     # *ACES* to *Linear*, *Rec. 2020* primaries.
442     XYZ_to_Rec2020 = [1.7166511880, -0.3556707838, -0.2533662814,
443                       -0.6666843518, 1.6164812366, 0.0157685458,
444                       0.0176398574, -0.0427706133, 0.9421031212]
445
446     cs = create_matrix_colorspace(
447         'Linear - Rec.2020',
448         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec2020],
449         aliases=["lin_rec2020"])
450     colorspaces.append(cs)
451
452     # *Linear* to *Rec. 2020 10 bit* Transfer Function*
453     cs = create_transfer_colorspace(
454         'Curve - Rec.2020',
455         'rec2020',
456         transfer_function_Rec2020_10bit_to_linear,
457         lut_directory,
458         lut_resolution_1d,
459         aliases=["crv_rec2020"])
460     colorspaces.append(cs)
461
462     # *ACES* to *Rec. 2020 10 bit* Primaries + Transfer Function*
463     cs = create_matrix_plus_transfer_colorspace(
464         'Rec.2020',
465         'rec2020',
466         transfer_function_Rec2020_10bit_to_linear,
467         lut_directory,
468         lut_resolution_1d,
469         from_reference_values=[aces.ACES_AP0_TO_XYZ, XYZ_to_Rec2020],
470         aliases=["rec2020"])
471     colorspaces.append(cs)
472
473     #
474     # ProPhoto
475     #
476     # *ACES* to *Linear*, *Pro Photo* primaries.
477     AP0_to_RIMM = [1.2412367771, -0.1685692287, -0.0726675484,
478                    0.0061203066, 1.083151174, -0.0892714806,
479                    -0.0032853314, 0.0099796402, 0.9933056912]
480
481     cs = create_matrix_colorspace(
482         'Linear - RIMM ROMM (ProPhoto)',
483         from_reference_values=[AP0_to_RIMM],
484         aliases=["lin_prophoto", "lin_rimm"])
485     colorspaces.append(cs)
486
487     #
488     # Adobe RGB
489     #
490     # *ACES* to *Linear*, *Adobe RGB* primaries.
491     AP0_to_ADOBERGB = [1.7245603168, -0.4199935942, -0.3045667227,
492                        -0.2764799142, 1.3727190877, -0.0962391734,
493                        -0.0261255258, -0.0901747807, 1.1163003065]
494
495     cs = create_matrix_colorspace(
496         'Linear - Adobe RGB',
497         from_reference_values=[AP0_to_ADOBERGB],
498         aliases=["lin_adobergb"])
499     colorspaces.append(cs)
500
501     #
502     # Adobe Wide Gamut RGB
503     #
504
505     # *ACES* to *Linear*, *Adobe Wide Gamut RGB* primaries.
506     AP0_to_ADOBERGB = [1.3809814778, -0.1158594573, -0.2651220205,
507                        0.0057015535, 1.0402949043, -0.0459964578,
508                        -0.0038908746, -0.0597091815, 1.0636000561]
509
510     cs = create_matrix_colorspace(
511         'Linear - Adobe Wide Gamut RGB',
512         from_reference_values=[AP0_to_ADOBERGB],
513         aliases=["lin_adobewidegamutrgb"])
514     colorspaces.append(cs)
515
516     return colorspaces
517
518
519 def create_raw():
520     # *Raw* utility space
521     name = "Raw"
522     raw = ColorSpace(name)
523     raw.description = 'The %s color space' % name
524     raw.aliases = []
525     raw.equality_group = name
526     raw.family = 'Utility'
527     raw.is_data = True
528
529     return raw
530
531