Removed generic methods for alloc and memcpy from MPPMask

This commit is contained in:
Prianka Liz Kariat 2023-06-15 16:07:56 +05:30
parent 327547ec2b
commit 1f77fa9de4
2 changed files with 18 additions and 25 deletions

View File

@ -95,10 +95,8 @@ NS_SWIFT_NAME(Mask)
width:(NSInteger)width width:(NSInteger)width
height:(NSInteger)height NS_DESIGNATED_INITIALIZER; height:(NSInteger)height NS_DESIGNATED_INITIALIZER;
// TODO: Add methods for CVPixelBuffer conversion. // TODO: Add methods for CVPixelBuffer conversion.
/** Unavailable. */ /** Unavailable. */
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;

View File

@ -16,19 +16,6 @@
#import "mediapipe/tasks/ios/common/sources/MPPCommon.h" #import "mediapipe/tasks/ios/common/sources/MPPCommon.h"
#import "mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.h" #import "mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.h"
namespace {
template <typename T>
T *allocateDataPtr(std::unique_ptr<T[]> &data, size_t length) {
data = std::unique_ptr<T[]>(new T[length]);
return data.get();
}
template <typename T>
void copyData(const T *destination, const T *source, size_t length) {
memcpy((void *)destination, source, length * sizeof(T));
}
} // namespace
@interface MPPMask () { @interface MPPMask () {
const UInt8 *_uint8Data; const UInt8 *_uint8Data;
const float *_float32Data; const float *_float32Data;
@ -84,8 +71,10 @@ void copyData(const T *destination, const T *source, size_t length) {
height:(NSInteger)height { height:(NSInteger)height {
self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeUInt8 error:nil]; self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeUInt8 error:nil];
if (self) { if (self) {
_uint8Data = allocateDataPtr(_uint8DataPtr, _width * _height); size_t length = _width * _height;
copyData(_uint8Data, uint8DataToCopy, _width * _height); _uint8DataPtr = std::unique_ptr<UInt8[]>(new UInt8[length]);
_uint8Data = _uint8DataPtr.get();
memcpy((UInt8 *)_uint8Data, uint8DataToCopy, length * sizeof(UInt8));
} }
return self; return self;
} }
@ -95,8 +84,10 @@ void copyData(const T *destination, const T *source, size_t length) {
height:(NSInteger)height { height:(NSInteger)height {
self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeFloat32 error:nil]; self = [self initWithWidth:width height:height dataType:MPPMaskDataTypeFloat32 error:nil];
if (self) { if (self) {
_float32Data = allocateDataPtr(_float32DataPtr, _width * _height); size_t length = _width * _height;
copyData(_float32Data, float32DataToCopy, _width * _height); _float32DataPtr = std::unique_ptr<float[]>(new float[length]);
_float32Data = _float32DataPtr.get();
memcpy((float *)_float32Data, float32DataToCopy, length * sizeof(float));
} }
return self; return self;
} }
@ -110,8 +101,11 @@ void copyData(const T *destination, const T *source, size_t length) {
if (_uint8DataPtr) { if (_uint8DataPtr) {
return _uint8DataPtr.get(); 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<UInt8[]>(new UInt8[length]);
UInt8 *data = _uint8DataPtr.get();
for (int i = 0; i < length; i++) {
data[i] = _float32Data[i] * 255; data[i] = _float32Data[i] * 255;
} }
return data; return data;
@ -125,14 +119,15 @@ void copyData(const T *destination, const T *source, size_t length) {
switch (_dataType) { switch (_dataType) {
case MPPMaskDataTypeUInt8: { case MPPMaskDataTypeUInt8: {
if (_float32DataPtr) { if (_float32DataPtr) {
NSLog(@"Get repeated");
return _float32DataPtr.get(); 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<float[]>(new float[length]);
float *data = _float32DataPtr.get();
for (int i = 0; i < length; i++) {
data[i] = (float)_uint8Data[i] / 255; data[i] = (float)_uint8Data[i] / 255;
} }
NSLog(@"Get new");
return data; return data;
} }
case MPPMaskDataTypeFloat32: { case MPPMaskDataTypeFloat32: {