Commit 78befd7b authored by Thomas Zander's avatar Thomas Zander

Some API cleanup.

* make kocolor use a dpointer
* better api dox
* put the deprecated macro at the right spot so it will actually do something
* deprecate KoColor::data()  like discussed some time ago.  It violates encapsulation.

svn path=/trunk/koffice/; revision=643426
parent 1efb2fd6
/*
* Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
* Copyright (C) 2007 Thomas Zander <zander@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
......@@ -17,86 +18,99 @@
* Boston, MA 02110-1301, USA.
*/
#include <QColor>
#include "kdebug.h"
#include "KoColor.h"
#include "KoColorProfile.h"
#include "KoColorSpace.h"
#include "KoColorSpaceRegistry.h"
class KoColor::Private {
public:
Private() : data(0), colorSpace(0) {}
~Private() {
delete [] data;
}
quint8 * data;
KoColorSpace * colorSpace;
};
KoColor::KoColor()
: d(new Private())
{
m_colorSpace = KoColorSpaceRegistry::instance()->colorSpace("LABA",0);
m_data = new quint8[m_colorSpace->pixelSize()];
memset(m_data, 0, m_colorSpace->pixelSize());
m_colorSpace->setAlpha(m_data, OPACITY_OPAQUE, 1);
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);
}
KoColor::KoColor(KoColorSpace * colorSpace)
: d(new Private())
{
m_colorSpace = colorSpace;
m_data = new quint8[m_colorSpace->pixelSize()];
memset(m_data, 0, m_colorSpace->pixelSize());
d->colorSpace = colorSpace;
d->data = new quint8[d->colorSpace->pixelSize()];
memset(d->data, 0, d->colorSpace->pixelSize());
}
KoColor::~KoColor()
{
delete [] m_data;
delete d;
}
KoColor::KoColor(const QColor & color, KoColorSpace * colorSpace)
: m_colorSpace(colorSpace)
: d(new Private())
{
d->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);
d->data = new quint8[colorSpace->pixelSize()];
memset(d->data, 0, d->colorSpace->pixelSize());
d->colorSpace->fromQColor(color, OPACITY_OPAQUE, d->data);
}
KoColor::KoColor(const QColor & color, quint8 alpha, KoColorSpace * colorSpace)
: m_colorSpace(colorSpace)
: d(new Private())
{
d->colorSpace = colorSpace;
Q_ASSERT(color.isValid());
Q_ASSERT(colorSpace);
m_data = new quint8[colorSpace->pixelSize()];
memset(m_data, 0, m_colorSpace->pixelSize());
d->data = new quint8[colorSpace->pixelSize()];
memset(d->data, 0, d->colorSpace->pixelSize());
m_colorSpace->fromQColor(color, alpha, m_data);
d->colorSpace->fromQColor(color, alpha, d->data);
}
KoColor::KoColor(const quint8 * data, KoColorSpace * colorSpace)
: m_colorSpace(colorSpace)
: d(new Private())
{
m_data = new quint8[colorSpace->pixelSize()];
memset(m_data, 0, m_colorSpace->pixelSize());
memmove(m_data, data, colorSpace->pixelSize());
d->colorSpace = colorSpace;
d->data = new quint8[colorSpace->pixelSize()];
memset(d->data, 0, d->colorSpace->pixelSize());
memmove(d->data, data, colorSpace->pixelSize());
}
KoColor::KoColor(const KoColor &src, KoColorSpace * colorSpace)
: m_colorSpace(colorSpace)
: d(new Private())
{
m_data = new quint8[colorSpace->pixelSize()];
memset(m_data, 0, m_colorSpace->pixelSize());
d->colorSpace = colorSpace;
d->data = new quint8[colorSpace->pixelSize()];
memset(d->data, 0, d->colorSpace->pixelSize());
src.colorSpace()->convertPixelsTo(src.data(), m_data, colorSpace, 1);
src.colorSpace()->convertPixelsTo(src.d->data, d->data, colorSpace, 1);
}
KoColor::KoColor(const KoColor & rhs)
: d(new Private())
{
if (this == &rhs) return;
m_data = 0;
m_colorSpace = rhs.colorSpace();
if(m_colorSpace && rhs.m_data)
d->colorSpace = rhs.colorSpace();
if(d->colorSpace && rhs.d->data)
{
m_data = new quint8[m_colorSpace->pixelSize()];
memcpy(m_data, rhs.data(), m_colorSpace->pixelSize());
d->data = new quint8[d->colorSpace->pixelSize()];
memcpy(d->data, rhs.d->data, d->colorSpace->pixelSize());
}
}
......@@ -104,56 +118,56 @@ KoColor & KoColor::operator=(const KoColor & rhs)
{
if (this == &rhs) return *this;
delete [] m_data;
m_data = 0;
m_colorSpace = rhs.colorSpace();
delete [] d->data;
d->data = 0;
d->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());
if (rhs.d->colorSpace && rhs.d->data) {
d->data = new quint8[d->colorSpace->pixelSize()];
memcpy(d->data, rhs.d->data, d->colorSpace->pixelSize());
}
return * this;
}
void KoColor::convertTo(KoColorSpace * cs)
{
//kDebug(DBG_AREA_CMS) << "Our colormodel: " << m_colorSpace->id().name()
//kDebug(DBG_AREA_CMS) << "Our colormodel: " << d->colorSpace->id().name()
// << ", new colormodel: " << cs->id().name() << "\n";
if (m_colorSpace == cs)
if (d->colorSpace == cs)
return;
quint8 * m_data2 = new quint8[cs->pixelSize()];
memset(m_data2, 0, cs->pixelSize());
quint8 * data = new quint8[cs->pixelSize()];
memset(data, 0, cs->pixelSize());
m_colorSpace->convertPixelsTo(m_data, m_data2, cs, 1);
d->colorSpace->convertPixelsTo(d->data, data, cs, 1);
delete [] m_data;
m_data = m_data2;
m_colorSpace = cs;
delete [] d->data;
d->data = data;
d->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;
delete [] d->data;
d->data = new quint8[colorSpace->pixelSize()];
memcpy(d->data, data, colorSpace->pixelSize());
d->colorSpace = colorSpace;
}
// To save the user the trouble of doing color->colorSpace()->toQColor(color->data(), &c, &a, profile
void KoColor::toQColor(QColor *c) const
{
if (m_colorSpace && m_data) {
m_colorSpace->toQColor(m_data, c);
if (d->colorSpace && d->data) {
d->colorSpace->toQColor(d->data, c);
}
}
void KoColor::toQColor(QColor *c, quint8 *opacity) const
{
if (m_colorSpace && m_data) {
m_colorSpace->toQColor(m_data, c, opacity);
if (d->colorSpace && d->data) {
d->colorSpace->toQColor(d->data, c, opacity);
}
}
......@@ -166,25 +180,23 @@ QColor KoColor::toQColor() const
void KoColor::fromQColor(const QColor& c) const
{
if (m_colorSpace && m_data) {
m_colorSpace->fromQColor(c, m_data);
if (d->colorSpace && d->data) {
d->colorSpace->fromQColor(c, d->data);
}
}
void KoColor::fromQColor(const QColor& c, quint8 opacity) const
{
if (m_colorSpace && m_data) {
m_colorSpace->fromQColor(c, opacity, m_data);
if (d->colorSpace && d->data) {
d->colorSpace->fromQColor(c, opacity, d->data);
}
}
#ifndef NDEBUG
void KoColor::dump() const
{
//kDebug(DBG_AREA_CMS) << "KoColor (" << this << "), " << m_colorSpace->id().name() << "\n";
Q3ValueVector<KoChannelInfo *> channels = m_colorSpace->channels();
//kDebug(DBG_AREA_CMS) << "KoColor (" << this << "), " << d->colorSpace->id().name() << "\n";
Q3ValueVector<KoChannelInfo *> channels = d->colorSpace->channels();
Q3ValueVector<KoChannelInfo *>::const_iterator begin = channels.begin();
Q3ValueVector<KoChannelInfo *>::const_iterator end = channels.end();
......@@ -195,26 +207,35 @@ void KoColor::dump() const
// 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";
//kDebug(DBG_AREA_CMS) << "Channel (byte): " << ch->name() << ": " << QString().setNum(d->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";
//kDebug(DBG_AREA_CMS) << "Channel (short): " << ch->name() << ": " << QString().setNum(*((const quint16 *)(d->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";
//kDebug(DBG_AREA_CMS) << "Channel (int): " << ch->name() << ": " << QString().setNum(*((const quint32 *)(d->data+ch->pos()))) << "\n";
}
}
}
#endif
void KoColor::fromKoColor(const KoColor& src)
{
src.colorSpace()->convertPixelsTo(src.m_data, m_data, colorSpace(), 1);
src.colorSpace()->convertPixelsTo(src.d->data, d->data, colorSpace(), 1);
}
KoColorProfile * KoColor::profile() const
{
return m_colorSpace->profile();
return d->colorSpace->profile();
}
quint8 * KoColor::data() const {
return d->data;
}
KoColorSpace * KoColor::colorSpace() const {
return d->colorSpace;
}
/*
* Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
* Copyright (C) 2007 Thomas Zander <zander@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
......@@ -58,14 +59,17 @@ public:
/// Copy constructor -- deep copies the colors.
KoColor(const KoColor & rhs);
/// Effective C++, item 11
KoColor &operator=(const KoColor &);
/**
* assignment operator to copy the data from the param color into this one.
* @param other the color we are going to copy
* @return this color
*/
KoColor &operator=(const KoColor &other);
/// For easy memcpy'ing etc.
quint8 * data() const { return m_data; }
KoColorSpace * colorSpace() const { return m_colorSpace; }
/// return the current colorSpace
KoColorSpace * colorSpace() const;
/// return the current profile
KoColorProfile * profile() const;
/// Convert this KoColor to the specified colorspace. If the specified colorspace is the
......@@ -74,29 +78,38 @@ public:
/// Replace the existing color data, and colorspace with the specified data.
void setColor(quint8 * data, KoColorSpace * colorSpace = 0);
/// Convert the color from src and replace the value of the current color with the converted data.
/// Don't convert the color if src and this have the same colorspace.
void fromKoColor(const KoColor& src);
/// To save the user the trouble of doing color->colorSpace()->toQColor(color->data(), &c, &a
void toQColor(QColor *c) const;
/**
* Returns a QColor version of the color after converting it to rgb8.
* This is equivalent to using color->colorSpace()->toQColor(color->data(), &c, &a);
*/
void toQColor(QColor *c, quint8 *opacity) const;
/// a convenience method for the above.
void toQColor(QColor *c) const;
/// a convenience method for the above.
QColor toQColor() const;
// what about making the next two methods static factory methods?
/// Convenient function for converting from a QColor
void fromQColor(const QColor& c) const;
/// Convenient function for converting from a QColor and setting the opacity
void fromQColor(const QColor& c, quint8 opacity) const;
QColor toQColor() const;
/// For easy memcpy'ing etc. WARNING; violates encapsulation.
quint8 * KDE_DEPRECATED data() const;
#ifndef NODEBUG
/// use kDebug calls to print internal info
void dump() const;
#endif
private:
quint8 * m_data;
KoColorSpace * m_colorSpace;
class Private;
Private * const d;
};
Q_DECLARE_METATYPE( KoColor )
......
......@@ -470,7 +470,7 @@ public:
/**
* Mix the colors given their weights and return in dst
* The sum of weights is assumed 255 */
KDE_DEPRECATED void mixColors(const quint8 **colors, const quint8 *weights, quint32 nColors, quint8 *dst) const;
void KDE_DEPRECATED mixColors(const quint8 **colors, const quint8 *weights, quint32 nColors, quint8 *dst) const;
/**
* @return the mix color operation of this colorspace (do not delete it locally, it's deleted by the colorspace).
......@@ -481,7 +481,7 @@ public:
* Convolve the given array of pointers to pixels and return the result
* in dst. The kernel values are clamped between -128 and 128
*/
KDE_DEPRECATED void convolveColors(quint8** colors, qint32* kernelValues, KoChannelInfo::enumChannelFlags channelFlags, quint8 *dst, qint32 factor, qint32 offset, qint32 nPixels) const;
void KDE_DEPRECATED convolveColors(quint8** colors, qint32* kernelValues, KoChannelInfo::enumChannelFlags channelFlags, quint8 *dst, qint32 factor, qint32 offset, qint32 nPixels) const;
/**
* @return the convolution operation of this colorspace (do not delete it locally, it's deleted by the colorspace).
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment