From 1f77fa9de43a9d0f995f4e9981478291aaefb701 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Thu, 15 Jun 2023 16:07:56 +0530 Subject: [PATCH] Removed generic methods for alloc and memcpy from MPPMask --- .../tasks/ios/vision/core/sources/MPPMask.h | 2 - .../tasks/ios/vision/core/sources/MPPMask.mm | 41 ++++++++----------- 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h index 0df60d7d8..65af32d10 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.h @@ -95,10 +95,8 @@ NS_SWIFT_NAME(Mask) width:(NSInteger)width height:(NSInteger)height NS_DESIGNATED_INITIALIZER; - // TODO: Add methods for CVPixelBuffer conversion. - /** Unavailable. */ - (instancetype)init NS_UNAVAILABLE; diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm index 5aac59ec2..84a4eb4b5 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm +++ b/mediapipe/tasks/ios/vision/core/sources/MPPMask.mm @@ -16,19 +16,6 @@ #import "mediapipe/tasks/ios/common/sources/MPPCommon.h" #import "mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.h" -namespace { -template -T *allocateDataPtr(std::unique_ptr &data, size_t length) { - data = std::unique_ptr(new T[length]); - return data.get(); -} - -template -void copyData(const T *destination, const T *source, size_t length) { - memcpy((void *)destination, source, length * sizeof(T)); -} -} // namespace - @interface MPPMask () { const UInt8 *_uint8Data; const float *_float32Data; @@ -84,8 +71,10 @@ void copyData(const T *destination, const T *source, size_t length) { height:(NSInteger)height { self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeUInt8 error:nil]; if (self) { - _uint8Data = allocateDataPtr(_uint8DataPtr, _width * _height); - copyData(_uint8Data, uint8DataToCopy, _width * _height); + size_t length = _width * _height; + _uint8DataPtr = std::unique_ptr(new UInt8[length]); + _uint8Data = _uint8DataPtr.get(); + memcpy((UInt8 *)_uint8Data, uint8DataToCopy, length * sizeof(UInt8)); } return self; } @@ -95,8 +84,10 @@ void copyData(const T *destination, const T *source, size_t length) { height:(NSInteger)height { self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeFloat32 error:nil]; if (self) { - _float32Data = allocateDataPtr(_float32DataPtr, _width * _height); - copyData(_float32Data, float32DataToCopy, _width * _height); + size_t length = _width * _height; + _float32DataPtr = std::unique_ptr(new float[length]); + _float32Data = _float32DataPtr.get(); + memcpy((float *)_float32Data, float32DataToCopy, length * sizeof(float)); } return self; } @@ -110,8 +101,11 @@ void copyData(const T *destination, const T *source, size_t length) { if (_uint8DataPtr) { return _uint8DataPtr.get(); } - UInt8 *data = allocateDataPtr(_uint8DataPtr, _width * _height); - for (int i = 0; i < _width * _height; i++) { + + size_t length = _width * _height; + _uint8DataPtr = std::unique_ptr(new UInt8[length]); + UInt8 *data = _uint8DataPtr.get(); + for (int i = 0; i < length; i++) { data[i] = _float32Data[i] * 255; } return data; @@ -125,14 +119,15 @@ void copyData(const T *destination, const T *source, size_t length) { switch (_dataType) { case MPPMaskDataTypeUInt8: { if (_float32DataPtr) { - NSLog(@"Get repeated"); return _float32DataPtr.get(); } - float *data = allocateDataPtr(_float32DataPtr, _width * _height); - for (int i = 0; i < _width * _height; i++) { + + size_t length = _width * _height; + _float32DataPtr = std::unique_ptr(new float[length]); + float *data = _float32DataPtr.get(); + for (int i = 0; i < length; i++) { data[i] = (float)_uint8Data[i] / 255; } - NSLog(@"Get new"); return data; } case MPPMaskDataTypeFloat32: {