From 0b04afa015eb3677e0c46cc7fac91634c8976b25 Mon Sep 17 00:00:00 2001 From: Robin Burchell Date: Mon, 25 Sep 2017 13:57:23 +0200 Subject: [PATCH] QGtkHelpers: Fix QImage -> GdkPixmap conversion for all cases If this isn't in one of the two correct formats, make it so. Also check that the image actually has alpha before telling GdkPixmap it does. This fixes copying images in some cases having a whacky color in Telegram Desktop. --- src/qgtkhelpers.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/qgtkhelpers.cpp b/src/qgtkhelpers.cpp index 79c5865..1204674 100644 --- a/src/qgtkhelpers.cpp +++ b/src/qgtkhelpers.cpp @@ -26,16 +26,26 @@ #include "qgtkhelpers.h" -QGtkRefPtr qt_imageToPixbuf(const QImage &image) +QGtkRefPtr qt_imageToPixbuf(const QImage &cimage) { - if (image.isNull()) + if (cimage.isNull()) return 0; + QImage image = cimage; + if (image.hasAlphaChannel()) { + if (image.format() != QImage::Format_RGBA8888) { + image = image.convertToFormat(QImage::Format_RGBA8888); + } + } else { + if (image.format() != QImage::Format_RGB888) { + image = image.convertToFormat(QImage::Format_RGB888); + } + } guchar *buf = (guchar*)malloc(image.byteCount()); memcpy(buf, image.constBits(), image.byteCount()); QGtkRefPtr gpb = gdk_pixbuf_new_from_data( buf, GDK_COLORSPACE_RGB, - true, // ### assert image is in RGBA + image.hasAlphaChannel(), 8, image.width(), image.height(),