mirror of
https://github.com/opencv/opencv.git
synced 2026-11-10 02:35:59 -06:00
imgcodecs(avif): add support for alpha channel quality control #30499 This pull request adds support for independent alpha channel quality control when encoding AVIF images, resolving #30451. ### What was happening OpenCV previously only controlled color channel compression via `IMWRITE_AVIF_QUALITY`. Alpha channels remained unhandled or were tied to color quality, making it impossible to adjust color and alpha compression independently (for example, keeping crisp lossless alpha edges while compressing background RGB content lossily to save file size). ### What changed 1. In `modules/imgcodecs/include/opencv2/imgcodecs.hpp`, added `IMWRITE_AVIF_ALPHA_QUALITY = 515` (range 0 to 100, default 95), and updated documentation for `IMWRITE_AVIF_QUALITY` to clarify that it controls color planes. 2. In `modules/imgcodecs/src/grfmt_avif.cpp`: - Registered `IMWRITE_AVIF_ALPHA_QUALITY` in `m_supported_encode_key`. - Handled `IMWRITE_AVIF_ALPHA_QUALITY` to set `encoder_->qualityAlpha` (and quantizers for fallback paths). - Removed the automatic fallback that synced alpha quality to color quality, keeping the two parameters decoupled and order-independent. 3. Added dedicated unit test `Imgcodecs_Avif.alpha_quality_control` in `modules/imgcodecs/test/test_avif.cpp` verifying independent lossy/lossless combinations, order independence, and bounds clamping. ### Testing - Verified with standalone reproducer (`reproduce_opencv_30451.cpp`): - Quality=100, AlphaQuality=10: 0 color difference (lossless color) with lossy alpha. - Quality=10, AlphaQuality=100: 0 alpha difference (lossless alpha) with lossy color. - Parameter order independence confirmed. - Native test suite: All 413 AVIF tests passed in `opencv_test_imgcodecs`. ### 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. - [x] The feature is well documented and sample code can be built with the project CMake