KoColor.cpp 5.75 KB
Newer Older
1 2 3
/*
 *  Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
 *
4 5 6 7
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * 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 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
 * Library General Public License for more details.
13
 *
14 15 16 17 18
 * You should have received a copy of the GNU Library General Public License
 * 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
}

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

40 41 42 43 44 45 46 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
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);
}

83 84 85 86
KoColor::KoColor(const KoColor & rhs)
{
    if (this == &rhs) return;

87
    m_data = 0;
88
    m_colorSpace = rhs.colorSpace();
89 90 91 92 93
    if(m_colorSpace && rhs.m_data)
    {
        m_data = new quint8[m_colorSpace->pixelSize()];
        memcpy(m_data, rhs.data(), m_colorSpace->pixelSize());
    }
94 95 96 97
}

KoColor & KoColor::operator=(const KoColor & rhs)
{
98 99
    if (this == &rhs) return *this;

100 101 102 103 104 105 106 107
    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());
    }
108 109 110
    return * this;
}

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
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;
}

138 139 140
// To save the user the trouble of doing color->colorSpace()->toQColor(color->data(), &c, &a, profile
void KoColor::toQColor(QColor *c) const
{
141 142 143 144 145 146
    if (m_colorSpace && m_data) {
        // XXX (bsar): There must be a better way, but I'm getting hopelessly confused about constness by now
        KoColorSpace * cs(const_cast<KoColorSpace*>(m_colorSpace));

        cs->toQColor(m_data, c);
    }
147 148 149 150
}

void KoColor::toQColor(QColor *c, quint8 *opacity) const
{
151 152 153 154 155
    if (m_colorSpace && m_data) {
        // XXX (bsar): There must be a better way, but I'm getting hopelessly confused about constness by now
        KoColorSpace * cs(const_cast<KoColorSpace*>(m_colorSpace));
        cs->toQColor(m_data, c, opacity);
    }
156 157 158 159 160 161 162 163
}

QColor KoColor::toQColor() const
{
    QColor c;
    toQColor(&c);
    return c;
}
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

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";
        }
    }

}