From acb67561a3753306a2262f7221baca2d727fd4cb Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Thu, 17 Mar 2016 13:33:50 +0100 Subject: Add imageToHBITMAP(), imageFromHBITMAP() Add new functions for image conversions wrapping their QtGui equivalents. [ChangeLog][QtWin] Added functions imageToHBITMAP() and imageFromHBITMAP() for conversion between QImage and HBITMAP, preserving the format where possible. Task-number: QTBUG-51124 Change-Id: I212b2dff182dce3a9b08753b6435d01b75bb5434 Reviewed-by: Simon Hausmann --- src/winextras/qwinfunctions.cpp | 31 +++++++++++++++++++++++++++++++ src/winextras/qwinfunctions.h | 2 ++ tests/auto/qpixmap/tst_qpixmap.cpp | 18 ++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/src/winextras/qwinfunctions.cpp b/src/winextras/qwinfunctions.cpp index 7b102a8..7089966 100644 --- a/src/winextras/qwinfunctions.cpp +++ b/src/winextras/qwinfunctions.cpp @@ -69,6 +69,8 @@ Q_GUI_EXPORT HBITMAP qt_createIconMask(const QBitmap &bitmap); Q_GUI_EXPORT HBITMAP qt_pixmapToWinHBITMAP(const QPixmap &p, int hbitmapFormat = 0); Q_GUI_EXPORT QPixmap qt_pixmapFromWinHBITMAP(HBITMAP bitmap, int hbitmapFormat = 0); Q_GUI_EXPORT HICON qt_pixmapToWinHICON(const QPixmap &p); +Q_GUI_EXPORT HBITMAP qt_imageToWinHBITMAP(const QImage &imageIn, int hbitmapFormat = 0); +Q_GUI_EXPORT QImage qt_imageFromWinHBITMAP(HBITMAP bitmap, int hbitmapFormat = 0); Q_GUI_EXPORT QImage qt_imageFromWinHBITMAP(HDC hdc, HBITMAP bitmap, int w, int h); Q_GUI_EXPORT QPixmap qt_pixmapFromWinHICON(HICON icon); @@ -138,6 +140,22 @@ HICON QtWin::toHICON(const QPixmap &p) return qt_pixmapToWinHICON(p); } +/*! + \since 5.12 + + Creates a \c HBITMAP equivalent of the QImage \a image, + based on the given \a format. Returns the \c HBITMAP handle. + + It is the caller's responsibility to free the \c HBITMAP data + after use. + + \sa imageFromHBITMAP() +*/ +HBITMAP QtWin::imageToHBITMAP(const QImage &image, QtWin::HBitmapFormat format) +{ + return qt_imageToWinHBITMAP(image, format); +} + /*! \since 5.2 @@ -152,6 +170,19 @@ QImage QtWin::imageFromHBITMAP(HDC hdc, HBITMAP bitmap, int width, int height) return qt_imageFromWinHBITMAP(hdc, bitmap, width, height); } +/*! + \since 5.12 + + Returns a QImage that is equivalent to the + given \a bitmap. The conversion is based on the specified \a format. + + \sa imageToHBITMAP() +*/ +QImage QtWin::imageFromHBITMAP(HBITMAP bitmap, QtWin::HBitmapFormat format) +{ + return qt_imageFromWinHBITMAP(bitmap, format); +} + /*! \since 5.2 diff --git a/src/winextras/qwinfunctions.h b/src/winextras/qwinfunctions.h index bc07401..fe461d1 100644 --- a/src/winextras/qwinfunctions.h +++ b/src/winextras/qwinfunctions.h @@ -81,7 +81,9 @@ namespace QtWin Q_WINEXTRAS_EXPORT HBITMAP toHBITMAP(const QPixmap &p, HBitmapFormat format = HBitmapNoAlpha); Q_WINEXTRAS_EXPORT QPixmap fromHBITMAP(HBITMAP bitmap, HBitmapFormat format = HBitmapNoAlpha); Q_WINEXTRAS_EXPORT HICON toHICON(const QPixmap &p); + Q_WINEXTRAS_EXPORT HBITMAP imageToHBITMAP(const QImage &image, QtWin::HBitmapFormat format = HBitmapNoAlpha); Q_WINEXTRAS_EXPORT QImage imageFromHBITMAP(HDC hdc, HBITMAP bitmap, int width, int height); + Q_WINEXTRAS_EXPORT QImage imageFromHBITMAP(HBITMAP bitmap, QtWin::HBitmapFormat format = HBitmapNoAlpha); Q_WINEXTRAS_EXPORT QPixmap fromHICON(HICON icon); Q_WINEXTRAS_EXPORT HRGN toHRGN(const QRegion ®ion); Q_WINEXTRAS_EXPORT QRegion fromHRGN(HRGN hrgn); diff --git a/tests/auto/qpixmap/tst_qpixmap.cpp b/tests/auto/qpixmap/tst_qpixmap.cpp index 8a63e25..8383194 100644 --- a/tests/auto/qpixmap/tst_qpixmap.cpp +++ b/tests/auto/qpixmap/tst_qpixmap.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -53,6 +54,8 @@ private slots: void fromHICON_data(); void fromHICON(); + void imageConversion(); + private: const QString m_dataDirectory; }; @@ -279,6 +282,21 @@ void tst_QPixmap::fromHICON() QVERIFY2(compareImages(imageFromHICON, imageFromFile, &errorMessage), errorMessage.constData()); } +void tst_QPixmap::imageConversion() +{ + // Extensive testing of all formats is done in QtGui; this merely tests + // the exports/linkage. + QImage image(73, 57, QImage::Format_ARGB32_Premultiplied); + image.fill(Qt::red); + QPainter painter(&image); + painter.drawLine(0, 0, image.width(), image.height()); + const HBITMAP hBitMap = QtWin::imageToHBITMAP(image); + QVERIFY(hBitMap); + const QImage fromHBitMap = QtWin::imageFromHBITMAP(hBitMap, QtWin::HBitmapPremultipliedAlpha); + QCOMPARE(fromHBitMap, image); + DeleteObject(hBitMap); +} + QTEST_MAIN(tst_QPixmap) #include "tst_qpixmap.moc" -- cgit v1.2.3