Internal change

PiperOrigin-RevId: 521980958
This commit is contained in:
MediaPipe Team 2023-04-05 01:07:18 -07:00 committed by Copybara-Service
parent 7417e48da4
commit 46f9270788
5 changed files with 52 additions and 51 deletions

View File

@ -33,7 +33,7 @@ namespace mediapipe {
namespace { namespace {
int CountOnes(uint32 n) { int CountOnes(uint32_t n) {
#if (defined(__i386__) || defined(__x86_64__)) && defined(__POPCNT__) && \ #if (defined(__i386__) || defined(__x86_64__)) && defined(__POPCNT__) && \
defined(__GNUC__) defined(__GNUC__)
return __builtin_popcount(n); return __builtin_popcount(n);
@ -47,20 +47,21 @@ int CountOnes(uint32 n) {
} // namespace } // namespace
const ImageFrame::Deleter ImageFrame::PixelDataDeleter::kArrayDelete = const ImageFrame::Deleter ImageFrame::PixelDataDeleter::kArrayDelete =
std::default_delete<uint8[]>(); std::default_delete<uint8_t[]>();
const ImageFrame::Deleter ImageFrame::PixelDataDeleter::kFree = free; const ImageFrame::Deleter ImageFrame::PixelDataDeleter::kFree = free;
const ImageFrame::Deleter ImageFrame::PixelDataDeleter::kAlignedFree = const ImageFrame::Deleter ImageFrame::PixelDataDeleter::kAlignedFree =
aligned_free; aligned_free;
const ImageFrame::Deleter ImageFrame::PixelDataDeleter::kNone = [](uint8* x) {}; const ImageFrame::Deleter ImageFrame::PixelDataDeleter::kNone = [](uint8_t* x) {
};
const uint32 ImageFrame::kDefaultAlignmentBoundary; const uint32_t ImageFrame::kDefaultAlignmentBoundary;
const uint32 ImageFrame::kGlDefaultAlignmentBoundary; const uint32_t ImageFrame::kGlDefaultAlignmentBoundary;
ImageFrame::ImageFrame() ImageFrame::ImageFrame()
: format_(ImageFormat::UNKNOWN), width_(0), height_(0), width_step_(0) {} : format_(ImageFormat::UNKNOWN), width_(0), height_(0), width_step_(0) {}
ImageFrame::ImageFrame(ImageFormat::Format format, int width, int height, ImageFrame::ImageFrame(ImageFormat::Format format, int width, int height,
uint32 alignment_boundary) uint32_t alignment_boundary)
: format_(format), width_(width), height_(height) { : format_(format), width_(width), height_(height) {
Reset(format, width, height, alignment_boundary); Reset(format, width, height, alignment_boundary);
} }
@ -71,7 +72,7 @@ ImageFrame::ImageFrame(ImageFormat::Format format, int width, int height)
} }
ImageFrame::ImageFrame(ImageFormat::Format format, int width, int height, ImageFrame::ImageFrame(ImageFormat::Format format, int width, int height,
int width_step, uint8* pixel_data, int width_step, uint8_t* pixel_data,
ImageFrame::Deleter deleter) { ImageFrame::Deleter deleter) {
AdoptPixelData(format, width, height, width_step, pixel_data, deleter); AdoptPixelData(format, width, height, width_step, pixel_data, deleter);
} }
@ -93,7 +94,7 @@ ImageFrame& ImageFrame::operator=(ImageFrame&& move_from) {
} }
void ImageFrame::Reset(ImageFormat::Format format, int width, int height, void ImageFrame::Reset(ImageFormat::Format format, int width, int height,
uint32 alignment_boundary) { uint32_t alignment_boundary) {
format_ = format; format_ = format;
width_ = width; width_ = width;
height_ = height; height_ = height;
@ -101,7 +102,7 @@ void ImageFrame::Reset(ImageFormat::Format format, int width, int height,
CHECK(IsValidAlignmentNumber(alignment_boundary)); CHECK(IsValidAlignmentNumber(alignment_boundary));
width_step_ = width * NumberOfChannels() * ByteDepth(); width_step_ = width * NumberOfChannels() * ByteDepth();
if (alignment_boundary == 1) { if (alignment_boundary == 1) {
pixel_data_ = {new uint8[height * width_step_], pixel_data_ = {new uint8_t[height * width_step_],
PixelDataDeleter::kArrayDelete}; PixelDataDeleter::kArrayDelete};
} else { } else {
// Increase width_step_ to the smallest multiple of alignment_boundary // Increase width_step_ to the smallest multiple of alignment_boundary
@ -109,14 +110,14 @@ void ImageFrame::Reset(ImageFormat::Format format, int width, int height,
// twiddling bits. alignment_boundary - 1 is a mask which sets all // twiddling bits. alignment_boundary - 1 is a mask which sets all
// the low order bits. // the low order bits.
width_step_ = ((width_step_ - 1) | (alignment_boundary - 1)) + 1; width_step_ = ((width_step_ - 1) | (alignment_boundary - 1)) + 1;
pixel_data_ = {reinterpret_cast<uint8*>(aligned_malloc(height * width_step_, pixel_data_ = {reinterpret_cast<uint8_t*>(aligned_malloc(
alignment_boundary)), height * width_step_, alignment_boundary)),
PixelDataDeleter::kAlignedFree}; PixelDataDeleter::kAlignedFree};
} }
} }
void ImageFrame::AdoptPixelData(ImageFormat::Format format, int width, void ImageFrame::AdoptPixelData(ImageFormat::Format format, int width,
int height, int width_step, uint8* pixel_data, int height, int width_step, uint8_t* pixel_data,
ImageFrame::Deleter deleter) { ImageFrame::Deleter deleter) {
format_ = format; format_ = format;
width_ = width; width_ = width;
@ -129,12 +130,12 @@ void ImageFrame::AdoptPixelData(ImageFormat::Format format, int width,
pixel_data_ = {pixel_data, deleter}; pixel_data_ = {pixel_data, deleter};
} }
std::unique_ptr<uint8[], ImageFrame::Deleter> ImageFrame::Release() { std::unique_ptr<uint8_t[], ImageFrame::Deleter> ImageFrame::Release() {
return std::move(pixel_data_); return std::move(pixel_data_);
} }
void ImageFrame::InternalCopyFrom(int width, int height, int width_step, void ImageFrame::InternalCopyFrom(int width, int height, int width_step,
int channel_size, const uint8* pixel_data) { int channel_size, const uint8_t* pixel_data) {
CHECK_EQ(width_, width); CHECK_EQ(width_, width);
CHECK_EQ(height_, height); CHECK_EQ(height_, height);
// row_bytes = channel_size * num_channels * width // row_bytes = channel_size * num_channels * width
@ -192,9 +193,9 @@ void ImageFrame::SetAlignmentPaddingAreas() {
const int pixel_size = ByteDepth() * NumberOfChannels(); const int pixel_size = ByteDepth() * NumberOfChannels();
const int padding_size = width_step_ - width_ * pixel_size; const int padding_size = width_step_ - width_ * pixel_size;
for (int row = 0; row < height_; ++row) { for (int row = 0; row < height_; ++row) {
uint8* row_start = pixel_data_.get() + width_step_ * row; uint8_t* row_start = pixel_data_.get() + width_step_ * row;
uint8* last_pixel_in_row = row_start + (width_ - 1) * pixel_size; uint8_t* last_pixel_in_row = row_start + (width_ - 1) * pixel_size;
uint8* padding = row_start + width_ * pixel_size; uint8_t* padding = row_start + width_ * pixel_size;
int padding_index = 0; int padding_index = 0;
while (padding_index + pixel_size - 1 < padding_size) { while (padding_index + pixel_size - 1 < padding_size) {
// Copy the entire last pixel in the row into this padding pixel. // Copy the entire last pixel in the row into this padding pixel.
@ -220,7 +221,7 @@ bool ImageFrame::IsContiguous() const {
return width_step_ == width_ * NumberOfChannels() * ByteDepth(); return width_step_ == width_ * NumberOfChannels() * ByteDepth();
} }
bool ImageFrame::IsAligned(uint32 alignment_boundary) const { bool ImageFrame::IsAligned(uint32_t alignment_boundary) const {
CHECK(IsValidAlignmentNumber(alignment_boundary)); CHECK(IsValidAlignmentNumber(alignment_boundary));
if (!pixel_data_) { if (!pixel_data_) {
return false; return false;
@ -236,7 +237,7 @@ bool ImageFrame::IsAligned(uint32 alignment_boundary) const {
} }
// static // static
bool ImageFrame::IsValidAlignmentNumber(uint32 alignment_boundary) { bool ImageFrame::IsValidAlignmentNumber(uint32_t alignment_boundary) {
return CountOnes(alignment_boundary) == 1; return CountOnes(alignment_boundary) == 1;
} }
@ -293,25 +294,25 @@ int ImageFrame::ChannelSize() const { return ChannelSizeForFormat(format_); }
int ImageFrame::ChannelSizeForFormat(ImageFormat::Format format) { int ImageFrame::ChannelSizeForFormat(ImageFormat::Format format) {
switch (format) { switch (format) {
case ImageFormat::GRAY8: case ImageFormat::GRAY8:
return sizeof(uint8); return sizeof(uint8_t);
case ImageFormat::SRGB: case ImageFormat::SRGB:
return sizeof(uint8); return sizeof(uint8_t);
case ImageFormat::SRGBA: case ImageFormat::SRGBA:
return sizeof(uint8); return sizeof(uint8_t);
case ImageFormat::GRAY16: case ImageFormat::GRAY16:
return sizeof(uint16); return sizeof(uint16_t);
case ImageFormat::SRGB48: case ImageFormat::SRGB48:
return sizeof(uint16); return sizeof(uint16_t);
case ImageFormat::SRGBA64: case ImageFormat::SRGBA64:
return sizeof(uint16); return sizeof(uint16_t);
case ImageFormat::VEC32F1: case ImageFormat::VEC32F1:
return sizeof(float); return sizeof(float);
case ImageFormat::VEC32F2: case ImageFormat::VEC32F2:
return sizeof(float); return sizeof(float);
case ImageFormat::LAB8: case ImageFormat::LAB8:
return sizeof(uint8); return sizeof(uint8_t);
case ImageFormat::SBGRA: case ImageFormat::SBGRA:
return sizeof(uint8); return sizeof(uint8_t);
default: default:
LOG(FATAL) << InvalidFormatString(format); LOG(FATAL) << InvalidFormatString(format);
} }
@ -347,7 +348,7 @@ int ImageFrame::ByteDepthForFormat(ImageFormat::Format format) {
} }
void ImageFrame::CopyFrom(const ImageFrame& image_frame, void ImageFrame::CopyFrom(const ImageFrame& image_frame,
uint32 alignment_boundary) { uint32_t alignment_boundary) {
// Reset the current image. // Reset the current image.
Reset(image_frame.Format(), image_frame.Width(), image_frame.Height(), Reset(image_frame.Format(), image_frame.Width(), image_frame.Height(),
alignment_boundary); alignment_boundary);
@ -359,29 +360,29 @@ void ImageFrame::CopyFrom(const ImageFrame& image_frame,
} }
void ImageFrame::CopyPixelData(ImageFormat::Format format, int width, void ImageFrame::CopyPixelData(ImageFormat::Format format, int width,
int height, const uint8* pixel_data, int height, const uint8_t* pixel_data,
uint32 alignment_boundary) { uint32_t alignment_boundary) {
CopyPixelData(format, width, height, 0 /* contiguous storage */, pixel_data, CopyPixelData(format, width, height, 0 /* contiguous storage */, pixel_data,
alignment_boundary); alignment_boundary);
} }
void ImageFrame::CopyPixelData(ImageFormat::Format format, int width, void ImageFrame::CopyPixelData(ImageFormat::Format format, int width,
int height, int width_step, int height, int width_step,
const uint8* pixel_data, const uint8_t* pixel_data,
uint32 alignment_boundary) { uint32_t alignment_boundary) {
Reset(format, width, height, alignment_boundary); Reset(format, width, height, alignment_boundary);
InternalCopyFrom(width, height, width_step, ChannelSizeForFormat(format), InternalCopyFrom(width, height, width_step, ChannelSizeForFormat(format),
pixel_data); pixel_data);
} }
void ImageFrame::CopyToBuffer(uint8* buffer, int buffer_size) const { void ImageFrame::CopyToBuffer(uint8_t* buffer, int buffer_size) const {
CHECK(buffer); CHECK(buffer);
CHECK_EQ(1, ByteDepth()); CHECK_EQ(1, ByteDepth());
const int data_size = width_ * height_ * NumberOfChannels(); const int data_size = width_ * height_ * NumberOfChannels();
CHECK_LE(data_size, buffer_size); CHECK_LE(data_size, buffer_size);
if (IsContiguous()) { if (IsContiguous()) {
// The data is stored contiguously, we can just copy. // The data is stored contiguously, we can just copy.
const uint8* src = reinterpret_cast<const uint8*>(pixel_data_.get()); const uint8_t* src = reinterpret_cast<const uint8_t*>(pixel_data_.get());
std::copy_n(src, data_size, buffer); std::copy_n(src, data_size, buffer);
} else { } else {
InternalCopyToBuffer(0 /* contiguous storage */, InternalCopyToBuffer(0 /* contiguous storage */,
@ -389,14 +390,14 @@ void ImageFrame::CopyToBuffer(uint8* buffer, int buffer_size) const {
} }
} }
void ImageFrame::CopyToBuffer(uint16* buffer, int buffer_size) const { void ImageFrame::CopyToBuffer(uint16_t* buffer, int buffer_size) const {
CHECK(buffer); CHECK(buffer);
CHECK_EQ(2, ByteDepth()); CHECK_EQ(2, ByteDepth());
const int data_size = width_ * height_ * NumberOfChannels(); const int data_size = width_ * height_ * NumberOfChannels();
CHECK_LE(data_size, buffer_size); CHECK_LE(data_size, buffer_size);
if (IsContiguous()) { if (IsContiguous()) {
// The data is stored contiguously, we can just copy. // The data is stored contiguously, we can just copy.
const uint16* src = reinterpret_cast<const uint16*>(pixel_data_.get()); const uint16_t* src = reinterpret_cast<const uint16_t*>(pixel_data_.get());
std::copy_n(src, data_size, buffer); std::copy_n(src, data_size, buffer);
} else { } else {
InternalCopyToBuffer(0 /* contiguous storage */, InternalCopyToBuffer(0 /* contiguous storage */,

View File

@ -51,8 +51,8 @@ TEST(ImageFrameOpencvTest, ConvertToMat) {
// Check adding constant images. // Check adding constant images.
const uint8_t frame1_val = 12; const uint8_t frame1_val = 12;
const uint8_t frame2_val = 34; const uint8_t frame2_val = 34;
SetToColor<uint8>(&frame1_val, &frame1); SetToColor<uint8_t>(&frame1_val, &frame1);
SetToColor<uint8>(&frame2_val, &frame2); SetToColor<uint8_t>(&frame2_val, &frame2);
// Get Mat wrapper around ImageFrame memory (zero copy). // Get Mat wrapper around ImageFrame memory (zero copy).
cv::Mat frame1_mat = formats::MatView(&frame1); cv::Mat frame1_mat = formats::MatView(&frame1);
cv::Mat frame2_mat = formats::MatView(&frame2); cv::Mat frame2_mat = formats::MatView(&frame2);
@ -62,7 +62,7 @@ TEST(ImageFrameOpencvTest, ConvertToMat) {
EXPECT_EQ(frame_avg, frame1_val + frame2_val); EXPECT_EQ(frame_avg, frame1_val + frame2_val);
// Check setting min/max pixels. // Check setting min/max pixels.
uint8* frame1_ptr = frame1.MutablePixelData(); uint8_t* frame1_ptr = frame1.MutablePixelData();
frame1_ptr[(i_width - 5) + (i_height - 5) * frame1.WidthStep()] = 1; frame1_ptr[(i_width - 5) + (i_height - 5) * frame1.WidthStep()] = 1;
frame1_ptr[(i_width - 6) + (i_height - 6) * frame1.WidthStep()] = 100; frame1_ptr[(i_width - 6) + (i_height - 6) * frame1.WidthStep()] = 100;
double min, max; double min, max;
@ -84,8 +84,8 @@ TEST(ImageFrameOpencvTest, ConvertToIpl) {
// Check adding constant images. // Check adding constant images.
const uint8_t frame1_val = 12; const uint8_t frame1_val = 12;
const uint8_t frame2_val = 34; const uint8_t frame2_val = 34;
SetToColor<uint8>(&frame1_val, &frame1); SetToColor<uint8_t>(&frame1_val, &frame1);
SetToColor<uint8>(&frame2_val, &frame2); SetToColor<uint8_t>(&frame2_val, &frame2);
const cv::Mat frame1_mat = formats::MatView(&frame1); const cv::Mat frame1_mat = formats::MatView(&frame1);
const cv::Mat frame2_mat = formats::MatView(&frame2); const cv::Mat frame2_mat = formats::MatView(&frame2);
const cv::Mat frame_sum = frame1_mat + frame2_mat; const cv::Mat frame_sum = frame1_mat + frame2_mat;
@ -93,7 +93,7 @@ TEST(ImageFrameOpencvTest, ConvertToIpl) {
EXPECT_EQ(frame_avg, frame1_val + frame2_val); EXPECT_EQ(frame_avg, frame1_val + frame2_val);
// Check setting min/max pixels. // Check setting min/max pixels.
uint8* frame1_ptr = frame1.MutablePixelData(); uint8_t* frame1_ptr = frame1.MutablePixelData();
frame1_ptr[(i_width - 5) + (i_height - 5) * frame1.WidthStep()] = 1; frame1_ptr[(i_width - 5) + (i_height - 5) * frame1.WidthStep()] = 1;
frame1_ptr[(i_width - 6) + (i_height - 6) * frame1.WidthStep()] = 100; frame1_ptr[(i_width - 6) + (i_height - 6) * frame1.WidthStep()] = 100;
double min, max; double min, max;

View File

@ -96,7 +96,7 @@ std::shared_ptr<cv::Mat> MatView(const mediapipe::Image* image) {
image->image_format()))}; image->image_format()))};
auto owner = auto owner =
std::make_shared<MatWithPixelLock>(const_cast<mediapipe::Image*>(image)); std::make_shared<MatWithPixelLock>(const_cast<mediapipe::Image*>(image));
uint8* data_ptr = owner->lock.Pixels(); uint8_t* data_ptr = owner->lock.Pixels();
CHECK(data_ptr != nullptr); CHECK(data_ptr != nullptr);
// Use Image to initialize in-place. Image still owns memory. // Use Image to initialize in-place. Image still owns memory.
if (steps[0] == sizes[1] * image->channels() * if (steps[0] == sizes[1] * image->channels() *

View File

@ -91,7 +91,7 @@ std::unique_ptr<cv::Mat> GetCvMask(const Location& location) {
new cv::Mat(mask.height(), mask.width(), CV_8UC1, cv::Scalar(0))); new cv::Mat(mask.height(), mask.width(), CV_8UC1, cv::Scalar(0)));
for (const auto& interval : location_data.mask().rasterization().interval()) { for (const auto& interval : location_data.mask().rasterization().interval()) {
for (int x = interval.left_x(); x <= interval.right_x(); ++x) { for (int x = interval.left_x(); x <= interval.right_x(); ++x) {
mat->at<uint8>(interval.y(), x) = 255; mat->at<uint8_t>(interval.y(), x) = 255;
} }
} }
return mat; return mat;
@ -174,7 +174,7 @@ void EnlargeLocation(Location& location, const float factor) {
} else { } else {
cv::erode(*mask, *mask, morph_element); cv::erode(*mask, *mask, morph_element);
} }
CreateCvMaskLocation<uint8>(*mask).ConvertToProto(&location_data); CreateCvMaskLocation<uint8_t>(*mask).ConvertToProto(&location_data);
break; break;
} }
} }

View File

@ -25,8 +25,8 @@ namespace mediapipe {
// segments per row. // segments per row.
static const int kWidth = 7; static const int kWidth = 7;
static const int kHeight = 3; static const int kHeight = 3;
const std::vector<uint8> kTestPatternVector = {0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, const std::vector<uint8_t> kTestPatternVector = {
0, 0, 0, 1, 0, 1, 0, 1, 0, 0}; 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0};
// Interval {y, x_start, x_end} representation of kTestPatternVector. // Interval {y, x_start, x_end} representation of kTestPatternVector.
const std::vector<std::vector<int>> kTestPatternIntervals = { const std::vector<std::vector<int>> kTestPatternIntervals = {
@ -67,8 +67,8 @@ TEST(LocationOpencvTest, CreateBBoxLocation) {
} }
TEST(LocationOpencvTest, CreateCvMaskLocation) { TEST(LocationOpencvTest, CreateCvMaskLocation) {
cv::Mat_<uint8> test_mask(kHeight, kWidth, cv::Mat_<uint8_t> test_mask(kHeight, kWidth,
const_cast<uint8*>(kTestPatternVector.data())); const_cast<uint8_t*>(kTestPatternVector.data()));
Location location = CreateCvMaskLocation(test_mask); Location location = CreateCvMaskLocation(test_mask);
auto intervals = location.ConvertToProto().mask().rasterization().interval(); auto intervals = location.ConvertToProto().mask().rasterization().interval();
EXPECT_EQ(intervals.size(), kTestPatternIntervals.size()); EXPECT_EQ(intervals.size(), kTestPatternIntervals.size());
@ -157,8 +157,8 @@ TEST(LocationOpenCvTest, GetCvMask) {
auto cv_mask = *GetCvMask(test_location); auto cv_mask = *GetCvMask(test_location);
EXPECT_EQ(cv_mask.cols * cv_mask.rows, kTestPatternVector.size()); EXPECT_EQ(cv_mask.cols * cv_mask.rows, kTestPatternVector.size());
int flat_idx = 0; int flat_idx = 0;
for (auto it = cv_mask.begin<uint8>(); it != cv_mask.end<uint8>(); ++it) { for (auto it = cv_mask.begin<uint8_t>(); it != cv_mask.end<uint8_t>(); ++it) {
const uint8 expected_value = kTestPatternVector[flat_idx] == 0 ? 0 : 255; const uint8_t expected_value = kTestPatternVector[flat_idx] == 0 ? 0 : 255;
EXPECT_EQ(*it, expected_value); EXPECT_EQ(*it, expected_value);
flat_idx++; flat_idx++;
} }