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++
Fix crash when an embedded profile is faulty
Luna Lovecraft8a5754b68f6f
diff --git a/libs/pigment/KoColorProfile.h b/libs/pigment/KoColorProfile.hindex 5eeb272270..5f8acbe4a9 100644--- a/libs/pigment/KoColorProfile.h+++ b/libs/pigment/KoColorProfile.h@@ -102,10 +102,17 @@ public:return QString();};/**- * @return true if you can use this profile can be used to convert color from a different- * profile to this one+ * @return true if this profile can be used to convert color from a different profile to this one*/virtual bool isSuitableForOutput() const = 0;+ /**+ * @return true if this profile can be used to convert color from this one to a different one+ */+ virtual bool isSuitableForInput() const = 0;+ /**+ * @return true if you can use this profile can be used in Krita+ */+ virtual bool isSuitableForWorkspace() const = 0;/*** @return true if this profile is suitable to use for printing*/diff --git a/libs/pigment/colorprofiles/KoDummyColorProfile.cpp b/libs/pigment/colorprofiles/KoDummyColorProfile.cppindex 31353941dd..589890e336 100644--- a/libs/pigment/colorprofiles/KoDummyColorProfile.cpp+++ b/libs/pigment/colorprofiles/KoDummyColorProfile.cpp@@ -32,6 +32,14 @@ bool KoDummyColorProfile::isSuitableForOutput() const{return true;}+bool KoDummyColorProfile::isSuitableForInput() const+{+ return true;+}+bool KoDummyColorProfile::isSuitableForWorkspace() const+{+ return true;+}bool KoDummyColorProfile::isSuitableForPrinting() const{diff --git a/libs/pigment/colorprofiles/KoDummyColorProfile.h b/libs/pigment/colorprofiles/KoDummyColorProfile.hindex 31b5d04f5d..fe6e1b36d1 100644--- a/libs/pigment/colorprofiles/KoDummyColorProfile.h+++ b/libs/pigment/colorprofiles/KoDummyColorProfile.h@@ -18,6 +18,8 @@ public:bool valid() const override;float version() const override;bool isSuitableForOutput() const override;+ bool isSuitableForInput() const override;+ bool isSuitableForWorkspace() const override;bool isSuitableForPrinting() const override;bool isSuitableForDisplay() const override;bool supportsPerceptual() const override;diff --git a/libs/ui/kis_png_converter.cpp b/libs/ui/kis_png_converter.cppindex 87d670e858..1de8bc9a6b 100644--- a/libs/ui/kis_png_converter.cpp+++ b/libs/ui/kis_png_converter.cpp@@ -566,37 +566,33 @@ KisImportExportErrorCode KisPNGConverter::buildImage(QIODevice* iod)bool loadedImageIsHDR = false;const KoColorProfile* profile = 0;+if (png_get_iCCP(png_ptr, info_ptr, &profile_name, &compression_type, &profile_data, &proflen)) {- QByteArray profile_rawdata;- // XXX: Hardcoded for icc type -- is that correct for us?- profile_rawdata.resize(proflen);- memcpy(profile_rawdata.data(), profile_data, proflen);+ QByteArray profile_rawdata(reinterpret_cast<char*>(profile_data), proflen);profile = KoColorSpaceRegistry::instance()->createColorProfile(csName.first, csName.second, profile_rawdata);- Q_CHECK_PTR(profile);if (profile) {- // dbgFile << "profile name: " << profile->productName() << " profile description: " << profile->productDescription() << " information sur le produit: " << profile->productInfo();- if (!profile->isSuitableForOutput()) {- dbgFile << "the profile is not suitable for output and therefore cannot be used in krita, we need to convert the image to a standard profile"; // TODO: in ko2 popup a selection menu to inform the user+ if (!profile->isSuitableForWorkspace()) {+ dbgFile << "the profile is not suitable for output and therefore cannot be used in krita, we need to convert the image to a standard profile";}}loadedImageIsHDR = strcmp(profile_name, "ITUR_2100_PQ_FULL") == 0;}- else {- dbgFile << "no embedded profile, will use the default profile";- if (color_nb_bits == 16 && !fromBlender && !qAppName().toLower().contains("test") && !m_batchMode) {- KisConfig cfg(true);- quint32 behaviour = cfg.pasteBehaviour();- if (behaviour == KisClipboard::PASTE_ASK) {- KisDlgPngImport dlg(m_path, csName.first, csName.second);- KisCursorOverrideHijacker hijacker;- Q_UNUSED(hijacker);- dlg.exec();- if (!dlg.profile().isEmpty()) {- profile = KoColorSpaceRegistry::instance()->profileByName(dlg.profile());- }+ else if (color_nb_bits == 16 && !fromBlender && !qAppName().toLower().contains("test") && !m_batchMode) {+ // Ask the user which color profile to use+ KisConfig cfg(true);+ quint32 behaviour = cfg.pasteBehaviour();+ if (behaviour == KisClipboard::PASTE_ASK) {+ KisDlgPngImport dlg(m_path, csName.first, csName.second);+ KisCursorOverrideHijacker hijacker;+ Q_UNUSED(hijacker);+ dlg.exec();+ if (!dlg.profile().isEmpty()) {+ profile = KoColorSpaceRegistry::instance()->profileByName(dlg.profile());}}+ }+ else {dbgFile << "no embedded profile, will use the default profile";}@@ -604,7 +600,9 @@ KisImportExportErrorCode KisPNGConverter::buildImage(QIODevice* iod)KoColorSpaceRegistry::instance()->colorSpaceId(csName.first, csName.second);// Check that the profile is used by the color space- if (profile && !KoColorSpaceRegistry::instance()->profileIsCompatible(profile, colorSpaceId)) {+ if (profile+ && (!KoColorSpaceRegistry::instance()->profileIsCompatible(profile, colorSpaceId)+ || !(profile->isSuitableForOutput() || profile->isSuitableForInput()))) {warnFile << "The profile " << profile->name() << " is not compatible with the color space model " << csName.first << " " << csName.second;profile = 0;}@@ -625,24 +623,18 @@ KisImportExportErrorCode KisPNGConverter::buildImage(QIODevice* iod)cs = p2020PQCS;- } else if (profile && profile->isSuitableForOutput()) {+ } else if (profile && profile->isSuitableForWorkspace()) {dbgFile << "image has embedded profile: " << profile->name() << "\n";cs = KoColorSpaceRegistry::instance()->colorSpace(csName.first, csName.second, profile);}else {- if (csName.first == RGBAColorModelID.id()) {- cs = KoColorSpaceRegistry::instance()->colorSpace(csName.first, csName.second, "sRGB-elle-V2-srgbtrc.icc");- } else if (csName.first == GrayAColorModelID.id()) {- cs = KoColorSpaceRegistry::instance()->colorSpace(csName.first, csName.second, "Gray-D50-elle-V2-srgbtrc.icc");- } else {- cs = KoColorSpaceRegistry::instance()->colorSpace(csName.first, csName.second, 0);- }+ // Loading a backup colorspace+ cs = KoColorSpaceRegistry::instance()->colorSpace(csName.first, csName.second, "");+ }- //TODO: two fixes : one tell the user about the problem and ask for a solution, and two once the kocolorspace include KoColorTransformation, use that instead of hacking a lcms transformation- // Create the cmsTransform if needed- if (profile) {- transform = KoColorSpaceRegistry::instance()->colorSpace(csName.first, csName.second, profile)->createColorConverter(cs, KoColorConversionTransformation::internalRenderingIntent(), KoColorConversionTransformation::internalConversionFlags());- }+ // Create the cmsTransform if needed+ if (profile && !profile->isSuitableForWorkspace() && profile->isSuitableForInput()) {+ transform = KoColorSpaceRegistry::instance()->colorSpace(csName.first, csName.second, profile)->createColorConverter(cs, KoColorConversionTransformation::internalRenderingIntent(), KoColorConversionTransformation::internalConversionFlags());}if (cs == 0) {diff --git a/plugins/color/lcms2engine/LcmsColorSpace.h b/plugins/color/lcms2engine/LcmsColorSpace.hindex e286bdd835..9430ba2f29 100644--- a/plugins/color/lcms2engine/LcmsColorSpace.h+++ b/plugins/color/lcms2engine/LcmsColorSpace.h@@ -227,7 +227,7 @@ protected:TYPE_BGR_8,KoColorConversionTransformation::internalRenderingIntent(),conversionFlags);- KIS_SAFE_ASSERT_RECOVER_NOOP(d->defaultTransformations->toRGB);+ KIS_SAFE_ASSERT_RECOVER_NOOP(d->defaultTransformations->toRGB || !d->colorProfile->isSuitableForInput());d->defaultTransformations->toRGB16 = cmsCreateTransform(d->profile->lcmsProfile(),this->colorSpaceType(),@@ -235,7 +235,7 @@ protected:TYPE_BGR_16,KoColorConversionTransformation::internalRenderingIntent(),conversionFlags);- KIS_SAFE_ASSERT_RECOVER_NOOP(d->defaultTransformations->toRGB16);+ KIS_SAFE_ASSERT_RECOVER_NOOP(d->defaultTransformations->toRGB16 || !d->colorProfile->isSuitableForInput());KoLcmsDefaultTransformations::s_transformations[ this->id()][ d->profile ] = d->defaultTransformations;}diff --git a/plugins/color/lcms2engine/colorprofiles/IccColorProfile.cpp b/plugins/color/lcms2engine/colorprofiles/IccColorProfile.cppindex 6238e48412..0852a79d22 100644--- a/plugins/color/lcms2engine/colorprofiles/IccColorProfile.cpp+++ b/plugins/color/lcms2engine/colorprofiles/IccColorProfile.cpp@@ -254,6 +254,21 @@ bool IccColorProfile::isSuitableForOutput() const}return false;}+bool IccColorProfile::isSuitableForInput() const+{+ if (d->shared->lcmsProfile) {+ return d->shared->lcmsProfile->isSuitableForInput() && d->shared->profileInfo->value.canCreateCyclicTransform;+ }+ return false;+}+bool IccColorProfile::isSuitableForWorkspace() const+{+ if (d->shared->lcmsProfile) {+ return d->shared->lcmsProfile->isSuitableForWorkspace() && d->shared->profileInfo->value.canCreateCyclicTransform;+ }+ return false;+}+bool IccColorProfile::isSuitableForPrinting() const{diff --git a/plugins/color/lcms2engine/colorprofiles/IccColorProfile.h b/plugins/color/lcms2engine/colorprofiles/IccColorProfile.hindex 217efca1ba..2d32577dc3 100644--- a/plugins/color/lcms2engine/colorprofiles/IccColorProfile.h+++ b/plugins/color/lcms2engine/colorprofiles/IccColorProfile.h@@ -54,6 +54,8 @@ public:virtual QString copyright() const = 0;virtual bool valid() const = 0;virtual bool isSuitableForOutput() const = 0;+ virtual bool isSuitableForInput() const = 0;+ virtual bool isSuitableForWorkspace() const = 0;virtual bool isSuitableForPrinting() const = 0;virtual bool isSuitableForDisplay() const = 0;virtual bool hasColorants() const = 0;@@ -88,6 +90,8 @@ public:float version() const override;QString colorModelID() const override;bool isSuitableForOutput() const override;+ bool isSuitableForInput() const override;+ bool isSuitableForWorkspace() const override;bool isSuitableForPrinting() const override;bool isSuitableForDisplay() const override;bool supportsPerceptual() const override;diff --git a/plugins/color/lcms2engine/colorprofiles/LcmsColorProfileContainer.cpp b/plugins/color/lcms2engine/colorprofiles/LcmsColorProfileContainer.cppindex 20ee00c18c..175a9e1bc0 100644--- a/plugins/color/lcms2engine/colorprofiles/LcmsColorProfileContainer.cpp+++ b/plugins/color/lcms2engine/colorprofiles/LcmsColorProfileContainer.cpp@@ -23,6 +23,7 @@#include <KisLazyStorage.h>#include <KisLazyValueWrapper.h>+#include <lcms2.h>namespace {struct ReverseCurveWrapper@@ -71,6 +72,7 @@ public:IccColorProfile::Data *data {0};bool valid {false};bool suitableForOutput {false};+ bool suitableForInput {false};bool hasColorants;using LazyBool = KisLazyStorage<KisLazyValueWrapper<bool>, std::function<bool()>>;@@ -297,9 +299,8 @@ bool LcmsColorProfileContainer::init()}// Check if the profile can convert (something->this)- d->suitableForOutput = cmsIsIntentSupported(d->profile,- INTENT_PERCEPTUAL,- LCMS_USED_AS_OUTPUT);+ d->suitableForOutput = cmsIsIntentSupported(d->profile, INTENT_PERCEPTUAL, LCMS_USED_AS_OUTPUT);+ d->suitableForInput = cmsIsIntentSupported(d->profile, INTENT_PERCEPTUAL, LCMS_USED_AS_INPUT);d->version = cmsGetProfileVersion(d->profile);d->defaultIntent = cmsGetHeaderRenderingIntent(d->profile);@@ -354,6 +355,14 @@ bool LcmsColorProfileContainer::isSuitableForOutput() const{return d->suitableForOutput;}+bool LcmsColorProfileContainer::isSuitableForInput() const+{+ return d->suitableForInput;+}+bool LcmsColorProfileContainer::isSuitableForWorkspace() const+{+ return d->suitableForOutput && d->suitableForInput;+}bool LcmsColorProfileContainer::isSuitableForPrinting() const{diff --git a/plugins/color/lcms2engine/colorprofiles/LcmsColorProfileContainer.h b/plugins/color/lcms2engine/colorprofiles/LcmsColorProfileContainer.hindex 91e5a54fcc..3692db33ea 100644--- a/plugins/color/lcms2engine/colorprofiles/LcmsColorProfileContainer.h+++ b/plugins/color/lcms2engine/colorprofiles/LcmsColorProfileContainer.h@@ -67,6 +67,8 @@ public:virtual float version() const;bool isSuitableForOutput() const override;+ bool isSuitableForInput() const override;+ bool isSuitableForWorkspace() const override;bool isSuitableForPrinting() const override;diff --git a/plugins/impex/jpeg/kis_jpeg_converter.cc b/plugins/impex/jpeg/kis_jpeg_converter.ccindex 15a4356239..193b213ab1 100644--- a/plugins/impex/jpeg/kis_jpeg_converter.cc+++ b/plugins/impex/jpeg/kis_jpeg_converter.cc@@ -5,7 +5,6 @@*/#include "kis_jpeg_converter.h"-#include <stdio.h>#include <stdint.h>#include <KoConfig.h>@@ -164,39 +163,38 @@ KisImportExportErrorCode KisJPEGConverter::decode(QIODevice *io)return ImportExportCodes::FormatColorSpaceUnsupported;}- uchar* profile_data = 0;- uint profile_len = 0;- const KoColorProfile* profile = 0;- QByteArray profile_rawdata;- if (read_icc_profile(&cinfo, &profile_data, &profile_len)) {- profile_rawdata.resize(profile_len);- memcpy(profile_rawdata.data(), profile_data, profile_len);- cmsHPROFILE hProfile = cmsOpenProfileFromMem(profile_data, profile_len);-- if (hProfile != (cmsHPROFILE) 0) {- profile = KoColorSpaceRegistry::instance()->createColorProfile(modelId, Integer8BitsColorDepthID.id(), profile_rawdata);- Q_CHECK_PTR(profile);- dbgFile <<"profile name:" << profile->name() <<" product information:" << profile->info();- if (!profile->isSuitableForOutput()) {- dbgFile << "the profile is not suitable for output and therefore cannot be used in krita, we need to convert the image to a standard profile"; // TODO: in ko2 popup a selection menu to inform the user- }- }+ uint8_t* profile_data = nullptr;+ uint32_t profile_len = 0;+ const KoColorProfile* profile = nullptr;++ if (read_icc_profile(&cinfo, &profile_data, &profile_len)) {+ QByteArray profile_rawdata(reinterpret_cast<char*>(profile_data), static_cast<int>(profile_len));+ // read_icc_profile allocates memory, and we need to free itfree(profile_data);++ profile = KoColorSpaceRegistry::instance()->createColorProfile(modelId, Integer8BitsColorDepthID.id(), profile_rawdata);+ dbgFile <<"profile name:" << profile->name() <<" product information:" << profile->info();+ //This is stadard for profiles like this+ if (!profile->isSuitableForWorkspace()) {+ dbgFile << "the profile is not suitable for workspace and therefore cannot be used in krita, we need to convert the image to a standard profile";+ }}const QString colorSpaceId =KoColorSpaceRegistry::instance()->colorSpaceId(modelId, Integer8BitsColorDepthID.id());// Check that the profile is used by the color space- if (profile && !KoColorSpaceRegistry::instance()->profileIsCompatible(profile, colorSpaceId)) {+ if (profile+ && (!KoColorSpaceRegistry::instance()->profileIsCompatible(profile, colorSpaceId)+ || !(profile->isSuitableForInput() || profile->isSuitableForOutput()))) {warnFile << "The profile " << profile->name() << " is not compatible with the color space model " << modelId;profile = 0;}// Retrieve a pointer to the colorspace- const KoColorSpace* cs;- if (profile && profile->isSuitableForOutput()) {+ const KoColorSpace* cs = nullptr;+ if (profile && profile->isSuitableForWorkspace()) {dbgFile << "image has embedded profile:" << profile -> name() << "";cs = KoColorSpaceRegistry::instance()->colorSpace(modelId, Integer8BitsColorDepthID.id(), profile);} else@@ -207,13 +205,15 @@ KisImportExportErrorCode KisJPEGConverter::decode(QIODevice *io)jpeg_destroy_decompress(&cinfo);return ImportExportCodes::FormatColorSpaceUnsupported;}- // TODO fixit- // Create the cmsTransform if needed+ // Create the cmsTransform if neededKoColorTransformation* transform = 0;- if (profile && !profile->isSuitableForOutput()) {- transform = KoColorSpaceRegistry::instance()->colorSpace(modelId, Integer8BitsColorDepthID.id(), profile)->createColorConverter(cs, KoColorConversionTransformation::internalRenderingIntent(), KoColorConversionTransformation::internalConversionFlags());+ if (profile && !profile->isSuitableForWorkspace() && profile->isSuitableForInput()) {+ transform = KoColorSpaceRegistry::instance()+ ->colorSpace(modelId, Integer8BitsColorDepthID.id(), profile)+ ->createColorConverter(cs, KoColorConversionTransformation::internalRenderingIntent(), KoColorConversionTransformation::internalConversionFlags());}+// Apparently an invalid transform was created from the profile. See bug https://bugs.kde.org/show_bug.cgi?id=255451.// After 2.3: warn the user!if (transform && !transform->isValid()) {diff --git a/plugins/impex/tiff/kis_tiff_import.cc b/plugins/impex/tiff/kis_tiff_import.ccindex 5f0e01eb88..ed6c998f7d 100644--- a/plugins/impex/tiff/kis_tiff_import.cc+++ b/plugins/impex/tiff/kis_tiff_import.cc@@ -1730,15 +1730,15 @@ KisImportExportErrorCode KisTIFFImport::readTIFFDirectory(KisDocument *m_doc,basicInfo.colorSpaceIdTag.second);// Check that the profile is used by the color space- if (profile- && !KoColorSpaceRegistry::instance()->profileIsCompatible(- profile,- colorSpaceId)) {- dbgFile << "The profile " << profile->name()- << " is not compatible with the color space model "- << basicInfo.colorSpaceIdTag.first << " "- << basicInfo.colorSpaceIdTag.second;- profile = nullptr;+ if (profile) {+ if (!KoColorSpaceRegistry::instance()->profileIsCompatible(profile, colorSpaceId)+ || !(profile->isSuitableForInput() || profile->isSuitableForOutput())) {+ dbgFile << "The profile " << profile->name()+ << " is not compatible with the color space model "+ << basicInfo.colorSpaceIdTag.first << " "+ << basicInfo.colorSpaceIdTag.second;+ profile = nullptr;+ }}// Do not use the linear gamma profile for 16 bits/channel by default, tiff@@ -1768,7 +1768,7 @@ KisImportExportErrorCode KisTIFFImport::readTIFFDirectory(KisDocument *m_doc,}// Retrieve a pointer to the colorspace- if (profile && profile->isSuitableForOutput()) {+ if (profile && profile->isSuitableForWorkspace()) {dbgFile << "image has embedded profile:" << profile->name() << "";basicInfo.cs = KoColorSpaceRegistry::instance()->colorSpace(basicInfo.colorSpaceIdTag.first,@@ -1789,18 +1789,18 @@ KisImportExportErrorCode KisTIFFImport::readTIFFDirectory(KisDocument *m_doc,return ImportExportCodes::FormatColorSpaceUnsupported;}- // Create the cmsTransform if needed- if (profile && !profile->isSuitableForOutput()) {- dbgFile << "The profile can't be used in krita, need conversion";- basicInfo.transform =- KoColorSpaceRegistry::instance()- ->colorSpace(basicInfo.colorSpaceIdTag.first,- basicInfo.colorSpaceIdTag.second,- profile)- ->createColorConverter(- basicInfo.cs,- KoColorConversionTransformation::internalRenderingIntent(),- KoColorConversionTransformation::internalConversionFlags());+ // Create the cmsTransform if needed+ if (profile && !profile->isSuitableForWorkspace() && profile->isSuitableForInput()) {+ dbgFile << "The profile can't be used in krita, need conversion";+ basicInfo.transform =+ KoColorSpaceRegistry::instance()+ ->colorSpace(basicInfo.colorSpaceIdTag.first,+ basicInfo.colorSpaceIdTag.second,+ profile)+ ->createColorConverter(+ basicInfo.cs,+ KoColorConversionTransformation::internalRenderingIntent(),+ KoColorConversionTransformation::internalConversionFlags());}KisImportExportErrorCode result = readImageFromPsd(m_doc, image, basicInfo);