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