Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Implement saving and loading HDR metadata to PNG.
Wolthera van Hövell tot Westerflier6312deb8cde7
diff --git a/libs/ui/kis_png_converter.cpp b/libs/ui/kis_png_converter.cppindex 6383825df1..8d910b0c85 100644--- a/libs/ui/kis_png_converter.cpp+++ b/libs/ui/kis_png_converter.cpp@@ -55,6 +55,7 @@#include <kis_paint_layer.h>#include <kis_painter.h>#include <kis_transaction.h>+#include <kis_hdr_metadata.h>#include <kis_assert.h>@@ -681,6 +682,28 @@ KisImportExportErrorCode KisPNGConverter::buildImage(QIODevice* iod)m_image->setResolution(100.0, 100.0);}+#if defined(PNG_cLLI_SUPPORTED)+ KisRelativeContentLightLevelInformation clli;+ if (png_get_cLLI(png_ptr, info_ptr, &clli.maxContentLightLevel, &clli.maxFrameAverageLightLevel)) {+ const double refWhiteMultiplier = 1.0/profile->hdrReferenceWhite().value_or(203.0);+ clli.maxContentLightLevel *= refWhiteMultiplier;+ clli.maxFrameAverageLightLevel *= refWhiteMultiplier;+ m_image->setRelativeContentLightLevelInformation(clli);+ }+#endif++#if defined(PNG_mDCV_SUPPORTED)+ KisColorVolumeInformation cvi;+ if (png_get_mDCV(png_ptr, info_ptr,+ &cvi.white.x, &cvi.white.y,+ &cvi.red.x, &cvi.red.y,+ &cvi.green.x, &cvi.green.y,+ &cvi.blue.x, &cvi.blue.y,+ &cvi.maxLuminance, &cvi.minLuminance)) {+ m_image->setColorVolumeInformation(cvi);+ }+#endif+double coeff = quint8_MAX / (double)(pow((double)2, color_nb_bits) - 1);KisPaintLayerSP layer = new KisPaintLayer(m_image.data(), m_image -> nextLayerName(), UCHAR_MAX);@@ -1123,7 +1146,7 @@ KisImportExportErrorCode KisPNGConverter::buildFile(QIODevice* iodevice, const Q// set sRGB only if the profile is sRGB -- http://www.w3.org/TR/PNG/#11sRGB says sRGB and iCCP should not both be presentconst bool sRGB = *device->colorSpace()->profile() == *KoColorSpaceRegistry::instance()->p709SRGBProfile();- const bool colorProfilePQ = *device->colorSpace()->profile() == *KoColorSpaceRegistry::instance()->p2020PQProfile();+ const bool colorProfilePQ = device->colorSpace()->profile()->getTransferCharacteristics() == TRC_ITU_R_BT_2100_0_PQ;/** This automatically writes the correct gamma and chroma chunks along with the sRGB chunk, but firefox's* color management is bugged, so once you give it any incentive to start color managing an sRGB image it@@ -1262,6 +1285,25 @@ KisImportExportErrorCode KisPNGConverter::buildFile(QIODevice* iodevice, const Q#endif}}+#if defined(PNG_cLLI_SUPPORTED)+ if (colorProfilePQ && m_doc->image().toStrongRef()->relativeContentLightLevelInformation()) {+ const std::optional<KisRelativeContentLightLevelInformation> clli = m_doc->image().toStrongRef()->relativeContentLightLevelInformation();+ const double hdrRefWhite = colorProfile->hdrReferenceWhite().value_or(203.0);+ const double maxcll = clli->maxContentLightLevel * hdrRefWhite;+ const double maxfall = clli->maxFrameAverageLightLevel * hdrRefWhite;+ png_set_cLLI(png_ptr, info_ptr, maxcll, maxfall);+ }+#endif+#if defined(PNG_mDCV_SUPPORTED)+ if (colorProfilePQ && m_doc->image().toStrongRef()->colorVolumeInformation()) {+ const std::optional<KisColorVolumeInformation> cvi = m_doc->image().toStrongRef()->colorVolumeInformation();+ png_set_mDCV(png_ptr, info_ptr,+ cvi->white.x, cvi->white.y,+ cvi->red.x, cvi->red.y,+ cvi->green.x,cvi->green.y,+ cvi->blue.x, cvi->blue.y, cvi->maxLuminance, cvi->minLuminance);+ }+#endif// save comments from the document information// warning: according to the official png spec, the keys need to be capitalized!diff --git a/plugins/impex/png/tests/kis_png_test.cpp b/plugins/impex/png/tests/kis_png_test.cppindex b97c222d67..32022e2f56 100644--- a/plugins/impex/png/tests/kis_png_test.cpp+++ b/plugins/impex/png/tests/kis_png_test.cpp@@ -11,6 +11,7 @@#include <QCoreApplication>#include <KoColorProfileQuery.h>+#include <kis_hdr_metadata.h>#include "filestest.h"#include <testui.h>@@ -174,16 +175,22 @@ void KisPngTest::testRoundtripCicpIccProfile_data(){QTest::addColumn<ColorPrimaries>("primaries");QTest::addColumn<TransferCharacteristics>("transfer");+ QTest::addColumn<bool>("cicpOnly");- QTest::addRow("rec2020") << PRIMARIES_ITU_R_BT_2020_2_AND_2100_0 << TRC_ITU_R_BT_2020_2_12bit;- QTest::addRow("rec2100 PQ") << PRIMARIES_ITU_R_BT_2020_2_AND_2100_0 << TRC_ITU_R_BT_2100_0_PQ;- QTest::addRow("rec709") << PRIMARIES_ITU_R_BT_709_5 << TRC_ITU_R_BT_709_5;+ QTest::addRow("rec2020") << PRIMARIES_ITU_R_BT_2020_2_AND_2100_0 << TRC_ITU_R_BT_2020_2_12bit << false;+ QTest::addRow("rec2100 PQ") << PRIMARIES_ITU_R_BT_2020_2_AND_2100_0 << TRC_ITU_R_BT_2100_0_PQ << false;+ QTest::addRow("rec709") << PRIMARIES_ITU_R_BT_709_5 << TRC_ITU_R_BT_709_5 << false;++ QTest::addRow("rec2020 cicp") << PRIMARIES_ITU_R_BT_2020_2_AND_2100_0 << TRC_ITU_R_BT_2020_2_12bit << true;+ QTest::addRow("rec2100 PQ cicp") << PRIMARIES_ITU_R_BT_2020_2_AND_2100_0 << TRC_ITU_R_BT_2100_0_PQ << true;+ QTest::addRow("rec709 cicp") << PRIMARIES_ITU_R_BT_709_5 << TRC_ITU_R_BT_709_5 << true;}void KisPngTest::testRoundtripCicpIccProfile(){QFETCH(ColorPrimaries, primaries);QFETCH(TransferCharacteristics, transfer);+ QFETCH(bool, cicpOnly);QVector<double> colorants;const KoColorProfile *profile = KoColorSpaceRegistry::instance()->profileFor(KoColorProfileQuery(primaries, transfer));@@ -194,11 +201,14 @@ void KisPngTest::testRoundtripCicpIccProfile()doc->setFileBatchMode(true);KisImageSP image = new KisImage(new KisSurrogateUndoStore(), imageRect.width(), imageRect.height(), cs, "test image");doc->setCurrentImage(image);- const QString name = "roundtrip_cicp_test_"+QString::number(primaries)+"_"+QString::number(transfer)+".png";+ const QString cicpOnlyString = cicpOnly? "_cicp_only": QString();+ const QString name = "roundtrip_cicp_test_"+QString::number(primaries)+"_"+QString::number(transfer)+cicpOnlyString+".png";KisPropertiesConfigurationSP exportConfiguration = new KisPropertiesConfiguration();exportConfiguration->setProperty("saveSRGBProfile", false);exportConfiguration->setProperty("forceSRGB", false);+ exportConfiguration->setProperty("storeColorSpaceInfo", true);+ exportConfiguration->setProperty("writeCicpIfPossible", cicpOnly);doc->exportDocumentSync(name, "image/png", exportConfiguration);//---//@@ -245,5 +255,61 @@ void KisPngTest::testLoadPngWithLegacyProfile()QCOMPARE(profile->getTransferCharacteristics(), TRC_ITU_R_BT_2100_0_PQ);}+void KisPngTest::testRoundtripHDRMetadata()+{+ const KoColorProfile *profile = KoColorSpaceRegistry::instance()->p2020PQProfile();++ QRect imageRect(0,0,512,512);+ const KoColorSpace * cs = KoColorSpaceRegistry::instance()->colorSpace(RGBAColorModelID.id(), Integer8BitsColorDepthID.id(), profile);++ QScopedPointer<KisDocument> doc(KisPart::instance()->createDocument());+ doc->setFileBatchMode(true);+ KisImageSP image = new KisImage(new KisSurrogateUndoStore(), imageRect.width(), imageRect.height(), cs, "test image");++ KisRelativeContentLightLevelInformation clli;+ clli.maxFrameAverageLightLevel = 12.3456;+ clli.maxContentLightLevel = 6.54321;+ KisColorVolumeInformation cvi;+ cvi.white.x = 0.3;+ cvi.white.y = 0.3;+ cvi.red.x = 0.3;+ cvi.red.y = 0.3;+ cvi.green.x = 0.3;+ cvi.green.y = 0.3;+ cvi.blue.x = 0.3;+ cvi.blue.y = 0.3;++ image->setRelativeContentLightLevelInformation(clli);+ image->setColorVolumeInformation(cvi);++ doc->setCurrentImage(image);+ const QString name = "roundtrip_hdr_metadata.png";++ KisPropertiesConfigurationSP exportConfiguration = new KisPropertiesConfiguration();+ exportConfiguration->setProperty("saveSRGBProfile", false);+ exportConfiguration->setProperty("forceSRGB", false);+ doc->exportDocumentSync(name, "image/png", exportConfiguration);++ QScopedPointer<KisDocument> doc2(KisPart::instance()->createDocument());+ KisImportExportManager manager(doc2.data());+ doc2->setFileBatchMode(true);++ KisImportExportErrorCode loadingStatus =+ manager.importDocument(name, QString());++ QVERIFY(loadingStatus.isOk());++ KisImageSP image2 = doc2->image();+ image2->initialRefreshGraph();+ image2->waitForDone();++ QVERIFY(image2->relativeContentLightLevelInformation());+ QVERIFY(image2->colorVolumeInformation());++ QVERIFY(qAbs(clli.maxContentLightLevel - image2->relativeContentLightLevelInformation()->maxContentLightLevel) < 0.0001);+ QVERIFY(qAbs(clli.maxFrameAverageLightLevel - image2->relativeContentLightLevelInformation()->maxFrameAverageLightLevel) < 0.0001);+ QVERIFY(cvi == image2->colorVolumeInformation());+}+KISTEST_MAIN(KisPngTest)diff --git a/plugins/impex/png/tests/kis_png_test.h b/plugins/impex/png/tests/kis_png_test.hindex b5da349c5c..5bec050fd3 100644--- a/plugins/impex/png/tests/kis_png_test.h+++ b/plugins/impex/png/tests/kis_png_test.h@@ -21,6 +21,9 @@ private Q_SLOTS:void testRoundtripCicpIccProfile();void testLoadPngWithLegacyProfile();++ void testRoundtripHDRMetadata();+};#endif