mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 15:53:03 +04:00
Merge branch 4.x
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html
|
||||
#include "test_precomp.hpp"
|
||||
#include "test_common.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace opencv_test { namespace {
|
||||
|
||||
// See https://github.com/opencv/opencv/issues/27789
|
||||
// See https://github.com/opencv/opencv/issues/23233
|
||||
TEST(Imgcodecs_BMP, encode_decode_over1GB_regression27789)
|
||||
{
|
||||
applyTestTag( CV_TEST_TAG_MEMORY_2GB, CV_TEST_TAG_LONG );
|
||||
|
||||
// Create large Mat over 1GB
|
||||
// 20000 px * 18000 px * 24 bpp(3ch) = 1,080,000,000 bytes
|
||||
// 1 GiB = 1,073,741,824 bytes
|
||||
cv::Mat src(20000, 18000, CV_8UC3, cv::Scalar(0,0,0));
|
||||
|
||||
// Encode large BMP file.
|
||||
std::vector<uint8_t> buf;
|
||||
bool ret = false;
|
||||
ASSERT_NO_THROW(ret = cv::imencode(".bmp", src, buf, {}));
|
||||
ASSERT_TRUE(ret);
|
||||
|
||||
src.release(); // To reduce usage memory, it is needed.
|
||||
|
||||
// Decode large BMP file.
|
||||
cv::Mat dst;
|
||||
ASSERT_NO_THROW(dst = cv::imdecode(buf, cv::IMREAD_COLOR));
|
||||
ASSERT_FALSE(dst.empty());
|
||||
}
|
||||
|
||||
TEST(Imgcodecs_BMP, write_read_over1GB_regression27789)
|
||||
{
|
||||
// tag CV_TEST_TAG_VERYLONG applied to skip on CI. The test writes ~1GB file.
|
||||
applyTestTag( CV_TEST_TAG_MEMORY_2GB, CV_TEST_TAG_VERYLONG );
|
||||
string bmpFilename = cv::tempfile(".bmp"); // To remove it, test must use EXPECT_* instead of ASSERT_*.
|
||||
|
||||
// Create large Mat over 1GB
|
||||
// 20000 px * 18000 px * 24 bpp(3ch) = 1,080,000,000 bytes
|
||||
// 1 GiB = 1,073,741,824 bytes
|
||||
cv::Mat src(20000, 18000, CV_8UC3, cv::Scalar(0,0,0));
|
||||
|
||||
// Write large BMP file.
|
||||
bool ret = false;
|
||||
EXPECT_NO_THROW(ret = cv::imwrite(bmpFilename, src, {}));
|
||||
EXPECT_TRUE(ret);
|
||||
|
||||
// Read large BMP file.
|
||||
cv::Mat dst;
|
||||
EXPECT_NO_THROW(dst = cv::imread(bmpFilename, cv::IMREAD_COLOR));
|
||||
EXPECT_FALSE(dst.empty());
|
||||
|
||||
remove(bmpFilename.c_str());
|
||||
}
|
||||
|
||||
|
||||
}} // namespace
|
||||
@@ -157,6 +157,51 @@ TEST(Imgcodecs_Png, decode_regression27295)
|
||||
EXPECT_TRUE(img.empty());
|
||||
}
|
||||
|
||||
// The program must not crash even when decoding a corrupted APNG image.
|
||||
// See https://github.com/opencv/opencv/issues/27744
|
||||
#if defined(HAVE_PNG) // APNG is supported only with using libpng
|
||||
TEST(Imgcodecs_Png, decode_regression27744)
|
||||
{
|
||||
// Create APNG stream
|
||||
Animation anim;
|
||||
for(size_t i = 0 ; i < 3 ; i++) {
|
||||
Mat frame(120, 120, CV_8UC3, Scalar(0,0,0));
|
||||
putText(frame, cv::format("%d", static_cast<int>(i)), Point(5, 28), FONT_HERSHEY_SIMPLEX, .5, Scalar(100, 255, 0, 255), 2);
|
||||
anim.frames.push_back(frame);
|
||||
anim.durations.push_back(30);
|
||||
}
|
||||
bool ret = false;
|
||||
vector<uchar> buff;
|
||||
EXPECT_NO_THROW(ret = imencodeanimation(".png", anim, buff));
|
||||
ASSERT_TRUE(ret) << "imencodeanimation() returns false";
|
||||
|
||||
// Find IDAT chunk
|
||||
const vector<uchar> IDAT = {'I', 'D', 'A', 'T' };
|
||||
std::vector<uchar>::iterator it = std::search(buff.begin(), buff.end(), IDAT.begin(), IDAT.end());
|
||||
ASSERT_FALSE(it == buff.end()) << "IDAT chunk not found";
|
||||
|
||||
// Determine the range to test
|
||||
// APNG stream contains as { len0, len1, len2, len3, 'I', 'D', 'A' 'T', ... }
|
||||
size_t idx = std::distance(buff.begin(), it); // 'I' position
|
||||
size_t len = (buff[idx-4] << 24) + (buff[idx-3] << 16) +
|
||||
(buff[idx-2] << 8) + (buff[idx-1]); // IDAT chunk length
|
||||
idx = idx + 4; // Move to IDAT body
|
||||
|
||||
// Test
|
||||
for(size_t i = 0; i < len; i++, idx++) {
|
||||
vector<uint8_t> work = buff;
|
||||
work[idx] = static_cast<uint8_t>((static_cast<uint32_t>(work[idx]) + 1) & 0xff);
|
||||
|
||||
Mat dst;
|
||||
EXPECT_NO_THROW(dst = imdecode(work, cv::IMREAD_COLOR));
|
||||
if(dst.empty()) {
|
||||
// libpng detects some error, but the program is not crashed. Test is passed.
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
typedef testing::TestWithParam<string> Imgcodecs_Png_PngSuite;
|
||||
|
||||
// Parameterized test for decoding PNG files from the PNGSuite test set
|
||||
|
||||
@@ -619,4 +619,13 @@ TEST(Imgcodecs_Params, imencode_regression_22752)
|
||||
EXPECT_ANY_THROW(cv::imencode("test.jpg", img, buf, params)); // parameters size or missing JPEG codec
|
||||
}
|
||||
|
||||
TEST(Imgcodecs, decode_over2GB)
|
||||
{
|
||||
applyTestTag(CV_TEST_TAG_MEMORY_6GB);
|
||||
// empty buffer more than 2GB size
|
||||
std::vector<uint8_t> buf(size_t(INT_MAX) + 4096);
|
||||
cv::Mat dst;
|
||||
EXPECT_THROW(dst = cv::imdecode(buf, cv::IMREAD_COLOR), cv::Exception);
|
||||
}
|
||||
|
||||
}} // namespace
|
||||
|
||||
Reference in New Issue
Block a user