diff --git a/src/util/tests/u_ycbcr_test.cpp b/src/util/tests/u_ycbcr_test.cpp index e3b1a48292d..e4dc42c8a50 100644 --- a/src/util/tests/u_ycbcr_test.cpp +++ b/src/util/tests/u_ycbcr_test.cpp @@ -518,3 +518,60 @@ TEST(u_ycbcr_test, to_ycbcr_range_and_back) test_to_ycbcr_range_and_back_coeffs(util_ycbcr_bt709_coeffs); test_to_ycbcr_range_and_back_coeffs(util_ycbcr_bt2020_coeffs); } + +static void +test_narrow_ycbcr_to_narrow_rgb_coeffs(const float coeffs[3]) +{ + const unsigned bpc[3] = {8, 8, 8}; + + float in_range[3][2], out_range[3][2]; + util_get_narrow_range_coeffs(in_range, bpc); + util_get_narrow_range_rgb_coeffs(out_range, bpc); + + float mat[3][4]; + util_get_ycbcr_to_rgb_matrix(mat, coeffs); + util_ycbcr_adjust_from_range(mat, in_range); + util_ycbcr_adjust_to_range(mat, out_range); + + const struct { + uint8_t input[3]; + uint8_t expected[3]; + } test_data[] = { + { { 16, 128, 128 }, { 16, 16, 16 } }, + { { 235, 128, 128 }, { 235, 235, 235 } }, + { { 126, 128, 128 }, { 126, 126, 126 } }, + }; + + for (size_t i = 0; i < ARRAY_SIZE(test_data); ++i) { + float input[3] = { + test_data[i].input[0] / 255.0f, + test_data[i].input[1] / 255.0f, + test_data[i].input[2] / 255.0f, + }; + + float result[3]; + for (int c = 0; c < 3; ++c) { + result[c] = input[0] * mat[c][0] + + input[1] * mat[c][1] + + input[2] * mat[c][2] + + mat[c][3]; + } + + float expected[3] = { + test_data[i].expected[0] / 255.0f, + test_data[i].expected[1] / 255.0f, + test_data[i].expected[2] / 255.0f, + }; + + EXPECT_NEAR(expected[0], result[0], 1e-6); + EXPECT_NEAR(expected[1], result[1], 1e-6); + EXPECT_NEAR(expected[2], result[2], 1e-6); + } +} + +TEST(u_ycbcr_test, narrow_ycbcr_to_narrow_rgb) +{ + test_narrow_ycbcr_to_narrow_rgb_coeffs(util_ycbcr_bt601_coeffs); + test_narrow_ycbcr_to_narrow_rgb_coeffs(util_ycbcr_bt709_coeffs); + test_narrow_ycbcr_to_narrow_rgb_coeffs(util_ycbcr_bt2020_coeffs); +} diff --git a/src/util/u_ycbcr.h b/src/util/u_ycbcr.h index 448da7f12d0..0f3c52a110a 100644 --- a/src/util/u_ycbcr.h +++ b/src/util/u_ycbcr.h @@ -151,7 +151,7 @@ util_ycbcr_adjust_to_range(float mat[3][4], mat[i][0] = mat[i][0] * tmp; mat[i][1] = mat[i][1] * tmp; mat[i][2] = mat[i][2] * tmp; - mat[i][3] -= range[i][1] * tmp; + mat[i][3] = mat[i][3] * tmp - range[i][1] * tmp; } }