From 462929916304c131a4cbecf3a92e113eeca0075d Mon Sep 17 00:00:00 2001 From: Suleyman TURKMEN Date: Mon, 23 Jun 2025 12:47:21 +0300 Subject: [PATCH] Merge pull request #27469 from sturkmen72:png-fixes Fix for 2 channel PNGs #27469 closes #26825 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --- modules/imgcodecs/src/grfmt_png.cpp | 8 ++------ modules/imgcodecs/test/test_animation.cpp | 5 +++-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/modules/imgcodecs/src/grfmt_png.cpp b/modules/imgcodecs/src/grfmt_png.cpp index 20653cf3c1..a47db5aa2a 100644 --- a/modules/imgcodecs/src/grfmt_png.cpp +++ b/modules/imgcodecs/src/grfmt_png.cpp @@ -508,11 +508,6 @@ bool PngDecoder::readData( Mat& img ) { return false; } - // Asking for blend over with no alpha is invalid. - if (bop == 1 && mat_cur.channels() != 4) - { - return false; - } memcpy(&m_chunkIHDR.p[8], &chunk.p[12], 8); @@ -1654,7 +1649,8 @@ bool PngEncoder::writeanimation(const Animation& animation, const std::vector 2) + cvtColor(tmp, tmp, COLOR_BGRA2RGBA); apngFrame.setMat(tmp); deflateRectOp(apngFrame.getPixels(), x0, y0, w0, h0, bpp, rowbytes, zbuf_size, 0); diff --git a/modules/imgcodecs/test/test_animation.cpp b/modules/imgcodecs/test/test_animation.cpp index 5fead70135..2d45132ffd 100644 --- a/modules/imgcodecs/test/test_animation.cpp +++ b/modules/imgcodecs/test/test_animation.cpp @@ -669,14 +669,15 @@ TEST(Imgcodecs_APNG, animation_imread_preview) { // Set the path to the test image directory and filename for loading. const string root = cvtest::TS::ptr()->get_data_path(); - const string filename = root + "readwrite/033.png"; + const string filename = root + "readwrite/034.png"; cv::Mat imread_result; cv::imread(filename, imread_result, cv::IMREAD_UNCHANGED); EXPECT_FALSE(imread_result.empty()); Animation animation; - imreadanimation(filename, animation); + ASSERT_TRUE(imreadanimation(filename, animation)); EXPECT_FALSE(animation.still_image.empty()); + EXPECT_EQ((size_t)2, animation.frames.size()); EXPECT_EQ(0, cv::norm(animation.still_image, imread_result, cv::NORM_INF)); }