From 2cce88080e8d320a547a870e9bf3f2f9f86fa2e0 Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Fri, 6 Jan 2023 15:18:12 -0800 Subject: [PATCH] Internal change PiperOrigin-RevId: 500271109 --- mediapipe/calculators/image/scale_image_utils.cc | 6 ++++++ mediapipe/calculators/image/scale_image_utils_test.cc | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/mediapipe/calculators/image/scale_image_utils.cc b/mediapipe/calculators/image/scale_image_utils.cc index 490d0336a..86a53ffc5 100644 --- a/mediapipe/calculators/image/scale_image_utils.cc +++ b/mediapipe/calculators/image/scale_image_utils.cc @@ -142,6 +142,9 @@ absl::Status FindOutputDimensions(int input_width, // static_cast(input_height)); try_width = (try_width / 2) * 2; try_height = (try_height / 2) * 2; + // The output width/height should be greater than 0. + try_width = std::max(try_width, 1); + try_height = std::max(try_height, 1); if (target_height <= 0 || try_height <= target_height) { // The resulting height based on the target width and aspect ratio @@ -160,6 +163,9 @@ absl::Status FindOutputDimensions(int input_width, // static_cast(input_width)); try_width = (try_width / 2) * 2; try_height = (try_height / 2) * 2; + // The output width/height should be greater than 0. + try_width = std::max(try_width, 1); + try_height = std::max(try_height, 1); if (target_width <= 0 || try_width <= target_width) { // The resulting width based on the target width and aspect ratio diff --git a/mediapipe/calculators/image/scale_image_utils_test.cc b/mediapipe/calculators/image/scale_image_utils_test.cc index bda1fa4d6..b4810071c 100644 --- a/mediapipe/calculators/image/scale_image_utils_test.cc +++ b/mediapipe/calculators/image/scale_image_utils_test.cc @@ -124,6 +124,16 @@ TEST(ScaleImageUtilsTest, FindOutputDimensionsPreserveRatio) { &output_width, &output_height)); EXPECT_EQ(151, output_width); EXPECT_EQ(101, output_height); + // Scale to height 1. + MP_ASSERT_OK(FindOutputDimensions(10000, 10, 100, 0, 0, true, 2, + &output_width, &output_height)); + EXPECT_EQ(100, output_width); + EXPECT_EQ(1, output_height); + // Scale to width 1. + MP_ASSERT_OK(FindOutputDimensions(10, 10000, 0, 100, 0, true, 2, + &output_width, &output_height)); + EXPECT_EQ(1, output_width); + EXPECT_EQ(100, output_height); } // Tests scaling without keeping the aspect ratio fixed.