File indexing completed on 2024-05-12 15:59:34

0001 /*
0002  *  SPDX-FileCopyrightText: 2010 Cyrille Berger <cberger@cberger.net>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #include "KoColorSpaceFactory.h"
0008 
0009 #include "DebugPigment.h"
0010 
0011 #include <QMutexLocker>
0012 
0013 #include "KoColorProfile.h"
0014 #include "KoColorSpace.h"
0015 #include "KoColorSpaceRegistry.h"
0016 
0017 #include "kis_assert.h"
0018 
0019 struct Q_DECL_HIDDEN KoColorSpaceFactory::Private {
0020     QList<KoColorProfile*> colorprofiles;
0021     QHash<QString, KoColorSpace* > availableColorspaces;
0022     QMutex mutex;
0023 #ifndef NDEBUG
0024     QHash<KoColorSpace*, QString> stackInformation;
0025 #endif
0026 };
0027 
0028 KoColorSpaceFactory::KoColorSpaceFactory() : d(new Private)
0029 {
0030 }
0031 
0032 KoColorSpaceFactory::~KoColorSpaceFactory()
0033 {
0034 #ifndef NDEBUG
0035     // Check that all color spaces have been released
0036     int count = 0;
0037     count += d->availableColorspaces.size();
0038 
0039     for(QHash<KoColorSpace*, QString>::const_iterator it = d->stackInformation.constBegin();
0040         it != d->stackInformation.constEnd(); ++it)
0041     {
0042         errorPigment << "*******************************************";
0043         errorPigment << it.key()->id() << " still in used, and grabbed in: ";
0044         errorPigment << it.value();
0045     }
0046 #endif
0047     Q_FOREACH (KoColorProfile* profile, d->colorprofiles) {
0048         KoColorSpaceRegistry::instance()->removeProfile(profile);
0049         delete profile;
0050     }
0051     delete d;
0052 }
0053 
0054 const KoColorProfile *KoColorSpaceFactory::colorProfile(const QByteArray &rawData, KoColorSpaceFactory::ProfileRegistrationInterface *registrationInterface) const
0055 {
0056     KoColorProfile* colorProfile = createColorProfile(rawData);
0057     if (colorProfile && colorProfile->valid()) {
0058         if (const KoColorProfile* existingProfile = registrationInterface->profileByName(colorProfile->name())) {
0059             delete colorProfile;
0060             return existingProfile;
0061         }
0062         registrationInterface->registerNewProfile(colorProfile);
0063         d->colorprofiles.append(colorProfile);
0064     }
0065     return colorProfile;
0066 }
0067 
0068 const KoColorSpace *KoColorSpaceFactory::grabColorSpace(const KoColorProfile * profile)
0069 {
0070     QMutexLocker l(&d->mutex);
0071     Q_ASSERT(profile);
0072     auto it = d->availableColorspaces.find(profile->name());
0073     KoColorSpace* cs;
0074 
0075     if (it == d->availableColorspaces.end()) {
0076         cs = createColorSpace(profile);
0077         KIS_ASSERT_X(cs != nullptr, "KoColorSpaceFactory::grabColorSpace", "createColorSpace returned nullptr.");
0078         if (cs) {
0079             d->availableColorspaces[profile->name()] = cs;
0080         }
0081     }
0082     else {
0083         cs = it.value();
0084     }
0085 
0086     return cs;
0087 }
0088