From 52e9c8b4be6718bda1a46859d6b1c13664c57c2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Mon, 9 Mar 2020 18:05:14 +0100 Subject: macOS: Streamline QImage to NSImage conversion The conversion uses NSBitmapImageRep and correctly sets the display pixel ratio and size of the resulting image, reducing the need for clients to deal with this. The function returns an auto-released object, as is customary for class-functions like these. Change-Id: I5124d1d8145a7f5266921b22fda1987798771ec1 Reviewed-by: Timur Pocheptsov --- src/gui/painting/qcoregraphics.mm | 41 +++++++++++++++++++++++++------------- src/gui/painting/qcoregraphics_p.h | 8 +++++++- 2 files changed, 34 insertions(+), 15 deletions(-) (limited to 'src/gui') diff --git a/src/gui/painting/qcoregraphics.mm b/src/gui/painting/qcoregraphics.mm index e2497eaadb..b5620535fb 100644 --- a/src/gui/painting/qcoregraphics.mm +++ b/src/gui/painting/qcoregraphics.mm @@ -47,6 +47,8 @@ #include #include +QT_USE_NAMESPACE + QT_BEGIN_NAMESPACE // ---------------------- Images ---------------------- @@ -124,23 +126,34 @@ QImage qt_mac_toQImage(CGImageRef image) #ifdef Q_OS_MACOS -static NSImage *qt_mac_cgimage_to_nsimage(CGImageRef image) -{ - NSImage *newImage = [[NSImage alloc] initWithCGImage:image size:NSZeroSize]; - return newImage; -} +QT_END_NAMESPACE -NSImage *qt_mac_create_nsimage(const QPixmap &pm) +@implementation NSImage (QtExtras) ++ (instancetype)imageFromQImage:(const QImage &)image { - if (pm.isNull()) - return 0; - QImage image = pm.toImage(); - CGImageRef cgImage = qt_mac_toCGImage(image); - NSImage *nsImage = qt_mac_cgimage_to_nsimage(cgImage); - nsImage.size = (pm.size() / pm.devicePixelRatioF()).toCGSize(); - CGImageRelease(cgImage); - return nsImage; + if (image.isNull()) + return nil; + + QCFType cgImage = image.toCGImage(); + if (!cgImage) + return nil; + + // We set up the NSImage using an explicit NSBitmapImageRep, instead of + // [NSImage initWithCGImage:size:], as the former allows us to correctly + // set the size of the representation to account for the device pixel + // ratio of the original image, which in turn will be reflected by the + // NSImage. + auto nsImage = [[NSImage alloc] initWithSize:NSZeroSize]; + auto *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage]; + imageRep.size = (image.size() / image.devicePixelRatioF()).toCGSize(); + [nsImage addRepresentation:[imageRep autorelease]]; + Q_ASSERT(CGSizeEqualToSize(nsImage.size, imageRep.size)); + + return [nsImage autorelease]; } +@end + +QT_BEGIN_NAMESPACE NSImage *qt_mac_create_nsimage(const QIcon &icon, int defaultSize) { diff --git a/src/gui/painting/qcoregraphics_p.h b/src/gui/painting/qcoregraphics_p.h index ba2cde8325..e1697b0f38 100644 --- a/src/gui/painting/qcoregraphics_p.h +++ b/src/gui/painting/qcoregraphics_p.h @@ -69,9 +69,15 @@ QT_BEGIN_NAMESPACE Q_GUI_EXPORT CGBitmapInfo qt_mac_bitmapInfoForImage(const QImage &image); #ifdef HAVE_APPKIT -Q_GUI_EXPORT NSImage *qt_mac_create_nsimage(const QPixmap &pm); Q_GUI_EXPORT NSImage *qt_mac_create_nsimage(const QIcon &icon, int defaultSize = 0); Q_GUI_EXPORT QPixmap qt_mac_toQPixmap(const NSImage *image, const QSizeF &size); + +QT_END_NAMESPACE +@interface NSImage (QtExtras) ++ (instancetype)imageFromQImage:(const QT_PREPEND_NAMESPACE(QImage) &)image; +@end +QT_BEGIN_NAMESPACE + #endif Q_GUI_EXPORT CGImageRef qt_mac_toCGImage(const QImage &qImage); Q_GUI_EXPORT CGImageRef qt_mac_toCGImageMask(const QImage &qImage); -- cgit v1.2.3