File indexing completed on 2024-11-10 04:56:24

0001 /*
0002     SPDX-FileCopyrightText: 2023 Xaver Hugl <xaver.hugl@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <QTest>
0008 
0009 #include "core/colorspace.h"
0010 
0011 using namespace KWin;
0012 
0013 class TestColorspaces : public QObject
0014 {
0015     Q_OBJECT
0016 
0017 public:
0018     TestColorspaces() = default;
0019 
0020 private Q_SLOTS:
0021     void roundtripConversion_data();
0022     void roundtripConversion();
0023 };
0024 
0025 static bool compareVectors(const QVector3D &one, const QVector3D &two, float maxDifference)
0026 {
0027     const bool ret = std::abs(one.x() - two.x()) <= maxDifference
0028         && std::abs(one.y() - two.y()) <= maxDifference
0029         && std::abs(one.z() - two.z()) <= maxDifference;
0030     if (!ret) {
0031         qWarning() << one << "!=" << two << "within" << maxDifference;
0032     }
0033     return ret;
0034 }
0035 
0036 void TestColorspaces::roundtripConversion_data()
0037 {
0038     QTest::addColumn<NamedColorimetry>("srcColorimetry");
0039     QTest::addColumn<NamedTransferFunction>("srcTransferFunction");
0040     QTest::addColumn<NamedColorimetry>("dstColorimetry");
0041     QTest::addColumn<NamedTransferFunction>("dstTransferFunction");
0042     QTest::addColumn<double>("requiredAccuracy");
0043 
0044     const double resolution10bit = std::pow(1.0 / 2.0, 10);
0045     QTest::addRow("BT709 (sRGB) <-> BT2020 (linear)") << NamedColorimetry::BT709 << NamedTransferFunction::sRGB << NamedColorimetry::BT2020 << NamedTransferFunction::linear << resolution10bit;
0046     QTest::addRow("BT709 (gamma 2.2) <-> BT2020 (linear)") << NamedColorimetry::BT709 << NamedTransferFunction::gamma22 << NamedColorimetry::BT2020 << NamedTransferFunction::linear << resolution10bit;
0047     QTest::addRow("BT709 (scRGB) <-> BT2020 (linear)") << NamedColorimetry::BT709 << NamedTransferFunction::scRGB << NamedColorimetry::BT2020 << NamedTransferFunction::linear << resolution10bit;
0048     QTest::addRow("BT709 (linear) <-> BT2020 (linear)") << NamedColorimetry::BT709 << NamedTransferFunction::linear << NamedColorimetry::BT2020 << NamedTransferFunction::linear << resolution10bit;
0049     QTest::addRow("BT709 (PQ) <-> BT2020 (linear)") << NamedColorimetry::BT709 << NamedTransferFunction::PerceptualQuantizer << NamedColorimetry::BT2020 << NamedTransferFunction::linear << 3 * resolution10bit;
0050 }
0051 
0052 void TestColorspaces::roundtripConversion()
0053 {
0054     QFETCH(NamedColorimetry, srcColorimetry);
0055     QFETCH(NamedTransferFunction, srcTransferFunction);
0056     QFETCH(NamedColorimetry, dstColorimetry);
0057     QFETCH(NamedTransferFunction, dstTransferFunction);
0058     QFETCH(double, requiredAccuracy);
0059 
0060     const auto src = ColorDescription(srcColorimetry, srcTransferFunction, 100, 0, 100, 100);
0061     const auto dst = ColorDescription(dstColorimetry, dstTransferFunction, 100, 0, 100, 100);
0062 
0063     const QVector3D red(1, 0, 0);
0064     const QVector3D green(0, 1, 0);
0065     const QVector3D blue(0, 0, 1);
0066     const QVector3D white(1, 1, 1);
0067 
0068     QVERIFY(compareVectors(dst.mapTo(src.mapTo(red, dst), src), red, requiredAccuracy));
0069     QVERIFY(compareVectors(dst.mapTo(src.mapTo(green, dst), src), green, requiredAccuracy));
0070     QVERIFY(compareVectors(dst.mapTo(src.mapTo(blue, dst), src), blue, requiredAccuracy));
0071     QVERIFY(compareVectors(dst.mapTo(src.mapTo(white, dst), src), white, requiredAccuracy));
0072 }
0073 
0074 QTEST_MAIN(TestColorspaces)
0075 
0076 #include "test_colorspaces.moc"