Repository: google/mediapipe
File: mediapipe/tasks/ios/vision/core/utils/sources/MPPImage+Utils.mm (Objective-C++)

Code before:
  return std::make_unique<ImageFrame>(imageFormat, width, height, destinationBytesPerRow,
                                      static_cast<uint8 *>(destBuffer.data));
}

+ (UInt8 *)pixelDataFromImageFrame:(ImageFrame &)imageFrame
                        shouldCopy:(BOOL)shouldCopy
                             error:(NSError **)error {
  vImage_Buffer sourceBuffer = VImageBufferFromImageFrame(imageFrame);

  // Pre-multiply the raw pixels from a `mediapipe::Image` before creating a `CGImage` to ensure
  // that pixels are displayed correctly irrespective of their alpha values.
  vImage_Error premultiplyError;
  vImage_Buffer destinationBuffer;

  switch (imageFrame.Format()) {
    case ImageFormat::SRGBA: {
      destinationBuffer =
          shouldCopy ? EmptyVImageBufferFromImageFrame(imageFrame, true) : sourceBuffer;
      premultiplyError =
          vImagePremultiplyData_RGBA8888(&sourceBuffer, &destinationBuffer, kvImageNoFlags);
      break;
    }
    default: {
      [MPPCommonUtils createCustomError:error
                               withCode:MPPTasksErrorCodeInternalError
                            description:@"An internal error occured"];
      return NULL;
    }
  }

  if (premultiplyError != kvImageNoError) {
    [MPPCommonUtils createCustomError:error
                             withCode:MPPTasksErrorCodeInternalError
                          description:@"An internal error occured."];

    return NULL;
  }

  return (UInt8 *)destinationBuffer.data;
}

@end

@implementation MPPCVPixelBufferUtils

+ (std::unique_ptr<ImageFrame>)imageFrameFromCVPixelBuffer:(CVPixelBufferRef)pixelBuffer
                                                     error:(NSError **)error {
  OSType pixelBufferFormat = CVPixelBufferGetPixelFormatType(pixelBuffer);
  std::unique_ptr<ImageFrame> imageFrame = nullptr;

  switch (pixelBufferFormat) {

Review comment (suggestion):
Can you provide a more unique error message so we can differentiate between them? Applies everywhere.

Code after:
                                      static_cast<uint8 *>(destBuffer.data));
}

+ (UInt8 *)pixelDataFromImageFrame:(ImageFrame &)imageFrame
                        shouldCopy:(BOOL)shouldCopy
                             error:(NSError **)error {
  vImage_Buffer sourceBuffer = CreateVImageBufferFromImageFrame(imageFrame);

  // Pre-multiply the raw pixels from a `mediapipe::Image` before creating a `CGImage` to ensure
  // that pixels are displayed correctly irrespective of their alpha values.
  vImage_Error premultiplyError;
  vImage_Buffer destinationBuffer;

  switch (imageFrame.Format()) {
    case ImageFormat::SRGBA: {
      destinationBuffer =
          shouldCopy ? CreateEmptyVImageBufferFromImageFrame(imageFrame, true) : sourceBuffer;
      premultiplyError =
          vImagePremultiplyData_RGBA8888(&sourceBuffer, &destinationBuffer, kvImageNoFlags);
      break;
    }
    default: {
      [MPPCommonUtils createCustomError:error
                               withCode:MPPTasksErrorCodeInternalError
                            description:@"An error occured while processing the output image "
                                        @"pixels of the vision task."];
      return NULL;
    }
  }

  if (premultiplyError != kvImageNoError) {
    [MPPCommonUtils
        createCustomError:error
                 withCode:MPPTasksErrorCodeInternalError
              description:
                  @"An error occured while processing the output image pixels of the vision task."];

    return NULL;
  }

  return (UInt8 *)destinationBuffer.data;
}

@end

@implementation MPPCVPixelBufferUtils

+ (std::unique_ptr<ImageFrame>)imageFrameFromCVPixelBuffer:(CVPixelBufferRef)pixelBuffer
                                                     error:(NSError **)error {
  OSType pixelBufferFormat = CVPixelBufferGetPixelFormatType(pixelBuffer);
  std::unique_ptr<ImageFrame> imageFrame = nullptr;