From 8e5dbc03fe0c8264e667de5bbae4d0ab04dcab6b Mon Sep 17 00:00:00 2001 From: Suleyman TURKMEN Date: Mon, 14 Oct 2024 11:23:02 +0300 Subject: [PATCH] Merge pull request #26298 from sturkmen72:avif Proposed solution for the issue 26297 #26298 closes #26297 ### 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 --- CMakeLists.txt | 2 +- modules/imgcodecs/src/grfmt_avif.cpp | 8 ++++++-- modules/imgcodecs/test/test_avif.cpp | 11 ++++++++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d103ca195..67c4f35d5d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -216,7 +216,7 @@ OCV_OPTION(WITH_1394 "Include IEEE1394 support" OFF OCV_OPTION(WITH_AVFOUNDATION "Use AVFoundation for Video I/O (iOS/visionOS/Mac)" ON VISIBLE_IF APPLE VERIFY HAVE_AVFOUNDATION) -OCV_OPTION(WITH_AVIF "Enable AVIF support" OFF +OCV_OPTION(WITH_AVIF "Enable AVIF support" ON VERIFY HAVE_AVIF) OCV_OPTION(WITH_CAP_IOS "Enable iOS video capture" ON VISIBLE_IF IOS diff --git a/modules/imgcodecs/src/grfmt_avif.cpp b/modules/imgcodecs/src/grfmt_avif.cpp index 98ddb73362..4752c6ece4 100644 --- a/modules/imgcodecs/src/grfmt_avif.cpp +++ b/modules/imgcodecs/src/grfmt_avif.cpp @@ -142,8 +142,7 @@ static constexpr size_t kAvifSignatureSize = 500; AvifDecoder::AvifDecoder() { m_buf_supported = true; channels_ = 0; - decoder_ = avifDecoderCreate(); - decoder_->strictFlags = AVIF_STRICT_DISABLED; + decoder_ = nullptr; } AvifDecoder::~AvifDecoder() { @@ -181,6 +180,11 @@ bool AvifDecoder::checkSignature(const String &signature) const { ImageDecoder AvifDecoder::newDecoder() const { return makePtr(); } bool AvifDecoder::readHeader() { + if (decoder_) + return true; + + decoder_ = avifDecoderCreate(); + decoder_->strictFlags = AVIF_STRICT_DISABLED; if (!m_buf.empty()) { CV_Assert(m_buf.type() == CV_8UC1); CV_Assert(m_buf.rows == 1); diff --git a/modules/imgcodecs/test/test_avif.cpp b/modules/imgcodecs/test/test_avif.cpp index 0d8a718756..ba623110c0 100644 --- a/modules/imgcodecs/test/test_avif.cpp +++ b/modules/imgcodecs/test/test_avif.cpp @@ -337,11 +337,20 @@ TEST_P(Imgcodecs_Avif_Animation_WriteDecodeSuite, encode_decode) { std::vector buf(size); EXPECT_TRUE(file.read(reinterpret_cast(buf.data()), size)); file.close(); - EXPECT_EQ(0, remove(output.c_str())); std::vector anim; ASSERT_TRUE(cv::imdecodemulti(buf, imread_mode_, anim)); ValidateRead(anim_original, anim); + + if (imread_mode_ == IMREAD_UNCHANGED) { + ImageCollection collection(output, IMREAD_UNCHANGED); + anim.clear(); + for (auto&& i : collection) + anim.push_back(i); + ValidateRead(anim_original, anim); + } + + EXPECT_EQ(0, remove(output.c_str())); } INSTANTIATE_TEST_CASE_P(