Fixed formatting of MPPImage+Utils.mm
This commit is contained in:
parent
4668d683d5
commit
b9c869494d
|
@ -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
|
||||||
|
|
||||||
|
@ -70,8 +68,8 @@ static void FreeRefConReleaseCallback(void *refCon, const void *baseAddress) {
|
||||||
error:(NSError **)error;
|
error:(NSError **)error;
|
||||||
|
|
||||||
+ (UInt8 *)pixelDataFromImageFrame:(ImageFrame &)imageFrame
|
+ (UInt8 *)pixelDataFromImageFrame:(ImageFrame &)imageFrame
|
||||||
shouldCopy:(BOOL)shouldCopy
|
shouldCopy:(BOOL)shouldCopy
|
||||||
error:(NSError **)error;
|
error:(NSError **)error;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -160,12 +156,12 @@ static void FreeRefConReleaseCallback(void *refCon, const void *baseAddress) {
|
||||||
|
|
||||||
// Uses default deleter
|
// Uses default deleter
|
||||||
return std::make_unique<ImageFrame>(imageFormat, width, height, destinationBytesPerRow,
|
return std::make_unique<ImageFrame>(imageFormat, width, height, destinationBytesPerRow,
|
||||||
static_cast<uint8 *>(destBuffer.data));
|
static_cast<uint8 *>(destBuffer.data));
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (UInt8 *)pixelDataFromImageFrame:(ImageFrame &)imageFrame
|
+ (UInt8 *)pixelDataFromImageFrame:(ImageFrame &)imageFrame
|
||||||
shouldCopy:(BOOL)shouldCopy
|
shouldCopy:(BOOL)shouldCopy
|
||||||
error:(NSError **)error {
|
error:(NSError **)error {
|
||||||
vImage_Buffer sourceBuffer = VImageBufferFromImageFrame(imageFrame);
|
vImage_Buffer sourceBuffer = VImageBufferFromImageFrame(imageFrame);
|
||||||
|
|
||||||
// Pre-multiply the raw pixels from a `mediapipe::Image` before creating a `CGImage` to ensure
|
// Pre-multiply the raw pixels from a `mediapipe::Image` before creating a `CGImage` to ensure
|
||||||
|
@ -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
|
||||||
|
@ -249,46 +245,45 @@ static void FreeRefConReleaseCallback(void *refCon, const void *baseAddress) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
UInt8 *pixelData = [MPPPixelDataUtils pixelDataFromImageFrame:imageFrame
|
UInt8 *pixelData = [MPPPixelDataUtils pixelDataFromImageFrame:imageFrame
|
||||||
shouldCopy:YES
|
shouldCopy:YES
|
||||||
error:error];
|
error:error];
|
||||||
|
|
||||||
if (!pixelData) {
|
if (!pixelData) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint8_t permute_map[4] = {2, 1, 0, 3};
|
const uint8_t permute_map[4] = {2, 1, 0, 3};
|
||||||
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) !=
|
||||||
[MPPCommonUtils createCustomError:error
|
kvImageNoError) {
|
||||||
withCode:MPPTasksErrorCodeInternalError
|
[MPPCommonUtils createCustomError:error
|
||||||
description:@"An internal error occured."];
|
withCode:MPPTasksErrorCodeInternalError
|
||||||
return NULL;
|
description:@"An internal error occured."];
|
||||||
}
|
return NULL;
|
||||||
|
}
|
||||||
CVPixelBufferRef outputBuffer;
|
|
||||||
|
|
||||||
OSType pixelBufferFormatType = kCVPixelFormatType_32BGRA;
|
CVPixelBufferRef outputBuffer;
|
||||||
|
|
||||||
|
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
|
||||||
withCode:MPPTasksErrorCodeInternalError
|
withCode:MPPTasksErrorCodeInternalError
|
||||||
description:@"An internal error occured."];
|
description:@"An internal error occured."];
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation MPPCGImageUtils
|
@implementation MPPCGImageUtils
|
||||||
|
@ -371,31 +366,31 @@ 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();
|
||||||
|
|
||||||
CGImageRef cgImageRef = NULL;
|
CGImageRef cgImageRef = NULL;
|
||||||
|
|
||||||
if (provider && colorSpace) {
|
if (provider && colorSpace) {
|
||||||
size_t bitsPerComponent = 8;
|
size_t bitsPerComponent = 8;
|
||||||
size_t channelCount = 4;
|
size_t channelCount = 4;
|
||||||
|
|
||||||
cgImageRef =
|
cgImageRef =
|
||||||
CGImageCreate(internalImageFrame->Width(), internalImageFrame->Height(), bitsPerComponent,
|
CGImageCreate(internalImageFrame->Width(), internalImageFrame->Height(), bitsPerComponent,
|
||||||
bitsPerComponent * channelCount, internalImageFrame->WidthStep(), colorSpace,
|
bitsPerComponent * channelCount, internalImageFrame->WidthStep(), colorSpace,
|
||||||
bitmapInfo, provider, nullptr, YES, kCGRenderingIntentDefault);
|
bitmapInfo, provider, nullptr, YES, kCGRenderingIntentDefault);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can safely pass `NULL` to these functions according to iOS docs.
|
// Can safely pass `NULL` to these functions according to iOS docs.
|
||||||
CGDataProviderRelease(provider);
|
CGDataProviderRelease(provider);
|
||||||
CGColorSpaceRelease(colorSpace);
|
CGColorSpaceRelease(colorSpace);
|
||||||
|
|
||||||
if (!cgImageRef) {
|
if (!cgImageRef) {
|
||||||
[MPPCommonUtils createCustomError:error
|
[MPPCommonUtils createCustomError:error
|
||||||
withCode:MPPTasksErrorCodeInternalError
|
withCode:MPPTasksErrorCodeInternalError
|
||||||
description:@"An internal error occured."];
|
description:@"An internal error occured."];
|
||||||
}
|
}
|
||||||
|
|
||||||
return cgImageRef;
|
return cgImageRef;
|
||||||
|
@ -455,10 +450,12 @@ static void FreeRefConReleaseCallback(void *refCon, const void *baseAddress) {
|
||||||
}
|
}
|
||||||
case MPPImageSourceTypePixelBuffer: {
|
case MPPImageSourceTypePixelBuffer: {
|
||||||
if (!shouldCopyPixelData) {
|
if (!shouldCopyPixelData) {
|
||||||
[MPPCommonUtils createCustomError:error
|
[MPPCommonUtils
|
||||||
withCode:MPPTasksErrorCodeInvalidArgumentError
|
createCustomError:error
|
||||||
description:@"When the source type is pixel buffer, you cannot request uncopied data"];
|
withCode:MPPTasksErrorCodeInvalidArgumentError
|
||||||
return nil;
|
description:
|
||||||
|
@"When the source type is pixel buffer, you cannot request uncopied data"];
|
||||||
|
return nil;
|
||||||
}
|
}
|
||||||
CVPixelBufferRef pixelBuffer =
|
CVPixelBufferRef pixelBuffer =
|
||||||
[MPPCVPixelBufferUtils cvPixelBufferFromImageFrame:*(image.GetImageFrameSharedPtr())
|
[MPPCVPixelBufferUtils cvPixelBufferFromImageFrame:*(image.GetImageFrameSharedPtr())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user