From 2aee94752a7f8d023507203ec5c6b3a64edf2e7b Mon Sep 17 00:00:00 2001 From: Suleyman TURKMEN Date: Mon, 6 Jan 2025 14:25:08 +0300 Subject: [PATCH] Merge pull request #26714 from sturkmen72:png Fix for png durations and memory leak #26714 ### 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 - [ ] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] 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 | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/modules/imgcodecs/src/grfmt_png.cpp b/modules/imgcodecs/src/grfmt_png.cpp index b76a4cfaaf..c04fd2bf55 100644 --- a/modules/imgcodecs/src/grfmt_png.cpp +++ b/modules/imgcodecs/src/grfmt_png.cpp @@ -423,7 +423,6 @@ bool PngDecoder::readData( Mat& img ) fseek(m_f, -8, SEEK_CUR); else m_buf_pos -= 8; - } else m_mat_next.copyTo(mat_cur); @@ -456,9 +455,9 @@ bool PngDecoder::readData( Mat& img ) memcpy(frameNext.getPixels(), frameCur.getPixels(), imagesize); compose_frame(frameCur.getRows(), frameRaw.getRows(), bop, x0, y0, w0, h0, mat_cur); - if (delay_den < 1000) - delay_num = cvRound(1000.0 / delay_den); - m_animation.durations.push_back(delay_num); + if (!delay_den) + delay_den = 100; + m_animation.durations.push_back(cvRound(1000.*delay_num/delay_den)); if (mat_cur.channels() == img.channels()) mat_cur.copyTo(img); @@ -514,9 +513,9 @@ bool PngDecoder::readData( Mat& img ) if (processing_finish()) { compose_frame(frameCur.getRows(), frameRaw.getRows(), bop, x0, y0, w0, h0, mat_cur); - if (delay_den < 1000) - delay_num = cvRound(1000.0 / delay_den); - m_animation.durations.push_back(delay_num); + if (!delay_den) + delay_den = 100; + m_animation.durations.push_back(cvRound(1000.*delay_num/delay_den)); if (mat_cur.channels() == img.channels()) mat_cur.copyTo(img); @@ -736,6 +735,15 @@ bool PngDecoder::processing_start(void* frame_ptr, const Mat& img) { static uint8_t header[8] = { 137, 80, 78, 71, 13, 10, 26, 10 }; + if (m_png_ptr) + { + png_structp png_ptr = (png_structp)m_png_ptr; + png_infop info_ptr = (png_infop)m_info_ptr; + png_infop end_info = (png_infop)m_end_info; + png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); + m_png_ptr = m_info_ptr = m_end_info = nullptr; + } + png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); png_infop info_ptr = png_create_info_struct(png_ptr);