diff --git a/mediapipe/tasks/ios/vision/core/utils/sources/MPPImage+Utils.mm b/mediapipe/tasks/ios/vision/core/utils/sources/MPPImage+Utils.mm index 87f9a8a3b..0f55f6459 100644 --- a/mediapipe/tasks/ios/vision/core/utils/sources/MPPImage+Utils.mm +++ b/mediapipe/tasks/ios/vision/core/utils/sources/MPPImage+Utils.mm @@ -133,6 +133,9 @@ using ::mediapipe::ImageFrame; size_t height = CVPixelBufferGetHeight(pixelBuffer); size_t stride = CVPixelBufferGetBytesPerRow(pixelBuffer); + size_t destinationChannelCount = 3; + size_t destinationStride = destinationChannelCount * width; + uint8_t *rgbPixelData = [MPPPixelDataUtils rgbPixelDataFromPixelData:(uint8_t *)CVPixelBufferGetBaseAddress(pixelBuffer) withWidth:CVPixelBufferGetWidth(pixelBuffer) @@ -148,7 +151,7 @@ using ::mediapipe::ImageFrame; } std::unique_ptr imageFrame = absl::make_unique( - ::mediapipe::ImageFormat::SRGB, width, height, stride, static_cast(rgbPixelData), + ::mediapipe::ImageFormat::SRGB, width, height, destinationStride, static_cast(rgbPixelData), /*deleter=*/free); return imageFrame; @@ -183,11 +186,14 @@ using ::mediapipe::ImageFrame; NSInteger bitsPerComponent = 8; NSInteger channelCount = 4; + size_t bytesPerRow = channelCount * width; + + NSInteger destinationChannelCount = 3; + size_t destinationBytesPerRow = destinationChannelCount * width; + UInt8 *pixelDataToReturn = NULL; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); - size_t bytesPerRow = channelCount * width; - // iOS infers bytesPerRow if it is set to 0. // See https://developer.apple.com/documentation/coregraphics/1455939-cgbitmapcontextcreate // But for segmentation test image, this was not the case. @@ -219,9 +225,13 @@ using ::mediapipe::ImageFrame; CGColorSpaceRelease(colorSpace); + if (!pixelDataToReturn) { + return nullptr; + } + std::unique_ptr imageFrame = absl::make_unique(mediapipe::ImageFormat::SRGB, (int)width, (int)height, - (int)bytesPerRow, static_cast(pixelDataToReturn), + (int)destinationBytesPerRow, static_cast(pixelDataToReturn), /*deleter=*/free); return imageFrame;