diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2021-02-11 11:23:05 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2021-03-23 14:16:55 +0100 |
commit | 0ff1dc7110d373c85ce41e90073291b5e2eb16ad (patch) | |
tree | 216f75b47033d1e3e47e2963033e47887001f94a /tests/auto | |
parent | d3b24a14bb9e6c258e9183dc05893020d81c9a9f (diff) |
Fix alpha handling of QImage::setPixelv6.0.3
It was treated differently depending on format, made it consistently
behave the same for all formats (following the behavior of the primary
formats).
Change-Id: Ie24e19957d076fdf3ebd333074e26ede187489eb
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
(cherry picked from commit c32cd44d34910cfd42e32537578e4a573138a282)
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/gui/image/qimage/tst_qimage.cpp | 72 |
1 files changed, 61 insertions, 11 deletions
diff --git a/tests/auto/gui/image/qimage/tst_qimage.cpp b/tests/auto/gui/image/qimage/tst_qimage.cpp index 440fe7e72f..b4942ef013 100644 --- a/tests/auto/gui/image/qimage/tst_qimage.cpp +++ b/tests/auto/gui/image/qimage/tst_qimage.cpp @@ -101,6 +101,10 @@ private slots: void setPixel_data(); void setPixel(); + void setPixelWithAlpha_data(); + void setPixelWithAlpha(); + void setPixelColorWithAlpha_data(); + void setPixelColorWithAlpha(); void defaultColorTable_data(); void defaultColorTable(); @@ -1455,6 +1459,62 @@ void tst_QImage::setPixel() } } +void tst_QImage::setPixelWithAlpha_data() +{ + QTest::addColumn<QImage::Format>("format"); + + for (int c = QImage::Format_RGB32; c < QImage::NImageFormats; ++c) { + if (c == QImage::Format_Grayscale8) + continue; + if (c == QImage::Format_Grayscale16) + continue; + if (c == QImage::Format_Alpha8) + continue; + QTest::newRow(qPrintable(formatToString(QImage::Format(c)))) << QImage::Format(c); + } +} + +void tst_QImage::setPixelWithAlpha() +{ + QFETCH(QImage::Format, format); + QImage image(1, 1, format); + QRgb referenceColor = qRgba(0, 170, 85, 170); + image.setPixel(0, 0, referenceColor); + + if (!image.hasAlphaChannel()) + referenceColor = 0xff000000 | referenceColor; + + QRgb color = image.pixel(0, 0); + QCOMPARE(qRed(color) & 0xf0, qRed(referenceColor) & 0xf0); + QCOMPARE(qGreen(color) & 0xf0, qGreen(referenceColor) & 0xf0); + QCOMPARE(qBlue(color) & 0xf0, qBlue(referenceColor) & 0xf0); + QCOMPARE(qAlpha(color) & 0xf0, qAlpha(referenceColor) & 0xf0); +} + +void tst_QImage::setPixelColorWithAlpha_data() +{ + setPixelWithAlpha_data(); +} + +void tst_QImage::setPixelColorWithAlpha() +{ + QFETCH(QImage::Format, format); + QImage image(1, 1, format); + image.setPixelColor(0, 0, QColor(170, 85, 255, 170)); + QRgb referenceColor = qRgba(170, 85, 255, 170); + + if (!image.hasAlphaChannel()) + referenceColor = 0xff000000 | referenceColor; + else if (image.pixelFormat().premultiplied() == QPixelFormat::Premultiplied) + referenceColor = qPremultiply(referenceColor); + + QRgb color = image.pixel(0, 0); + QCOMPARE(qRed(color) & 0xf0, qRed(referenceColor) & 0xf0); + QCOMPARE(qGreen(color) & 0xf0, qGreen(referenceColor) & 0xf0); + QCOMPARE(qBlue(color) & 0xf0, qBlue(referenceColor) & 0xf0); + QCOMPARE(qAlpha(color) & 0xf0, qAlpha(referenceColor) & 0xf0); +} + void tst_QImage::convertToFormatPreserveDotsPrMeter() { QImage img(100, 100, QImage::Format_ARGB32_Premultiplied); @@ -2350,17 +2410,7 @@ void tst_QImage::fillColor() void tst_QImage::fillColorWithAlpha_data() { - QTest::addColumn<QImage::Format>("format"); - - for (int c = QImage::Format_RGB32; c < QImage::NImageFormats; ++c) { - if (c == QImage::Format_Grayscale8) - continue; - if (c == QImage::Format_Grayscale16) - continue; - if (c == QImage::Format_Alpha8) - continue; - QTest::newRow(qPrintable(formatToString(QImage::Format(c)))) << QImage::Format(c); - } + setPixelWithAlpha_data(); } void tst_QImage::fillColorWithAlpha() |