Fixed formatting of MPPImage+Utils.mm

This commit is contained in:
Prianka Liz Kariat 2023-10-19 19:59:59 +05:30
parent 4668d683d5
commit b9c869494d

View File

@ -54,9 +54,7 @@ static void FreeDataProviderReleaseCallback(void *buffer, const void *data, size
delete[] buffer; delete[] buffer;
} }
static void FreeRefConReleaseCallback(void *refCon, const void *baseAddress) { static void FreeRefConReleaseCallback(void *refCon, const void *baseAddress) { delete[] refCon; }
delete[] refCon;
}
} // namespace } // namespace
@ -80,9 +78,7 @@ static void FreeRefConReleaseCallback(void *refCon, const void *baseAddress) {
+ (std::unique_ptr<ImageFrame>)imageFrameFromCVPixelBuffer:(CVPixelBufferRef)pixelBuffer + (std::unique_ptr<ImageFrame>)imageFrameFromCVPixelBuffer:(CVPixelBufferRef)pixelBuffer
error:(NSError **)error; error:(NSError **)error;
+ (CVPixelBufferRef)cvPixelBufferFromImageFrame:(ImageFrame &)imageFrame error:(NSError **)error;
+ (CVPixelBufferRef)cvPixelBufferFromImageFrame:(ImageFrame &)imageFrame
error:(NSError **)error;
@end @end
@interface MPPCGImageUtils : NSObject @interface MPPCGImageUtils : NSObject
@ -177,7 +173,8 @@ static void FreeRefConReleaseCallback(void *refCon, const void *baseAddress) {
case ImageFormat::SRGBA: { case ImageFormat::SRGBA: {
destinationBuffer = destinationBuffer =
shouldCopy ? EmptyVImageBufferFromImageFrame(imageFrame, true) : sourceBuffer; shouldCopy ? EmptyVImageBufferFromImageFrame(imageFrame, true) : sourceBuffer;
premultiplyError = vImagePremultiplyData_RGBA8888(&sourceBuffer, &destinationBuffer, kvImageNoFlags); premultiplyError =
vImagePremultiplyData_RGBA8888(&sourceBuffer, &destinationBuffer, kvImageNoFlags);
break; break;
} }
default: { default: {
@ -224,16 +221,15 @@ static void FreeRefConReleaseCallback(void *refCon, const void *baseAddress) {
default: { default: {
[MPPCommonUtils createCustomError:error [MPPCommonUtils createCustomError:error
withCode:MPPTasksErrorCodeInvalidArgumentError withCode:MPPTasksErrorCodeInvalidArgumentError
description:@"Unsupported pixel format for CVPixelBuffer. Supported pixel format is kCVPixelFormatType_32BGRA"]; description:@"Unsupported pixel format for CVPixelBuffer. Supported "
@"pixel format is kCVPixelFormatType_32BGRA"];
} }
} }
return imageFrame; return imageFrame;
} }
+ (CVPixelBufferRef)cvPixelBufferFromImageFrame:(ImageFrame &)imageFrame + (CVPixelBufferRef)cvPixelBufferFromImageFrame:(ImageFrame &)imageFrame error:(NSError **)error {
error:(NSError **)error {
// Supporting only RGBA and BGRA since creation of CVPixelBuffers with RGB format // Supporting only RGBA and BGRA since creation of CVPixelBuffers with RGB format
// is restrictred in iOS. Thus, the APIs will never receive an input pixel buffer in RGB format // is restrictred in iOS. Thus, the APIs will never receive an input pixel buffer in RGB format
// and in turn the resulting image frame will never be of the RGB format. Moreover, writing unit // and in turn the resulting image frame will never be of the RGB format. Moreover, writing unit
@ -261,7 +257,8 @@ static void FreeRefConReleaseCallback(void *refCon, const void *baseAddress) {
vImage_Buffer sourceBuffer = EmptyVImageBufferFromImageFrame(imageFrame, NO); vImage_Buffer sourceBuffer = EmptyVImageBufferFromImageFrame(imageFrame, NO);
sourceBuffer.data = pixelData; sourceBuffer.data = pixelData;
if (vImagePermuteChannels_ARGB8888(&sourceBuffer, &sourceBuffer, permute_map, kvImageNoFlags) != kvImageNoError) { if (vImagePermuteChannels_ARGB8888(&sourceBuffer, &sourceBuffer, permute_map, kvImageNoFlags) !=
kvImageNoError) {
[MPPCommonUtils createCustomError:error [MPPCommonUtils createCustomError:error
withCode:MPPTasksErrorCodeInternalError withCode:MPPTasksErrorCodeInternalError
description:@"An internal error occured."]; description:@"An internal error occured."];
@ -272,14 +269,13 @@ static void FreeRefConReleaseCallback(void *refCon, const void *baseAddress) {
OSType pixelBufferFormatType = kCVPixelFormatType_32BGRA; OSType pixelBufferFormatType = kCVPixelFormatType_32BGRA;
// If pixel data is copied, then pass in a release callback that will be invoked when the // If pixel data is copied, then pass in a release callback that will be invoked when the
// pixel buffer is destroyed. If data is not copied, the responsibility of deletion is on the // pixel buffer is destroyed. If data is not copied, the responsibility of deletion is on the
// owner of the data (a.k.a C++ Image Frame). // owner of the data (a.k.a C++ Image Frame).
if (CVPixelBufferCreateWithBytes(kCFAllocatorDefault, imageFrame.Width(), imageFrame.Height(), if (CVPixelBufferCreateWithBytes(kCFAllocatorDefault, imageFrame.Width(), imageFrame.Height(),
pixelBufferFormatType, pixelData, imageFrame.WidthStep(), pixelBufferFormatType, pixelData, imageFrame.WidthStep(),
FreeRefConReleaseCallback, FreeRefConReleaseCallback, pixelData, NULL,
pixelData, NULL, &outputBuffer) == kCVReturnSuccess) { &outputBuffer) == kCVReturnSuccess) {
return outputBuffer; return outputBuffer;
} }
[MPPCommonUtils createCustomError:error [MPPCommonUtils createCustomError:error
@ -288,7 +284,6 @@ static void FreeRefConReleaseCallback(void *refCon, const void *baseAddress) {
return NULL; return NULL;
} }
@end @end
@implementation MPPCGImageUtils @implementation MPPCGImageUtils
@ -371,8 +366,8 @@ static void FreeRefConReleaseCallback(void *refCon, const void *baseAddress) {
CGDataProviderReleaseDataCallback callback = nullptr; CGDataProviderReleaseDataCallback callback = nullptr;
CGDataProviderRef provider = CGDataProviderCreateWithData( CGDataProviderRef provider = CGDataProviderCreateWithData(
pixelData, pixelData, pixelData, pixelData, internalImageFrame->WidthStep() * internalImageFrame->Height(),
internalImageFrame->WidthStep() * internalImageFrame->Height(), callback); callback);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
@ -455,9 +450,11 @@ static void FreeRefConReleaseCallback(void *refCon, const void *baseAddress) {
} }
case MPPImageSourceTypePixelBuffer: { case MPPImageSourceTypePixelBuffer: {
if (!shouldCopyPixelData) { if (!shouldCopyPixelData) {
[MPPCommonUtils createCustomError:error [MPPCommonUtils
createCustomError:error
withCode:MPPTasksErrorCodeInvalidArgumentError withCode:MPPTasksErrorCodeInvalidArgumentError
description:@"When the source type is pixel buffer, you cannot request uncopied data"]; description:
@"When the source type is pixel buffer, you cannot request uncopied data"];
return nil; return nil;
} }
CVPixelBufferRef pixelBuffer = CVPixelBufferRef pixelBuffer =