KoColor.cpp 6.1 KB
Newer Older
1 2 3
/*
 *  Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
 *
4
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6 7
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
8
 *
9 10 11
 * 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
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15 16 17 18
 * 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.
*/
19 20
#include <QColor>
 
21
#include "kdebug.h"
22
#include "KoColor.h"
23 24
#include "KoColorProfile.h"
#include "KoColorSpace.h"
25
#include "KoColorSpaceRegistry.h"
26 27 28

KoColor::KoColor()
{
29
    m_colorSpace = KoColorSpaceRegistry::instance()->colorSpace("LABA",0);
30 31
    m_data = new quint8[m_colorSpace->pixelSize()];
    memset(m_data, 0, m_colorSpace->pixelSize());
32
    m_colorSpace->setAlpha(m_data, OPACITY_OPAQUE, 1);
33
}
34 35 36 37 38 39 40
KoColor::KoColor(KoColorSpace * colorSpace)
{
    m_colorSpace = colorSpace;
    m_data = new quint8[m_colorSpace->pixelSize()];
    memset(m_data, 0, m_colorSpace->pixelSize());
}

41 42 43 44 45 46

KoColor::~KoColor()
{
    delete [] m_data;
}

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
KoColor::KoColor(const QColor & color, KoColorSpace * colorSpace)
    : m_colorSpace(colorSpace)
{
    Q_ASSERT(color.isValid());
    Q_ASSERT(colorSpace);
    
    m_data = new quint8[colorSpace->pixelSize()];
    memset(m_data, 0, m_colorSpace->pixelSize());

    m_colorSpace->fromQColor(color, OPACITY_OPAQUE, m_data);
}


KoColor::KoColor(const QColor & color, quint8 alpha, KoColorSpace * colorSpace)
    : m_colorSpace(colorSpace)
{
    Q_ASSERT(color.isValid());
    Q_ASSERT(colorSpace);
    m_data = new quint8[colorSpace->pixelSize()];
    memset(m_data, 0, m_colorSpace->pixelSize());

    m_colorSpace->fromQColor(color, alpha, m_data);
}

KoColor::KoColor(const quint8 * data, KoColorSpace * colorSpace)
    : m_colorSpace(colorSpace)
{

    m_data = new quint8[colorSpace->pixelSize()];
    memset(m_data, 0, m_colorSpace->pixelSize());
    memmove(m_data, data, colorSpace->pixelSize());
}


KoColor::KoColor(const KoColor &src, KoColorSpace * colorSpace)
    : m_colorSpace(colorSpace)
{
    m_data = new quint8[colorSpace->pixelSize()];
    memset(m_data, 0, m_colorSpace->pixelSize());

    src.colorSpace()->convertPixelsTo(src.data(), m_data, colorSpace, 1);
}

90 91 92 93
KoColor::KoColor(const KoColor & rhs)
{
    if (this == &rhs) return;

94
    m_data = 0;
95
    m_colorSpace = rhs.colorSpace();
96 97 98 99 100
    if(m_colorSpace && rhs.m_data)
    {
        m_data = new quint8[m_colorSpace->pixelSize()];
        memcpy(m_data, rhs.data(), m_colorSpace->pixelSize());
    }
101 102 103 104
}

KoColor & KoColor::operator=(const KoColor & rhs)
{
105 106
    if (this == &rhs) return *this;

107 108 109 110 111 112 113 114
    delete [] m_data;
    m_data = 0;
    m_colorSpace = rhs.colorSpace();

    if (rhs.m_colorSpace && rhs.m_data) {
        m_data = new quint8[m_colorSpace->pixelSize()];
        memcpy(m_data, rhs.m_data, m_colorSpace->pixelSize());
    }
115 116 117
    return * this;
}

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
void KoColor::convertTo(KoColorSpace * cs)
{
    //kDebug(DBG_AREA_CMS) << "Our colormodel: " << m_colorSpace->id().name()
    //      << ", new colormodel: " << cs->id().name() << "\n";

    if (m_colorSpace == cs)
        return;

    quint8 * m_data2 = new quint8[cs->pixelSize()];
    memset(m_data2, 0, cs->pixelSize());

    m_colorSpace->convertPixelsTo(m_data, m_data2, cs, 1);

    delete [] m_data;
    m_data = m_data2;
    m_colorSpace = cs;
}


void KoColor::setColor(quint8 * data, KoColorSpace * colorSpace)
{
    delete [] m_data;
    m_data = new quint8[colorSpace->pixelSize()];
    memcpy(m_data, data, colorSpace->pixelSize());
    m_colorSpace = colorSpace;
}

145 146 147
// To save the user the trouble of doing color->colorSpace()->toQColor(color->data(), &c, &a, profile
void KoColor::toQColor(QColor *c) const
{
148
    if (m_colorSpace && m_data) {
149
        m_colorSpace->toQColor(m_data, c);
150
    }
151 152 153 154
}

void KoColor::toQColor(QColor *c, quint8 *opacity) const
{
155
    if (m_colorSpace && m_data) {
156
        m_colorSpace->toQColor(m_data, c, opacity);
157
    }
158 159 160 161 162 163 164 165
}

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

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
void KoColor::fromQColor(const QColor& c) const
{
    if (m_colorSpace && m_data) {
        m_colorSpace->fromQColor(c, m_data);
    }

}

void KoColor::fromQColor(const QColor& c, quint8 opacity) const
{
    if (m_colorSpace && m_data) {
        m_colorSpace->fromQColor(c, opacity, m_data);
    }

}

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
void KoColor::dump() const
{

    //kDebug(DBG_AREA_CMS) << "KoColor (" << this << "), " << m_colorSpace->id().name() << "\n";
    Q3ValueVector<KoChannelInfo *> channels = m_colorSpace->channels();

    Q3ValueVector<KoChannelInfo *>::const_iterator begin = channels.begin();
    Q3ValueVector<KoChannelInfo *>::const_iterator end = channels.end();

    for (Q3ValueVector<KoChannelInfo *>::const_iterator it = begin; it != end; ++it)
    {
        KoChannelInfo * ch = (*it);
        // XXX: setNum always takes a byte.
        if (ch->size() == sizeof(quint8)) {
            // Byte
            //kDebug(DBG_AREA_CMS) << "Channel (byte): " << ch->name() << ": " << QString().setNum(m_data[ch->pos()]) << "\n";
        }
        else if (ch->size() == sizeof(quint16)) {
            // Short (may also by an nvidia half)
            //kDebug(DBG_AREA_CMS) << "Channel (short): " << ch->name() << ": " << QString().setNum(*((const quint16 *)(m_data+ch->pos())))  << "\n";
        }
        else if (ch->size() == sizeof(quint32)) {
            // Integer (may also be float... Find out how to distinguish these!)
            //kDebug(DBG_AREA_CMS) << "Channel (int): " << ch->name() << ": " << QString().setNum(*((const quint32 *)(m_data+ch->pos())))  << "\n";
        }
    }

}
211 212 213

void KoColor::fromKoColor(const KoColor& src)
{
214
    src.colorSpace()->convertPixelsTo(src.m_data, m_data, colorSpace(), 1);
215
}
Cyrille Berger's avatar
Cyrille Berger committed
216 217 218 219 220

KoColorProfile *  KoColor::profile() const
{
    return m_colorSpace->profile();
}