Skip to content

Commit

Permalink
Deprecate ImageFrame::ByteDepth
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MediaPipe Team authored and copybara-github committed Jan 12, 2024
1 parent 1f27bd5 commit 08fa0a6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 42 deletions.
43 changes: 9 additions & 34 deletions mediapipe/framework/formats/image_frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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};
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()) {
Expand All @@ -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()) {
Expand All @@ -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()) {
Expand Down
17 changes: 9 additions & 8 deletions mediapipe/framework/formats/image_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <memory>
#include <string>

#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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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_; }

Expand All @@ -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.
Expand Down

0 comments on commit 08fa0a6

Please sign in to comment.