X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=test%2Fcolour_conversion_test.cc;h=d9bf2e77bbd116c8fff4bf556ff57a75f1435778;hb=ff896d5f5ec20e1371b423bb746c32fa55cc126a;hp=7193a5efb46db5e4b13a66260d15a90d56f4f0a2;hpb=b47a54b85aee24795008463681cb31f4307ffe84;p=libdcp.git diff --git a/test/colour_conversion_test.cc b/test/colour_conversion_test.cc index 7193a5ef..d9bf2e77 100644 --- a/test/colour_conversion_test.cc +++ b/test/colour_conversion_test.cc @@ -28,9 +28,9 @@ using boost::shared_ptr; using namespace dcp; static void -check_gamma (shared_ptr tf, int bit_depth, float gamma) +check_gamma (shared_ptr tf, int bit_depth, bool inverse, float gamma) { - double const * lut = tf->lut (bit_depth); + double const * lut = tf->lut (bit_depth, inverse); int const count = rint (pow (2.0, bit_depth)); for (int i = 0; i < count; ++i) { @@ -39,9 +39,9 @@ check_gamma (shared_ptr tf, int bit_depth, float gamma) } static void -check_modified_gamma (shared_ptr tf, int bit_depth, double power, double threshold, double A, double B) +check_modified_gamma (shared_ptr tf, int bit_depth, bool inverse, double power, double threshold, double A, double B) { - double const * lut = tf->lut (bit_depth); + double const * lut = tf->lut (bit_depth, inverse); int const count = rint (pow (2.0, bit_depth)); for (int i = 0; i < count; ++i) { @@ -54,29 +54,89 @@ check_modified_gamma (shared_ptr tf, int bit_depth, doub } } +/** Check that the gamma correction LUTs are right for sRGB */ BOOST_AUTO_TEST_CASE (colour_conversion_test1) { ColourConversion cc = ColourConversion::srgb_to_xyz (); - check_modified_gamma (cc.in(), 8, 2.4, 0.04045, 0.055, 12.92); - check_modified_gamma (cc.in(), 12, 2.4, 0.04045, 0.055, 12.92); - check_modified_gamma (cc.in(), 16, 2.4, 0.04045, 0.055, 12.92); + check_modified_gamma (cc.in(), 8, false, 2.4, 0.04045, 0.055, 12.92); + check_modified_gamma (cc.in(), 12, false, 2.4, 0.04045, 0.055, 12.92); + check_modified_gamma (cc.in(), 16, false, 2.4, 0.04045, 0.055, 12.92); - check_gamma (cc.out(), 8, 1 / 2.6); - check_gamma (cc.out(), 12, 1 / 2.6); - check_gamma (cc.out(), 16, 1 / 2.6); + check_gamma (cc.out(), 8, true, 1 / 2.6); + check_gamma (cc.out(), 12, true, 1 / 2.6); + check_gamma (cc.out(), 16, true, 1 / 2.6); } +/** Check that the gamma correction LUTs are right for REC709 */ BOOST_AUTO_TEST_CASE (colour_conversion_test2) { ColourConversion cc = ColourConversion::rec709_to_xyz (); - check_modified_gamma (cc.in(), 8, 2.4, 0.081, 0.099, 4.5); - check_modified_gamma (cc.in(), 12, 2.4, 0.081, 0.099, 4.5); - check_modified_gamma (cc.in(), 16, 2.4, 0.081, 0.099, 4.5); + check_gamma (cc.in(), 8, false, 2.2); + check_gamma (cc.in(), 12, false, 2.2); + check_gamma (cc.in(), 16, false, 2.2); - check_gamma (cc.out(), 8, 1 / 2.6); - check_gamma (cc.out(), 12, 1 / 2.6); - check_gamma (cc.out(), 16, 1 / 2.6); + check_gamma (cc.out(), 8, true, 1 / 2.6); + check_gamma (cc.out(), 12, true, 1 / 2.6); + check_gamma (cc.out(), 16, true, 1 / 2.6); } +/** Check that the xyz_to_rgb matrix is the inverse of the rgb_to_xyz one */ +BOOST_AUTO_TEST_CASE (colour_conversion_matrix_test) +{ + ColourConversion c = ColourConversion::srgb_to_xyz (); + + boost::numeric::ublas::matrix A = c.rgb_to_xyz (); + boost::numeric::ublas::matrix B = c.xyz_to_rgb (); + + BOOST_CHECK_CLOSE (A(0, 0) * B(0, 0) + A(0, 1) * B(1, 0) + A(0, 2) * B(2, 0), 1, 0.1); + BOOST_CHECK (fabs (A(0, 0) * B(0, 1) + A(0, 1) * B(1, 1) + A(0, 2) * B(2, 1)) < 1e-6); + BOOST_CHECK (fabs (A(0, 0) * B(0, 2) + A(0, 1) * B(1, 2) + A(0, 2) * B(2, 2)) < 1e-6); + + BOOST_CHECK (fabs (A(1, 0) * B(0, 0) + A(1, 1) * B(1, 0) + A(1, 2) * B(2, 0)) < 1e-6); + BOOST_CHECK_CLOSE (A(1, 0) * B(0, 1) + A(1, 1) * B(1, 1) + A(1, 2) * B(2, 1), 1, 0.1); + BOOST_CHECK (fabs (A(1, 0) * B(0, 2) + A(1, 1) * B(1, 2) + A(1, 2) * B(2, 2)) < 1e-6); + + BOOST_CHECK (fabs (A(2, 0) * B(0, 0) + A(2, 1) * B(1, 0) + A(2, 2) * B(2, 0)) < 1e-6); + BOOST_CHECK (fabs (A(2, 0) * B(0, 1) + A(2, 1) * B(1, 1) + A(2, 2) * B(2, 1)) < 1e-6); + BOOST_CHECK_CLOSE (A(2, 0) * B(0, 2) + A(2, 1) * B(1, 2) + A(2, 2) * B(2, 2), 1, 0.1); +} + +BOOST_AUTO_TEST_CASE (colour_conversion_bradford_test) +{ + ColourConversion c = ColourConversion::srgb_to_xyz (); + + /* CIE "A" illuminant from http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html + un-normalised using a factor k where k = 1 / (1 + x + z) + */ + c.set_adjusted_white (Chromaticity (0.447576324, 0.407443172)); + + boost::numeric::ublas::matrix b = c.bradford (); + + /* Check the conversion matrix against the one quoted on brucelindbloom.com */ + BOOST_CHECK_CLOSE (b(0, 0), 1.2164557, 0.1); + BOOST_CHECK_CLOSE (b(0, 1), 0.1109905, 0.1); + BOOST_CHECK_CLOSE (b(0, 2), -0.1549325, 0.1); + BOOST_CHECK_CLOSE (b(1, 0), 0.1533326, 0.1); + BOOST_CHECK_CLOSE (b(1, 1), 0.9152313, 0.1); + BOOST_CHECK_CLOSE (b(1, 2), -0.0559953, 0.1); + BOOST_CHECK_CLOSE (b(2, 0), -0.0239469, 0.1); + BOOST_CHECK_CLOSE (b(2, 1), 0.0358984, 0.1); + BOOST_CHECK_CLOSE (b(2, 2), 0.3147529, 0.1); + + /* Same for CIE "B" illuminant */ + c.set_adjusted_white (Chromaticity (0.99072 * 0.351747305, 0.351747305)); + + b = c.bradford (); + + BOOST_CHECK_CLOSE (b(0, 0), 1.0641402, 0.1); + BOOST_CHECK_CLOSE (b(0, 1), 0.0325780, 0.1); + BOOST_CHECK_CLOSE (b(0, 2), -0.0489436, 0.1); + BOOST_CHECK_CLOSE (b(1, 0), 0.0446103, 0.1); + BOOST_CHECK_CLOSE (b(1, 1), 0.9766379, 0.1); + BOOST_CHECK_CLOSE (b(1, 2), -0.0174854, 0.1); + BOOST_CHECK_CLOSE (b(2, 0), -0.0078485, 0.1); + BOOST_CHECK_CLOSE (b(2, 1), 0.0119945, 0.1); + BOOST_CHECK_CLOSE (b(2, 2), 0.7785377, 0.1); +}