From 08fa0a6c4a9c58981e058a90dc9e99da4fb5002d Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Fri, 12 Jan 2024 05:41:18 -0800 Subject: [PATCH] Deprecate ImageFrame::ByteDepth ByteDepth and ChannelSize are exactly the same. This CL deprecates one of them, namely ByteDepth, because the name "ChannelSize" is arguably more readable. PiperOrigin-RevId: 597817336 --- mediapipe/framework/formats/image_frame.cc | 43 +++++----------------- mediapipe/framework/formats/image_frame.h | 17 +++++---- 2 files changed, 18 insertions(+), 42 deletions(-) diff --git a/mediapipe/framework/formats/image_frame.cc b/mediapipe/framework/formats/image_frame.cc index 472da76a90..fe84f82915 100644 --- a/mediapipe/framework/formats/image_frame.cc +++ b/mediapipe/framework/formats/image_frame.cc @@ -101,7 +101,7 @@ void ImageFrame::Reset(ImageFormat::Format format, int width, int height, height_ = height; ABSL_CHECK_NE(ImageFormat::UNKNOWN, format_); ABSL_CHECK(IsValidAlignmentNumber(alignment_boundary)); - width_step_ = width * NumberOfChannels() * ByteDepth(); + width_step_ = width * NumberOfChannels() * ChannelSize(); if (alignment_boundary == 1) { pixel_data_ = {new uint8_t[height * width_step_], PixelDataDeleter::kArrayDelete}; @@ -126,7 +126,7 @@ void ImageFrame::AdoptPixelData(ImageFormat::Format format, int width, width_step_ = width_step; ABSL_CHECK_NE(ImageFormat::UNKNOWN, format_); - ABSL_CHECK_GE(width_step_, width * NumberOfChannels() * ByteDepth()); + ABSL_CHECK_GE(width_step_, width * NumberOfChannels() * ChannelSize()); pixel_data_ = {pixel_data, deleter}; } @@ -191,7 +191,7 @@ void ImageFrame::SetAlignmentPaddingAreas() { ABSL_CHECK_GE(width_, 1); ABSL_CHECK_GE(height_, 1); - const int pixel_size = ByteDepth() * NumberOfChannels(); + const int pixel_size = ChannelSize() * NumberOfChannels(); const int padding_size = width_step_ - width_ * pixel_size; for (int row = 0; row < height_; ++row) { uint8_t* row_start = pixel_data_.get() + width_step_ * row; @@ -219,7 +219,7 @@ bool ImageFrame::IsContiguous() const { if (!pixel_data_) { return false; } - return width_step_ == width_ * NumberOfChannels() * ByteDepth(); + return width_step_ == width_ * NumberOfChannels() * ChannelSize(); } bool ImageFrame::IsAligned(uint32_t alignment_boundary) const { @@ -323,35 +323,10 @@ int ImageFrame::ChannelSizeForFormat(ImageFormat::Format format) { } } -int ImageFrame::ByteDepth() const { return ByteDepthForFormat(format_); } +int ImageFrame::ByteDepth() const { return ChannelSizeForFormat(format_); } int ImageFrame::ByteDepthForFormat(ImageFormat::Format format) { - switch (format) { - case ImageFormat::GRAY8: - return 1; - case ImageFormat::GRAY16: - return 2; - case ImageFormat::SRGB: - return 1; - case ImageFormat::SRGBA: - return 1; - case ImageFormat::SRGB48: - return 2; - case ImageFormat::SRGBA64: - return 2; - case ImageFormat::VEC32F1: - return 4; - case ImageFormat::VEC32F2: - return 4; - case ImageFormat::VEC32F4: - return 4; - case ImageFormat::LAB8: - return 1; - case ImageFormat::SBGRA: - return 1; - default: - ABSL_LOG(FATAL) << InvalidFormatString(format); - } + return ChannelSizeForFormat(format); } void ImageFrame::CopyFrom(const ImageFrame& image_frame, @@ -384,7 +359,7 @@ void ImageFrame::CopyPixelData(ImageFormat::Format format, int width, void ImageFrame::CopyToBuffer(uint8_t* buffer, int buffer_size) const { ABSL_CHECK(buffer); - ABSL_CHECK_EQ(1, ByteDepth()); + ABSL_CHECK_EQ(1, ChannelSize()); const int data_size = width_ * height_ * NumberOfChannels(); ABSL_CHECK_LE(data_size, buffer_size); if (IsContiguous()) { @@ -399,7 +374,7 @@ void ImageFrame::CopyToBuffer(uint8_t* buffer, int buffer_size) const { void ImageFrame::CopyToBuffer(uint16_t* buffer, int buffer_size) const { ABSL_CHECK(buffer); - ABSL_CHECK_EQ(2, ByteDepth()); + ABSL_CHECK_EQ(2, ChannelSize()); const int data_size = width_ * height_ * NumberOfChannels(); ABSL_CHECK_LE(data_size, buffer_size); if (IsContiguous()) { @@ -414,7 +389,7 @@ void ImageFrame::CopyToBuffer(uint16_t* buffer, int buffer_size) const { void ImageFrame::CopyToBuffer(float* buffer, int buffer_size) const { ABSL_CHECK(buffer); - ABSL_CHECK_EQ(4, ByteDepth()); + ABSL_CHECK_EQ(4, ChannelSize()); const int data_size = width_ * height_ * NumberOfChannels(); ABSL_CHECK_LE(data_size, buffer_size); if (IsContiguous()) { diff --git a/mediapipe/framework/formats/image_frame.h b/mediapipe/framework/formats/image_frame.h index 642e9f30b8..8451b47c5f 100644 --- a/mediapipe/framework/formats/image_frame.h +++ b/mediapipe/framework/formats/image_frame.h @@ -40,6 +40,7 @@ #include #include +#include "absl/base/attributes.h" #include "mediapipe/framework/formats/image_format.pb.h" #include "mediapipe/framework/port.h" #include "mediapipe/framework/tool/type_util.h" @@ -86,11 +87,11 @@ class ImageFrame { // and GL_UNPACK_ALIGNMENT parameters. static const uint32_t kGlDefaultAlignmentBoundary = 4; - // Returns number of channels for an ImageFormat. + // Returns number of channels for the given image format. static int NumberOfChannelsForFormat(ImageFormat::Format format); - // Returns the channel size for an ImageFormat. + // Returns the size of each channel in bytes for the given image format. static int ChannelSizeForFormat(ImageFormat::Format format); - // Returns depth of each channel in bytes for an ImageFormat. + ABSL_DEPRECATED("Use ChannelSizeForFormat() instead") static int ByteDepthForFormat(ImageFormat::Format format); ImageFrame(const ImageFrame&) = delete; @@ -150,17 +151,17 @@ class ImageFrame { int Width() const { return width_; } // Returns the height of the image in pixels. int Height() const { return height_; } - // Returns the channel size. - int ChannelSize() const; // Returns the number of channels. int NumberOfChannels() const; - // Returns the depth of each image channel in bytes. + // Returns the size of each channel in bytes. + int ChannelSize() const; + ABSL_DEPRECATED("Use ChannelSize() instead") int ByteDepth() const; // Returns the byte offset between a pixel value and the same pixel // and channel in the next row. Notice, that for alignment reasons, // there may be unused padding bytes at the end of each row - // (WidthStep() - Width()*NumberOfChannels*ByteDepth() will give the + // (WidthStep() - Width()*NumberOfChannels*ChannelSize() will give the // number of unused bytes). int WidthStep() const { return width_step_; } @@ -180,7 +181,7 @@ class ImageFrame { // Returns the total size the pixel data would take if it was stored // contiguously (which may not be the case). int PixelDataSizeStoredContiguously() const { - return Width() * Height() * ByteDepth() * NumberOfChannels(); + return Width() * Height() * ChannelSize() * NumberOfChannels(); } // Initializes ImageFrame from pixel data without copying.