From ffd0cf49a71b9c2c65c371610d3e2f365db85d2d Mon Sep 17 00:00:00 2001 From: Jules <16029431+jules-ai@users.noreply.github.com> Date: Sun, 24 May 2026 16:41:17 +0800 Subject: [PATCH] Merge pull request #28667 from jules-ai:jules_fix_jpeg_sampling_factor Fix JPEG chroma configuration & Standardize component settings #28667 ### Problem Incorrectly treated Chroma as a single channel. In $YC_bC_r$, chroma must be set in both separate channels ($C_b$ and $C_r$). ### Fix - Explicit Setup: Configured all three components ($Y$, $C_b$, $C_r$) individually. - Standardization: Removed reliance on library defaults to prevent undefined behavior. ### 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 - [ ] 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_jpeg.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/modules/imgcodecs/src/grfmt_jpeg.cpp b/modules/imgcodecs/src/grfmt_jpeg.cpp index 499036cedf..a2520179b8 100644 --- a/modules/imgcodecs/src/grfmt_jpeg.cpp +++ b/modules/imgcodecs/src/grfmt_jpeg.cpp @@ -825,10 +825,12 @@ bool JpegEncoder::write( const Mat& img, const std::vector& params ) if( (channels > 1) && ( sampling_factor != 0 ) ) { - cinfo.comp_info[0].v_samp_factor = (sampling_factor >> 16 ) & 0xF; cinfo.comp_info[0].h_samp_factor = (sampling_factor >> 20 ) & 0xF; - cinfo.comp_info[1].v_samp_factor = 1; - cinfo.comp_info[1].h_samp_factor = 1; + cinfo.comp_info[0].v_samp_factor = (sampling_factor >> 16 ) & 0xF; + cinfo.comp_info[1].h_samp_factor = (sampling_factor >> 12 ) & 0xF; + cinfo.comp_info[1].v_samp_factor = (sampling_factor >> 8 ) & 0xF; + cinfo.comp_info[2].h_samp_factor = (sampling_factor >> 4 ) & 0xF; + cinfo.comp_info[2].v_samp_factor = (sampling_factor >> 0 ) & 0xF; } if (luma_quality >= 0 && chroma_quality >= 0) @@ -843,6 +845,8 @@ bool JpegEncoder::write( const Mat& img, const std::vector& params ) cinfo.comp_info[0].h_samp_factor = 1; cinfo.comp_info[1].v_samp_factor = 1; cinfo.comp_info[1].h_samp_factor = 1; + cinfo.comp_info[2].v_samp_factor = 1; + cinfo.comp_info[2].h_samp_factor = 1; } jpeg_default_qtables( &cinfo, TRUE ); #else