From d8dc664b945d8832eab97c5df0e1ea47ebf895fd Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Thu, 24 Jul 2014 15:54:58 +0200 Subject: Ensure valid data after QImage::invertPixels QImage::invertPixels may produce invalid data after inversions of images with premultiplied alpha, because the inverted colors will be larger than the alpha. This patch converts any image with a premultiplied alpha channel to ARGB32 before inverting the pixels, and then back to the original format after the inversion. Support is added for correct inversion of RGBA8888 and RGB30 formats. Task-number: QTBUG-39901 Change-Id: Ief24c55f495e67ef2ad6429b5b418d02963a64dd Reviewed-by: Gunnar Sletta --- tests/auto/gui/image/qimage/tst_qimage.cpp | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'tests/auto/gui/image/qimage/tst_qimage.cpp') diff --git a/tests/auto/gui/image/qimage/tst_qimage.cpp b/tests/auto/gui/image/qimage/tst_qimage.cpp index d885ab6dd4..f922deb815 100644 --- a/tests/auto/gui/image/qimage/tst_qimage.cpp +++ b/tests/auto/gui/image/qimage/tst_qimage.cpp @@ -178,6 +178,9 @@ private slots: void convertToImageFormat_data(); void convertToImageFormat(); + void invertPixelsRGB_data(); + void invertPixelsRGB(); + void cleanupFunctions(); private: @@ -2585,6 +2588,7 @@ void tst_QImage::convertToImageFormat_data() QTest::newRow("Convert Format_RGBA8888") << QImage::Format_RGBA8888; QTest::newRow("Convert Format_RGBA8888_Premultiplied") << QImage::Format_RGBA8888_Premultiplied; } + void tst_QImage::convertToImageFormat() { QFETCH(QImage::Format, image_format); @@ -2594,6 +2598,40 @@ void tst_QImage::convertToImageFormat() QCOMPARE(format, image_format); } +void tst_QImage::invertPixelsRGB_data() +{ + QTest::addColumn("image_format"); + + QTest::newRow("invertPixels RGB16") << QImage::Format_RGB16; + QTest::newRow("invertPixels RGB32") << QImage::Format_RGB32; + QTest::newRow("invertPixels BGR30") << QImage::Format_BGR30; + QTest::newRow("invertPixels RGB444") << QImage::Format_RGB444; + QTest::newRow("invertPixels RGB555") << QImage::Format_RGB555; + QTest::newRow("invertPixels RGB888") << QImage::Format_RGB888; + + QTest::newRow("invertPixels ARGB32") << QImage::Format_ARGB32; + QTest::newRow("invertPixels ARGB32pm") << QImage::Format_ARGB32_Premultiplied; + QTest::newRow("invertPixels RGBA8888") << QImage::Format_RGBA8888; + QTest::newRow("invertPixels RGBA8888pm") << QImage::Format_RGBA8888_Premultiplied; + QTest::newRow("invertPixels RGBA4444pm") << QImage::Format_ARGB4444_Premultiplied; +} + +void tst_QImage::invertPixelsRGB() +{ + QFETCH(QImage::Format, image_format); + + QImage image(1, 1, image_format); + image.fill(QColor::fromRgb(32, 64, 96)); + image.invertPixels(); + + QCOMPARE(image.format(), image_format); + + uint pixel = image.pixel(0, 0); + QCOMPARE(qRed(pixel) >> 4, (255 - 32) >> 4); + QCOMPARE(qGreen(pixel) >> 4, (255 - 64) >> 4); + QCOMPARE(qBlue(pixel) >> 4, (255 - 96) >> 4); +} + static void cleanupFunction(void* info) { bool *called = static_cast(info); -- cgit v1.2.3