summaryrefslogtreecommitdiffstats
path: root/chalk/chalkcolor/kis_colorspace_factory_registry.cc
blob: d9b2591ca15d738b1b997a83386c7e241ab293b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 *  Copyright (c) 2003 Patrick Julien  <freak@codepimps.org>
 *  Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kdebug.h"
#include <tdeparts/plugin.h>
#include <kservice.h>
#include <ktrader.h>
#include <tdeparts/componentfactory.h>
#include <tdemessagebox.h>
#include <tdelocale.h>
#include "kis_debug_areas.h"
#include "kis_colorspace.h"
#include "kis_profile.h"
#include "kis_colorspace_factory_registry.h"
#include "kis_alpha_colorspace.h"
#include "kis_lab_colorspace.h"


KisColorSpaceFactoryRegistry::KisColorSpaceFactoryRegistry(TQStringList profileFilenames)
{
    // Create the built-in colorspaces

    m_alphaCs = new KisAlphaColorSpace(this, 0);

    // Load the profiles
    if (!profileFilenames.empty()) {
        KisProfile * profile = 0;
        for ( TQStringList::Iterator it = profileFilenames.begin(); it != profileFilenames.end(); ++it ) {
            profile = new KisProfile(*it);
            TQ_CHECK_PTR(profile);

            profile->load();
            if (profile->valid()) {
                m_profileMap[profile->productName()] = profile;
            }
        }
    }

    KisProfile *labProfile = new KisProfile(cmsCreateLabProfile(NULL));
    addProfile(labProfile);
    add(new KisLabColorSpaceFactory());
/* XXX where to put this
    KisHistogramProducerFactoryRegistry::instance()->add(
                new KisBasicHistogramProducerFactory<KisBasicU16HistogramProducer>
                (KisID("LABAHISTO", i18n("L*a*b* Histogram")), new KisLabColorSpace(this, 0);) );
*/

    // Load all colorspace modules
    TDETrader::OfferList offers = TDETrader::self()->query(TQString::fromLatin1("Chalk/ColorSpace"),
                                                         TQString::fromLatin1("(Type == 'Service') and "
                                                                             "([X-Chalk-Version] == 2)"));

    if (offers.empty()) {
        KMessageBox::sorry(0, i18n("Cannot start Chalk: no colorspaces available."));
        abort();
    }

    TDETrader::OfferList::ConstIterator iter;
    for(iter = offers.begin(); iter != offers.end(); ++iter)
    {
        KService::Ptr service = *iter;
        int errCode = 0;
        KParts::Plugin* plugin =
             KParts::ComponentFactory::createInstanceFromService<KParts::Plugin> ( service, this, 0, TQStringList(), &errCode);
        if ( plugin )
            kdDebug(DBG_AREA_PLUGINS) << "found colorspace " << service->property("Name").toString() << "\n";
        else {
            kdDebug(41006) << "found plugin " << service->property("Name").toString() << ", " << errCode << "\n";
            if( errCode == KParts::ComponentFactory::ErrNoLibrary)
            {
                kdWarning(41006) << " Error loading plugin was : ErrNoLibrary " << KLibLoader::self()->lastErrorMessage() << endl;
            }
        }
    }
}

KisColorSpaceFactoryRegistry::KisColorSpaceFactoryRegistry()
{
}

KisColorSpaceFactoryRegistry::~KisColorSpaceFactoryRegistry()
{
}

KisProfile *  KisColorSpaceFactoryRegistry::getProfileByName(const TQString & name)
{
    if (m_profileMap.find(name) == m_profileMap.end()) {
        return 0;
    }

    return m_profileMap[name];
}

TQValueVector<KisProfile *>  KisColorSpaceFactoryRegistry::profilesFor(KisID id)
{
    return profilesFor(get(id));
}

TQValueVector<KisProfile *>  KisColorSpaceFactoryRegistry::profilesFor(KisColorSpaceFactory * csf)
{

    TQValueVector<KisProfile *>  profiles;

    TQMap<TQString, KisProfile * >::Iterator it;
    for (it = m_profileMap.begin(); it != m_profileMap.end(); ++it) {
        KisProfile *  profile = it.data();
        if (profile->colorSpaceSignature() == csf->colorSpaceSignature()) {
            profiles.push_back(profile);
        }
    }
    return profiles;
}

void KisColorSpaceFactoryRegistry::addProfile(KisProfile *p)
{
      if (p->valid()) {
          m_profileMap[p->productName()] = p;
      }
}

void KisColorSpaceFactoryRegistry::addPaintDeviceAction(KisColorSpace* cs,
        KisPaintDeviceAction* action) {
    m_paintDevActionMap[cs->id()].append(action);
}

TQValueVector<KisPaintDeviceAction *>
KisColorSpaceFactoryRegistry::paintDeviceActionsFor(KisColorSpace* cs) {
    return m_paintDevActionMap[cs->id()];
}

KisColorSpace * KisColorSpaceFactoryRegistry::getColorSpace(const KisID & csID, const TQString & pName)
{
    TQString profileName = pName;

    if(profileName.isEmpty())
    {
        KisColorSpaceFactory *csf = get(csID);

        if(!csf)
            return 0;

        profileName = csf->defaultProfile();
    }

    TQString name = csID.id() + "<comb>" + profileName;

    if (m_csMap.find(name) == m_csMap.end()) {
        KisColorSpaceFactory *csf = get(csID);
        if(!csf)
            return 0;

        KisProfile *p = getProfileByName(profileName);
        if(!p && profileName != "")
            return 0;
        KisColorSpace *cs = csf->createColorSpace(this, p);
        if(!cs)
            return 0;

        m_csMap[name] = cs;
    }

    if(m_csMap.contains(name))
        return m_csMap[name];
    else
        return 0;
}


KisColorSpace * KisColorSpaceFactoryRegistry::getColorSpace(const KisID & csID, const KisProfile * profile)
{
    if( profile )
    {
        KisColorSpace *cs = getColorSpace( csID, profile->productName());

        if(!cs)
        {
            // The profile was not stored and thus not the combination either
            KisColorSpaceFactory *csf = get(csID);
            if(!csf)
                return 0;

            cs = csf->createColorSpace(this, const_cast<KisProfile *>(profile));
            if(!cs )
                return 0;

            TQString name = csID.id() + "<comb>" + profile->productName();
            m_csMap[name] = cs;
        }

        return cs;
    } else {
        return getColorSpace( csID, "");
    }
}

KisColorSpace * KisColorSpaceFactoryRegistry::getAlpha8()
{
   return m_alphaCs;
}

KisColorSpace * KisColorSpaceFactoryRegistry::getRGB8()
{
    return getColorSpace(KisID("RGBA", ""), "");
}

#include "kis_colorspace_factory_registry.moc"