From 83486ed01b7e948b4d7dd0d8f356a3ae4970821c Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Fri, 16 Jun 2023 19:56:04 +0530 Subject: [PATCH] Updated init method implementations in MPPMask --- mediapipe/framework/tool/ios.bzl | 2 +- .../tasks/ios/vision/core/sources/MPPMask.h | 17 +++++- .../tasks/ios/vision/core/sources/MPPMask.mm | 56 ++++++++----------- 3 files changed, 39 insertions(+), 36 deletions(-) diff --git a/mediapipe/framework/tool/ios.bzl b/mediapipe/framework/tool/ios.bzl index c97b092e1..a0fe0be55 100644 --- a/mediapipe/framework/tool/ios.bzl +++ b/mediapipe/framework/tool/ios.bzl @@ -14,7 +14,7 @@ """MediaPipe Task Library Helper Rules for iOS""" -MPP_TASK_MINIMUM_OS_VERSION = "11.0" +MPP_TASK_MINIMUM_OS_VERSION = "12.0" # When the static framework is built with bazel, the all header files are moved # to the "Headers" directory with no header path prefixes. This auxiliary rule diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h index 65af32d10..176e9b20d 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h @@ -71,21 +71,31 @@ NS_SWIFT_NAME(Mask) * Initializes an `MPPMask` object of type `MPPMaskDataTypeUInt8` with the given `UInt8*` data, * width and height. * + * If `shouldCopy` is set to `YES`, the newly created `MPPMask` stores a reference to a deep copied + * `uint8Data`. Since deep copies are expensive, it is recommended to not set `shouldCopy` unless + * the `MPPMask` must outlive the passed in `uint8Data`. + * * @param uint8Data A pointer to the memory location of the `UInt8` data array. * @param width The width of the mask. * @param height The height of the mask. + * @param shouldCopy The height of the mask. * * @return A new `MPPMask` instance with the given `UInt8*` data, width and height. */ - (nullable instancetype)initWithUInt8Data:(const UInt8 *)uint8Data width:(NSInteger)width - height:(NSInteger)height NS_DESIGNATED_INITIALIZER; + height:(NSInteger)height + shouldCopy:(BOOL)shouldCopy NS_DESIGNATED_INITIALIZER; /** * Initializes an `MPPMask` object of type `MPPMaskDataTypeFloat32` with the given `float*` data, * width and height. * - * @param uint8Data A pointer to the memory location of the `float` data array. + * If `shouldCopy` is set to `YES`, the newly created `MPPMask` stores a reference to a deep copied + * `float32Data`. Since deep copies are expensive, it is recommended to not set `shouldCopy` unless + * the `MPPMask` must outlive the passed in `float32Data`. + * + * @param float32Data A pointer to the memory location of the `float` data array. * @param width The width of the mask. * @param height The height of the mask. * @@ -93,7 +103,8 @@ NS_SWIFT_NAME(Mask) */ - (nullable instancetype)initWithFloat32Data:(const float *)float32Data width:(NSInteger)width - height:(NSInteger)height NS_DESIGNATED_INITIALIZER; + height:(NSInteger)height + shouldCopy:(BOOL)shouldCopy NS_DESIGNATED_INITIALIZER; // TODO: Add methods for CVPixelBuffer conversion. diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm index 84a4eb4b5..3342218a6 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm @@ -48,46 +48,36 @@ - (nullable instancetype)initWithUInt8Data:(const UInt8 *)uint8Data width:(NSInteger)width - height:(NSInteger)height { + height:(NSInteger)height + shouldCopy:(BOOL)shouldCopy { self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeUInt8 error:nil]; if (self) { - _uint8Data = uint8Data; + if (shouldCopy) { + size_t length = _width * _height; + _float32DataPtr = std::unique_ptr(new float[length]); + _float32Data = _float32DataPtr.get(); + memcpy((float *)_float32Data, float32DataToCopy, length * sizeof(float)); + } else { + _uint8Data = uint8Data; + } } return self; } - (nullable instancetype)initWithFloat32Data:(const float *)float32Data width:(NSInteger)width - height:(NSInteger)height { + height:(NSInteger)height + shouldCopy:(BOO)shouldCopy { self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeFloat32 error:nil]; if (self) { - _float32Data = float32Data; - } - return self; -} - -- (instancetype)initWithUInt8DataToCopy:(const UInt8 *)uint8DataToCopy - width:(NSInteger)width - height:(NSInteger)height { - self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeUInt8 error:nil]; - if (self) { - size_t length = _width * _height; - _uint8DataPtr = std::unique_ptr(new UInt8[length]); - _uint8Data = _uint8DataPtr.get(); - memcpy((UInt8 *)_uint8Data, uint8DataToCopy, length * sizeof(UInt8)); - } - return self; -} - -- (instancetype)initWithFloat32DataToCopy:(const float *)float32DataToCopy - width:(NSInteger)width - height:(NSInteger)height { - self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeFloat32 error:nil]; - if (self) { - size_t length = _width * _height; - _float32DataPtr = std::unique_ptr(new float[length]); - _float32Data = _float32DataPtr.get(); - memcpy((float *)_float32Data, float32DataToCopy, length * sizeof(float)); + if (shouldCopy) { + size_t length = _width * _height; + _uint8DataPtr = std::unique_ptr(new UInt8[length]); + _uint8Data = _uint8DataPtr.get(); + memcpy((UInt8 *)_uint8Data, uint8DataToCopy, length * sizeof(UInt8)); + } else { + _float32Data = float32Data; + } } return self; } @@ -143,11 +133,13 @@ case MPPMaskDataTypeUInt8: return [[MPPMask alloc] initWithUInt8DataToCopy:self.uint8Data width:self.width - height:self.height]; + height:self.height + shouldCopy:YES]; case MPPMaskDataTypeFloat32: return [[MPPMask alloc] initWithFloat32DataToCopy:self.float32Data width:self.width - height:self.height]; + height:self.height + shouldCopy:YES]; } }