From 46f92707880c78f45ea9c855d55edabbaa5e07d6 Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Wed, 5 Apr 2023 01:07:18 -0700 Subject: [PATCH] Internal change PiperOrigin-RevId: 521980958 --- mediapipe/framework/formats/image_frame.cc | 73 ++++++++++--------- .../formats/image_frame_opencv_test.cc | 12 +-- mediapipe/framework/formats/image_opencv.cc | 2 +- .../framework/formats/location_opencv.cc | 4 +- .../framework/formats/location_opencv_test.cc | 12 +-- 5 files changed, 52 insertions(+), 51 deletions(-) diff --git a/mediapipe/framework/formats/image_frame.cc b/mediapipe/framework/formats/image_frame.cc index 913ffae24..772c91014 100644 --- a/mediapipe/framework/formats/image_frame.cc +++ b/mediapipe/framework/formats/image_frame.cc @@ -33,7 +33,7 @@ namespace mediapipe { namespace { -int CountOnes(uint32 n) { +int CountOnes(uint32_t n) { #if (defined(__i386__) || defined(__x86_64__)) && defined(__POPCNT__) && \ defined(__GNUC__) return __builtin_popcount(n); @@ -47,20 +47,21 @@ int CountOnes(uint32 n) { } // namespace const ImageFrame::Deleter ImageFrame::PixelDataDeleter::kArrayDelete = - std::default_delete(); + std::default_delete(); const ImageFrame::Deleter ImageFrame::PixelDataDeleter::kFree = free; const ImageFrame::Deleter ImageFrame::PixelDataDeleter::kAlignedFree = 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 ImageFrame::kGlDefaultAlignmentBoundary; +const uint32_t ImageFrame::kDefaultAlignmentBoundary; +const uint32_t ImageFrame::kGlDefaultAlignmentBoundary; ImageFrame::ImageFrame() : format_(ImageFormat::UNKNOWN), width_(0), height_(0), width_step_(0) {} ImageFrame::ImageFrame(ImageFormat::Format format, int width, int height, - uint32 alignment_boundary) + uint32_t alignment_boundary) : format_(format), width_(width), height_(height) { 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, - int width_step, uint8* pixel_data, + int width_step, uint8_t* pixel_data, ImageFrame::Deleter 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, - uint32 alignment_boundary) { + uint32_t alignment_boundary) { format_ = format; width_ = width; height_ = height; @@ -101,7 +102,7 @@ void ImageFrame::Reset(ImageFormat::Format format, int width, int height, CHECK(IsValidAlignmentNumber(alignment_boundary)); width_step_ = width * NumberOfChannels() * ByteDepth(); if (alignment_boundary == 1) { - pixel_data_ = {new uint8[height * width_step_], + pixel_data_ = {new uint8_t[height * width_step_], PixelDataDeleter::kArrayDelete}; } else { // 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 // the low order bits. width_step_ = ((width_step_ - 1) | (alignment_boundary - 1)) + 1; - pixel_data_ = {reinterpret_cast(aligned_malloc(height * width_step_, - alignment_boundary)), + pixel_data_ = {reinterpret_cast(aligned_malloc( + height * width_step_, alignment_boundary)), PixelDataDeleter::kAlignedFree}; } } 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) { format_ = format; width_ = width; @@ -129,12 +130,12 @@ void ImageFrame::AdoptPixelData(ImageFormat::Format format, int width, pixel_data_ = {pixel_data, deleter}; } -std::unique_ptr ImageFrame::Release() { +std::unique_ptr ImageFrame::Release() { return std::move(pixel_data_); } 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(height_, height); // row_bytes = channel_size * num_channels * width @@ -192,9 +193,9 @@ void ImageFrame::SetAlignmentPaddingAreas() { const int pixel_size = ByteDepth() * NumberOfChannels(); const int padding_size = width_step_ - width_ * pixel_size; for (int row = 0; row < height_; ++row) { - uint8* row_start = pixel_data_.get() + width_step_ * row; - uint8* last_pixel_in_row = row_start + (width_ - 1) * pixel_size; - uint8* padding = row_start + width_ * pixel_size; + uint8_t* row_start = pixel_data_.get() + width_step_ * row; + uint8_t* last_pixel_in_row = row_start + (width_ - 1) * pixel_size; + uint8_t* padding = row_start + width_ * pixel_size; int padding_index = 0; while (padding_index + pixel_size - 1 < padding_size) { // 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(); } -bool ImageFrame::IsAligned(uint32 alignment_boundary) const { +bool ImageFrame::IsAligned(uint32_t alignment_boundary) const { CHECK(IsValidAlignmentNumber(alignment_boundary)); if (!pixel_data_) { return false; @@ -236,7 +237,7 @@ bool ImageFrame::IsAligned(uint32 alignment_boundary) const { } // static -bool ImageFrame::IsValidAlignmentNumber(uint32 alignment_boundary) { +bool ImageFrame::IsValidAlignmentNumber(uint32_t alignment_boundary) { return CountOnes(alignment_boundary) == 1; } @@ -293,25 +294,25 @@ int ImageFrame::ChannelSize() const { return ChannelSizeForFormat(format_); } int ImageFrame::ChannelSizeForFormat(ImageFormat::Format format) { switch (format) { case ImageFormat::GRAY8: - return sizeof(uint8); + return sizeof(uint8_t); case ImageFormat::SRGB: - return sizeof(uint8); + return sizeof(uint8_t); case ImageFormat::SRGBA: - return sizeof(uint8); + return sizeof(uint8_t); case ImageFormat::GRAY16: - return sizeof(uint16); + return sizeof(uint16_t); case ImageFormat::SRGB48: - return sizeof(uint16); + return sizeof(uint16_t); case ImageFormat::SRGBA64: - return sizeof(uint16); + return sizeof(uint16_t); case ImageFormat::VEC32F1: return sizeof(float); case ImageFormat::VEC32F2: return sizeof(float); case ImageFormat::LAB8: - return sizeof(uint8); + return sizeof(uint8_t); case ImageFormat::SBGRA: - return sizeof(uint8); + return sizeof(uint8_t); default: LOG(FATAL) << InvalidFormatString(format); } @@ -347,7 +348,7 @@ int ImageFrame::ByteDepthForFormat(ImageFormat::Format format) { } void ImageFrame::CopyFrom(const ImageFrame& image_frame, - uint32 alignment_boundary) { + uint32_t alignment_boundary) { // Reset the current image. Reset(image_frame.Format(), image_frame.Width(), image_frame.Height(), alignment_boundary); @@ -359,29 +360,29 @@ void ImageFrame::CopyFrom(const ImageFrame& image_frame, } void ImageFrame::CopyPixelData(ImageFormat::Format format, int width, - int height, const uint8* pixel_data, - uint32 alignment_boundary) { + int height, const uint8_t* pixel_data, + uint32_t alignment_boundary) { CopyPixelData(format, width, height, 0 /* contiguous storage */, pixel_data, alignment_boundary); } void ImageFrame::CopyPixelData(ImageFormat::Format format, int width, int height, int width_step, - const uint8* pixel_data, - uint32 alignment_boundary) { + const uint8_t* pixel_data, + uint32_t alignment_boundary) { Reset(format, width, height, alignment_boundary); InternalCopyFrom(width, height, width_step, ChannelSizeForFormat(format), pixel_data); } -void ImageFrame::CopyToBuffer(uint8* buffer, int buffer_size) const { +void ImageFrame::CopyToBuffer(uint8_t* buffer, int buffer_size) const { CHECK(buffer); CHECK_EQ(1, ByteDepth()); const int data_size = width_ * height_ * NumberOfChannels(); CHECK_LE(data_size, buffer_size); if (IsContiguous()) { // The data is stored contiguously, we can just copy. - const uint8* src = reinterpret_cast(pixel_data_.get()); + const uint8_t* src = reinterpret_cast(pixel_data_.get()); std::copy_n(src, data_size, buffer); } else { 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_EQ(2, ByteDepth()); const int data_size = width_ * height_ * NumberOfChannels(); CHECK_LE(data_size, buffer_size); if (IsContiguous()) { // The data is stored contiguously, we can just copy. - const uint16* src = reinterpret_cast(pixel_data_.get()); + const uint16_t* src = reinterpret_cast(pixel_data_.get()); std::copy_n(src, data_size, buffer); } else { InternalCopyToBuffer(0 /* contiguous storage */, diff --git a/mediapipe/framework/formats/image_frame_opencv_test.cc b/mediapipe/framework/formats/image_frame_opencv_test.cc index f75915d06..ae6f90f81 100644 --- a/mediapipe/framework/formats/image_frame_opencv_test.cc +++ b/mediapipe/framework/formats/image_frame_opencv_test.cc @@ -51,8 +51,8 @@ TEST(ImageFrameOpencvTest, ConvertToMat) { // Check adding constant images. const uint8_t frame1_val = 12; const uint8_t frame2_val = 34; - SetToColor(&frame1_val, &frame1); - SetToColor(&frame2_val, &frame2); + SetToColor(&frame1_val, &frame1); + SetToColor(&frame2_val, &frame2); // Get Mat wrapper around ImageFrame memory (zero copy). cv::Mat frame1_mat = formats::MatView(&frame1); cv::Mat frame2_mat = formats::MatView(&frame2); @@ -62,7 +62,7 @@ TEST(ImageFrameOpencvTest, ConvertToMat) { EXPECT_EQ(frame_avg, frame1_val + frame2_val); // 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 - 6) + (i_height - 6) * frame1.WidthStep()] = 100; double min, max; @@ -84,8 +84,8 @@ TEST(ImageFrameOpencvTest, ConvertToIpl) { // Check adding constant images. const uint8_t frame1_val = 12; const uint8_t frame2_val = 34; - SetToColor(&frame1_val, &frame1); - SetToColor(&frame2_val, &frame2); + SetToColor(&frame1_val, &frame1); + SetToColor(&frame2_val, &frame2); const cv::Mat frame1_mat = formats::MatView(&frame1); const cv::Mat frame2_mat = formats::MatView(&frame2); const cv::Mat frame_sum = frame1_mat + frame2_mat; @@ -93,7 +93,7 @@ TEST(ImageFrameOpencvTest, ConvertToIpl) { EXPECT_EQ(frame_avg, frame1_val + frame2_val); // 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 - 6) + (i_height - 6) * frame1.WidthStep()] = 100; double min, max; diff --git a/mediapipe/framework/formats/image_opencv.cc b/mediapipe/framework/formats/image_opencv.cc index 9ccaa632b..7d9ce4a13 100644 --- a/mediapipe/framework/formats/image_opencv.cc +++ b/mediapipe/framework/formats/image_opencv.cc @@ -96,7 +96,7 @@ std::shared_ptr MatView(const mediapipe::Image* image) { image->image_format()))}; auto owner = std::make_shared(const_cast(image)); - uint8* data_ptr = owner->lock.Pixels(); + uint8_t* data_ptr = owner->lock.Pixels(); CHECK(data_ptr != nullptr); // Use Image to initialize in-place. Image still owns memory. if (steps[0] == sizes[1] * image->channels() * diff --git a/mediapipe/framework/formats/location_opencv.cc b/mediapipe/framework/formats/location_opencv.cc index de59633ca..6e15b299a 100644 --- a/mediapipe/framework/formats/location_opencv.cc +++ b/mediapipe/framework/formats/location_opencv.cc @@ -91,7 +91,7 @@ std::unique_ptr GetCvMask(const Location& location) { new cv::Mat(mask.height(), mask.width(), CV_8UC1, cv::Scalar(0))); for (const auto& interval : location_data.mask().rasterization().interval()) { for (int x = interval.left_x(); x <= interval.right_x(); ++x) { - mat->at(interval.y(), x) = 255; + mat->at(interval.y(), x) = 255; } } return mat; @@ -174,7 +174,7 @@ void EnlargeLocation(Location& location, const float factor) { } else { cv::erode(*mask, *mask, morph_element); } - CreateCvMaskLocation(*mask).ConvertToProto(&location_data); + CreateCvMaskLocation(*mask).ConvertToProto(&location_data); break; } } diff --git a/mediapipe/framework/formats/location_opencv_test.cc b/mediapipe/framework/formats/location_opencv_test.cc index 5740d2b17..6e3a89b58 100644 --- a/mediapipe/framework/formats/location_opencv_test.cc +++ b/mediapipe/framework/formats/location_opencv_test.cc @@ -25,8 +25,8 @@ namespace mediapipe { // segments per row. static const int kWidth = 7; static const int kHeight = 3; -const std::vector kTestPatternVector = {0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, - 0, 0, 0, 1, 0, 1, 0, 1, 0, 0}; +const std::vector kTestPatternVector = { + 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. const std::vector> kTestPatternIntervals = { @@ -67,8 +67,8 @@ TEST(LocationOpencvTest, CreateBBoxLocation) { } TEST(LocationOpencvTest, CreateCvMaskLocation) { - cv::Mat_ test_mask(kHeight, kWidth, - const_cast(kTestPatternVector.data())); + cv::Mat_ test_mask(kHeight, kWidth, + const_cast(kTestPatternVector.data())); Location location = CreateCvMaskLocation(test_mask); auto intervals = location.ConvertToProto().mask().rasterization().interval(); EXPECT_EQ(intervals.size(), kTestPatternIntervals.size()); @@ -157,8 +157,8 @@ TEST(LocationOpenCvTest, GetCvMask) { auto cv_mask = *GetCvMask(test_location); EXPECT_EQ(cv_mask.cols * cv_mask.rows, kTestPatternVector.size()); int flat_idx = 0; - for (auto it = cv_mask.begin(); it != cv_mask.end(); ++it) { - const uint8 expected_value = kTestPatternVector[flat_idx] == 0 ? 0 : 255; + for (auto it = cv_mask.begin(); it != cv_mask.end(); ++it) { + const uint8_t expected_value = kTestPatternVector[flat_idx] == 0 ? 0 : 255; EXPECT_EQ(*it, expected_value); flat_idx++; }