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++
Make profile aliases weak
Dmitry Kazakovd91734d933b3
diff --git a/libs/pigment/KoColorProfileStorage.cpp b/libs/pigment/KoColorProfileStorage.cppindex 04b39dfce0..9d1194231e 100644--- a/libs/pigment/KoColorProfileStorage.cpp+++ b/libs/pigment/KoColorProfileStorage.cpp@@ -92,15 +92,33 @@ void KoColorProfileStorage::addProfileAlias(const QString &name, const QString &d->profileAlias[name] = to;}+void KoColorProfileStorage::removeProfileAlias(const QString &name)+{+ QWriteLocker l(&d->lock);+ d->profileAlias.remove(name);+}+QString KoColorProfileStorage::profileAlias(const QString &name) const{QReadLocker l(&d->lock);- return d->profileAlias.value(name, name);+ /**+ * Profile aliases are considered 'weak', i.e. they can+ * be overridden by a read profile added to the storage.+ */+ return d->profileMap.contains(name) ? name : d->profileAlias.value(name, name);}const KoColorProfile *KoColorProfileStorage::profileByName(const QString &name) const{QReadLocker l(&d->lock);+ /**+ * Profile aliases are considered 'weak', i.e. they can+ * be overridden by a read profile added to the storage.+ */+ if (d->profileMap.contains(name)) {+ return d->profileMap[name];+ }+return d->profileMap.value(d->profileAlias.value(name, name), 0);}diff --git a/libs/pigment/KoColorProfileStorage.h b/libs/pigment/KoColorProfileStorage.hindex 19cbf0ccd4..ff4f9d9661 100644--- a/libs/pigment/KoColorProfileStorage.h+++ b/libs/pigment/KoColorProfileStorage.h@@ -63,9 +63,18 @@ public:/*** Create an alias to a profile with a different name. Then @ref profileByName* will return the profile @p to when passed @p name as a parameter.+ *+ * Aliases are considered 'weak'. If a real profile with the name \p name+ * is present (or added), it shadows the existing alias. If such an explicit+ * profile gets removed, then the alias takes its place again.*/void addProfileAlias(const QString& name, const QString& to);+ /**+ * Remove a profile \p name if exists+ */+ void removeProfileAlias(const QString &name);+/*** @return the profile alias, or name if not aliased*/diff --git a/libs/pigment/KoColorSpaceRegistry.cpp b/libs/pigment/KoColorSpaceRegistry.cppindex 800303c59a..e76979cbe9 100644--- a/libs/pigment/KoColorSpaceRegistry.cpp+++ b/libs/pigment/KoColorSpaceRegistry.cpp@@ -261,6 +261,11 @@ void KoColorSpaceRegistry::addProfileAlias(const QString& name, const QString& td->profileStorage.addProfileAlias(name, to);}+void KoColorSpaceRegistry::removeProfileAlias(const QString& name)+{+ d->profileStorage.removeProfileAlias(name);+}+QString KoColorSpaceRegistry::profileAlias(const QString& name) const{return d->profileStorage.profileAlias(name);diff --git a/libs/pigment/KoColorSpaceRegistry.h b/libs/pigment/KoColorSpaceRegistry.hindex dd775cbce6..e346476651 100644--- a/libs/pigment/KoColorSpaceRegistry.h+++ b/libs/pigment/KoColorSpaceRegistry.h@@ -104,9 +104,18 @@ public:/*** Create an alias to a profile with a different name. Then @ref profileByName* will return the profile @p to when passed @p name as a parameter.+ *+ * Aliases are considered 'weak'. If a real profile with the name \p name+ * is present (or added), it shadows the existing alias. If such an explicit+ * profile gets removed, then the alias takes its place again.*/void addProfileAlias(const QString& name, const QString& to);+ /**+ * Remove profile alias \p name if exists+ */+ void removeProfileAlias(const QString& name);+/*** @return the profile alias, or name if not aliased*/diff --git a/plugins/color/lcms2engine/LcmsEnginePlugin.cpp b/plugins/color/lcms2engine/LcmsEnginePlugin.cppindex d06e79a1b0..9d714989fa 100644--- a/plugins/color/lcms2engine/LcmsEnginePlugin.cpp+++ b/plugins/color/lcms2engine/LcmsEnginePlugin.cpp@@ -167,14 +167,18 @@ LcmsEnginePlugin::LcmsEnginePlugin(QObject *parent, const QVariantList &)KoColorProfile *rgbProfile = LcmsColorProfileContainer::createFromLcmsProfile(cmsCreate_sRGBProfile());registry->addProfile(rgbProfile);- // TODO: decide whether we want to do it like this.- QVector<double> rec2020Col;- KoColorProfile *rec2100pq = new IccColorProfile(rec2020Col, PRIMARIES_ITU_R_BT_2020_2_AND_2100_0, TRC_ITU_R_BT_2100_0_PQ);+ KoColorProfile *rec2100pq = new IccColorProfile({}, PRIMARIES_ITU_R_BT_2020_2_AND_2100_0, TRC_ITU_R_BT_2100_0_PQ);registry->addProfile(rec2100pq);- // NOTE: we don't add an alias for the legacy PNG-based profile here- // (ITUR_2100_PQ_FULL.ICC), our color conversion system can detect it- // by its name and link properly+ /**+ * By default we remap the legacy HDR profile into the new Rec2020PQ+ * profile with CICP values. This profile will also have CRWL tag set+ * to the official 203 nits. The legacy HDR profile loaded from older+ * .kra files will have a different mapping to map to 80 nits used in+ * Krita previously.+ */+ const QString legacyRec2020PQProfileName = "High Dynamic Range UHDTV Wide Color Gamut Display (Rec. 2020) - SMPTE ST 2084 PQ EOTF";+ registry->addProfileAlias(legacyRec2020PQProfileName, rec2100pq->name());registry->add(new RgbU8ColorSpaceFactory());registry->add(new RgbU16ColorSpaceFactory());diff --git a/plugins/color/lcms2engine/tests/TestColorSpaceRegistry.cpp b/plugins/color/lcms2engine/tests/TestColorSpaceRegistry.cppindex 8bdc54de58..9dd98c0709 100644--- a/plugins/color/lcms2engine/tests/TestColorSpaceRegistry.cpp+++ b/plugins/color/lcms2engine/tests/TestColorSpaceRegistry.cpp@@ -12,6 +12,10 @@#include <simpletest.h>#include <testpigment.h>+#include <kis_debug.h>+#include <qimage_test_util.h>+#include <KoColorSpaceEngine.h>+#include <KisAdaptedLock.h>void TestColorSpaceRegistry::testConstruction(){@@ -90,4 +94,181 @@ void TestColorSpaceRegistry::testLab()}+void TestColorSpaceRegistry::testColorProfileAliasAddUnique()+{+ auto *registry = KoColorSpaceRegistry::instance();++ const QString aliasSource = "test_alias.icc";+ const QString aliasDestination = registry->p2020G10Profile()->name();++ registry->addProfileAlias(aliasSource, aliasDestination);++ {+ auto *profile = registry->profileByName(aliasSource);+ QVERIFY(profile);+ QCOMPARE(profile->name(), aliasDestination);+ }++ registry->removeProfileAlias(aliasSource);++ {+ auto *profile = registry->profileByName(aliasSource);+ QVERIFY(!profile);+ }++}++void TestColorSpaceRegistry::testColorProfileAliasAddWhenProfileExists()+{+ auto *registry = KoColorSpaceRegistry::instance();++ KoColorProfile *originalProfile = const_cast<KoColorProfile*>(registry->p709G10Profile());++ const QString aliasSource = originalProfile->name();+ const QString aliasDestination = registry->p2020G10Profile()->name();+ KIS_ASSERT(aliasSource != aliasDestination);++ // add alias+ registry->addProfileAlias(aliasSource, aliasDestination);++ {+ /**+ * The alias is added but it takes no effect, since the actual profile+ * with this name already exists+ */+ auto *profile = registry->profileByName(aliasSource);+ QVERIFY(profile);+ QCOMPARE(profile->name(), aliasSource);+ }++ // now remove the profile+ registry->removeProfile(originalProfile);++ {+ /**+ * The alias is active now!+ */+ auto *profile = registry->profileByName(aliasSource);+ QVERIFY(profile);+ QCOMPARE(profile->name(), aliasDestination);+ }++ // now remove the alias+ registry->removeProfileAlias(aliasSource);++ {+ /**+ * Nothing is found now. We have removed both, the profile and the alias+ */+ auto *profile = registry->profileByName(aliasSource);+ QVERIFY(!profile);+ }++ // restore the profile+ registry->addProfile(originalProfile);+ {+ // no alias is present now+ auto *profile = registry->profileByName(aliasSource);+ QVERIFY(profile);+ QCOMPARE(profile->name(), aliasSource);+ }+}++namespace {+struct AliasRemovalAdapter+{+ AliasRemovalAdapter(const QString &aliasSource)+ : m_aliasSource(aliasSource)+ {+ auto *registry = KoColorSpaceRegistry::instance();+ m_originalAliasTarget = registry->profileAlias(aliasSource);+ }++ inline void lock() {+ if (m_originalAliasTarget != m_aliasSource) {+ auto *registry = KoColorSpaceRegistry::instance();+ registry->removeProfileAlias(m_aliasSource);+ }+ }++ inline void unlock() {+ if (m_originalAliasTarget != m_aliasSource) {+ auto *registry = KoColorSpaceRegistry::instance();+ registry->addProfileAlias(m_aliasSource, m_originalAliasTarget);+ }+ }++private:+ QString m_aliasSource;+ QString m_originalAliasTarget;+};++KIS_DECLARE_ADAPTED_LOCK(AliasRemovalLock, AliasRemovalAdapter)+}++void TestColorSpaceRegistry::testColorProfileAliasOverrideByProfile()+{+ /**+ * The profile aliases are considered 'weak', i.e. when a real+ * profile with the same name is loaded, the alias gets overridden.+ *+ * When this explicit profile is unloaded, the alias is expected+ * to be re-enabled again.+ */+ auto *registry = KoColorSpaceRegistry::instance();+ const QString legacyProfileName = "High Dynamic Range UHDTV Wide Color Gamut Display (Rec. 2020) - SMPTE ST 2084 PQ EOTF";+ const QString legacyProfileFileName = "ITUR_2100_PQ_FULL.ICC";++ AliasRemovalLock lock(legacyProfileName);++ QVERIFY(!registry->profileByName(legacyProfileName));++ const QString aliasSource = legacyProfileName;+ const QString aliasDestination = registry->p2020PQProfile()->name();+ KIS_ASSERT(aliasSource != aliasDestination);++ registry->addProfileAlias(legacyProfileName, aliasDestination);++ {+ auto *profile = registry->profileByName(aliasSource);+ QVERIFY(profile);+ QCOMPARE(profile->name(), aliasDestination);+ }++ {+ const QString profileFilePath = TestUtil::fetchDataFileLazy(legacyProfileFileName);+ KIS_ASSERT(QFile::exists(profileFilePath));++ {+ KoColorSpaceEngine *iccEngine = KoColorSpaceEngineRegistry::instance()->get("icc");+ KIS_ASSERT(iccEngine);+ iccEngine->addProfile(profileFilePath);+ }+ }++ {+ // the profile has overridden the alias+ auto *profile = registry->profileByName(aliasSource);+ QVERIFY(profile);+ QCOMPARE(profile->name(), aliasSource);+ }++ registry->removeProfile(const_cast<KoColorProfile*>(registry->profileByName(aliasSource)));++ {+ // alias is active again+ auto *profile = registry->profileByName(aliasSource);+ QVERIFY(profile);+ QCOMPARE(profile->name(), aliasDestination);+ }++ registry->removeProfileAlias(aliasSource);++ {+ // no alias exists anymore, hence no profile+ auto *profile = registry->profileByName(aliasSource);+ QVERIFY(!profile);+ }+}+KISTEST_MAIN(TestColorSpaceRegistry)diff --git a/plugins/color/lcms2engine/tests/TestColorSpaceRegistry.h b/plugins/color/lcms2engine/tests/TestColorSpaceRegistry.hindex fbc7958ed3..4253d0c7d9 100644--- a/plugins/color/lcms2engine/tests/TestColorSpaceRegistry.h+++ b/plugins/color/lcms2engine/tests/TestColorSpaceRegistry.h@@ -15,6 +15,10 @@ private Q_SLOTS:void testRgbU8();void testRgbU16();void testLab();++ void testColorProfileAliasAddUnique();+ void testColorProfileAliasAddWhenProfileExists();+ void testColorProfileAliasOverrideByProfile();};#endif