KoColor.cpp 9.57 KB
Newer Older
1 2
/*
 *  Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
Thomas Zander's avatar
Thomas Zander committed
3
 *  Copyright (C) 2007 Thomas Zander <zander@kde.org>
4
 *  Copyright (C) 2007 Cyrille Berger <cberger@cberger.net>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8 9
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
10
 *
11 12 13
 * This library 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
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17 18 19 20
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/
21
#include <QColor>
Thomas Zander's avatar
Thomas Zander committed
22

23 24
#include <QDomDocument>

Boudewijn Rempt's avatar
Boudewijn Rempt committed
25
#include "DebugPigment.h"
26
#include "KoColor.h"
27 28


29
#include "KoColorModelStandardIds.h"
30 31
#include "KoColorProfile.h"
#include "KoColorSpace.h"
32
#include "KoColorSpaceRegistry.h"
33
#include "KoChannelInfo.h"
34

35

36 37
class KoColor::Private
{
Thomas Zander's avatar
Thomas Zander committed
38 39
public:
    Private() : data(0), colorSpace(0) {}
40

Thomas Zander's avatar
Thomas Zander committed
41
    ~Private() {
42
        delete [] data;
Thomas Zander's avatar
Thomas Zander committed
43
    }
44

Thomas Zander's avatar
Thomas Zander committed
45
    quint8 * data;
46
    const KoColorSpace * colorSpace;
Thomas Zander's avatar
Thomas Zander committed
47 48
};

49
KoColor::KoColor()
50
        : d(new Private())
51
{
52
    d->colorSpace = KoColorSpaceRegistry::instance()->lab16(0);
Thomas Zander's avatar
Thomas Zander committed
53 54
    d->data = new quint8[d->colorSpace->pixelSize()];
    memset(d->data, 0, d->colorSpace->pixelSize());
55
    d->colorSpace->setOpacity(d->data, OPACITY_OPAQUE_U8, 1);
56
}
57

58
KoColor::KoColor(const KoColorSpace * colorSpace)
59
        : d(new Private())
60
{
Cyrille Berger's avatar
Cyrille Berger committed
61
    Q_ASSERT(colorSpace);
62
    d->colorSpace = KoColorSpaceRegistry::instance()->permanentColorspace(colorSpace);
Thomas Zander's avatar
Thomas Zander committed
63 64
    d->data = new quint8[d->colorSpace->pixelSize()];
    memset(d->data, 0, d->colorSpace->pixelSize());
65 66
}

67 68 69

KoColor::~KoColor()
{
Thomas Zander's avatar
Thomas Zander committed
70
    delete d;
71 72
}

73
KoColor::KoColor(const QColor & color, const KoColorSpace * colorSpace)
74
        : d(new Private())
75 76 77
{
    Q_ASSERT(color.isValid());
    Q_ASSERT(colorSpace);
78
    d->colorSpace = KoColorSpaceRegistry::instance()->permanentColorspace(colorSpace);
79

Thomas Zander's avatar
Thomas Zander committed
80 81 82
    d->data = new quint8[colorSpace->pixelSize()];
    memset(d->data, 0, d->colorSpace->pixelSize());

83
    d->colorSpace->fromQColor(color, d->data);
84 85
}

86
KoColor::KoColor(const quint8 * data, const KoColorSpace * colorSpace)
87
        : d(new Private())
88
{
Cyrille Berger's avatar
Cyrille Berger committed
89 90
    Q_ASSERT(colorSpace);
    Q_ASSERT(data);
91
    d->colorSpace = KoColorSpaceRegistry::instance()->permanentColorspace(colorSpace);
Thomas Zander's avatar
Thomas Zander committed
92 93 94
    d->data = new quint8[colorSpace->pixelSize()];
    memset(d->data, 0, d->colorSpace->pixelSize());
    memmove(d->data, data, colorSpace->pixelSize());
95 96 97
}


98
KoColor::KoColor(const KoColor &src, const KoColorSpace * colorSpace)
99
        : d(new Private())
100
{
Cyrille Berger's avatar
Cyrille Berger committed
101
    Q_ASSERT(colorSpace);
102
    d->colorSpace = KoColorSpaceRegistry::instance()->permanentColorspace(colorSpace);
Thomas Zander's avatar
Thomas Zander committed
103 104
    d->data = new quint8[colorSpace->pixelSize()];
    memset(d->data, 0, d->colorSpace->pixelSize());
105

106
    src.colorSpace()->convertPixelsTo(src.d->data, d->data, colorSpace, 1, KoColorConversionTransformation::InternalRenderingIntent, KoColorConversionTransformation::InternalConversionFlags);
107 108
}

109
KoColor::KoColor(const KoColor & rhs)
110
        : d(new Private())
111
{
Thomas Zander's avatar
Thomas Zander committed
112
    d->colorSpace = rhs.colorSpace();
113
    Q_ASSERT(*d->colorSpace == *KoColorSpaceRegistry::instance()->permanentColorspace(d->colorSpace));
114
    if (d->colorSpace && rhs.d->data) {
Thomas Zander's avatar
Thomas Zander committed
115 116
        d->data = new quint8[d->colorSpace->pixelSize()];
        memcpy(d->data, rhs.d->data, d->colorSpace->pixelSize());
117
    }
118 119 120 121
}

KoColor & KoColor::operator=(const KoColor & rhs)
{
122 123
    if (this == &rhs) return *this;

Thomas Zander's avatar
Thomas Zander committed
124 125 126
    delete [] d->data;
    d->data = 0;
    d->colorSpace = rhs.colorSpace();
127

Thomas Zander's avatar
Thomas Zander committed
128
    if (rhs.d->colorSpace && rhs.d->data) {
129
        Q_ASSERT(d->colorSpace == KoColorSpaceRegistry::instance()->permanentColorspace(d->colorSpace)); // here we want to do a check on pointer, since d->colorSpace is supposed to already be a permanent one
Thomas Zander's avatar
Thomas Zander committed
130 131
        d->data = new quint8[d->colorSpace->pixelSize()];
        memcpy(d->data, rhs.d->data, d->colorSpace->pixelSize());
132
    }
133 134 135
    return * this;
}

136
bool KoColor::operator==(const KoColor &other) const
137
{
138 139
    if (!(*colorSpace() == *other.colorSpace()))
        return false;
140 141 142
    return memcmp(d->data, other.d->data, d->colorSpace->pixelSize()) == 0;
}

143
void KoColor::convertTo(const KoColorSpace * cs)
144
{
Boudewijn Rempt's avatar
Boudewijn Rempt committed
145
    //dbgPigment <<"Our colormodel:" << d->colorSpace->id().name()
146 147
    //      << ", new colormodel: " << cs->id().name() << "\n";

148
    if (*d->colorSpace == *cs)
149 150
        return;

Thomas Zander's avatar
Thomas Zander committed
151 152
    quint8 * data = new quint8[cs->pixelSize()];
    memset(data, 0, cs->pixelSize());
153

154
    d->colorSpace->convertPixelsTo(d->data, data, cs, 1, KoColorConversionTransformation::InternalRenderingIntent, KoColorConversionTransformation::InternalConversionFlags);
155

Thomas Zander's avatar
Thomas Zander committed
156 157
    delete [] d->data;
    d->data = data;
158
    d->colorSpace = KoColorSpaceRegistry::instance()->permanentColorspace(cs);
159 160 161
}


162
void KoColor::setColor(const quint8 * data, const KoColorSpace * colorSpace)
163
{
Cyrille Berger's avatar
Cyrille Berger committed
164 165
    Q_ASSERT(data);
    Q_ASSERT(colorSpace);
166 167 168 169 170
    if(d->colorSpace->pixelSize() != colorSpace->pixelSize())
    {
        delete [] d->data;
        d->data = new quint8[colorSpace->pixelSize()];
    }
Thomas Zander's avatar
Thomas Zander committed
171
    memcpy(d->data, data, colorSpace->pixelSize());
172
    d->colorSpace = KoColorSpaceRegistry::instance()->permanentColorspace(colorSpace);
173 174
}

175 176 177
// To save the user the trouble of doing color->colorSpace()->toQColor(color->data(), &c, &a, profile
void KoColor::toQColor(QColor *c) const
{
Cyrille Berger's avatar
Cyrille Berger committed
178
    Q_ASSERT(c);
Thomas Zander's avatar
Thomas Zander committed
179 180
    if (d->colorSpace && d->data) {
        d->colorSpace->toQColor(d->data, c);
181
    }
182 183 184 185 186 187 188 189
}

QColor KoColor::toQColor() const
{
    QColor c;
    toQColor(&c);
    return c;
}
190

191 192
void KoColor::fromQColor(const QColor& c) const
{
Thomas Zander's avatar
Thomas Zander committed
193 194
    if (d->colorSpace && d->data) {
        d->colorSpace->fromQColor(c, d->data);
195 196 197
    }
}

Thomas Zander's avatar
Thomas Zander committed
198
#ifndef NDEBUG
199 200
void KoColor::dump() const
{
Boudewijn Rempt's avatar
Boudewijn Rempt committed
201
    //dbgPigment <<"KoColor (" << this <<")," << d->colorSpace->id().name() <<"";
Adrian Page's avatar
q3--  
Adrian Page committed
202
    QList<KoChannelInfo *> channels = d->colorSpace->channels();
203

Laurent Montel's avatar
Laurent Montel committed
204 205
    QList<KoChannelInfo *>::const_iterator begin = channels.constBegin();
    QList<KoChannelInfo *>::const_iterator end = channels.constEnd();
206

207
    for (QList<KoChannelInfo *>::const_iterator it = begin; it != end; ++it) {
208 209 210 211
        KoChannelInfo * ch = (*it);
        // XXX: setNum always takes a byte.
        if (ch->size() == sizeof(quint8)) {
            // Byte
Boudewijn Rempt's avatar
Boudewijn Rempt committed
212
            //dbgPigment <<"Channel (byte):" << ch->name() <<":" << QString().setNum(d->data[ch->pos()]) <<"";
213
        } else if (ch->size() == sizeof(quint16)) {
214
            // Short (may also by an nvidia half)
Boudewijn Rempt's avatar
Boudewijn Rempt committed
215
            //dbgPigment <<"Channel (short):" << ch->name() <<":" << QString().setNum(*((const quint16 *)(d->data+ch->pos())))  <<"";
216
        } else if (ch->size() == sizeof(quint32)) {
217
            // Integer (may also be float... Find out how to distinguish these!)
Boudewijn Rempt's avatar
Boudewijn Rempt committed
218
            //dbgPigment <<"Channel (int):" << ch->name() <<":" << QString().setNum(*((const quint32 *)(d->data+ch->pos())))  <<"";
219 220 221
        }
    }
}
Thomas Zander's avatar
Thomas Zander committed
222
#endif
223 224 225

void KoColor::fromKoColor(const KoColor& src)
{
226
    src.colorSpace()->convertPixelsTo(src.d->data, d->data, colorSpace(), 1, KoColorConversionTransformation::InternalRenderingIntent, KoColorConversionTransformation::InternalConversionFlags);
227
}
Cyrille Berger's avatar
Cyrille Berger committed
228

229
const KoColorProfile *  KoColor::profile() const
Cyrille Berger's avatar
Cyrille Berger committed
230
{
Thomas Zander's avatar
Thomas Zander committed
231 232 233
    return d->colorSpace->profile();
}

234 235
quint8 * KoColor::data()
{
Thomas Zander's avatar
Thomas Zander committed
236 237 238
    return d->data;
}

239 240
const quint8 * KoColor::data() const
{
Thomas Zander's avatar
Thomas Zander committed
241
    return d->data;
Cyrille Berger's avatar
Cyrille Berger committed
242
}
Thomas Zander's avatar
Thomas Zander committed
243

244 245
const KoColorSpace * KoColor::colorSpace() const
{
Thomas Zander's avatar
Thomas Zander committed
246 247 248
    return d->colorSpace;
}

249 250
void KoColor::toXML(QDomDocument& doc, QDomElement& colorElt) const
{
251
    d->colorSpace->colorToXML(d->data, doc, colorElt);
252 253
}

254
void KoColor::setOpacity(quint8 alpha)
255
{
256
    d->colorSpace->setOpacity(d->data, alpha, 1);
257
}
258
void KoColor::setOpacity(qreal alpha)
259
{
260 261 262 263 264 265 266 267 268
    d->colorSpace->setOpacity(d->data, alpha, 1);
}
quint8 KoColor::opacityU8() const
{
    return d->colorSpace->opacityU8(d->data);
}
qreal KoColor::opacityF() const
{
    return d->colorSpace->opacityF(d->data);
269 270
}

Boudewijn Rempt's avatar
Boudewijn Rempt committed
271
KoColor KoColor::fromXML(const QDomElement& elt, const QString & bitDepthId, const QHash<QString, QString> & aliases)
272
{
273
    QString modelId;
274
    if (elt.tagName() == "CMYK") {
275
        modelId = CMYKAColorModelID.id();
276
    } else if (elt.tagName() == "RGB") {
277
        modelId = RGBAColorModelID.id();
278
    } else if (elt.tagName() == "sRGB") {
279
        modelId = RGBAColorModelID.id();
280
    } else if (elt.tagName() == "Lab") {
281
        modelId = LABAColorModelID.id();
282
    } else if (elt.tagName() == "XYZ") {
283
        modelId = XYZAColorModelID.id();
284
    } else if (elt.tagName() == "Gray") {
285
        modelId = GrayAColorModelID.id();
286
    } else if (elt.tagName() == "YCbCr") {
287 288 289
        modelId = YCbCrAColorModelID.id();
    }
    QString profileName;
290 291 292
    if (elt.tagName() != "sRGB") {
        profileName = elt.attribute("space", "");
        if (aliases.contains(profileName)) {
293 294
            profileName = aliases.value(profileName);
        }
295
        if (!KoColorSpaceRegistry::instance()->profileByName(profileName)) {
296
            profileName.clear();
297
        }
298
    }
299 300
    const KoColorSpace* cs = KoColorSpaceRegistry::instance()->colorSpace(modelId, bitDepthId, profileName);
    if (cs == 0) {
301 302
        QList<KoID> list =  KoColorSpaceRegistry::instance()->colorDepthList(modelId, KoColorSpaceRegistry::AllColorSpaces);
        if (!list.empty()) {
303
            cs = KoColorSpaceRegistry::instance()->colorSpace(modelId, list[0].id(), profileName);
304 305
        }
    }
306
    if (cs) {
307
        KoColor c(cs);
308
        cs->colorFromXML(c.data(), elt);
309 310 311 312
        return c;
    } else {
        return KoColor();
    }
313
}