F:/KPlato/koffice/libs/pigment/KoColor.cpp

Aller à la documentation de ce fichier.
00001 /*
00002  *  Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this library; see the file COPYING.LIB.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 #include <QColor>
00020  
00021 #include "kdebug.h"
00022 #include "KoColor.h"
00023 #include "KoColorProfile.h"
00024 #include "KoColorSpace.h"
00025 #include "KoColorSpaceRegistry.h"
00026 
00027 KoColor::KoColor()
00028 {
00029     m_colorSpace = KoColorSpaceRegistry::instance()->colorSpace("LABA",0);
00030     m_data = new quint8[m_colorSpace->pixelSize()];
00031     memset(m_data, 0, m_colorSpace->pixelSize());
00032     m_colorSpace->setAlpha(m_data, OPACITY_OPAQUE, 1);
00033 }
00034 
00035 KoColor::~KoColor()
00036 {
00037     delete [] m_data;
00038 }
00039 
00040 KoColor::KoColor(const QColor & color, KoColorSpace * colorSpace)
00041     : m_colorSpace(colorSpace)
00042 {
00043     Q_ASSERT(color.isValid());
00044     Q_ASSERT(colorSpace);
00045     
00046     m_data = new quint8[colorSpace->pixelSize()];
00047     memset(m_data, 0, m_colorSpace->pixelSize());
00048 
00049     m_colorSpace->fromQColor(color, OPACITY_OPAQUE, m_data);
00050 }
00051 
00052 
00053 KoColor::KoColor(const QColor & color, quint8 alpha, KoColorSpace * colorSpace)
00054     : m_colorSpace(colorSpace)
00055 {
00056     Q_ASSERT(color.isValid());
00057     Q_ASSERT(colorSpace);
00058     m_data = new quint8[colorSpace->pixelSize()];
00059     memset(m_data, 0, m_colorSpace->pixelSize());
00060 
00061     m_colorSpace->fromQColor(color, alpha, m_data);
00062 }
00063 
00064 KoColor::KoColor(const quint8 * data, KoColorSpace * colorSpace)
00065     : m_colorSpace(colorSpace)
00066 {
00067 
00068     m_data = new quint8[colorSpace->pixelSize()];
00069     memset(m_data, 0, m_colorSpace->pixelSize());
00070     memmove(m_data, data, colorSpace->pixelSize());
00071 }
00072 
00073 
00074 KoColor::KoColor(const KoColor &src, KoColorSpace * colorSpace)
00075     : m_colorSpace(colorSpace)
00076 {
00077     m_data = new quint8[colorSpace->pixelSize()];
00078     memset(m_data, 0, m_colorSpace->pixelSize());
00079 
00080     src.colorSpace()->convertPixelsTo(src.data(), m_data, colorSpace, 1);
00081 }
00082 
00083 KoColor::KoColor(const KoColor & rhs)
00084 {
00085     if (this == &rhs) return;
00086 
00087     m_data = 0;
00088     m_colorSpace = rhs.colorSpace();
00089     if(m_colorSpace && rhs.m_data)
00090     {
00091         m_data = new quint8[m_colorSpace->pixelSize()];
00092         memcpy(m_data, rhs.data(), m_colorSpace->pixelSize());
00093     }
00094 }
00095 
00096 KoColor & KoColor::operator=(const KoColor & rhs)
00097 {
00098     if (this == &rhs) return *this;
00099 
00100     delete [] m_data;
00101     m_data = 0;
00102     m_colorSpace = rhs.colorSpace();
00103 
00104     if (rhs.m_colorSpace && rhs.m_data) {
00105         m_data = new quint8[m_colorSpace->pixelSize()];
00106         memcpy(m_data, rhs.m_data, m_colorSpace->pixelSize());
00107     }
00108     return * this;
00109 }
00110 
00111 void KoColor::convertTo(KoColorSpace * cs)
00112 {
00113     //kDebug(DBG_AREA_CMS) << "Our colormodel: " << m_colorSpace->id().name()
00114     //      << ", new colormodel: " << cs->id().name() << "\n";
00115 
00116     if (m_colorSpace == cs)
00117         return;
00118 
00119     quint8 * m_data2 = new quint8[cs->pixelSize()];
00120     memset(m_data2, 0, cs->pixelSize());
00121 
00122     m_colorSpace->convertPixelsTo(m_data, m_data2, cs, 1);
00123 
00124     delete [] m_data;
00125     m_data = m_data2;
00126     m_colorSpace = cs;
00127 }
00128 
00129 
00130 void KoColor::setColor(quint8 * data, KoColorSpace * colorSpace)
00131 {
00132     delete [] m_data;
00133     m_data = new quint8[colorSpace->pixelSize()];
00134     memcpy(m_data, data, colorSpace->pixelSize());
00135     m_colorSpace = colorSpace;
00136 }
00137 
00138 // To save the user the trouble of doing color->colorSpace()->toQColor(color->data(), &c, &a, profile
00139 void KoColor::toQColor(QColor *c) const
00140 {
00141     if (m_colorSpace && m_data) {
00142         // XXX (bsar): There must be a better way, but I'm getting hopelessly confused about constness by now
00143         KoColorSpace * cs(const_cast<KoColorSpace*>(m_colorSpace));
00144 
00145         cs->toQColor(m_data, c);
00146     }
00147 }
00148 
00149 void KoColor::toQColor(QColor *c, quint8 *opacity) const
00150 {
00151     if (m_colorSpace && m_data) {
00152         // XXX (bsar): There must be a better way, but I'm getting hopelessly confused about constness by now
00153         KoColorSpace * cs(const_cast<KoColorSpace*>(m_colorSpace));
00154         cs->toQColor(m_data, c, opacity);
00155     }
00156 }
00157 
00158 QColor KoColor::toQColor() const
00159 {
00160     QColor c;
00161     toQColor(&c);
00162     return c;
00163 }
00164 
00165 void KoColor::dump() const
00166 {
00167 
00168     //kDebug(DBG_AREA_CMS) << "KoColor (" << this << "), " << m_colorSpace->id().name() << "\n";
00169     Q3ValueVector<KoChannelInfo *> channels = m_colorSpace->channels();
00170 
00171     Q3ValueVector<KoChannelInfo *>::const_iterator begin = channels.begin();
00172     Q3ValueVector<KoChannelInfo *>::const_iterator end = channels.end();
00173 
00174     for (Q3ValueVector<KoChannelInfo *>::const_iterator it = begin; it != end; ++it)
00175     {
00176         KoChannelInfo * ch = (*it);
00177         // XXX: setNum always takes a byte.
00178         if (ch->size() == sizeof(quint8)) {
00179             // Byte
00180             //kDebug(DBG_AREA_CMS) << "Channel (byte): " << ch->name() << ": " << QString().setNum(m_data[ch->pos()]) << "\n";
00181         }
00182         else if (ch->size() == sizeof(quint16)) {
00183             // Short (may also by an nvidia half)
00184             //kDebug(DBG_AREA_CMS) << "Channel (short): " << ch->name() << ": " << QString().setNum(*((const quint16 *)(m_data+ch->pos())))  << "\n";
00185         }
00186         else if (ch->size() == sizeof(quint32)) {
00187             // Integer (may also be float... Find out how to distinguish these!)
00188             //kDebug(DBG_AREA_CMS) << "Channel (int): " << ch->name() << ": " << QString().setNum(*((const quint32 *)(m_data+ch->pos())))  << "\n";
00189         }
00190     }
00191 
00192 }

Généré le Wed Nov 22 23:41:13 2006 pour KPlato par  doxygen 1.5.1-p1