diff options
| author | Carl Hetherington <cth@carlh.net> | 2015-01-03 21:33:30 +0000 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2015-01-03 21:33:30 +0000 |
| commit | 9655ed4740f08899698052f232256fcf7b77c58e (patch) | |
| tree | 195e8f30ec4c5dd5beeecfb371acf7a3ceaa7898 /test | |
| parent | 986bb0acba0aa73fc2ac0190a32e8fa8c6e636b4 (diff) | |
Various fixes to XYZ/RGB transforms.
Diffstat (limited to 'test')
| -rw-r--r-- | test/colour_conversion_test.cc | 22 | ||||
| -rw-r--r-- | test/image_test.cc | 4 | ||||
| -rw-r--r-- | test/rgb_xyz_test.cc | 129 | ||||
| -rw-r--r-- | test/wscript | 1 |
4 files changed, 143 insertions, 13 deletions
diff --git a/test/colour_conversion_test.cc b/test/colour_conversion_test.cc index 060148a6..aafd2cad 100644 --- a/test/colour_conversion_test.cc +++ b/test/colour_conversion_test.cc @@ -30,22 +30,22 @@ using namespace dcp; static void check_gamma (shared_ptr<const TransferFunction> tf, int bit_depth, float gamma) { - float const * lut = tf->lut (bit_depth); + double const * lut = tf->lut (bit_depth); int const count = pow (2, bit_depth); for (int i = 0; i < count; ++i) { - BOOST_CHECK_CLOSE (lut[i], pow (float(i) / (count - 1), gamma), 0.001); + BOOST_CHECK_CLOSE (lut[i], pow (double(i) / (count - 1), gamma), 0.001); } } static void -check_modified_gamma (shared_ptr<const TransferFunction> tf, int bit_depth, float power, float threshold, float A, float B) +check_modified_gamma (shared_ptr<const TransferFunction> tf, int bit_depth, double power, double threshold, double A, double B) { - float const * lut = tf->lut (bit_depth); + double const * lut = tf->lut (bit_depth); int const count = pow (2, bit_depth); for (int i = 0; i < count; ++i) { - float const x = float(i) / (count - 1); + double const x = double(i) / (count - 1); if (x > threshold) { BOOST_CHECK_CLOSE (lut[i], pow ((x + A) / (1 + A), power), 0.001); } else { @@ -62,9 +62,9 @@ BOOST_AUTO_TEST_CASE (colour_conversion_test1) 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_gamma (cc.out(), 8, 2.6); - check_gamma (cc.out(), 12, 2.6); - check_gamma (cc.out(), 16, 2.6); + check_gamma (cc.out(), 8, 1 / 2.6); + check_gamma (cc.out(), 12, 1 / 2.6); + check_gamma (cc.out(), 16, 1 / 2.6); } BOOST_AUTO_TEST_CASE (colour_conversion_test2) @@ -75,8 +75,8 @@ BOOST_AUTO_TEST_CASE (colour_conversion_test2) 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.out(), 8, 2.6); - check_gamma (cc.out(), 12, 2.6); - check_gamma (cc.out(), 16, 2.6); + check_gamma (cc.out(), 8, 1 / 2.6); + check_gamma (cc.out(), 12, 1 / 2.6); + check_gamma (cc.out(), 16, 1 / 2.6); } diff --git a/test/image_test.cc b/test/image_test.cc index 26f27499..6b024c1a 100644 --- a/test/image_test.cc +++ b/test/image_test.cc @@ -44,11 +44,11 @@ public: } - uint8_t** data () const { + uint16_t * const * data () const { return 0; } - int* stride () const { + int const * stride () const { return 0; } }; diff --git a/test/rgb_xyz_test.cc b/test/rgb_xyz_test.cc new file mode 100644 index 00000000..b33a0f4a --- /dev/null +++ b/test/rgb_xyz_test.cc @@ -0,0 +1,129 @@ +/* + Copyright (C) 2014 Carl Hetherington <cth@carlh.net> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include "image.h" +#include "rgb_xyz.h" +#include "xyz_frame.h" +#include "colour_conversion.h" +#include <boost/test/unit_test.hpp> + +using std::max; +using boost::shared_ptr; + +class SimpleImage : public dcp::Image +{ +public: + SimpleImage (dcp::Size size) + : Image (size) + { + /* 48bpp */ + _stride[0] = _size.width * 6; + _data[0] = new uint16_t[size.height * stride()[0] / 2]; + } + + ~SimpleImage () + { + delete[] _data[0]; + } + + uint16_t * const * data () const { + return _data; + } + + int const * stride () const { + return _stride; + } + +private: + uint16_t* _data[1]; + int _stride[1]; +}; + +/** Convert a test image from sRGB to XYZ and check that the transforms are right */ +BOOST_AUTO_TEST_CASE (rgb_xyz_test) +{ + unsigned int seed = 0; + dcp::Size const size (640, 480); + + shared_ptr<const dcp::Image> rgb (new SimpleImage (size)); + for (int y = 0; y < size.height; ++y) { + uint16_t* p = rgb->data()[0] + y * rgb->stride()[0] / 2; + 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<dcp::XYZFrame> xyz = dcp::rgb_to_xyz (rgb, dcp::ColourConversion::srgb_to_xyz); + + for (int y = 0; y < size.height; ++y) { + uint16_t* p = rgb->data()[0] + y * rgb->stride()[0] / 2; + for (int x = 0; x < size.width; ++x) { + + double cr = *p++ / 65535.0; + double cg = *p++ / 65535.0; + double cb = *p++ / 65535.0; + + /* Input gamma */ + + if (cr < 0.04045) { + cr /= 12.92; + } else { + cr = pow ((cr + 0.055) / 1.055, 2.4); + } + + if (cg < 0.04045) { + cg /= 12.92; + } else { + cg = pow ((cg + 0.055) / 1.055, 2.4); + } + + if (cb < 0.04045) { + cb /= 12.92; + } else { + cb = pow ((cb + 0.055) / 1.055, 2.4); + } + + /* Matrix */ + + double cx = cr * 0.4124564 + cg * 0.3575761 + cb * 0.1804375; + double cy = cr * 0.2126729 + cg * 0.7151522 + cb * 0.0721750; + double cz = cr * 0.0193339 + cg * 0.1191920 + cb * 0.9503041; + + /* Compand */ + + cx *= 48 / 52.37; + cy *= 48 / 52.37; + cz *= 48 / 52.37; + + /* Output gamma */ + + cx = pow (cx, 1 / 2.6); + cy = pow (cy, 1 / 2.6); + cz = pow (cz, 1 / 2.6); + + BOOST_REQUIRE_CLOSE (cx * 4095, xyz->data(0)[y * size.width + x], 1); + BOOST_REQUIRE_CLOSE (cy * 4095, xyz->data(1)[y * size.width + x], 1); + BOOST_REQUIRE_CLOSE (cz * 4095, xyz->data(2)[y * size.width + x], 1); + } + } +} diff --git a/test/wscript b/test/wscript index c19d2533..1208dd0e 100644 --- a/test/wscript +++ b/test/wscript @@ -44,6 +44,7 @@ def build(bld): kdm_test.cc read_dcp_test.cc recovery_test.cc + rgb_xyz_test.cc round_trip_test.cc smpte_load_font_test.cc subtitle_test.cc |
