From e74cc53775f999bb64b7e890a6ebaec82be44699 Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Mon, 8 Feb 2021 18:56:10 +0100 Subject: [PATCH] Fix creating CGImage from wxBitmap For internal purposes wxBitmap with mask should be converted to CGImage with alpha values only (with no mask). This is i.a. needed to assure compatibility of wxGraphicsBitmap format with other ports where these bitmaps are in pure ARGB format. --- src/osx/core/bitmap.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/osx/core/bitmap.cpp b/src/osx/core/bitmap.cpp index b08535ee65..178c8d02ff 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -1,3 +1,7 @@ + + + + ///////////////////////////////////////////////////////////////////////////// // Name: src/osx/core/bitmap.cpp // Purpose: wxBitmap @@ -672,13 +676,25 @@ CGImageRef wxBitmapRefData::CreateCGImage() const if ( m_bitmapMask ) { + // First apply mask to image CGImageRef imageMask = CGBitmapContextCreateImage(m_bitmapMask->GetHBITMAP()); - CGImageRef imageBmp = image; + CGImageRef imageMasked = CGImageCreateWithMask(image, imageMask); - image = CGImageCreateWithMask(imageBmp, imageMask); + // Convert masked image to plain ARGB image without mask + int w = GetWidth(); + int h = GetHeight(); + CGContextRef hBmpAlpha = CGBitmapContextCreate(NULL, w, h, 8, GetBytesPerRow(), wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst); + CGRect r = CGRectMake(0, 0, w, h); + CGContextDrawImage(hBmpAlpha, r, imageMasked); + CGContextTranslateCTM(hBmpAlpha, 0, h); + CGContextScaleCTM(hBmpAlpha, GetScaleFactor(), -GetScaleFactor()); - CGImageRelease(imageBmp); + CGImageRelease(imageMasked); CGImageRelease(imageMask); + + CGImageRelease(image); + image = CGBitmapContextCreateImage(hBmpAlpha); + CGContextRelease(hBmpAlpha); } } else