HEIF: Resolve primary image index from image source

The primary image, in the case of multi-image sources, may not
necessarily be the first image.

Test images produced with patched version of enc-heif that
allowed overriding the primary image. The correct primary
image is green, while the other image is red.

Change-Id: Iba26b13fdb4898d7babc5fb173ad38b16f864e80
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 1bf5692d3f)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tor Arne Vestbø 2025-08-15 12:43:12 +02:00 committed by Qt Cherry-pick Bot
parent ab05e973c2
commit f99d7b309d
5 changed files with 28 additions and 3 deletions

View File

@ -116,8 +116,10 @@ bool QIIOFHelper::initRead()
cgImageSource = CGImageSourceCreateWithDataProvider(cgDataProvider, nullptr);
if (cgImageSource)
cfImageDict = CGImageSourceCopyPropertiesAtIndex(cgImageSource, 0, nullptr);
if (cgImageSource) {
auto primaryIndex = CGImageSourceGetPrimaryImageIndex(cgImageSource);
cfImageDict = CGImageSourceCopyPropertiesAtIndex(cgImageSource, primaryIndex, nullptr);
}
return (cgImageSource);
}
@ -127,7 +129,8 @@ bool QIIOFHelper::readImage(QImage *out)
if (!out || !initRead())
return false;
QCFType<CGImageRef> cgImage = CGImageSourceCreateImageAtIndex(cgImageSource, 0, nullptr);
auto primaryIndex = CGImageSourceGetPrimaryImageIndex(cgImageSource);
QCFType<CGImageRef> cgImage = CGImageSourceCreateImageAtIndex(cgImageSource, primaryIndex, nullptr);
if (!cgImage)
return false;

View File

@ -16,6 +16,8 @@ qt_internal_add_test(tst_qheif
set(heif_resource_files
"../../shared/images/heif/col320x480.heic"
"../../shared/images/heif/newlogoCCW.heic"
"../../shared/images/heif/primary-index-0.heic"
"../../shared/images/heif/primary-index-1.heic"
)
qt_internal_add_resource(tst_qheif "heif"

View File

@ -15,6 +15,9 @@ private slots:
void readProperties_data();
void readProperties();
void writeImage();
void primaryIndex_data();
void primaryIndex();
};
void tst_qheif::initTestCase()
@ -123,5 +126,22 @@ void tst_qheif::writeImage()
}
}
void tst_qheif::primaryIndex_data()
{
QTest::addColumn<int>("primaryIndex");
QTest::newRow("0 (first/default)") << 0;
QTest::newRow("1 (second)") << 1;
}
void tst_qheif::primaryIndex()
{
QFETCH(int, primaryIndex);
QImage image(QString(":/heif/primary-index-%1.heic").arg(primaryIndex));
QVERIFY(!image.isNull());
QCOMPARE(image.pixelColor(50, 50), QColor(Qt::green));
}
QTEST_MAIN(tst_qheif)
#include "tst_qheif.moc"

Binary file not shown.

Binary file not shown.