From cd06d901af2b7c707db430293ab6efae354f4742 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Thu, 25 Feb 2016 16:29:49 +0100 Subject: Implement ordered dithering for image format conversions QImage::convertToFormat was ignoring its conversion flag argument, only performing dithering when converting to indexed formats. This patch updates the documentation and implements ordered dithering for other conversions. Change-Id: I807353d61669694185b7e595ef262d80d9fbb3f1 Reviewed-by: Gunnar Sletta --- tests/auto/gui/image/qimage/tst_qimage.cpp | 76 ++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) (limited to 'tests') diff --git a/tests/auto/gui/image/qimage/tst_qimage.cpp b/tests/auto/gui/image/qimage/tst_qimage.cpp index cc75846fdd..90b88d0992 100644 --- a/tests/auto/gui/image/qimage/tst_qimage.cpp +++ b/tests/auto/gui/image/qimage/tst_qimage.cpp @@ -195,6 +195,9 @@ private slots: void pixelColor(); void pixel(); + void ditherGradient_data(); + void ditherGradient(); + private: const QString m_prefix; }; @@ -3106,5 +3109,78 @@ void tst_QImage::pixel() } } +void tst_QImage::ditherGradient_data() +{ + QTest::addColumn("image"); + QTest::addColumn("format"); + QTest::addColumn("flags"); + QTest::addColumn("minimumExpectedGradient"); + + QImage rgb32(256, 16, QImage::Format_RGB32); + QLinearGradient gradient(QRectF(rgb32.rect()).topLeft(), QRectF(rgb32.rect()).topRight()); + gradient.setColorAt(0.0, QColor(0, 0, 0)); + gradient.setColorAt(1.0, QColor(255, 255, 255)); + QPainter p; + p.begin(&rgb32); + p.fillRect(rgb32.rect(), gradient); + p.end(); + + QTest::newRow("rgb32 -> rgb444 (no dither)") << rgb32 << QImage::Format_RGB444 << 0 << 16; + QTest::newRow("rgb32 -> rgb444 (dithering)") << rgb32 << QImage::Format_RGB444 << int(Qt::PreferDither | Qt::OrderedDither) << 33; + QTest::newRow("rgb32 -> argb4444pm (dithering)") << rgb32 << QImage::Format_ARGB4444_Premultiplied << int(Qt::PreferDither | Qt::OrderedDither) << 33; + QTest::newRow("rgb32 -> rgb16 (no dither)") << rgb32 << QImage::Format_RGB16 << 0 << 32; + QTest::newRow("rgb32 -> rgb16 (dithering)") << rgb32 << QImage::Format_RGB16 << int(Qt::PreferDither | Qt::OrderedDither) << 65; + QTest::newRow("rgb32 -> rgb666 (no dither)") << rgb32 << QImage::Format_RGB666 << 0 << 64; + QTest::newRow("rgb32 -> rgb666 (dithering)") << rgb32 << QImage::Format_RGB666 << int(Qt::PreferDither | Qt::OrderedDither) << 129; + + // Test we get the same results for opaque input in the ARGBPM implementation. + rgb32 = qMove(rgb32).convertToFormat(QImage::Format_ARGB32_Premultiplied); + QTest::newRow("argb32pm -> argb4444pm (no dither)") << rgb32 << QImage::Format_ARGB4444_Premultiplied << 0 << 16; + QTest::newRow("argb32pm -> rgb444 (dithering)") << rgb32 << QImage::Format_RGB444 << int(Qt::PreferDither | Qt::OrderedDither) << 33; + QTest::newRow("argb32pm -> argb4444pm (dithering)") << rgb32 << QImage::Format_ARGB4444_Premultiplied << int(Qt::PreferDither | Qt::OrderedDither) << 33; + QTest::newRow("argb32pm -> argb8565pm (no dither)") << rgb32 << QImage::Format_ARGB8565_Premultiplied << 0 << 32; + QTest::newRow("argb32pm -> argb8565pm (dithering)") << rgb32 << QImage::Format_ARGB8565_Premultiplied << int(Qt::PreferDither | Qt::OrderedDither) << 65; + QTest::newRow("argb32pm -> argb6666pm (no dither)") << rgb32 << QImage::Format_ARGB6666_Premultiplied << 0 << 64; + QTest::newRow("argb32pm -> argb6666pm (dithering)") << rgb32 << QImage::Format_ARGB6666_Premultiplied << int(Qt::PreferDither | Qt::OrderedDither) << 129; + + QImage rgb30(1024, 16, QImage::Format_RGB30); + QLinearGradient gradient30(QRectF(rgb30.rect()).topLeft(), QRectF(rgb30.rect()).topRight()); + gradient30.setColorAt(0.0, QColor(0, 0, 0)); + gradient30.setColorAt(1.0, QColor(255, 255, 255)); + p.begin(&rgb30); + p.fillRect(rgb30.rect(), gradient30); + p.end(); + + QTest::newRow("rgb30 -> rgb32 (no dither)") << rgb30 << QImage::Format_RGB32 << 0 << 256; + QTest::newRow("rgb30 -> rgb32 (dithering)") << rgb30 << QImage::Format_RGB32 << int(Qt::PreferDither | Qt::OrderedDither) << 513; + QTest::newRow("rgb30 -> rgb888 (no dither)") << rgb30 << QImage::Format_RGB888 << 0 << 256; + QTest::newRow("rgb30 -> rgb888 (dithering)") << rgb30 << QImage::Format_RGB888 << int(Qt::PreferDither | Qt::OrderedDither) << 513; +} + +void tst_QImage::ditherGradient() +{ + QFETCH(QImage, image); + QFETCH(QImage::Format, format); + QFETCH(int, flags); + QFETCH(int, minimumExpectedGradient); + + QImage converted = image.convertToFormat(format, (Qt::ImageConversionFlags)flags); + int observedGradientSteps = 0; + int lastTotal = -1; + for (int i = 0; i < converted.width(); ++i) { + int total = 0; + for (int j = 0; j < converted.height(); ++j) { + uint c = converted.pixel(i, j); + QCOMPARE(qAlpha(c), 255); + total += qRed(c); + } + if (total > lastTotal) { + observedGradientSteps++; + lastTotal = total; + } + } + QVERIFY(observedGradientSteps >= minimumExpectedGradient); +} + QTEST_GUILESS_MAIN(tst_QImage) #include "tst_qimage.moc" -- cgit v1.2.3