KoColor.cpp 6.61 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
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7 8
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
9
 *
10 11 12
 * 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
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16 17 18 19
 * 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.
*/
20
#include <QColor>
Thomas Zander's avatar
Thomas Zander committed
21

22
#include "kdebug.h"
23
#include "KoColor.h"
24 25
#include "KoColorProfile.h"
#include "KoColorSpace.h"
26
#include "KoColorSpaceRegistry.h"
27

Thomas Zander's avatar
Thomas Zander committed
28 29 30 31 32 33 34 35 36 37
class KoColor::Private {
public:
    Private() : data(0), colorSpace(0) {}
    ~Private() {
        delete [] data;
    }
    quint8 * data;
    KoColorSpace * colorSpace;
};

38
KoColor::KoColor()
Thomas Zander's avatar
Thomas Zander committed
39
    : d(new Private())
40
{
Thomas Zander's avatar
Thomas Zander committed
41 42 43 44
    d->colorSpace = KoColorSpaceRegistry::instance()->colorSpace("LABA",0);
    d->data = new quint8[d->colorSpace->pixelSize()];
    memset(d->data, 0, d->colorSpace->pixelSize());
    d->colorSpace->setAlpha(d->data, OPACITY_OPAQUE, 1);
45
}
46
KoColor::KoColor(KoColorSpace * colorSpace)
Thomas Zander's avatar
Thomas Zander committed
47
    : d(new Private())
48
{
Thomas Zander's avatar
Thomas Zander committed
49 50 51
    d->colorSpace = colorSpace;
    d->data = new quint8[d->colorSpace->pixelSize()];
    memset(d->data, 0, d->colorSpace->pixelSize());
52 53
}

54 55 56

KoColor::~KoColor()
{
Thomas Zander's avatar
Thomas Zander committed
57
    delete d;
58 59
}

60
KoColor::KoColor(const QColor & color, KoColorSpace * colorSpace)
Thomas Zander's avatar
Thomas Zander committed
61
    : d(new Private())
62
{
Thomas Zander's avatar
Thomas Zander committed
63
    d->colorSpace = colorSpace;
64 65 66
    Q_ASSERT(color.isValid());
    Q_ASSERT(colorSpace);

Thomas Zander's avatar
Thomas Zander committed
67 68 69 70
    d->data = new quint8[colorSpace->pixelSize()];
    memset(d->data, 0, d->colorSpace->pixelSize());

    d->colorSpace->fromQColor(color, OPACITY_OPAQUE, d->data);
71 72 73 74
}


KoColor::KoColor(const QColor & color, quint8 alpha, KoColorSpace * colorSpace)
Thomas Zander's avatar
Thomas Zander committed
75
    : d(new Private())
76
{
Thomas Zander's avatar
Thomas Zander committed
77
    d->colorSpace = colorSpace;
78 79
    Q_ASSERT(color.isValid());
    Q_ASSERT(colorSpace);
Thomas Zander's avatar
Thomas Zander committed
80 81
    d->data = new quint8[colorSpace->pixelSize()];
    memset(d->data, 0, d->colorSpace->pixelSize());
82

Thomas Zander's avatar
Thomas Zander committed
83
    d->colorSpace->fromQColor(color, alpha, d->data);
84 85 86
}

KoColor::KoColor(const quint8 * data, KoColorSpace * colorSpace)
Thomas Zander's avatar
Thomas Zander committed
87
    : d(new Private())
88
{
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, KoColorSpace * colorSpace)
Thomas Zander's avatar
Thomas Zander committed
97
    : d(new Private())
98
{
Thomas Zander's avatar
Thomas Zander committed
99 100 101
    d->colorSpace = colorSpace;
    d->data = new quint8[colorSpace->pixelSize()];
    memset(d->data, 0, d->colorSpace->pixelSize());
102

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

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

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

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

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

132 133
void KoColor::convertTo(KoColorSpace * cs)
{
Thomas Zander's avatar
Thomas Zander committed
134
    //kDebug(DBG_AREA_CMS) << "Our colormodel: " << d->colorSpace->id().name()
135 136
    //      << ", new colormodel: " << cs->id().name() << "\n";

Thomas Zander's avatar
Thomas Zander committed
137
    if (d->colorSpace == cs)
138 139
        return;

Thomas Zander's avatar
Thomas Zander committed
140 141
    quint8 * data = new quint8[cs->pixelSize()];
    memset(data, 0, cs->pixelSize());
142

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

Thomas Zander's avatar
Thomas Zander committed
145 146 147
    delete [] d->data;
    d->data = data;
    d->colorSpace = cs;
148 149 150 151 152
}


void KoColor::setColor(quint8 * data, KoColorSpace * colorSpace)
{
Thomas Zander's avatar
Thomas Zander committed
153 154 155 156
    delete [] d->data;
    d->data = new quint8[colorSpace->pixelSize()];
    memcpy(d->data, data, colorSpace->pixelSize());
    d->colorSpace = colorSpace;
157 158
}

159 160 161
// To save the user the trouble of doing color->colorSpace()->toQColor(color->data(), &c, &a, profile
void KoColor::toQColor(QColor *c) const
{
Thomas Zander's avatar
Thomas Zander committed
162 163
    if (d->colorSpace && d->data) {
        d->colorSpace->toQColor(d->data, c);
164
    }
165 166 167 168
}

void KoColor::toQColor(QColor *c, quint8 *opacity) const
{
Thomas Zander's avatar
Thomas Zander committed
169 170
    if (d->colorSpace && d->data) {
        d->colorSpace->toQColor(d->data, c, opacity);
171
    }
172 173 174 175 176 177 178 179
}

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

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

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

Thomas Zander's avatar
Thomas Zander committed
195
#ifndef NDEBUG
196 197
void KoColor::dump() const
{
Thomas Zander's avatar
Thomas Zander committed
198
    //kDebug(DBG_AREA_CMS) << "KoColor (" << this << "), " << d->colorSpace->id().name() << "\n";
Adrian Page's avatar
q3--  
Adrian Page committed
199
    QList<KoChannelInfo *> channels = d->colorSpace->channels();
200

Adrian Page's avatar
q3--  
Adrian Page committed
201 202
    QList<KoChannelInfo *>::const_iterator begin = channels.begin();
    QList<KoChannelInfo *>::const_iterator end = channels.end();
203

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

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

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

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

KoColorSpace * KoColor::colorSpace() const {
    return d->colorSpace;
}