diff options
author | Allan Sandfeld Jensen <allan.jensen@theqtcompany.com> | 2015-02-11 11:12:43 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@theqtcompany.com> | 2015-03-10 12:27:54 +0000 |
commit | e2c91dd77bd85cdcb8e316a0f582e4f36ea4bcfb (patch) | |
tree | 5eabdd03dd9a41523d09eaf3d51265d80d3b6b83 /tests/auto/gui/painting/qcolor | |
parent | 395e205f2f51620212b8a8d710a02b5daf5e97b5 (diff) |
Introduce QRgba64 structure for 64bit RGBA values
This structure is meant to replace QRgb where higher precision is
needed.
Change-Id: I49d441e2133371a8b91c2e6af0c137bcc5fcb9ed
Reviewed-by: Gunnar Sletta <gunnar@sletta.org>
Diffstat (limited to 'tests/auto/gui/painting/qcolor')
-rw-r--r-- | tests/auto/gui/painting/qcolor/tst_qcolor.cpp | 85 |
1 files changed, 84 insertions, 1 deletions
diff --git a/tests/auto/gui/painting/qcolor/tst_qcolor.cpp b/tests/auto/gui/painting/qcolor/tst_qcolor.cpp index 4fafbf9827..7d17794c0b 100644 --- a/tests/auto/gui/painting/qcolor/tst_qcolor.cpp +++ b/tests/auto/gui/painting/qcolor/tst_qcolor.cpp @@ -38,6 +38,7 @@ #include <qcolor.h> #include <qdebug.h> +#include <qrgba64.h> #include <private/qdrawingprimitive_sse2_p.h> class tst_QColor : public QObject @@ -105,6 +106,9 @@ private slots: void premultiply(); void unpremultiply_sse4(); + void qrgba64(); + void qrgba64Premultiply(); + void qrgba64Equivalence(); #ifdef Q_DEAD_CODE_FROM_QT4_X11 void setallowX11ColorNames(); @@ -1455,7 +1459,7 @@ void tst_QColor::unpremultiply_sse4() for (uint a = 0; a < 256; a++) { for (uint c = 0; c <= a; c++) { QRgb p = qRgba(c, a-c, c, a); - QCOMPARE(qUnpremultiply(p), qUnpremultiply_sse4(p)); + QCOMPARE(qUnpremultiply_sse4(p), qUnpremultiply(p)); } } return; @@ -1464,5 +1468,84 @@ void tst_QColor::unpremultiply_sse4() QSKIP("SSE4 not supported on this CPU."); } +void tst_QColor::qrgba64() +{ + QRgba64 rgb64 = QRgba64::fromRgba(0x22, 0x33, 0x44, 0xff); + QCOMPARE(rgb64.red(), quint16(0x2222)); + QCOMPARE(rgb64.green(), quint16(0x3333)); + QCOMPARE(rgb64.blue(), quint16(0x4444)); + QCOMPARE(rgb64.alpha(), quint16(0xffff)); + + QColor c(rgb64); + QCOMPARE(c.red(), 0x22); + QCOMPARE(c.green(), 0x33); + QCOMPARE(c.blue(), 0x44); + + QCOMPARE(c.rgba64(), rgb64); + + QColor c2 = QColor::fromRgb(0x22, 0x33, 0x44, 0xff); + QCOMPARE(c, c2); + QCOMPARE(c2.rgba64(), rgb64); + + rgb64.setAlpha(0x8000); + rgb64.setGreen(0x8844); + rgb64 = rgb64.premultiplied(); + QCOMPARE(rgb64.red(), quint16(0x1111)); + QCOMPARE(rgb64.blue(), quint16(0x2222)); + QCOMPARE(rgb64.green(), quint16(0x4422)); +} + +void tst_QColor::qrgba64Premultiply() +{ + // Tests that qPremultiply(qUnpremultiply(rgba64)) returns rgba64. + for (uint a = 0; a < 0x10000; a+=7) { + const uint step = std::max(a/1024, 1u); + for (uint c = 0; c <= a; c+=step) { + QRgba64 p = qRgba64(c, a-c, a-c/2, a); + QRgba64 pp = qPremultiply(qUnpremultiply(p)); + QCOMPARE(pp, p); + } + } +} + +void tst_QColor::qrgba64Equivalence() +{ + // Any ARGB32 converted back and forth. + for (uint a = 0; a < 256; a++) { + for (uint c = 0; c < 256; c++) { + QRgb p1 = qRgba(c, 255-c, 255-c, a); + QRgba64 p64 = QRgba64::fromArgb32(p1); + QCOMPARE(p64.toArgb32(), p1); + } + } + // Any unpremultiplied ARGB32 value premultipled in RGB64 (except alpha 0). + for (uint a = 1; a < 256; a++) { + for (uint c = 0; c < 256; c++) { + QRgb p1 = qRgba(c, 255-c, 255-c, a); + QRgb pp1 = qPremultiply(p1); + QRgba64 pp64 = qPremultiply(QRgba64::fromArgb32(p1)); + QRgb pp2 = pp64.toArgb32(); + // 64bit premultiplied is more accurate than 32bit, so allow slight difference. + QCOMPARE(qAlpha(pp2), qAlpha(pp1)); + QVERIFY(qAbs(qRed(pp2)-qRed(pp1)) <= 1); + QVERIFY(qAbs(qGreen(pp2)-qGreen(pp1)) <= 1); + QVERIFY(qAbs(qBlue(pp2)-qBlue(pp1)) <= 1); + // But verify the added accuracy means we can return to accurate unpremultiplied ARGB32. + QRgba64 pu64 = qUnpremultiply(pp64); + QRgb p2 = pu64.toArgb32(); + QCOMPARE(p2, p1); + } + } + // Any premultiplied ARGB32 value unpremultipled in RGB64. + for (uint a = 0; a < 256; a++) { + for (uint c = 0; c <= a; c++) { + QRgb pp = qRgba(c, a-c, a-c, a); + QRgb pu = qUnpremultiply(pp); + QRgba64 pu64 = qUnpremultiply(QRgba64::fromArgb32(pp)); + QCOMPARE(pu64.toArgb32(), pu); + } + } +} + QTEST_MAIN(tst_QColor) #include "tst_qcolor.moc" |