summaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2018-03-28 11:33:23 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2018-04-10 12:20:45 +0000
commit2268b6bcdd1b4ddcd78753b81c477f0aa2855261 (patch)
tree1e7277daf115d05dfbbab0d2f47223e181ef9589 /tests/auto
parent5e7a64d1e321ddba751c0b18daf7920fde7c7de0 (diff)
QtGui: Add qt_imageToWinHBITMAP(), qt_imageFromWinHBITMAP()
Add functions for converting QImage to HBITMAP and back supporting additional formats of QImage (RGB888, RGB555, Indexed8 and Mono). Add test with roundtrip to tst_qimage similar to tst_QPixmap::toWinHBITMAP(). Task-number: QTBUG-51124 Change-Id: Ib568898e7162686bfa527d828785628eb0b78e21 Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/gui/image/qimage/qimage.pro2
-rw-r--r--tests/auto/gui/image/qimage/tst_qimage.cpp112
2 files changed, 114 insertions, 0 deletions
diff --git a/tests/auto/gui/image/qimage/qimage.pro b/tests/auto/gui/image/qimage/qimage.pro
index 56618e0bfa..39fb4d0cc3 100644
--- a/tests/auto/gui/image/qimage/qimage.pro
+++ b/tests/auto/gui/image/qimage/qimage.pro
@@ -7,4 +7,6 @@ qtConfig(c++11): CONFIG += c++11
android:!android-embedded: RESOURCES += qimage.qrc
+win32:!winrt: LIBS += -lgdi32 -luser32
+
TESTDATA += images/*
diff --git a/tests/auto/gui/image/qimage/tst_qimage.cpp b/tests/auto/gui/image/qimage/tst_qimage.cpp
index 55a670eb6f..1f52018d7f 100644
--- a/tests/auto/gui/image/qimage/tst_qimage.cpp
+++ b/tests/auto/gui/image/qimage/tst_qimage.cpp
@@ -44,6 +44,10 @@
#include <CoreGraphics/CoreGraphics.h>
#endif
+#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
+# include <qt_windows.h>
+#endif
+
Q_DECLARE_METATYPE(QImage::Format)
Q_DECLARE_METATYPE(Qt::GlobalColor)
@@ -223,6 +227,11 @@ private slots:
void hugeQImage();
+#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
+ void toWinHBITMAP_data();
+ void toWinHBITMAP();
+#endif // Q_OS_WIN && !Q_OS_WINRT
+
private:
const QString m_prefix;
};
@@ -3487,5 +3496,108 @@ void tst_QImage::hugeQImage()
#endif
}
+#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
+QT_BEGIN_NAMESPACE
+Q_GUI_EXPORT HBITMAP qt_imageToWinHBITMAP(const QImage &p, int hbitmapFormat = 0);
+Q_GUI_EXPORT QImage qt_imageFromWinHBITMAP(HBITMAP bitmap, int hbitmapFormat = 0);
+QT_END_NAMESPACE
+
+static inline QColor COLORREFToQColor(COLORREF cr)
+{
+ return QColor(GetRValue(cr), GetGValue(cr), GetBValue(cr));
+}
+
+void tst_QImage::toWinHBITMAP_data()
+{
+ QTest::addColumn<QImage::Format>("format");
+ QTest::addColumn<QColor>("color");
+ QTest::addColumn<QColor>("bottomRightColor");
+
+ const QColor red(Qt::red);
+ const QColor green(Qt::green);
+ const QColor blue(Qt::blue);
+ const QColor gray(Qt::gray);
+ const QColor gray555(0x5a, 0x5a, 0x5a); // Note: Interpolation 8<->5 bit occurs.
+ const QColor white(Qt::white);
+ const QColor black(Qt::black);
+
+ QTest::newRow("argb32p-red") << QImage::Format_ARGB32_Premultiplied << red << gray;
+ QTest::newRow("argb32p-green") << QImage::Format_ARGB32_Premultiplied << green << gray;
+ QTest::newRow("argb32p-blue") << QImage::Format_ARGB32_Premultiplied << blue << gray;
+ QTest::newRow("rgb888-red") << QImage::Format_RGB888 << red << gray;
+ QTest::newRow("rgb888-green") << QImage::Format_RGB888 << green << gray;
+ QTest::newRow("rgb888-blue") << QImage::Format_RGB888 << blue << gray;
+ QTest::newRow("indexed8-red") << QImage::Format_Indexed8 << red << gray;
+ QTest::newRow("indexed8-green") << QImage::Format_Indexed8 << green << gray;
+ QTest::newRow("indexed8-blue") << QImage::Format_Indexed8 << blue << gray;
+ QTest::newRow("rgb555-red") << QImage::Format_RGB555 << red << gray555;
+ QTest::newRow("rgb555-green") << QImage::Format_RGB555 << green << gray555;
+ QTest::newRow("rgb555-blue") << QImage::Format_RGB555 << blue << gray555;
+ QTest::newRow("mono") << QImage::Format_Mono << white << black;
+}
+
+// Test image filled with color, black pixel at botttom right corner.
+static inline QImage createTestImage(QImage::Format format, int width, int height,
+ const QColor &fillColor, const QColor &bottomRightColor)
+{
+ QImage image(QSize(width, height), format);
+ image.fill(fillColor);
+ QPainter painter(&image);
+ QPen pen = painter.pen();
+ pen.setColor(bottomRightColor);
+ painter.setPen(pen);
+ painter.drawPoint(width -1, height - 1);
+ return image;
+}
+
+void tst_QImage::toWinHBITMAP()
+{
+ static const int width = 73;
+ static const int height = 57;
+
+ QFETCH(QImage::Format, format);
+ QFETCH(QColor, color);
+ QFETCH(QColor, bottomRightColor);
+
+ // Cannot paint on indexed/mono images.
+ const QImage image = format == QImage::Format_Indexed8 || format == QImage::Format_Mono
+ ? createTestImage(QImage::Format_RGB32, width, height, color, bottomRightColor).convertToFormat(format)
+ : createTestImage(format, width, height, color, bottomRightColor);
+
+ const HBITMAP bitmap = qt_imageToWinHBITMAP(image);
+
+ QVERIFY(bitmap != 0);
+
+ // Verify size
+ BITMAP bitmapInfo;
+ memset(&bitmapInfo, 0, sizeof(BITMAP));
+
+ const int res = GetObject(bitmap, sizeof(BITMAP), &bitmapInfo);
+ QVERIFY(res);
+ QCOMPARE(width, int(bitmapInfo.bmWidth));
+ QCOMPARE(height, int(bitmapInfo.bmHeight));
+
+ const HDC displayDc = GetDC(0);
+ const HDC bitmapDc = CreateCompatibleDC(displayDc);
+
+ const HBITMAP nullBitmap = static_cast<HBITMAP>(SelectObject(bitmapDc, bitmap));
+
+ QCOMPARE(COLORREFToQColor(GetPixel(bitmapDc, 0, 0)), color);
+ QCOMPARE(COLORREFToQColor(GetPixel(bitmapDc, width - 1, 3)), color);
+ QCOMPARE(COLORREFToQColor(GetPixel(bitmapDc, 3, height - 1)), color);
+ QCOMPARE(COLORREFToQColor(GetPixel(bitmapDc, width - 1, height - 1)), bottomRightColor);
+
+ const QImage convertedBack = qt_imageFromWinHBITMAP(bitmap);
+ QCOMPARE(convertedBack.convertToFormat(QImage::Format_ARGB32_Premultiplied),
+ image.convertToFormat(QImage::Format_ARGB32_Premultiplied));
+
+ // Clean up
+ SelectObject(bitmapDc, nullBitmap);
+ DeleteObject(bitmap);
+ DeleteDC(bitmapDc);
+ ReleaseDC(0, displayDc);
+}
+#endif // Q_OS_WIN && !Q_OS_WINRT
+
QTEST_GUILESS_MAIN(tst_QImage)
#include "tst_qimage.moc"