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
This commit is contained in:
Kumataro 2024-05-27 23:33:43 +09:00 committed by GitHub
parent c5976f7865
commit b6593517c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 70 additions and 5 deletions

View File

@ -134,7 +134,7 @@ if(WITH_ARITH_DEC)
set(D_ARITH_CODING_SUPPORTED 1) set(D_ARITH_CODING_SUPPORTED 1)
endif() endif()
set(JPEG_LIB_VERSION 62) set(JPEG_LIB_VERSION 70)
# OpenCV # OpenCV
set(JPEG_LIB_VERSION "${VERSION}-${JPEG_LIB_VERSION}" PARENT_SCOPE) set(JPEG_LIB_VERSION "${VERSION}-${JPEG_LIB_VERSION}" PARENT_SCOPE)

View File

@ -87,8 +87,8 @@ enum ImwriteFlags {
IMWRITE_JPEG_PROGRESSIVE = 2, //!< Enable JPEG features, 0 or 1, default is False. 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_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_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_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. 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_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_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. IMWRITE_PNG_STRATEGY = 17, //!< One of cv::ImwritePNGFlags, default is IMWRITE_PNG_STRATEGY_RLE.

View File

@ -783,9 +783,9 @@ bool JpegEncoder::write( const Mat& img, const std::vector<int>& params )
cinfo.comp_info[1].h_samp_factor = 1; cinfo.comp_info[1].h_samp_factor = 1;
} }
#if JPEG_LIB_VERSION >= 70
if (luma_quality >= 0 && chroma_quality >= 0) 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[0] = jpeg_quality_scaling(luma_quality);
cinfo.q_scale_factor[1] = jpeg_quality_scaling(chroma_quality); cinfo.q_scale_factor[1] = jpeg_quality_scaling(chroma_quality);
if ( luma_quality != chroma_quality ) if ( luma_quality != chroma_quality )
@ -797,8 +797,11 @@ bool JpegEncoder::write( const Mat& img, const std::vector<int>& params )
cinfo.comp_info[1].h_samp_factor = 1; cinfo.comp_info[1].h_samp_factor = 1;
} }
jpeg_default_qtables( &cinfo, TRUE ); 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 #endif // #if JPEG_LIB_VERSION >= 70
}
jpeg_start_compress( &cinfo, TRUE ); jpeg_start_compress( &cinfo, TRUE );

View File

@ -7,6 +7,10 @@ namespace opencv_test { namespace {
#ifdef HAVE_JPEG #ifdef HAVE_JPEG
extern "C" {
#include "jpeglib.h"
}
/** /**
* Test for check whether reading exif orientation tag was processed successfully or not * 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 * 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<std::tuple<int, int>> 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<uint8_t> jpegNormal;
ASSERT_NO_THROW(cv::imencode(".jpg", src, jpegNormal));
std::vector<int> 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<uint8_t> 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 #endif // HAVE_JPEG
}} // namespace }} // namespace