colour-science / colour

Colour Science for Python
https://www.colour-science.org
BSD 3-Clause "New" or "Revised" License
2.14k stars 263 forks source link

[BUG]: Possible incorrect value when using YCbCr_to_RGB() #1251

Closed mbechard closed 8 months ago

mbechard commented 8 months ago

Description

The below code snippet outputs the values array([1023, 744, 940], dtype=int64)

Why is the value of R 1023 and not in the legal range of [64,940]?

Code for Reproduction

import numpy as np
import colour as cs

y = np.array([940, 512, 940])
cs.models.rgb.ycbcr.YCbCr_to_RGB(y, in_bits=10, out_bits=10, in_legal=True, in_int=True, out_int=True, out_legal=True)

Exception Message

No response

Environment Information

No response

KelSolaar commented 8 months ago

Hi @mbechard,

A test you could do is using the float output, doing so:

print(cs.models.rgb.ycbcr.YCbCr_to_RGB(np.array([940, 512, 940]), in_bits=10, out_bits=10, in_legal=True, in_int=True, out_int=False, out_legal=True))

Yields:

[ 1.56301998  0.72738521  0.91886608]

Which is well outside 1. We try really hard to never clip anything in Colour so that inversion is always possible but the side effect is that there is no guard rail for such issues!

What you could do to see the maximum possible values for the corner of the RGB cube is that:

x = np.array([
    [0, 0, 0],
    [1023, 0, 0],
    [0, 1023, 0],
    [0, 0, 1023],
    [1023, 1023, 0],
    [0, 1023, 1023],
    [1023, 0, 1023],
    [1023, 1023, 1023],
])

y = cs.models.rgb.ycbcr.RGB_to_YCbCr(x, in_bits=10, out_bits=10, in_legal=False, in_int=True, out_int=True, out_legal=True)
print(y)

z = cs.models.rgb.ycbcr.YCbCr_to_RGB(y, in_bits=10, out_bits=10, in_legal=True, in_int=True, out_int=True, out_legal=False)
print(z)

np.testing.assert_allclose(x, z, atol=1)
[[ 64 512 512]
 [250 409 960]
 [691 167 105]
 [127 960 471]
 [877  64 553]
 [754 615  64]
 [313 857 919]
 [940 512 512]]
[[   0    0    0]
 [1023    0    0]
 [   0 1023    1]
 [   0    0 1023]
 [1023 1023    0]
 [   0 1023 1023]
 [1023    0 1022]
 [1023 1023 1023]]

I will let @nick-shaw chime in to provide more information if necessary!

KelSolaar commented 8 months ago

Oh and btw, unrelated, your team is doing an outstanding work on TouchDesigner. Fantastic piece of software!

mbechard commented 8 months ago

Ah thanks, I think I got turned around comparing various color space conversion tools. I think this isn't a bug then. Thanks for the kind words about TouchDesigner!