Revert "Revert "Updated init method implementations in MPPMask""

This reverts commit 52f6b8d899.
This commit is contained in:
Prianka Liz Kariat 2023-06-16 19:56:32 +05:30
parent 52f6b8d899
commit fec2fc77e0
3 changed files with 39 additions and 36 deletions

View File

@ -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

View File

@ -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.

View File

@ -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<float[]>(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<UInt8[]>(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<float[]>(new float[length]);
_float32Data = _float32DataPtr.get();
memcpy((float *)_float32Data, float32DataToCopy, length * sizeof(float));
if (shouldCopy) {
size_t length = _width * _height;
_uint8DataPtr = std::unique_ptr<UInt8[]>(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];
}
}