diff --git a/lib/base/rect.h b/lib/base/rect.h index 16ac425d..58a7a25a 100644 --- a/lib/base/rect.h +++ b/lib/base/rect.h @@ -110,7 +110,7 @@ class RectT { bool IsSame(const RectT& other) const { return x0_ == other.x0_ && xsize_ == other.xsize_ && y0_ == other.y0_ && - ysize_ <= other.ysize_; + ysize_ == other.ysize_; } // Returns true if this Rect fully resides in the given image. ImageT could be diff --git a/lib/base/rect_test.cc b/lib/base/rect_test.cc new file mode 100644 index 00000000..44af327f --- /dev/null +++ b/lib/base/rect_test.cc @@ -0,0 +1,40 @@ +// Copyright (c) the JPEG XL Project Authors. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#include "lib/base/rect.h" + +#include + +#include "lib/base/testing.h" + +namespace jpegli { +namespace { + +TEST(RectTest, IsSameRequiresEqualSize) { + const RectT base(0, 0, 16, 16); + EXPECT_TRUE(base.IsSame(RectT(0, 0, 16, 16))); + EXPECT_FALSE(base.IsSame(RectT(1, 0, 16, 16))); + EXPECT_FALSE(base.IsSame(RectT(0, 1, 16, 16))); + EXPECT_FALSE(base.IsSame(RectT(0, 0, 8, 16))); + // A rect that is shorter or taller than `other` is not the same rect. The + // height comparison must be an equality, not `<=`. + EXPECT_FALSE(base.IsSame(RectT(0, 0, 16, 8))); + EXPECT_FALSE(base.IsSame(RectT(0, 0, 16, 32))); +} + +// Mirrors the APNG fcTL viewport check in third_party/apngdis/dec.cc: a frame +// whose viewport extends past the image bottom must not be treated as fully +// inside the image, otherwise later per-row copies read out of bounds. +TEST(RectTest, IntersectionIsSameRejectsOversizedHeight) { + const RectT image(0, 0, 16, 16); + const RectT inside(0, 0, 16, 16); + const RectT too_tall(0, 0, 16, 100); + EXPECT_TRUE(inside.Intersection(image).IsSame(inside)); + EXPECT_FALSE(too_tall.Intersection(image).IsSame(too_tall)); +} + +} // namespace +} // namespace jpegli diff --git a/lib/jpegli_lists.cmake b/lib/jpegli_lists.cmake index 9535e26e..8edaa21b 100644 --- a/lib/jpegli_lists.cmake +++ b/lib/jpegli_lists.cmake @@ -241,6 +241,7 @@ set(JPEGLI_INTERNAL_TESTLIB_FILES ) set(JPEGLI_INTERNAL_TESTS + base/rect_test.cc cms/tone_mapping_test.cc cms/transfer_functions_test.cc extras/butteraugli_test.cc