kis_colorspace_factory_registry.cpp
1/*
2 * Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
3 * Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#include "kdebug.h"
21#include <tdeparts/plugin.h>
22#include <kservice.h>
23#include <ktrader.h>
24#include <tdeparts/componentfactory.h>
25#include <tdemessagebox.h>
26#include <tdelocale.h>
27#include "kis_debug_areas.h"
28#include "kis_colorspace.h"
29#include "kis_profile.h"
30#include "kis_colorspace_factory_registry.h"
31#include "kis_alpha_colorspace.h"
32#include "kis_lab_colorspace.h"
33
34
36{
37 // Create the built-in colorspaces
38
39 m_alphaCs = new KisAlphaColorSpace(this, 0);
40
41 // Load the profiles
42 if (!profileFilenames.empty()) {
43 KisProfile * profile = 0;
44 for ( TQStringList::Iterator it = profileFilenames.begin(); it != profileFilenames.end(); ++it ) {
45 profile = new KisProfile(*it);
46 TQ_CHECK_PTR(profile);
47
48 profile->load();
49 if (profile->valid()) {
50 m_profileMap[profile->productName()] = profile;
51 }
52 }
53 }
54
55 KisProfile *labProfile = new KisProfile(cmsCreateLabProfile(NULL));
56 addProfile(labProfile);
57 add(new KisLabColorSpaceFactory());
58/* XXX where to put this
59 KisHistogramProducerFactoryRegistry::instance()->add(
60 new KisBasicHistogramProducerFactory<KisBasicU16HistogramProducer>
61 (KisID("LABAHISTO", i18n("L*a*b* Histogram")), new KisLabColorSpace(this, 0);) );
62*/
63
64 // Load all colorspace modules
65 TDETrader::OfferList offers = TDETrader::self()->query(TQString::fromLatin1("Chalk/ColorSpace"),
66 TQString::fromLatin1("(Type == 'Service') and "
67 "([X-Chalk-Version] == 2)"));
68
69 if (offers.empty()) {
70 KMessageBox::sorry(0, i18n("Cannot start Chalk: no colorspaces available."));
71 abort();
72 }
73
74 TDETrader::OfferList::ConstIterator iter;
75 for(iter = offers.begin(); iter != offers.end(); ++iter)
76 {
77 KService::Ptr service = *iter;
78 int errCode = 0;
79 KParts::Plugin* plugin =
80 KParts::ComponentFactory::createInstanceFromService<KParts::Plugin> ( service, this, 0, TQStringList(), &errCode);
81 if ( plugin )
82 kdDebug(DBG_AREA_PLUGINS) << "found colorspace " << service->property("Name").toString() << "\n";
83 else {
84 kdDebug(41006) << "found plugin " << service->property("Name").toString() << ", " << errCode << "\n";
85 if( errCode == KParts::ComponentFactory::ErrNoLibrary)
86 {
87 kdWarning(41006) << " Error loading plugin was : ErrNoLibrary " << KLibLoader::self()->lastErrorMessage() << endl;
88 }
89 }
90 }
91}
92
93KisColorSpaceFactoryRegistry::KisColorSpaceFactoryRegistry()
94{
95}
96
97KisColorSpaceFactoryRegistry::~KisColorSpaceFactoryRegistry()
98{
99}
100
101KisProfile * KisColorSpaceFactoryRegistry::getProfileByName(const TQString & name)
102{
103 if (m_profileMap.find(name) == m_profileMap.end()) {
104 return 0;
105 }
106
107 return m_profileMap[name];
108}
109
110TQValueVector<KisProfile *> KisColorSpaceFactoryRegistry::profilesFor(KisID id)
111{
112 return profilesFor(get(id));
113}
114
115TQValueVector<KisProfile *> KisColorSpaceFactoryRegistry::profilesFor(KisColorSpaceFactory * csf)
116{
117
118 TQValueVector<KisProfile *> profiles;
119
120 TQMap<TQString, KisProfile * >::Iterator it;
121 for (it = m_profileMap.begin(); it != m_profileMap.end(); ++it) {
122 KisProfile * profile = it.data();
123 if (profile->colorSpaceSignature() == csf->colorSpaceSignature()) {
124 profiles.push_back(profile);
125 }
126 }
127 return profiles;
128}
129
131{
132 if (p->valid()) {
133 m_profileMap[p->productName()] = p;
134 }
135}
136
138 KisPaintDeviceAction* action) {
139 m_paintDevActionMap[cs->id()].append(action);
140}
141
142TQValueVector<KisPaintDeviceAction *>
144 return m_paintDevActionMap[cs->id()];
145}
146
148{
149 TQString profileName = pName;
150
151 if(profileName.isEmpty())
152 {
153 KisColorSpaceFactory *csf = get(csID);
154
155 if(!csf)
156 return 0;
157
158 profileName = csf->defaultProfile();
159 }
160
161 TQString name = csID.id() + "<comb>" + profileName;
162
163 if (m_csMap.find(name) == m_csMap.end()) {
164 KisColorSpaceFactory *csf = get(csID);
165 if(!csf)
166 return 0;
167
168 KisProfile *p = getProfileByName(profileName);
169 if(!p && profileName != "")
170 return 0;
171 KisColorSpace *cs = csf->createColorSpace(this, p);
172 if(!cs)
173 return 0;
174
175 m_csMap[name] = cs;
176 }
177
178 if(m_csMap.contains(name))
179 return m_csMap[name];
180 else
181 return 0;
182}
183
184
185KisColorSpace * KisColorSpaceFactoryRegistry::getColorSpace(const KisID & csID, const KisProfile * profile)
186{
187 if( profile )
188 {
189 KisColorSpace *cs = getColorSpace( csID, profile->productName());
190
191 if(!cs)
192 {
193 // The profile was not stored and thus not the combination either
194 KisColorSpaceFactory *csf = get(csID);
195 if(!csf)
196 return 0;
197
198 cs = csf->createColorSpace(this, const_cast<KisProfile *>(profile));
199 if(!cs )
200 return 0;
201
202 TQString name = csID.id() + "<comb>" + profile->productName();
203 m_csMap[name] = cs;
204 }
205
206 return cs;
207 } else {
208 return getColorSpace( csID, "");
209 }
210}
211
213{
214 return m_alphaCs;
215}
216
221
222#include "kis_colorspace_factory_registry.moc"
TQValueVector< KisProfile * > profilesFor(KisColorSpaceFactory *cs)
Return the vector of profiles for this colorspacefactory.
TQValueVector< KisPaintDeviceAction * > paintDeviceActionsFor(KisColorSpace *cs)
Get a list of KisConstructPaintDeviceAction for a colorspace.
KisColorSpace * getAlpha8()
Convenience method to get the often used alpha colorspace.
KisColorSpaceFactoryRegistry(TQStringList profileFileNames)
Create a new colorspacefactory registry.
KisColorSpace * getRGB8()
Convenience method to get an RGB colorspace with the default lcms profile.
KisColorSpace * getColorSpace(const KisID &csID, const TQString &profileName)
Return the colorspace + profile as named, or NULL if impossible combination.
KisProfile * getProfileByName(const TQString &name)
Return the profile associated with the given product name, or 0.
void addProfile(KisProfile *p)
Add the profile to the list.
void addPaintDeviceAction(KisColorSpace *cs, KisPaintDeviceAction *action)
add a KisConstructPaintDeviceAction to the registry for a colorspace
A colorspace is the definition of a certain color model in Chalk.
virtual KisID id() const =0
Chalk definition for use in .kra files and internally: unchanging name + i18n'able description.
KisColorSpaceFactory * get(const KisID &name) const
This function allow to get an object from its KisID.
void add(KisColorSpaceFactory * item)
add an object to the registry
Chalk has a large number of extensible resources.
Definition kis_id.h:57
Defines an action to do with a paint device.