Internal change

PiperOrigin-RevId: 500271109
This commit is contained in:
MediaPipe Team 2023-01-06 15:18:12 -08:00 committed by Copybara-Service
parent 667fd81ddc
commit 2cce88080e
2 changed files with 16 additions and 0 deletions

View File

@ -142,6 +142,9 @@ absl::Status FindOutputDimensions(int input_width, //
static_cast<double>(input_height)); static_cast<double>(input_height));
try_width = (try_width / 2) * 2; try_width = (try_width / 2) * 2;
try_height = (try_height / 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) { if (target_height <= 0 || try_height <= target_height) {
// The resulting height based on the target width and aspect ratio // The resulting height based on the target width and aspect ratio
@ -160,6 +163,9 @@ absl::Status FindOutputDimensions(int input_width, //
static_cast<double>(input_width)); static_cast<double>(input_width));
try_width = (try_width / 2) * 2; try_width = (try_width / 2) * 2;
try_height = (try_height / 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) { if (target_width <= 0 || try_width <= target_width) {
// The resulting width based on the target width and aspect ratio // The resulting width based on the target width and aspect ratio

View File

@ -124,6 +124,16 @@ TEST(ScaleImageUtilsTest, FindOutputDimensionsPreserveRatio) {
&output_width, &output_height)); &output_width, &output_height));
EXPECT_EQ(151, output_width); EXPECT_EQ(151, output_width);
EXPECT_EQ(101, output_height); 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. // Tests scaling without keeping the aspect ratio fixed.