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++
Update HDR Reference White metadata on color space change
Dmitry Kazakova55aa09645c2
diff --git a/libs/image/kis_image.cc b/libs/image/kis_image.ccindex a169da7999..ec7f0e3f65 100644--- a/libs/image/kis_image.cc+++ b/libs/image/kis_image.cc@@ -197,6 +197,8 @@ public:});}+ updateHDRMetadataOnColorSpaceChange(colorSpace);+connect(q, SIGNAL(sigImageModified()), KisMemoryStatisticsServer::instance(), SLOT(notifyImageChanged()));connect(undoStore.data(), SIGNAL(historyStateChanged()), &signalRouter, SLOT(emitImageModifiedNotification()));}@@ -300,6 +302,23 @@ public:KoColorConversionTransformation::Intent renderingIntent,KoColorConversionTransformation::ConversionFlags conversionFlags);+ void updateHDRMetadataOnColorSpaceChange(const KoColorSpace *newColorSpace) {+ const KoColorProfile *profile = newColorSpace->profile();+ if (!profile) return;++ if (profile->hdrReferenceWhite()) {+ diffuseWhiteLightLevel = profile->hdrReferenceWhite();+ } else if (profile->getTransferCharacteristics() == TRC_ITU_R_BT_2100_0_PQ) {+ diffuseWhiteLightLevel = 203.0;+ } else if (!newColorSpace->hasHighDynamicRange()) {+ relativeContentLightLevelInformation = std::nullopt;+ colorVolumeInformation = std::nullopt;+ diffuseWhiteLightLevel = std::nullopt;+ }++ LEAVE_FUNCTION() << ppVar(diffuseWhiteLightLevel);+ }+struct SetImageProjectionColorSpace;};@@ -494,16 +513,16 @@ void KisImage::copyFromImageImpl(const KisImage &rhs, int policy)}}+ m_d->colorVolumeInformation = rhs.m_d->colorVolumeInformation;+ m_d->relativeContentLightLevelInformation = rhs.m_d->relativeContentLightLevelInformation;+ m_d->diffuseWhiteLightLevel = rhs.m_d->diffuseWhiteLightLevel;+bool exactCopy = policy & EXACT_COPY;if (exactCopy || rhs.m_d->isolationRootNode || rhs.m_d->overlaySelectionMask) {m_d->isolateLayer = rhs.m_d->isolateLayer;m_d->isolateGroup = rhs.m_d->isolateGroup;- m_d->colorVolumeInformation = rhs.m_d->colorVolumeInformation;- m_d->relativeContentLightLevelInformation = rhs.m_d->relativeContentLightLevelInformation;- m_d->diffuseWhiteLightLevel = rhs.m_d->diffuseWhiteLightLevel;-QQueue<KisNodeSP> linearizedNodes;KisLayerUtils::recursiveApplyNodes(rhs.root(),[&linearizedNodes](KisNodeSP node) {@@ -1584,6 +1603,7 @@ bool KisImage::assignImageProfile(const KoColorProfile *profile, bool blockAllUpvoid KisImage::setProjectionColorSpace(const KoColorSpace * colorSpace){m_d->colorSpace = colorSpace;+ m_d->updateHDRMetadataOnColorSpaceChange(m_d->colorSpace);}const KoColorSpace * KisImage::colorSpace() constdiff --git a/libs/image/tests/kis_image_test.cpp b/libs/image/tests/kis_image_test.cppindex 75e03ff16d..35a9c9e159 100644--- a/libs/image/tests/kis_image_test.cpp+++ b/libs/image/tests/kis_image_test.cpp@@ -1303,4 +1303,134 @@ void KisImageTest::testPaintOverlayMask()KIS_DUMP_DEVICE_2(p.image->projection(), refRect, "03_deactivated", "dd");}+void KisImageTest::testHdrReferenceWhite_data()+{+ QTest::addColumn<const KoColorSpace*>("srcSpace");+ QTest::addColumn<const KoColorSpace*>("dstSpace");+ QTest::addColumn<double>("expectedOriginalHdrReferenceWhite");+ QTest::addColumn<double>("srcHdrReferenceWhite");+ QTest::addColumn<double>("expectedDstHdrReferenceWhite");++ const double noHdrReferenceWhite = -1;++ auto *sRGBu8 = KoColorSpaceRegistry::instance()->colorSpace(+ RGBAColorModelID.id(),+ Integer8BitsColorDepthID.id(),+ KoColorSpaceRegistry::instance()->p709SRGBProfile());++ auto *rec2020g10u16 = KoColorSpaceRegistry::instance()->colorSpace(+ RGBAColorModelID.id(),+ Integer16BitsColorDepthID.id(),+ KoColorSpaceRegistry::instance()->p2020G10Profile());++ auto *rec2020g10f16 = KoColorSpaceRegistry::instance()->colorSpace(+ RGBAColorModelID.id(),+ Float16BitsColorDepthID.id(),+ KoColorSpaceRegistry::instance()->p2020G10Profile());++ auto *rec2020pqu16_203 = KoColorSpaceRegistry::instance()->colorSpace(+ RGBAColorModelID.id(),+ Integer16BitsColorDepthID.id(),+ KoColorSpaceRegistry::instance()->profileByName("Krita Rec. 2100 Perceptual Quantizer (203cd/m²)"));++ auto *rec2020pqu16_80 = KoColorSpaceRegistry::instance()->colorSpace(+ RGBAColorModelID.id(),+ Integer16BitsColorDepthID.id(),+ KoColorSpaceRegistry::instance()->profileByName("Krita Rec. 2100 Perceptual Quantizer (80cd/m²)"));++ // from SDR to display-referred HDR+ QTest::addRow("srgb-to-rec2020pq_203")+ << sRGBu8 << rec2020pqu16_203+ << noHdrReferenceWhite << noHdrReferenceWhite << 203.0;++ // from scene-referred HDR to display-referred HDR+ QTest::addRow("rec2020g10f16-to-rec2020pq_203")+ << rec2020g10f16 << rec2020pqu16_203+ << noHdrReferenceWhite << noHdrReferenceWhite << 203.0;++ // from scene-referred HDR with custom HDR Reference White+ // to display-referred HDR+ QTest::addRow("rec2020g10f16_116-to-rec2020pq_203")+ << rec2020g10f16 << rec2020pqu16_203+ << noHdrReferenceWhite << 116.0 << 203.0;++ // from display-referred HDR (80 nits) to another+ // display-referred HDR (203 nits)+ QTest::addRow("rec2020pq_80-to-rec2020pq_203")+ << rec2020pqu16_80 << rec2020pqu16_203+ << 80.0 << noHdrReferenceWhite << 203.0;++ // from display referred HDR to untagged scene-referred+ // (the metadata should persist)+ QTest::addRow("rec2020pq_203-to-rec2020g10f16")+ << rec2020pqu16_203 << rec2020g10f16+ << 203.0 << noHdrReferenceWhite << 203.0;++ // from display referred HDR to clipped integer scene-referred+ // (the metadata should be dropped)+ QTest::addRow("rec2020pq_203-to-rec2020g10u")+ << rec2020pqu16_203 << rec2020g10u16+ << 203.0 << noHdrReferenceWhite << noHdrReferenceWhite;++ // from display referred HDR to clipped integer sRGB+ // (the metadata should be dropped)+ QTest::addRow("rec2020pq_203-to-srgb")+ << rec2020pqu16_203 << sRGBu8+ << 203.0 << noHdrReferenceWhite << noHdrReferenceWhite;+}++void KisImageTest::testHdrReferenceWhite()+{+ auto optionalFromDouble = [] (double value) {+ return value >= 0 ? std::make_optional(value) : std::optional<double>();+ };++ QFETCH(const KoColorSpace*, srcSpace);+ QFETCH(const KoColorSpace*, dstSpace);++ QFETCH(double, expectedOriginalHdrReferenceWhite);+ QFETCH(double, srcHdrReferenceWhite);+ QFETCH(double, expectedDstHdrReferenceWhite);++ const QRect imageRect(0, 0, 32,32);++ KisUndoStore *undoStore = new KisSurrogateUndoStore();+ KisImageSP image = new KisImage(undoStore, imageRect.width(), imageRect.height(), srcSpace, "test image");++ QCOMPARE(image->hdrReferenceWhiteLightLevelMetadata(), optionalFromDouble(expectedOriginalHdrReferenceWhite));++ // assign the new reference white only when requested+ if (srcHdrReferenceWhite >= 0) {+ image->setHdrReferenceWhiteLightLevelMetadata(optionalFromDouble(srcHdrReferenceWhite));+ QCOMPARE(image->hdrReferenceWhiteLightLevelMetadata(), optionalFromDouble(srcHdrReferenceWhite));+ }++ image->convertImageColorSpace(dstSpace, KoColorConversionTransformation::internalRenderingIntent(), KoColorConversionTransformation::internalConversionFlags());+ image->waitForDone();++ QCOMPARE(image->hdrReferenceWhiteLightLevelMetadata(), optionalFromDouble(expectedDstHdrReferenceWhite));+}++void KisImageTest::testHdrReferenceWhiteCloning()+{+ const QRect imageRect(0, 0, 32,32);++ auto *rec2020g10f16 = KoColorSpaceRegistry::instance()->colorSpace(+ RGBAColorModelID.id(),+ Float16BitsColorDepthID.id(),+ KoColorSpaceRegistry::instance()->p2020G10Profile());++ KisUndoStore *undoStore = new KisSurrogateUndoStore();+ KisImageSP image = new KisImage(undoStore, imageRect.width(), imageRect.height(), rec2020g10f16, "test image");++ image->setHdrReferenceWhiteLightLevelMetadata(116);++ KisImageSP clonedExactCopyImage = image->clone(true);+ QCOMPARE(clonedExactCopyImage->hdrReferenceWhiteLightLevelMetadata(), 116);++ KisImageSP clonedInexactCopyImage = image->clone(false);+ QCOMPARE(clonedInexactCopyImage->hdrReferenceWhiteLightLevelMetadata(), 116);++}+KISTEST_MAIN(KisImageTest)diff --git a/libs/image/tests/kis_image_test.h b/libs/image/tests/kis_image_test.hindex aa708cee8c..270324a7d7 100644--- a/libs/image/tests/kis_image_test.h+++ b/libs/image/tests/kis_image_test.h@@ -47,6 +47,11 @@ private Q_SLOTS:void testMergePassThroughOverPaintLayer();void testPaintOverlayMask();++ void testHdrReferenceWhite_data();+ void testHdrReferenceWhite();++ void testHdrReferenceWhiteCloning();};#endifdiff --git a/libs/pigment/HDR_DESIGN_NOTES.md b/libs/pigment/HDR_DESIGN_NOTES.mdindex a793e6d892..8f9b669a3f 100644--- a/libs/pigment/HDR_DESIGN_NOTES.md+++ b/libs/pigment/HDR_DESIGN_NOTES.md@@ -56,9 +56,18 @@* If the profile is Rec2020PQ, it already has CRWL tag declaring its HDR Reference White Level2. When loading from .kra* Load HDR metadata from .kra (warn if CRWL data in the ICC is inconsistent with the one in .kra)- * TODO: should we **remove** the “Diffuse White” level information from KisImage when the profile is Rec2020PQ? To make sure there is only one source of truth* If the color space is “scene referred”, then CRWL tag of the ICC profile is **not** used in any way. Only the metadata from KisImage is used* if the color space is Rec2020PQ, then CRWL tag of the ICC profile is used. The metadata from KisImage is **not** used+3. Automatic setting of the "Diffuse White" level metadata+ * When the image color space is converted into a display-referred+ profile with a CRWL tag, the "Diffuse White" metadata is+ automatically synced with CRWL tag.+ * When an image is converted back from a display-referred profile+ into a scene-referred profile, the "Diffuse White" metadata+ is kept intact. The reason is that resetting this metadata+ would make relative content light level information invalid.+ * When and HDR image with HDR metadata is converted into+ non-HDR space, then HDR metadata is removed.## Links