From 305a24553017b06620c38f2936348519aff721cd Mon Sep 17 00:00:00 2001 From: Giles Payne Date: Mon, 15 Jun 2020 23:15:05 +0900 Subject: [PATCH 1/4] Fix framework_name option in build script --- platforms/ios/build_framework.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platforms/ios/build_framework.py b/platforms/ios/build_framework.py index d0abf7038a..58d6517320 100755 --- a/platforms/ios/build_framework.py +++ b/platforms/ios/build_framework.py @@ -389,7 +389,7 @@ if __name__ == "__main__": parser.add_argument('--enable_nonfree', default=False, dest='enablenonfree', action='store_true', help='enable non-free modules (disabled by default)') parser.add_argument('--debug', default=False, dest='debug', action='store_true', help='Build "Debug" binaries (disabled by default)') parser.add_argument('--debug_info', default=False, dest='debug_info', action='store_true', help='Build with debug information (useful for Release mode: BUILD_WITH_DEBUG_INFO=ON)') - parser.add_argument('--framework_name', default='opencv2', dest='framework_name', action='store_true', help='Name of OpenCV framework (default: opencv2, will change to OpenCV in future version)') + parser.add_argument('--framework_name', default='opencv2', dest='framework_name', help='Name of OpenCV framework (default: opencv2, will change to OpenCV in future version)') parser.add_argument('--legacy_build', default=False, dest='legacy_build', action='store_true', help='Build legacy opencv2 framework (default: False, equivalent to "--framework_name=opencv2 --without=objc")') args = parser.parse_args() From 04bee3f9b70843e3ad59a5398d70c3e33f21e376 Mon Sep 17 00:00:00 2001 From: Giles Payne Date: Mon, 15 Jun 2020 23:16:22 +0900 Subject: [PATCH 2/4] Fix testFitEllipse test --- modules/imgproc/misc/objc/test/ImgprocTest.swift | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/modules/imgproc/misc/objc/test/ImgprocTest.swift b/modules/imgproc/misc/objc/test/ImgprocTest.swift index f9f89f3c88..c7668836fb 100644 --- a/modules/imgproc/misc/objc/test/ImgprocTest.swift +++ b/modules/imgproc/misc/objc/test/ImgprocTest.swift @@ -671,9 +671,12 @@ class ImgprocTest: OpenCVTestCase { let points = [Point2f(x: 0, y: 0), Point2f(x: -1, y: 1), Point2f(x: 1, y: 1), Point2f(x: 1, y: -1), Point2f(x: -1, y: -1)] let rrect = Imgproc.fitEllipse(points: points) - assertPoint2fEquals(Point2f(x: 0, y: 0), rrect.center, OpenCVTestCase.FEPS) - XCTAssertEqual(Float(2.828), rrect.size.width, accuracy: OpenCVTestCase.FEPS) - XCTAssertEqual(Float(2.828), rrect.size.height, accuracy: OpenCVTestCase.FEPS) + let FIT_ELLIPSE_CENTER_EPS:Float = 0.01 + let FIT_ELLIPSE_SIZE_EPS:Float = 0.4 + + assertPoint2fEquals(Point2f(x: 0, y: 0), rrect.center, FIT_ELLIPSE_CENTER_EPS) + XCTAssertEqual(Float(2.828), rrect.size.width, accuracy: FIT_ELLIPSE_SIZE_EPS) + XCTAssertEqual(Float(2.828), rrect.size.height, accuracy: FIT_ELLIPSE_SIZE_EPS) } func testFitLine() throws { From 13962b85332196c62d1ff74364e76f07baf4eff9 Mon Sep 17 00:00:00 2001 From: Giles Payne Date: Mon, 15 Jun 2020 23:17:19 +0900 Subject: [PATCH 3/4] Use cv::Ptr instead of raw pointers --- modules/core/misc/objc/common/Mat.h | 6 +-- modules/core/misc/objc/common/Mat.mm | 20 +++------- modules/objc/generator/gen_objc.py | 56 ++++++++++++++-------------- 3 files changed, 36 insertions(+), 46 deletions(-) diff --git a/modules/core/misc/objc/common/Mat.h b/modules/core/misc/objc/common/Mat.h index e1b249e87d..ece6dd2e8f 100644 --- a/modules/core/misc/objc/common/Mat.h +++ b/modules/core/misc/objc/common/Mat.h @@ -26,7 +26,7 @@ NS_ASSUME_NONNULL_BEGIN @interface Mat : NSObject #ifdef __cplusplus -@property(readonly) cv::Mat* nativePtr; +@property(readonly) cv::Ptr nativePtr; @property(readonly) cv::Mat& nativeRef; #endif @@ -35,8 +35,8 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)init; - (void)dealloc; #ifdef __cplusplus -- (instancetype)initWithNativeMat:(cv::Mat*)nativeMat; -+ (instancetype)fromNativePtr:(cv::Mat*)nativePtr; +- (instancetype)initWithNativeMat:(cv::Ptr)nativeMat; ++ (instancetype)fromNativePtr:(cv::Ptr)nativePtr; + (instancetype)fromNative:(cv::Mat&)nativeRef; #endif - (instancetype)initWithRows:(int)rows cols:(int)cols type:(int)type; diff --git a/modules/core/misc/objc/common/Mat.mm b/modules/core/misc/objc/common/Mat.mm index c75b7d8a94..c075e26046 100644 --- a/modules/core/misc/objc/common/Mat.mm +++ b/modules/core/misc/objc/common/Mat.mm @@ -39,39 +39,31 @@ static bool updateIdx(cv::Mat* mat, std::vector& indices, int inc) { } - (cv::Mat&)nativeRef { - return *(cv::Mat*)_nativePtr; + return *_nativePtr; } - (instancetype)init { self = [super init]; if (self) { - _nativePtr = new cv::Mat(); + _nativePtr = cv::Ptr(new cv::Mat()); } return self; } -- (void)dealloc { - if (_nativePtr != NULL) { - _nativePtr->release(); - delete _nativePtr; - } - _nsdata = NULL; -} - -- (instancetype)initWithNativeMat:(cv::Mat*)nativePtr { +- (instancetype)initWithNativeMat:(cv::Ptr)nativePtr { self = [super init]; if (self) { - _nativePtr = new cv::Mat(*nativePtr); + _nativePtr = nativePtr; } return self; } -+ (instancetype)fromNativePtr:(cv::Mat*)nativePtr { ++ (instancetype)fromNativePtr:(cv::Ptr)nativePtr { return [[Mat alloc] initWithNativeMat:nativePtr]; } + (instancetype)fromNative:(cv::Mat&)nativeRef { - return [[Mat alloc] initWithNativeMat:&nativeRef]; + return [[Mat alloc] initWithNativeMat:cv::Ptr(new cv::Mat(nativeRef))]; } - (instancetype)initWithRows:(int)rows cols:(int)cols type:(int)type { diff --git a/modules/objc/generator/gen_objc.py b/modules/objc/generator/gen_objc.py index 0e64fcab2a..335f5985f3 100755 --- a/modules/objc/generator/gen_objc.py +++ b/modules/objc/generator/gen_objc.py @@ -231,8 +231,12 @@ class ClassInfo(GeneralInfo): self.objc_name = m[1:] self.base = '' self.is_base_class = True + self.native_ptr_name = "nativePtr" if decl[1]: self.base = re.sub(r"^.*:", "", decl[1].split(",")[0]).strip().replace(self.objc_name, "") + if self.base: + self.is_base_class = False + self.native_ptr_name = "nativePtr" + self.objc_name def __repr__(self): return Template("CLASS $namespace::$classpath.$name : $base").substitute(**self.__dict__) @@ -296,7 +300,6 @@ class ClassInfo(GeneralInfo): if self.base: self.objc_header_template = T_OBJC_CLASS_HEADER self.objc_body_template = T_OBJC_CLASS_BODY - self.is_base_class = False else: self.base = "NSObject" if self.name != Module: @@ -326,19 +329,18 @@ class ClassInfo(GeneralInfo): nativePointerHandling = Template( """ #ifdef __cplusplus -@property(readonly)$cName* nativePtr; +@property(readonly)cv::Ptr<$cName> $native_ptr_name; #endif -- (void)dealloc; - #ifdef __cplusplus -- (instancetype)initWithNativePtr:($cName*)nativePtr; -+ (instancetype)fromNative:($cName*)nativePtr; +- (instancetype)initWithNativePtr:(cv::Ptr<$cName>)nativePtr; ++ (instancetype)fromNative:(cv::Ptr<$cName>)nativePtr; #endif """ ).substitute( - cName = self.fullName(isCPP=True) - ) if self.is_base_class else "", + cName = self.fullName(isCPP=True), + native_ptr_name = self.native_ptr_name + ), manualMethodDeclations = "", methodDeclarations = self.method_declarations.getvalue(), name = self.name, @@ -353,28 +355,24 @@ class ClassInfo(GeneralInfo): module = M, nativePointerHandling=Template( """ -- (void)dealloc { - if (_nativePtr != NULL) { - delete _nativePtr; - } -} - -- (instancetype)initWithNativePtr:($cName*)nativePtr { - self = [super init]; +- (instancetype)initWithNativePtr:(cv::Ptr<$cName>)nativePtr { + self = [super $init_call]; if (self) { - _nativePtr = nativePtr; + _$native_ptr_name = nativePtr; } return self; } -+ (instancetype)fromNative:($cName*)nativePtr { ++ (instancetype)fromNative:(cv::Ptr<$cName>)nativePtr { return [[$objcName alloc] initWithNativePtr:nativePtr]; } """ ).substitute( - cName=self.fullName(isCPP=True), - objcName=self.objc_name - ) if self.is_base_class else "", + cName = self.fullName(isCPP=True), + objcName = self.objc_name, + native_ptr_name = self.native_ptr_name, + init_call = "init" if self.is_base_class else "initWithNativePtr:nativePtr" + ), manualMethodDeclations = "", methodImplementations = self.method_implementations.getvalue(), name = self.name, @@ -576,7 +574,7 @@ class ObjectiveCWrapperGenerator(object): type_dict.setdefault(name, {}).update( { "objc_type" : classinfo.objc_name + "*", "from_cpp" : "[" + classinfo.objc_name + " fromNative:%(n)s]", - "to_cpp" : "*(" + classinfo.namespace.replace(".", "::") + "::" + classinfo.objc_name + "*)(%(n)s.nativePtr)" } + "to_cpp" : "*(%(n)s." + classinfo.native_ptr_name + ")" } ) # missing_consts { Module : { public : [[name, val],...], private : [[]...] } } @@ -596,7 +594,7 @@ class ObjectiveCWrapperGenerator(object): type_dict.setdefault("Ptr_"+name, {}).update( { "objc_type" : classinfo.objc_name + "*", "c_type" : name, - "to_cpp": "%(n)s.nativePtr", + "to_cpp": "%(n)s." + classinfo.native_ptr_name, "from_cpp_ptr": "[" + name + " fromNativePtr:%(n)s]"} ) logging.info('ok: class %s, name: %s, base: %s', classinfo, name, classinfo.base) @@ -934,19 +932,19 @@ class ObjectiveCWrapperGenerator(object): elif ret_type.startswith("Ptr_"): cpp_type = type_dict[ret_type]["c_type"] namespace_prefix = self.get_namespace_prefix(cpp_type) - ret_val = namespace_prefix + cpp_type + "* retVal = " + ret_val = "cv::Ptr<" + namespace_prefix + cpp_type + "> retVal = " ret = "return [" + type_dict[ret_type]["objc_type"][:-1] + " fromNative:retVal];" elif ret_type == "void": ret_val = "" ret = "" elif ret_type == "": # c-tor constructor = True - ret_val = "return [self initWithNativePtr:new " - tail = "]" + ret_val = "return [self initWithNativePtr:cv::Ptr<" + fi.fullClass(isCPP=True) + ">(new " + tail = ")]" ret = "" elif self.isWrapped(ret_type): # wrapped class namespace_prefix = self.get_namespace_prefix(ret_type) - ret_val = namespace_prefix + ret_type + "* retVal = new " + namespace_prefix + ret_type + "(" + ret_val = "cv::Ptr<" + namespace_prefix + ret_type + "> retVal = new " + namespace_prefix + ret_type + "(" tail = ")" ret_type_dict = type_dict[ret_type] from_cpp = ret_type_dict["from_cpp_ptr"] if ret_type_dict.has_key("from_cpp_ptr") else ret_type_dict["from_cpp"] @@ -993,7 +991,7 @@ class ObjectiveCWrapperGenerator(object): prologue = "\n " + "\n ".join(prologue) if prologue else "", epilogue = "\n " + "\n ".join(epilogue) if epilogue else "", static = "+" if static else "-", - obj_deref = ("MAKE_PTR(" + fi.fullClass(isCPP=True) + ")->" if not ci.is_base_class else "_nativePtr->") if not static and not constructor else "", + obj_deref = ("self." + ci.native_ptr_name + "->") if not static and not constructor else "", cv_name = fi.cv_name if static else fi.fullClass(isCPP=True) if constructor else fi.name, cv_args = ", ".join(cv_args), tail = tail @@ -1068,7 +1066,7 @@ typedef NS_ENUM(int, {2}) {{ objc_type = type_data.get("objc_type", pi.ctype) ci.addImports(pi.ctype, False) ci.method_declarations.write("@property " + ("(readonly) " if not pi.rw else "") + objc_type + " " + pi.name + ";\n") - ptr_ref = "MAKE_PTR(" + ci.fullName(isCPP=True) + ")->" if not ci.is_base_class else "_nativePtr->" + ptr_ref = "self." + ci.native_ptr_name + "->" if not ci.is_base_class else "self.nativePtr->" if type_data.has_key("v_type"): vector_type = type_data["v_type"] full_cpp_type = (self.get_namespace_prefix(vector_type) if (vector_type.find("::") == -1) else "") + vector_type From 177fbc7bbb554e973a16f9bd62841d2c0256d658 Mon Sep 17 00:00:00 2001 From: Giles Payne Date: Tue, 16 Jun 2020 08:15:27 +0900 Subject: [PATCH 4/4] Cleanup unneeded raw pointer handling code --- modules/core/misc/objc/common/CVObjcUtil.h | 2 -- modules/core/misc/objc/common/Mat.h | 1 - 2 files changed, 3 deletions(-) diff --git a/modules/core/misc/objc/common/CVObjcUtil.h b/modules/core/misc/objc/common/CVObjcUtil.h index be28f75af7..9d7d986708 100644 --- a/modules/core/misc/objc/common/CVObjcUtil.h +++ b/modules/core/misc/objc/common/CVObjcUtil.h @@ -15,8 +15,6 @@ typedef union { float f; int32_t i; } V32; #ifdef __cplusplus #import -#define MAKE_PTR(t) (*((cv::Ptr*)self.nativePtr)) - template std::vector objc2cv(NSArray* _Nonnull array, CV& (* _Nonnull converter)(OBJC* _Nonnull)) { std::vector ret; for (OBJC* obj in array) { diff --git a/modules/core/misc/objc/common/Mat.h b/modules/core/misc/objc/common/Mat.h index ece6dd2e8f..1542309277 100644 --- a/modules/core/misc/objc/common/Mat.h +++ b/modules/core/misc/objc/common/Mat.h @@ -33,7 +33,6 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - Constructors - (instancetype)init; -- (void)dealloc; #ifdef __cplusplus - (instancetype)initWithNativeMat:(cv::Ptr)nativeMat; + (instancetype)fromNativePtr:(cv::Ptr)nativePtr;