KoColor.cpp 9.82 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, KoColorConversionTransformation::Intent renderingIntent, KoColorConversionTransformation::ConversionFlags conversionFlags)
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, renderingIntent, conversionFlags);
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 163 164 165 166
void KoColor::convertTo(const KoColorSpace * cs)
{
    convertTo(cs,
              KoColorConversionTransformation::InternalRenderingIntent,
              KoColorConversionTransformation::InternalConversionFlags);
}
167

168
void KoColor::setColor(const quint8 * data, const KoColorSpace * colorSpace)
169
{
Cyrille Berger's avatar
Cyrille Berger committed
170 171
    Q_ASSERT(data);
    Q_ASSERT(colorSpace);
172 173 174 175 176
    if(d->colorSpace->pixelSize() != colorSpace->pixelSize())
    {
        delete [] d->data;
        d->data = new quint8[colorSpace->pixelSize()];
    }
Thomas Zander's avatar
Thomas Zander committed
177
    memcpy(d->data, data, colorSpace->pixelSize());
178
    d->colorSpace = KoColorSpaceRegistry::instance()->permanentColorspace(colorSpace);
179 180
}

181 182 183
// 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
184
    Q_ASSERT(c);
Thomas Zander's avatar
Thomas Zander committed
185 186
    if (d->colorSpace && d->data) {
        d->colorSpace->toQColor(d->data, c);
187
    }
188 189 190 191 192 193 194 195
}

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

197 198
void KoColor::fromQColor(const QColor& c) const
{
Thomas Zander's avatar
Thomas Zander committed
199 200
    if (d->colorSpace && d->data) {
        d->colorSpace->fromQColor(c, d->data);
201 202 203
    }
}

Thomas Zander's avatar
Thomas Zander committed
204
#ifndef NDEBUG
205 206
void KoColor::dump() const
{
Boudewijn Rempt's avatar
Boudewijn Rempt committed
207
    //dbgPigment <<"KoColor (" << this <<")," << d->colorSpace->id().name() <<"";
Adrian Page's avatar
q3--  
Adrian Page committed
208
    QList<KoChannelInfo *> channels = d->colorSpace->channels();
209

Laurent Montel's avatar
Laurent Montel committed
210 211
    QList<KoChannelInfo *>::const_iterator begin = channels.constBegin();
    QList<KoChannelInfo *>::const_iterator end = channels.constEnd();
212

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

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

235
const KoColorProfile *  KoColor::profile() const
Cyrille Berger's avatar
Cyrille Berger committed
236
{
Thomas Zander's avatar
Thomas Zander committed
237 238 239
    return d->colorSpace->profile();
}

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

245 246
const quint8 * KoColor::data() const
{
Thomas Zander's avatar
Thomas Zander committed
247
    return d->data;
Cyrille Berger's avatar
Cyrille Berger committed
248
}
Thomas Zander's avatar
Thomas Zander committed
249

250 251
const KoColorSpace * KoColor::colorSpace() const
{
Thomas Zander's avatar
Thomas Zander committed
252 253 254
    return d->colorSpace;
}

255 256
void KoColor::toXML(QDomDocument& doc, QDomElement& colorElt) const
{
257
    d->colorSpace->colorToXML(d->data, doc, colorElt);
258 259
}

260
void KoColor::setOpacity(quint8 alpha)
261
{
262
    d->colorSpace->setOpacity(d->data, alpha, 1);
263
}
264
void KoColor::setOpacity(qreal alpha)
265
{
266 267 268 269 270 271 272 273 274
    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);
275 276
}

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