From 386be68f9361feb9375fd1563fc4b8671aaea7b2 Mon Sep 17 00:00:00 2001 From: Suleyman TURKMEN Date: Sun, 9 Nov 2025 18:23:13 +0300 Subject: [PATCH] Merge pull request #27981 from sturkmen72:fix_alpha_handling Fix alpha handling for blending in PNG format #27981 ### Pull Request Readiness Checklist closes #27974 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 - [ ] 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 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/imgcodecs/src/grfmt_png.cpp b/modules/imgcodecs/src/grfmt_png.cpp index dbb3c41cf2..a505266a8b 100644 --- a/modules/imgcodecs/src/grfmt_png.cpp +++ b/modules/imgcodecs/src/grfmt_png.cpp @@ -715,7 +715,7 @@ void PngDecoder::compose_frame(std::vector& rows_dst, const std::vect // Blending mode for (unsigned int i = 0; i < w; i++, sp += channels, dp += channels) { - uint16_t alpha = sp[3]; + uint16_t alpha = channels < 4 ? 0 : sp[3]; if (channels < 4 || alpha == 65535 || dp[3] == 0) { // Fully opaque OR destination fully transparent: direct copy @@ -745,7 +745,7 @@ void PngDecoder::compose_frame(std::vector& rows_dst, const std::vect // Blending mode for (unsigned int i = 0; i < w; i++, sp += channels, dp += channels) { - uint8_t alpha = sp[3]; + uint8_t alpha = channels < 4 ? 0 : sp[3]; if (channels < 4 || alpha == 255 || dp[3] == 0) { // Fully opaque OR destination fully transparent: direct copy