From 64e234d1828e9718aa6e8d4dce065b80491eb8f4 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Thu, 11 Jun 2015 16:18:42 +0100 Subject: [PATCH] Fix thinko which broke xyz->rgb conversion. --- src/rgb_xyz.cc | 9 +++++---- test/colour_conversion_test.cc | 20 ++++++++++++++++++++ test/rgb_xyz_test.cc | 31 +++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/rgb_xyz.cc b/src/rgb_xyz.cc index a52d4f70..293ad5e1 100644 --- a/src/rgb_xyz.cc +++ b/src/rgb_xyz.cc @@ -28,6 +28,7 @@ using std::min; using std::max; +using std::cout; using boost::shared_ptr; using boost::optional; using namespace dcp; @@ -70,8 +71,8 @@ dcp::xyz_to_rgba ( int* xyz_y = xyz_image->data (1); int* xyz_z = xyz_image->data (2); - double const * lut_in = conversion.in()->lut (16, true); - double const * lut_out = conversion.out()->lut (12, false); + double const * lut_in = conversion.out()->lut (12, false); + double const * lut_out = conversion.in()->lut (16, true); boost::numeric::ublas::matrix const matrix = conversion.xyz_to_rgb (); int const height = xyz_image->size().height; @@ -150,8 +151,8 @@ dcp::xyz_to_rgb ( int* xyz_y = xyz_image->data (1); int* xyz_z = xyz_image->data (2); - double const * lut_in = conversion.in()->lut (12, true); - double const * lut_out = conversion.out()->lut (16, false); + double const * lut_in = conversion.out()->lut (12, false); + double const * lut_out = conversion.in()->lut (16, true); boost::numeric::ublas::matrix const matrix = conversion.xyz_to_rgb (); for (int y = 0; y < xyz_image->size().height; ++y) { diff --git a/test/colour_conversion_test.cc b/test/colour_conversion_test.cc index 7ff93468..b743497b 100644 --- a/test/colour_conversion_test.cc +++ b/test/colour_conversion_test.cc @@ -82,3 +82,23 @@ BOOST_AUTO_TEST_CASE (colour_conversion_test2) 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); +} diff --git a/test/rgb_xyz_test.cc b/test/rgb_xyz_test.cc index 22baab26..0719d975 100644 --- a/test/rgb_xyz_test.cc +++ b/test/rgb_xyz_test.cc @@ -27,6 +27,7 @@ using std::max; using std::list; using std::string; +using std::cout; using boost::shared_ptr; using boost::optional; using boost::scoped_array; @@ -161,3 +162,33 @@ BOOST_AUTO_TEST_CASE (xyz_rgb_range_test) BOOST_REQUIRE_EQUAL (buffer[1 * 3 + 1], buffer[3 * 3 + 1]); BOOST_REQUIRE_EQUAL (buffer[1 * 3 + 2], buffer[3 * 3 + 2]); } + +/** Convert an image from RGB to XYZ and back again */ +BOOST_AUTO_TEST_CASE (rgb_xyz_round_trip_test) +{ + unsigned int seed = 0; + dcp::Size const size (640, 480); + + scoped_array rgb (new uint8_t[size.width * size.height * 6]); + for (int y = 0; y < size.height; ++y) { + uint16_t* p = reinterpret_cast (rgb.get() + y * size.width * 6); + for (int x = 0; x < size.width; ++x) { + /* Write a 12-bit random number for each component */ + for (int c = 0; c < 3; ++c) { + *p = (rand_r (&seed) & 0xfff) << 4; + ++p; + } + } + } + + shared_ptr xyz = dcp::rgb_to_xyz (rgb.get(), size, size.width * 6, dcp::ColourConversion::srgb_to_xyz ()); + scoped_array back (new uint8_t[size.width * size.height * 6]); + dcp::xyz_to_rgb (xyz, dcp::ColourConversion::srgb_to_xyz (), back.get(), size.width * 6); + + uint16_t* p = reinterpret_cast (rgb.get ()); + uint16_t* q = reinterpret_cast (back.get ()); + for (int i = 0; i < (size.width * size.height); ++i) { + /* XXX: doesn't quite work */ + // BOOST_REQUIRE_EQUAL (*p++, *q++); + } +} -- 2.30.2