Merge pull request #26706 from Kumataro:fix26705

imgcodecs: fix EXR tests
This commit is contained in:
Alexander Smorkalov 2025-01-09 15:46:52 +03:00 committed by GitHub
commit bdb6a968ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 6 deletions

View File

@ -235,6 +235,17 @@ bool ExrDecoder::readData( Mat& img )
( (m_iscolor && !m_ischroma) || color) ? 3 : alphasupported ? 2 : 1 ); // number of channels to read may exceed channels in output img
size_t xStride = floatsize * channelstoread;
// See https://github.com/opencv/opencv/issues/26705
// If ALGO_HINT_ACCURATE is set, read BGR and swap to RGB.
// If ALGO_HINT_APPROX is set, read RGB directly.
bool doReadRGB = m_use_rgb;
bool doPostColorSwap = false; // After decoding, swap BGR to RGB
if(m_use_rgb && (getDefaultAlgorithmHint() == ALGO_HINT_ACCURATE) )
{
doReadRGB = false;
doPostColorSwap = true;
}
AutoBuffer<char> copy_buffer;
if( !justcopy )
@ -373,7 +384,7 @@ bool ExrDecoder::readData( Mat& img )
if( m_iscolor )
{
if (m_use_rgb)
if (doReadRGB)
{
if( m_red && (m_red->xSampling != 1 || m_red->ySampling != 1) )
UpSample( data, channelstoread, step / xstep, m_red->xSampling, m_red->ySampling );
@ -397,7 +408,7 @@ bool ExrDecoder::readData( Mat& img )
if( chromatorgb )
{
if (m_use_rgb)
if (doReadRGB)
ChromaToRGB( (float *)data, m_height, channelstoread, step / xstep );
else
ChromaToBGR( (float *)data, m_height, channelstoread, step / xstep );
@ -424,7 +435,7 @@ bool ExrDecoder::readData( Mat& img )
{
if( chromatorgb )
{
if (m_use_rgb)
if (doReadRGB)
ChromaToRGB( (float *)buffer, 1, defaultchannels, step );
else
ChromaToBGR( (float *)buffer, 1, defaultchannels, step );
@ -452,7 +463,7 @@ bool ExrDecoder::readData( Mat& img )
}
if( color )
{
if (m_use_rgb)
if (doReadRGB)
{
if( m_red && (m_red->xSampling != 1 || m_red->ySampling != 1) )
UpSampleY( data, defaultchannels, step / xstep, m_red->ySampling );
@ -477,6 +488,11 @@ bool ExrDecoder::readData( Mat& img )
close();
if(doPostColorSwap)
{
cvtColor( img, img, cv::COLOR_BGR2RGB );
}
return result;
}

View File

@ -35,7 +35,8 @@ TEST(Imgcodecs_EXR, readWrite_32FC1)
ASSERT_TRUE(cv::imwrite(filenameOutput, img));
// Check generated file size to ensure that it's compressed with proper options
ASSERT_EQ(396u, getFileSize(filenameOutput));
ASSERT_LE(396u, getFileSize(filenameOutput)); // OpenEXR 2
ASSERT_LE( getFileSize(filenameOutput), 440u); // OpenEXR 3.2+
const Mat img2 = cv::imread(filenameOutput, IMREAD_UNCHANGED);
ASSERT_EQ(img2.type(), img.type());
ASSERT_EQ(img2.size(), img.size());
@ -199,7 +200,11 @@ TEST(Imgcodecs_EXR, read_YC_changeDepth)
cvtColor(img_rgb, img_rgb, COLOR_RGB2BGR);
EXPECT_TRUE(cvtest::norm(img, img_rgb, NORM_INF) == 0);
// See https://github.com/opencv/opencv/issues/26705
// If ALGO_HINT_ACCURATE is set, norm should be 0.
// If ALGO_HINT_APPROX is set, norm should be 1(or 0).
EXPECT_LE(cvtest::norm(img, img_rgb, NORM_INF),
(cv::getDefaultAlgorithmHint() == ALGO_HINT_ACCURATE)?0:1);
// Cannot test writing, EXR encoder doesn't support 8U depth
}