Commit 99a77e9e authored by Mathias Wein's avatar Mathias Wein

Create unit tests for La*b* color calculations

parent dab0e6ef
......@@ -17,6 +17,7 @@ ecm_add_tests(
TestKoIntegerMaths.cpp
TestConvolutionOpImpl.cpp
KoRgbU8ColorSpaceTester.cpp
TestLabColorSpaces.cpp
TestKoColorSpaceSanity.cpp
TestFallBackColorTransformation.cpp
TestKoChannelInfo.cpp
......
/*
* Copyright (c) 2020 Mathias Wein <lynx.mw+kde@gmail.com>
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "TestLabColorSpaces.h"
#include <algorithm>
#include <QTest>
#include <QVector>
#include "KoColor.h"
#include "KoLabColorSpaceMaths.h"
#include "KoLabColorSpaceTraits.h"
#define CHANNEL_L 0
#define CHANNEL_A 1
#define CHANNEL_B 2
#define CHANNEL_ALPHA 3
template<typename T> void loadZeroColor(T *values)
{
values[CHANNEL_L] = KoLabColorSpaceMathsTraits<T>::zeroValueL;
values[CHANNEL_A] = KoLabColorSpaceMathsTraits<T>::zeroValueAB;
values[CHANNEL_B] = KoLabColorSpaceMathsTraits<T>::zeroValueAB;
values[CHANNEL_ALPHA] = KoColorSpaceMathsTraits<T>::zeroValue;
}
template<typename T> void loadUnitColor(T *values)
{
values[CHANNEL_L] = KoLabColorSpaceMathsTraits<T>::unitValueL;
values[CHANNEL_A] = KoLabColorSpaceMathsTraits<T>::unitValueAB;
values[CHANNEL_B] = KoLabColorSpaceMathsTraits<T>::unitValueAB;
values[CHANNEL_ALPHA] = KoColorSpaceMathsTraits<T>::unitValue;
}
void TestLabColorSpaces::verifyNormalisedZero(const QVector<float> &values)
{
QCOMPARE(values[CHANNEL_L] + 1.f, 1.f);
QCOMPARE(values[CHANNEL_A] + 1.f, 1.f);
QCOMPARE(values[CHANNEL_B] + 1.f, 1.f);
QCOMPARE(values[CHANNEL_ALPHA] + 1.f, 1.f);
}
void TestLabColorSpaces::verifyNormalisedUnitValue(const QVector<float> &values)
{
QCOMPARE(values[CHANNEL_L], 1.f);
QCOMPARE(values[CHANNEL_A], 1.f);
QCOMPARE(values[CHANNEL_B], 1.f);
QCOMPARE(values[CHANNEL_ALPHA], 1.f);
}
void TestLabColorSpaces::testNormalisation()
{
QVector<float> normalized(4);
quint8 testData[MAX_PIXEL_SIZE];
// Minimum
// 8-bit int
loadZeroColor(testData);
KoLabU8Traits::normalisedChannelsValue(testData, normalized);
verifyNormalisedZero(normalized);
// 16-bit int
loadZeroColor(KoLabU16Traits::nativeArray(testData));
KoLabU16Traits::normalisedChannelsValue(testData, normalized);
verifyNormalisedZero(normalized);
#if HAVE_OPENEXR
// 16-bit float (half)
loadZeroColor(KoLabF16Traits::nativeArray(testData));
KoLabF16Traits::normalisedChannelsValue(testData, normalized);
verifyNormalisedZero(normalized);
#endif
// 32-bit float
loadZeroColor(KoLabF32Traits::nativeArray(testData));
KoLabF32Traits::normalisedChannelsValue(testData, normalized);
verifyNormalisedZero(normalized);
// 64-bit float
loadZeroColor(KoLabF64Traits::nativeArray(testData));
KoLabF64Traits::normalisedChannelsValue(testData, normalized);
verifyNormalisedZero(normalized);
// Maximum
// 8-bit int
loadUnitColor(testData);
KoLabU8Traits::normalisedChannelsValue(testData, normalized);
verifyNormalisedUnitValue(normalized);
// 16-bit int
loadUnitColor(KoLabU16Traits::nativeArray(testData));
KoLabU16Traits::normalisedChannelsValue(testData, normalized);
verifyNormalisedUnitValue(normalized);
#if HAVE_OPENEXR
// 16-bit float (half)
loadUnitColor(KoLabF16Traits::nativeArray(testData));
KoLabF16Traits::normalisedChannelsValue(testData, normalized);
verifyNormalisedUnitValue(normalized);
#endif
// 32-bit float
loadUnitColor(KoLabF32Traits::nativeArray(testData));
KoLabF32Traits::normalisedChannelsValue(testData, normalized);
verifyNormalisedUnitValue(normalized);
// 64-bit float
loadUnitColor(KoLabF64Traits::nativeArray(testData));
KoLabF64Traits::normalisedChannelsValue(testData, normalized);
verifyNormalisedUnitValue(normalized);
}
QTEST_GUILESS_MAIN(TestLabColorSpaces)
/*
* Copyright (c) 2020 Mathias Wein <lynx.mw+kde@gmail.com>
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef TESTLABCOLORSPACES_H
#define TESTLABCOLORSPACES_H
#include <QObject>
template <typename T> class QVector;
class TestLabColorSpaces : public QObject
{
Q_OBJECT
void verifyNormalisedZero(const QVector<float> &values);
void verifyNormalisedUnitValue(const QVector<float> &values);
private Q_SLOTS:
void testNormalisation();
};
#endif // TESTLABCOLORSPACES_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment