/
rimm_romm_rgb.py
493 lines (389 loc) · 14.7 KB
/
rimm_romm_rgb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# -*- coding: utf-8 -*-
"""
RIMM, ROMM and ERIMM Encodings
==============================
Defines the *RIMM, ROMM and ERIMM* encodings opto-electrical transfer functions
(OETF / OECF) and electro-optical transfer functions (EOTF / EOCF):
- :func:`colour.models.cctf_encoding_ROMMRGB`
- :func:`colour.models.cctf_decoding_ROMMRGB`
- :func:`colour.models.cctf_encoding_ProPhotoRGB`
- :func:`colour.models.cctf_decoding_ProPhotoRGB`
- :func:`colour.models.cctf_encoding_RIMMRGB`
- :func:`colour.models.cctf_decoding_RIMMRGB`
- :func:`colour.models.log_encoding_ERIMMRGB`
- :func:`colour.models.log_decoding_ERIMMRGB`
References
----------
- :cite:`ANSI2003a` : ANSI. (2003). Specification of ROMM RGB (pp. 1-2).
http://www.color.org/ROMMRGB.pdf
- :cite:`Spaulding2000b` : Spaulding, K. E., Woolfe, G. J., & Giorgianni, E.
J. (2000). Reference Input/Output Medium Metric RGB Color Encodings
(RIMM/ROMM RGB) (pp. 1-8). http://www.photo-lovers.org/pdf/color/romm.pdf
"""
import numpy as np
from colour.algebra import spow
from colour.utilities import (as_float, as_int, domain_range_scale,
from_range_1, to_domain_1)
__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013-2021 - Colour Developers'
__license__ = 'New BSD License - https://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = 'colour-developers@colour-science.org'
__status__ = 'Production'
__all__ = [
'cctf_encoding_ROMMRGB', 'cctf_decoding_ROMMRGB',
'cctf_encoding_ProPhotoRGB', 'cctf_decoding_ProPhotoRGB',
'cctf_encoding_RIMMRGB', 'cctf_decoding_RIMMRGB', 'log_encoding_ERIMMRGB',
'log_decoding_ERIMMRGB'
]
def cctf_encoding_ROMMRGB(X, bit_depth=8, out_int=False):
"""
Defines the *ROMM RGB* encoding colour component transfer function
(Encoding CCTF).
Parameters
----------
X : numeric or array_like
Linear data :math:`X_{ROMM}`.
bit_depth : int, optional
Bit depth used for conversion.
out_int : bool, optional
Whether to return value as integer code value or float equivalent of a
code value at a given bit depth.
Returns
-------
numeric or ndarray
Non-linear data :math:`X'_{ROMM}`.
Notes
-----
+----------------+-----------------------+---------------+
| **Domain \\*** | **Scale - Reference** | **Scale - 1** |
+================+=======================+===============+
| ``X`` | [0, 1] | [0, 1] |
+----------------+-----------------------+---------------+
+----------------+-----------------------+---------------+
| **Range \\*** | **Scale - Reference** | **Scale - 1** |
+================+=======================+===============+
| ``X_p`` | [0, 1] | [0, 1] |
+----------------+-----------------------+---------------+
\\* This definition has an output integer switch, thus the domain-range
scale information is only given for the floating point mode.
References
----------
:cite:`ANSI2003a`, :cite:`Spaulding2000b`
Examples
--------
>>> cctf_encoding_ROMMRGB(0.18) # doctest: +ELLIPSIS
0.3857114...
>>> cctf_encoding_ROMMRGB(0.18, out_int=True)
98
"""
X = to_domain_1(X)
I_max = 2 ** bit_depth - 1
E_t = 16 ** (1.8 / (1 - 1.8))
X_p = np.where(X < E_t, X * 16 * I_max, spow(X, 1 / 1.8) * I_max)
if out_int:
return as_int(np.round(X_p))
else:
return as_float(from_range_1(X_p / I_max))
def cctf_decoding_ROMMRGB(X_p, bit_depth=8, in_int=False):
"""
Defines the *ROMM RGB* decoding colour component transfer function
(Encoding CCTF).
Parameters
----------
X_p : numeric or array_like
Non-linear data :math:`X'_{ROMM}`.
bit_depth : int, optional
Bit depth used for conversion.
in_int : bool, optional
Whether to treat the input value as integer code value or float
equivalent of a code value at a given bit depth.
Returns
-------
numeric or ndarray
Linear data :math:`X_{ROMM}`.
Notes
-----
+----------------+-----------------------+---------------+
| **Domain \\*** | **Scale - Reference** | **Scale - 1** |
+================+=======================+===============+
| ``X_p`` | [0, 1] | [0, 1] |
+----------------+-----------------------+---------------+
+----------------+-----------------------+---------------+
| **Range \\*** | **Scale - Reference** | **Scale - 1** |
+================+=======================+===============+
| ``X`` | [0, 1] | [0, 1] |
+----------------+-----------------------+---------------+
\\* This definition has an input integer switch, thus the domain-range
scale information is only given for the floating point mode.
References
----------
:cite:`ANSI2003a`, :cite:`Spaulding2000b`
Examples
--------
>>> cctf_decoding_ROMMRGB(0.385711424751138) # doctest: +ELLIPSIS
0.1...
>>> cctf_decoding_ROMMRGB(98, in_int=True) # doctest: +ELLIPSIS
0.1...
"""
X_p = to_domain_1(X_p)
I_max = 2 ** bit_depth - 1
if not in_int:
X_p = X_p * I_max
E_t = 16 ** (1.8 / (1 - 1.8))
X = np.where(
X_p < 16 * E_t * I_max,
X_p / (16 * I_max),
spow(X_p / I_max, 1.8),
)
return as_float(from_range_1(X))
cctf_encoding_ProPhotoRGB = cctf_encoding_ROMMRGB
cctf_encoding_ProPhotoRGB.__doc__ = cctf_encoding_ProPhotoRGB.__doc__.replace(
'*ROMM RGB*', '*ProPhoto RGB*')
cctf_decoding_ProPhotoRGB = cctf_decoding_ROMMRGB
cctf_decoding_ProPhotoRGB.__doc__ = cctf_decoding_ROMMRGB.__doc__.replace(
'*ROMM RGB*', '*ProPhoto RGB*')
def cctf_encoding_RIMMRGB(X, bit_depth=8, out_int=False, E_clip=2.0):
"""
Defines the *RIMM RGB* encoding colour component transfer function
(Encoding CCTF).
*RIMM RGB* encoding non-linearity is based on that specified by
*Recommendation ITU-R BT.709-6*.
Parameters
----------
X : numeric or array_like
Linear data :math:`X_{RIMM}`.
bit_depth : int, optional
Bit depth used for conversion.
out_int : bool, optional
Whether to return value as integer code value or float equivalent of a
code value at a given bit depth.
E_clip : numeric, optional
Maximum exposure level.
Returns
-------
numeric or ndarray
Non-linear data :math:`X'_{RIMM}`.
Notes
-----
+----------------+-----------------------+---------------+
| **Domain \\*** | **Scale - Reference** | **Scale - 1** |
+================+=======================+===============+
| ``X`` | [0, 1] | [0, 1] |
+----------------+-----------------------+---------------+
+----------------+-----------------------+---------------+
| **Range \\*** | **Scale - Reference** | **Scale - 1** |
+================+=======================+===============+
| ``X_p`` | [0, 1] | [0, 1] |
+----------------+-----------------------+---------------+
\\* This definition has an output integer switch, thus the domain-range
scale information is only given for the floating point mode.
References
----------
:cite:`Spaulding2000b`
Examples
--------
>>> cctf_encoding_RIMMRGB(0.18) # doctest: +ELLIPSIS
0.2916737...
>>> cctf_encoding_RIMMRGB(0.18, out_int=True)
74
"""
X = to_domain_1(X)
I_max = 2 ** bit_depth - 1
V_clip = 1.099 * spow(E_clip, 0.45) - 0.099
q = I_max / V_clip
X_p = q * np.select([X < 0.0, X < 0.018, X >= 0.018, X > E_clip],
[0, 4.5 * X, 1.099 * spow(X, 0.45) - 0.099, I_max])
if out_int:
return as_int(np.round(X_p))
else:
return as_float(from_range_1(X_p / I_max))
def cctf_decoding_RIMMRGB(X_p, bit_depth=8, in_int=False, E_clip=2.0):
"""
Defines the *RIMM RGB* decoding colour component transfer function
(Encoding CCTF).
Parameters
----------
X_p : numeric or array_like
Non-linear data :math:`X'_{RIMM}`.
bit_depth : int, optional
Bit depth used for conversion.
in_int : bool, optional
Whether to treat the input value as integer code value or float
equivalent of a code value at a given bit depth.
E_clip : numeric, optional
Maximum exposure level.
Returns
-------
numeric or ndarray
Linear data :math:`X_{RIMM}`.
Notes
-----
+----------------+-----------------------+---------------+
| **Domain \\*** | **Scale - Reference** | **Scale - 1** |
+================+=======================+===============+
| ``X_p`` | [0, 1] | [0, 1] |
+----------------+-----------------------+---------------+
+----------------+-----------------------+---------------+
| **Range \\*** | **Scale - Reference** | **Scale - 1** |
+================+=======================+===============+
| ``X`` | [0, 1] | [0, 1] |
+----------------+-----------------------+---------------+
\\* This definition has an input integer switch, thus the domain-range
scale information is only given for the floating point mode.
References
----------
:cite:`Spaulding2000b`
Examples
--------
>>> cctf_decoding_RIMMRGB(0.291673732475746) # doctest: +ELLIPSIS
0.1...
>>> cctf_decoding_RIMMRGB(74, in_int=True) # doctest: +ELLIPSIS
0.1...
"""
X_p = to_domain_1(X_p)
I_max = 2 ** bit_depth - 1
if not in_int:
X_p = X_p * I_max
V_clip = 1.099 * spow(E_clip, 0.45) - 0.099
m = V_clip * X_p / I_max
with domain_range_scale('ignore'):
X = np.where(
X_p / I_max < cctf_encoding_RIMMRGB(
0.018, bit_depth, E_clip=E_clip),
m / 4.5,
spow((m + 0.099) / 1.099, 1 / 0.45),
)
return as_float(from_range_1(X))
def log_encoding_ERIMMRGB(X,
bit_depth=8,
out_int=False,
E_min=0.001,
E_clip=316.2):
"""
Defines the *ERIMM RGB* log encoding curve / opto-electronic transfer
function (OETF / OECF).
Parameters
----------
X : numeric or array_like
Linear data :math:`X_{ERIMM}`.
bit_depth : int, optional
Bit depth used for conversion.
out_int : bool, optional
Whether to return value as integer code value or float equivalent of a
code value at a given bit depth.
E_min : numeric, optional
Minimum exposure limit.
E_clip : numeric, optional
Maximum exposure limit.
Returns
-------
numeric or ndarray
Non-linear data :math:`X'_{ERIMM}`.
Notes
-----
+----------------+-----------------------+---------------+
| **Domain \\*** | **Scale - Reference** | **Scale - 1** |
+================+=======================+===============+
| ``X`` | [0, 1] | [0, 1] |
+----------------+-----------------------+---------------+
+----------------+-----------------------+---------------+
| **Range \\*** | **Scale - Reference** | **Scale - 1** |
+================+=======================+===============+
| ``X_p`` | [0, 1] | [0, 1] |
+----------------+-----------------------+---------------+
\\* This definition has an output integer switch, thus the domain-range
scale information is only given for the floating point mode.
References
----------
:cite:`Spaulding2000b`
Examples
--------
>>> log_encoding_ERIMMRGB(0.18) # doctest: +ELLIPSIS
0.4100523...
>>> log_encoding_ERIMMRGB(0.18, out_int=True)
105
"""
X = to_domain_1(X)
I_max = 2 ** bit_depth - 1
E_t = np.exp(1) * E_min
X_p = np.select([
X < 0.0,
X <= E_t,
X > E_t,
X > E_clip,
], [
0,
I_max * ((np.log(E_t) - np.log(E_min)) /
(np.log(E_clip) - np.log(E_min))) * (X / E_t),
I_max * (
(np.log(X) - np.log(E_min)) / (np.log(E_clip) - np.log(E_min))),
I_max,
])
if out_int:
return as_int(np.round(X_p))
else:
return as_float(from_range_1(X_p / I_max))
def log_decoding_ERIMMRGB(X_p,
bit_depth=8,
in_int=False,
E_min=0.001,
E_clip=316.2):
"""
Defines the *ERIMM RGB* log decoding curve / electro-optical transfer
function (EOTF / EOCF).
Parameters
----------
X_p : numeric or array_like
Non-linear data :math:`X'_{ERIMM}`.
bit_depth : int, optional
Bit depth used for conversion.
in_int : bool, optional
Whether to treat the input value as integer code value or float
equivalent of a code value at a given bit depth.
E_min : numeric, optional
Minimum exposure limit.
E_clip : numeric, optional
Maximum exposure limit.
Returns
-------
numeric or ndarray
Linear data :math:`X_{ERIMM}`.
Notes
-----
+----------------+-----------------------+---------------+
| **Domain \\*** | **Scale - Reference** | **Scale - 1** |
+================+=======================+===============+
| ``X_p`` | [0, 1] | [0, 1] |
+----------------+-----------------------+---------------+
+----------------+-----------------------+---------------+
| **Range \\*** | **Scale - Reference** | **Scale - 1** |
+================+=======================+===============+
| ``X`` | [0, 1] | [0, 1] |
+----------------+-----------------------+---------------+
\\* This definition has an input integer switch, thus the domain-range
scale information is only given for the floating point mode.
References
----------
:cite:`Spaulding2000b`
Examples
--------
>>> log_decoding_ERIMMRGB(0.410052389492129) # doctest: +ELLIPSIS
0.1...
>>> log_decoding_ERIMMRGB(105, in_int=True) # doctest: +ELLIPSIS
0.1...
"""
X_p = to_domain_1(X_p)
I_max = 2 ** bit_depth - 1
if not in_int:
X_p = X_p * I_max
E_t = np.exp(1) * E_min
X = np.where(
X_p <= I_max * (
(np.log(E_t) - np.log(E_min)) / (np.log(E_clip) - np.log(E_min))),
((np.log(E_clip) - np.log(E_min)) / (np.log(E_t) - np.log(E_min))) * (
(X_p * E_t) / I_max),
np.exp((X_p / I_max) * (np.log(E_clip) - np.log(E_min)) +
np.log(E_min)),
)
return as_float(from_range_1(X))