Remove unused locals.
[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     """
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