summaryrefslogtreecommitdiffstats
path: root/tests/auto/gui/image/qimage
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2021-02-11 11:23:05 +0100
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2021-03-22 14:29:14 +0100
commitc32cd44d34910cfd42e32537578e4a573138a282 (patch)
tree7a1f35bcb6a9fbe2f272ed2030515c1f81c44b50 /tests/auto/gui/image/qimage
parentb3544bfd4642c3b256c39d20bc58a9019d3e4d25 (diff)
Fix alpha handling of QImage::setPixel
It was treated differently depending on format, made it consistently behave the same for all formats (following the behavior of the primary formats). Pick-to: 6.1 6.0 5.15 Change-Id: Ie24e19957d076fdf3ebd333074e26ede187489eb Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
Diffstat (limited to 'tests/auto/gui/image/qimage')
-rw-r--r--tests/auto/gui/image/qimage/tst_qimage.cpp72
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 12345b2a97..0fb4263024 100644
--- a/tests/auto/gui/image/qimage/tst_qimage.cpp
+++ b/tests/auto/gui/image/qimage/tst_qimage.cpp
@@ -102,6 +102,10 @@ private slots:
void setPixel_data();
void setPixel();
+ void setPixelWithAlpha_data();
+ void setPixelWithAlpha();
+ void setPixelColorWithAlpha_data();
+ void setPixelColorWithAlpha();
void defaultColorTable_data();
void defaultColorTable();
@@ -1457,6 +1461,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);
@@ -2352,17 +2412,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()