forked from opencv/opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request opencv#18547 from mtfrctl:objc-conversions-macosx
Mat conversions for macOS/AppKit * Extract CoreGraphics conversion logics from ios_conversions.mm to apple_conversions.h, apple_conversions. Add macosx_conversions.mm * Add macosx.h * Add Mat+Conversions.h and Mat+Conversions.mm * Delete duplicated declaration from apple_conversion.mm * Use short license header * Add compile guard * Delete unused imports * Move precomp.hpp import from header to implementation * Add macosx.h to skip headers * Fix compile guard condition * Use short license header * Remove commented out unused code
- Loading branch information
Showing
11 changed files
with
248 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// This file is part of OpenCV project. | ||
// It is subject to the license terms in the LICENSE file found in the top-level directory | ||
// of this distribution and at http://opencv.org/license.html. | ||
|
||
#if !defined(__APPLE__) || !defined(__MACH__) | ||
#error This header should be used in macOS ObjC/Swift projects. | ||
#endif | ||
|
||
#import <AppKit/AppKit.h> | ||
#include "opencv2/core.hpp" | ||
|
||
//! @addtogroup imgcodecs_macosx | ||
//! @{ | ||
|
||
CV_EXPORTS CGImageRef MatToCGImage(const cv::Mat& image); | ||
CV_EXPORTS void CGImageToMat(const CGImageRef image, cv::Mat& m, bool alphaExist = false); | ||
CV_EXPORTS NSImage* MatToNSImage(const cv::Mat& image); | ||
CV_EXPORTS void NSImageToMat(const NSImage* image, cv::Mat& m, bool alphaExist = false); | ||
|
||
//! @} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// Mat+Converters.h | ||
// | ||
// Created by Masaya Tsuruta on 2020/10/08. | ||
// | ||
|
||
#pragma once | ||
|
||
#ifdef __cplusplus | ||
#import "opencv.hpp" | ||
#else | ||
#define CV_EXPORTS | ||
#endif | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <AppKit/AppKit.h> | ||
#import "Mat.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
CV_EXPORTS @interface Mat (Converters) | ||
|
||
-(CGImageRef)toCGImage; | ||
-(instancetype)initWithCGImage:(CGImageRef)image; | ||
-(instancetype)initWithCGImage:(CGImageRef)image alphaExist:(BOOL)alphaExist; | ||
-(NSImage*)toNSImage; | ||
-(instancetype)initWithNSImage:(NSImage*)image; | ||
-(instancetype)initWithNSImage:(NSImage*)image alphaExist:(BOOL)alphaExist; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// Mat+Converters.mm | ||
// | ||
// Created by Masaya Tsuruta on 2020/10/08. | ||
// | ||
|
||
#import "Mat+Converters.h" | ||
#import <opencv2/imgcodecs/macosx.h> | ||
|
||
@implementation Mat (Converters) | ||
|
||
-(CGImageRef)toCGImage { | ||
return MatToCGImage(self.nativeRef); | ||
} | ||
|
||
-(instancetype)initWithCGImage:(CGImageRef)image { | ||
return [self initWithCGImage:image alphaExist:NO]; | ||
} | ||
|
||
-(instancetype)initWithCGImage:(CGImageRef)image alphaExist:(BOOL)alphaExist { | ||
self = [self init]; | ||
if (self) { | ||
CGImageToMat(image, self.nativeRef, (bool)alphaExist); | ||
} | ||
return self; | ||
} | ||
|
||
-(NSImage*)toNSImage { | ||
return MatToNSImage(self.nativeRef); | ||
} | ||
|
||
-(instancetype)initWithNSImage:(NSImage*)image { | ||
return [self initWithNSImage:image alphaExist:NO]; | ||
} | ||
|
||
-(instancetype)initWithNSImage:(NSImage*)image alphaExist:(BOOL)alphaExist { | ||
self = [self init]; | ||
if (self) { | ||
NSImageToMat(image, self.nativeRef, (bool)alphaExist); | ||
} | ||
return self; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// This file is part of OpenCV project. | ||
// It is subject to the license terms in the LICENSE file found in the top-level directory | ||
// of this distribution and at http://opencv.org/license.html. | ||
|
||
#import <Accelerate/Accelerate.h> | ||
#import <AVFoundation/AVFoundation.h> | ||
#import <ImageIO/ImageIO.h> | ||
#include "opencv2/core.hpp" | ||
|
||
CV_EXPORTS CGImageRef MatToCGImage(const cv::Mat& image); | ||
CV_EXPORTS void CGImageToMat(const CGImageRef image, cv::Mat& m, bool alphaExist); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// This file is part of OpenCV project. | ||
// It is subject to the license terms in the LICENSE file found in the top-level directory | ||
// of this distribution and at http://opencv.org/license.html. | ||
|
||
#include "apple_conversions.h" | ||
#include "precomp.hpp" | ||
|
||
CGImageRef MatToCGImage(const cv::Mat& image) { | ||
NSData *data = [NSData dataWithBytes:image.data | ||
length:image.step.p[0] * image.rows]; | ||
|
||
CGColorSpaceRef colorSpace; | ||
|
||
if (image.elemSize() == 1) { | ||
colorSpace = CGColorSpaceCreateDeviceGray(); | ||
} else { | ||
colorSpace = CGColorSpaceCreateDeviceRGB(); | ||
} | ||
|
||
CGDataProviderRef provider = | ||
CGDataProviderCreateWithCFData((__bridge CFDataRef)data); | ||
|
||
// Preserve alpha transparency, if exists | ||
bool alpha = image.channels() == 4; | ||
CGBitmapInfo bitmapInfo = (alpha ? kCGImageAlphaLast : kCGImageAlphaNone) | kCGBitmapByteOrderDefault; | ||
|
||
// Creating CGImage from cv::Mat | ||
CGImageRef imageRef = CGImageCreate(image.cols, | ||
image.rows, | ||
8 * image.elemSize1(), | ||
8 * image.elemSize(), | ||
image.step.p[0], | ||
colorSpace, | ||
bitmapInfo, | ||
provider, | ||
NULL, | ||
false, | ||
kCGRenderingIntentDefault | ||
); | ||
|
||
CGDataProviderRelease(provider); | ||
CGColorSpaceRelease(colorSpace); | ||
|
||
return imageRef; | ||
} | ||
|
||
void CGImageToMat(const CGImageRef image, cv::Mat& m, bool alphaExist) { | ||
CGColorSpaceRef colorSpace = CGImageGetColorSpace(image); | ||
CGFloat cols = CGImageGetWidth(image), rows = CGImageGetHeight(image); | ||
CGContextRef contextRef; | ||
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast; | ||
if (CGColorSpaceGetModel(colorSpace) == kCGColorSpaceModelMonochrome) | ||
{ | ||
m.create(rows, cols, CV_8UC1); // 8 bits per component, 1 channel | ||
bitmapInfo = kCGImageAlphaNone; | ||
if (!alphaExist) | ||
bitmapInfo = kCGImageAlphaNone; | ||
else | ||
m = cv::Scalar(0); | ||
contextRef = CGBitmapContextCreate(m.data, m.cols, m.rows, 8, | ||
m.step[0], colorSpace, | ||
bitmapInfo); | ||
} | ||
else if (CGColorSpaceGetModel(colorSpace) == kCGColorSpaceModelIndexed) | ||
{ | ||
// CGBitmapContextCreate() does not support indexed color spaces. | ||
colorSpace = CGColorSpaceCreateDeviceRGB(); | ||
m.create(rows, cols, CV_8UC4); // 8 bits per component, 4 channels | ||
if (!alphaExist) | ||
bitmapInfo = kCGImageAlphaNoneSkipLast | | ||
kCGBitmapByteOrderDefault; | ||
else | ||
m = cv::Scalar(0); | ||
contextRef = CGBitmapContextCreate(m.data, m.cols, m.rows, 8, | ||
m.step[0], colorSpace, | ||
bitmapInfo); | ||
CGColorSpaceRelease(colorSpace); | ||
} | ||
else | ||
{ | ||
m.create(rows, cols, CV_8UC4); // 8 bits per component, 4 channels | ||
if (!alphaExist) | ||
bitmapInfo = kCGImageAlphaNoneSkipLast | | ||
kCGBitmapByteOrderDefault; | ||
else | ||
m = cv::Scalar(0); | ||
contextRef = CGBitmapContextCreate(m.data, m.cols, m.rows, 8, | ||
m.step[0], colorSpace, | ||
bitmapInfo); | ||
} | ||
CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), | ||
image); | ||
CGContextRelease(contextRef); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// This file is part of OpenCV project. | ||
// It is subject to the license terms in the LICENSE file found in the top-level directory | ||
// of this distribution and at http://opencv.org/license.html. | ||
|
||
#import <AppKit/AppKit.h> | ||
#include "apple_conversions.h" | ||
|
||
CV_EXPORTS NSImage* MatToNSImage(const cv::Mat& image); | ||
CV_EXPORTS void NSImageToMat(const NSImage* image, cv::Mat& m, bool alphaExist); | ||
|
||
NSImage* MatToNSImage(const cv::Mat& image) { | ||
// Creating CGImage from cv::Mat | ||
CGImageRef imageRef = MatToCGImage(image); | ||
|
||
// Getting NSImage from CGImage | ||
NSImage *nsImage = [[NSImage alloc] initWithCGImage:imageRef size:CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef))]; | ||
CGImageRelease(imageRef); | ||
|
||
return nsImage; | ||
} | ||
|
||
void NSImageToMat(const NSImage* image, cv::Mat& m, bool alphaExist) { | ||
CGImageRef imageRef = [image CGImageForProposedRect:NULL context:NULL hints:NULL]; | ||
CGImageToMat(imageRef, m, alphaExist); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters