KoColor.cpp 8.42 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

34

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

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

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

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

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

65 66 67

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

71
KoColor::KoColor(const QColor & color, const KoColorSpace * colorSpace)
Thomas Zander's avatar
Thomas Zander committed
72
    : d(new Private())
73 74 75
{
    Q_ASSERT(color.isValid());
    Q_ASSERT(colorSpace);
Cyrille Berger's avatar
Cyrille Berger committed
76
    d->colorSpace = colorSpace;
77

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

81
    d->colorSpace->fromQColor(color, d->data);
82 83
}

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


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

Thomas Zander's avatar
Thomas Zander committed
104
    src.colorSpace()->convertPixelsTo(src.d->data, d->data, colorSpace, 1);
105 106
}

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

KoColor & KoColor::operator=(const KoColor & rhs)
{
120 121
    if (this == &rhs) return *this;

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

Thomas Zander's avatar
Thomas Zander committed
126 127 128
    if (rhs.d->colorSpace && rhs.d->data) {
        d->data = new quint8[d->colorSpace->pixelSize()];
        memcpy(d->data, rhs.d->data, d->colorSpace->pixelSize());
129
    }
130 131 132
    return * this;
}

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

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

145
    if (*d->colorSpace == *cs)
146 147
        return;

Thomas Zander's avatar
Thomas Zander committed
148 149
    quint8 * data = new quint8[cs->pixelSize()];
    memset(data, 0, cs->pixelSize());
150

Thomas Zander's avatar
Thomas Zander committed
151
    d->colorSpace->convertPixelsTo(d->data, data, cs, 1);
152

Thomas Zander's avatar
Thomas Zander committed
153 154 155
    delete [] d->data;
    d->data = data;
    d->colorSpace = cs;
156 157 158
}


159
void KoColor::setColor(quint8 * data, const KoColorSpace * colorSpace)
160
{
Cyrille Berger's avatar
Cyrille Berger committed
161 162
    Q_ASSERT(data);
    Q_ASSERT(colorSpace);
Thomas Zander's avatar
Thomas Zander committed
163 164 165 166
    delete [] d->data;
    d->data = new quint8[colorSpace->pixelSize()];
    memcpy(d->data, data, colorSpace->pixelSize());
    d->colorSpace = colorSpace;
167 168
}

169 170 171
// 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
172
    Q_ASSERT(c);
Thomas Zander's avatar
Thomas Zander committed
173 174
    if (d->colorSpace && d->data) {
        d->colorSpace->toQColor(d->data, c);
175
    }
176 177 178 179 180 181 182 183
}

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

185 186
void KoColor::fromQColor(const QColor& c) const
{
Thomas Zander's avatar
Thomas Zander committed
187 188
    if (d->colorSpace && d->data) {
        d->colorSpace->fromQColor(c, d->data);
189 190 191
    }
}

Thomas Zander's avatar
Thomas Zander committed
192
#ifndef NDEBUG
193 194
void KoColor::dump() const
{
Boudewijn Rempt's avatar
Boudewijn Rempt committed
195
    //dbgPigment <<"KoColor (" << this <<")," << d->colorSpace->id().name() <<"";
Adrian Page's avatar
q3--  
Adrian Page committed
196
    QList<KoChannelInfo *> channels = d->colorSpace->channels();
197

Laurent Montel's avatar
Laurent Montel committed
198 199
    QList<KoChannelInfo *>::const_iterator begin = channels.constBegin();
    QList<KoChannelInfo *>::const_iterator end = channels.constEnd();
200

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

void KoColor::fromKoColor(const KoColor& src)
{
Thomas Zander's avatar
Thomas Zander committed
223
    src.colorSpace()->convertPixelsTo(src.d->data, d->data, colorSpace(), 1);
224
}
Cyrille Berger's avatar
Cyrille Berger committed
225

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

Thomas Zander's avatar
Thomas Zander committed
231 232 233 234 235
quint8 * KoColor::data() {
    return d->data;
}

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

239
const KoColorSpace * KoColor::colorSpace() const {
Thomas Zander's avatar
Thomas Zander committed
240 241 242
    return d->colorSpace;
}

243 244 245 246 247
void KoColor::toXML(QDomDocument& doc, QDomElement& colorElt) const
{
    d->colorSpace->colorToXML( d->data, doc, colorElt);
}

248 249 250 251 252 253
void KoColor::setOpacity(quint8 alpha )
{
    d->colorSpace->setAlpha( d->data, alpha, 1);
}
quint8 KoColor::opacity() const
{
254
    return d->colorSpace->alpha( d->data );
255 256
}

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