From d103d377d95c5f3f58e597ce133c718bed18e8fd Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Thu, 30 Jul 2026 09:33:57 +0000 Subject: [PATCH] imgcodecs: fix Apple conversions build with the macOS 10.5 SDK -[NSImage initWithCGImage:size:] and CGImageForProposedRect:context:hints: are 10.6+; fall back to NSBitmapImageRep on older SDKs. Define CF_RETURNS_RETAINED when the SDK does not provide it. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01NmeP4C25nM6MhcEXEpT6Ae --- modules/imgcodecs/src/apple_conversions.h | 5 +++++ modules/imgcodecs/src/macosx_conversions.mm | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/modules/imgcodecs/src/apple_conversions.h b/modules/imgcodecs/src/apple_conversions.h index 65d3a7e..1074e77 100644 --- a/modules/imgcodecs/src/apple_conversions.h +++ b/modules/imgcodecs/src/apple_conversions.h @@ -22,6 +22,11 @@ #import #endif +// Not defined by pre-10.6 SDKs +#ifndef CF_RETURNS_RETAINED +#define CF_RETURNS_RETAINED +#endif + CV_EXPORTS CGImageRef MatToCGImage(const cv::Mat& image) CF_RETURNS_RETAINED; CV_EXPORTS void CGImageToMat(const CGImageRef image, cv::Mat& m, bool alphaExist); diff --git a/modules/imgcodecs/src/macosx_conversions.mm b/modules/imgcodecs/src/macosx_conversions.mm index 960139e..dbac416 100644 --- a/modules/imgcodecs/src/macosx_conversions.mm +++ b/modules/imgcodecs/src/macosx_conversions.mm @@ -14,7 +14,15 @@ NSImage* MatToNSImage(const cv::Mat& image) { // Getting NSImage from CGImage NSSize imageSize = NSMakeSize(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)); +#if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && MAC_OS_X_VERSION_MAX_ALLOWED < 1060 + // -[NSImage initWithCGImage:size:] is available only on 10.6+ + NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:imageRef]; + NSImage *nsImage = [[NSImage alloc] initWithSize:imageSize]; + [nsImage addRepresentation:bitmapRep]; + [bitmapRep release]; +#else NSImage *nsImage = [[NSImage alloc] initWithCGImage:imageRef size:imageSize]; +#endif CGImageRelease(imageRef); @@ -22,6 +30,12 @@ NSImage* MatToNSImage(const cv::Mat& image) { } void NSImageToMat(const NSImage* image, cv::Mat& m, bool alphaExist) { +#if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && MAC_OS_X_VERSION_MAX_ALLOWED < 1060 + // -[NSImage CGImageForProposedRect:context:hints:] is available only on 10.6+ + NSBitmapImageRep *bitmapRep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]]; + CGImageRef imageRef = [bitmapRep CGImage]; +#else CGImageRef imageRef = [image CGImageForProposedRect:NULL context:NULL hints:NULL]; +#endif CGImageToMat(imageRef, m, alphaExist); }