From b6593517c4d2cd9f3d9e176e82ce43e6d0085e3e Mon Sep 17 00:00:00 2001 From: Kumataro Date: Mon, 27 May 2024 23:33:43 +0900 Subject: [PATCH] Merge pull request #25647 from Kumataro:fix25646 imgcodecs: support IMWRITE_JPEG_LUMA/CHROMA_QUALITY with internal libjpeg-turbo #25647 Close #25646 - increase JPEG_LIB_VERSION for internal libjpeg-turbo from 62 to 70 - add log when using IMWRITE_JPEG_LUMA/CHROMA_QUALITY with JPEG_LIB_VERSION<70 - add document IMWRITE_JPEG_LUMA/CHROMA_QUALITY requests JPEG_LIB_VERSION >= 70 ### 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 --- 3rdparty/libjpeg-turbo/CMakeLists.txt | 2 +- .../imgcodecs/include/opencv2/imgcodecs.hpp | 4 +- modules/imgcodecs/src/grfmt_jpeg.cpp | 7 ++- modules/imgcodecs/test/test_jpeg.cpp | 62 +++++++++++++++++++ 4 files changed, 70 insertions(+), 5 deletions(-) diff --git a/3rdparty/libjpeg-turbo/CMakeLists.txt b/3rdparty/libjpeg-turbo/CMakeLists.txt index 5ab3ee11e4..f41665f329 100644 --- a/3rdparty/libjpeg-turbo/CMakeLists.txt +++ b/3rdparty/libjpeg-turbo/CMakeLists.txt @@ -134,7 +134,7 @@ if(WITH_ARITH_DEC) set(D_ARITH_CODING_SUPPORTED 1) endif() -set(JPEG_LIB_VERSION 62) +set(JPEG_LIB_VERSION 70) # OpenCV set(JPEG_LIB_VERSION "${VERSION}-${JPEG_LIB_VERSION}" PARENT_SCOPE) diff --git a/modules/imgcodecs/include/opencv2/imgcodecs.hpp b/modules/imgcodecs/include/opencv2/imgcodecs.hpp index 0ca202722d..eba25ce1cf 100644 --- a/modules/imgcodecs/include/opencv2/imgcodecs.hpp +++ b/modules/imgcodecs/include/opencv2/imgcodecs.hpp @@ -87,8 +87,8 @@ enum ImwriteFlags { IMWRITE_JPEG_PROGRESSIVE = 2, //!< Enable JPEG features, 0 or 1, default is False. IMWRITE_JPEG_OPTIMIZE = 3, //!< Enable JPEG features, 0 or 1, default is False. IMWRITE_JPEG_RST_INTERVAL = 4, //!< JPEG restart interval, 0 - 65535, default is 0 - no restart. - IMWRITE_JPEG_LUMA_QUALITY = 5, //!< Separate luma quality level, 0 - 100, default is -1 - don't use. - IMWRITE_JPEG_CHROMA_QUALITY = 6, //!< Separate chroma quality level, 0 - 100, default is -1 - don't use. + IMWRITE_JPEG_LUMA_QUALITY = 5, //!< Separate luma quality level, 0 - 100, default is -1 - don't use. If JPEG_LIB_VERSION < 70, Not supported. + IMWRITE_JPEG_CHROMA_QUALITY = 6, //!< Separate chroma quality level, 0 - 100, default is -1 - don't use. If JPEG_LIB_VERSION < 70, Not supported. IMWRITE_JPEG_SAMPLING_FACTOR = 7, //!< For JPEG, set sampling factor. See cv::ImwriteJPEGSamplingFactorParams. IMWRITE_PNG_COMPRESSION = 16, //!< For PNG, it can be the compression level from 0 to 9. A higher value means a smaller size and longer compression time. If specified, strategy is changed to IMWRITE_PNG_STRATEGY_DEFAULT (Z_DEFAULT_STRATEGY). Default value is 1 (best speed setting). IMWRITE_PNG_STRATEGY = 17, //!< One of cv::ImwritePNGFlags, default is IMWRITE_PNG_STRATEGY_RLE. diff --git a/modules/imgcodecs/src/grfmt_jpeg.cpp b/modules/imgcodecs/src/grfmt_jpeg.cpp index 4a2aee12b0..4e3b1df48d 100644 --- a/modules/imgcodecs/src/grfmt_jpeg.cpp +++ b/modules/imgcodecs/src/grfmt_jpeg.cpp @@ -783,9 +783,9 @@ bool JpegEncoder::write( const Mat& img, const std::vector& params ) cinfo.comp_info[1].h_samp_factor = 1; } -#if JPEG_LIB_VERSION >= 70 if (luma_quality >= 0 && chroma_quality >= 0) { +#if JPEG_LIB_VERSION >= 70 cinfo.q_scale_factor[0] = jpeg_quality_scaling(luma_quality); cinfo.q_scale_factor[1] = jpeg_quality_scaling(chroma_quality); if ( luma_quality != chroma_quality ) @@ -797,8 +797,11 @@ bool JpegEncoder::write( const Mat& img, const std::vector& params ) cinfo.comp_info[1].h_samp_factor = 1; } jpeg_default_qtables( &cinfo, TRUE ); - } +#else + // See https://github.com/opencv/opencv/issues/25646 + CV_LOG_ONCE_WARNING(NULL, cv::format("IMWRITE_JPEG_LUMA/CHROMA_QUALITY are not supported bacause JPEG_LIB_VERSION < 70.")); #endif // #if JPEG_LIB_VERSION >= 70 + } jpeg_start_compress( &cinfo, TRUE ); diff --git a/modules/imgcodecs/test/test_jpeg.cpp b/modules/imgcodecs/test/test_jpeg.cpp index 12abc0a8e3..ee9da01aa7 100644 --- a/modules/imgcodecs/test/test_jpeg.cpp +++ b/modules/imgcodecs/test/test_jpeg.cpp @@ -7,6 +7,10 @@ namespace opencv_test { namespace { #ifdef HAVE_JPEG +extern "C" { +#include "jpeglib.h" +} + /** * Test for check whether reading exif orientation tag was processed successfully or not * The test info is the set of 8 images named testExifRotate_{1 to 8}.jpg @@ -308,6 +312,64 @@ TEST(Imgcodecs_Jpeg, encode_subsamplingfactor_usersetting_invalid) } } +//================================================================================================== +// See https://github.com/opencv/opencv/issues/25646 +typedef testing::TestWithParam> Imgcodecs_Jpeg_encode_withLumaChromaQuality; + +TEST_P(Imgcodecs_Jpeg_encode_withLumaChromaQuality, basic) +{ + const int luma = get<0>(GetParam()); + const int chroma = get<1>(GetParam()); + + cvtest::TS& ts = *cvtest::TS::ptr(); + string fname = string(ts.get_data_path()) + "../cv/shared/lena.png"; + + cv::Mat src = imread(fname, cv::IMREAD_COLOR); + ASSERT_FALSE(src.empty()); + + std::vector jpegNormal; + ASSERT_NO_THROW(cv::imencode(".jpg", src, jpegNormal)); + + std::vector param; + param.push_back(IMWRITE_JPEG_LUMA_QUALITY); + param.push_back(luma); + param.push_back(IMWRITE_JPEG_CHROMA_QUALITY); + param.push_back(chroma); + + std::vector jpegCustom; + ASSERT_NO_THROW(cv::imencode(".jpg", src, jpegCustom, param)); + +#if JPEG_LIB_VERSION >= 70 + // For jpeg7+, we can support IMWRITE_JPEG_LUMA_QUALITY and IMWRITE_JPEG_CHROMA_QUALITY. + if( (luma == 95 /* Default Luma Quality */ ) && ( chroma == 95 /* Default Chroma Quality */)) + { + EXPECT_EQ(jpegNormal, jpegCustom); + } + else + { + EXPECT_NE(jpegNormal, jpegCustom); + } +#else + // For jpeg6-, we cannot support IMWRITE_JPEG_LUMA/CHROMA_QUALITY because jpeg_default_qtables() is missing. + // - IMWRITE_JPEG_LUMA_QUALITY updates internal parameter of IMWRITE_JPEG_QUALITY. + // - IMWRITE_JPEG_CHROMA_QUALITY updates nothing. + if( luma == 95 /* Default Jpeg Quality */ ) + { + EXPECT_EQ(jpegNormal, jpegCustom); + } + else + { + EXPECT_NE(jpegNormal, jpegCustom); + } +#endif +} + +INSTANTIATE_TEST_CASE_P( /* nothing */, + Imgcodecs_Jpeg_encode_withLumaChromaQuality, + testing::Combine( + testing::Values(70, 95, 100), // IMWRITE_JPEG_LUMA_QUALITY + testing::Values(70, 95, 100) )); // IMWRITE_JPEG_CHROMA_QUALITY + #endif // HAVE_JPEG }} // namespace