kis_color.cpp
1/*
2 * Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18#include <tqcolor.h>
19
20#include "kdebug.h"
21#include "kis_debug_areas.h"
22#include "kis_color.h"
23#include "kis_profile.h"
24#include "kis_colorspace.h"
25#include "kis_colorspace_factory_registry.h"
26
28{
29 m_data = 0;
30 m_colorSpace = 0;
31}
32
33KisColor::~KisColor()
34{
35 delete [] m_data;
36}
37
38KisColor::KisColor(const TQColor & color, KisColorSpace * colorSpace)
39 : m_colorSpace(colorSpace)
40{
41 Q_ASSERT(color.isValid());
42 Q_ASSERT(colorSpace);
43
44 m_data = new TQ_UINT8[colorSpace->pixelSize()];
45 memset(m_data, 0, m_colorSpace->pixelSize());
46
47 m_colorSpace->fromTQColor(color, OPACITY_OPAQUE, m_data);
48}
49
50
51KisColor::KisColor(const TQColor & color, TQ_UINT8 alpha, KisColorSpace * colorSpace)
52 : m_colorSpace(colorSpace)
53{
54 Q_ASSERT(color.isValid());
55 Q_ASSERT(colorSpace);
56 m_data = new TQ_UINT8[colorSpace->pixelSize()];
57 memset(m_data, 0, m_colorSpace->pixelSize());
58
59 m_colorSpace->fromTQColor(color, alpha, m_data);
60}
61
62KisColor::KisColor(const TQ_UINT8 * data, KisColorSpace * colorSpace)
63 : m_colorSpace(colorSpace)
64{
65
66 m_data = new TQ_UINT8[colorSpace->pixelSize()];
67 memset(m_data, 0, m_colorSpace->pixelSize());
68 memmove(m_data, data, colorSpace->pixelSize());
69}
70
71
72KisColor::KisColor(const KisColor &src, KisColorSpace * colorSpace)
73 : m_colorSpace(colorSpace)
74{
75 m_data = new TQ_UINT8[colorSpace->pixelSize()];
76 memset(m_data, 0, m_colorSpace->pixelSize());
77
78 src.colorSpace()->convertPixelsTo(src.data(), m_data, colorSpace, 1);
79}
80
82{
83 if (this == &rhs) return;
84
85 m_colorSpace = rhs.colorSpace();
86 m_data = new TQ_UINT8[m_colorSpace->pixelSize()];
87 memset(m_data, 0, m_colorSpace->pixelSize());
88 memcpy(m_data, rhs.data(), m_colorSpace->pixelSize());
89}
90
92{
93 delete [] m_data;
94 m_data = 0;
95 m_colorSpace = rhs.colorSpace();
96
97 if (rhs.m_colorSpace && rhs.m_data) {
98 m_data = new TQ_UINT8[m_colorSpace->pixelSize()];
99 memcpy(m_data, rhs.m_data, m_colorSpace->pixelSize());
100 }
101 return * this;
102}
103
105{
106 //kdDebug(DBG_AREA_CMS) << "Our colormodel: " << m_colorSpace->id().name()
107 // << ", new colormodel: " << cs->id().name() << "\n";
108
109 if (m_colorSpace == cs)
110 return;
111
112 TQ_UINT8 * m_data2 = new TQ_UINT8[cs->pixelSize()];
113 memset(m_data2, 0, cs->pixelSize());
114
115 m_colorSpace->convertPixelsTo(m_data, m_data2, cs, 1);
116
117 delete [] m_data;
118 m_data = m_data2;
119 m_colorSpace = cs;
120}
121
122
123void KisColor::setColor(TQ_UINT8 * data, KisColorSpace * colorSpace)
124{
125 delete [] m_data;
126 m_data = new TQ_UINT8[colorSpace->pixelSize()];
127 memcpy(m_data, data, colorSpace->pixelSize());
128 m_colorSpace = colorSpace;
129}
130
131// To save the user the trouble of doing color->colorSpace()->toTQColor(color->data(), &c, &a, profile
132void KisColor::toTQColor(TQColor *c) const
133{
134 if (m_colorSpace && m_data) {
135 // XXX (bsar): There must be a better way, but I'm getting hopelessly confused about constness by now
136 KisColorSpace * cs(const_cast<KisColorSpace*>(m_colorSpace));
137
138 cs->toTQColor(m_data, c);
139 }
140}
141
142void KisColor::toTQColor(TQColor *c, TQ_UINT8 *opacity) const
143{
144 if (m_colorSpace && m_data) {
145 // XXX (bsar): There must be a better way, but I'm getting hopelessly confused about constness by now
146 KisColorSpace * cs(const_cast<KisColorSpace*>(m_colorSpace));
147 cs->toTQColor(m_data, c, opacity);
148 }
149}
150
151TQColor KisColor::toTQColor() const
152{
153 TQColor c;
154 toTQColor(&c);
155 return c;
156}
157
158void KisColor::dump() const
159{
160
161 //kdDebug(DBG_AREA_CMS) << "KisColor (" << this << "), " << m_colorSpace->id().name() << "\n";
162 TQValueVector<KisChannelInfo *> channels = m_colorSpace->channels();
163
164 TQValueVector<KisChannelInfo *>::const_iterator begin = channels.begin();
165 TQValueVector<KisChannelInfo *>::const_iterator end = channels.end();
166
167 for (TQValueVector<KisChannelInfo *>::const_iterator it = begin; it != end; ++it)
168 {
169 KisChannelInfo * ch = (*it);
170 // XXX: setNum always takes a byte.
171 if (ch->size() == sizeof(TQ_UINT8)) {
172 // Byte
173 //kdDebug(DBG_AREA_CMS) << "Channel (byte): " << ch->name() << ": " << TQString().setNum(m_data[ch->pos()]) << "\n";
174 }
175 else if (ch->size() == sizeof(TQ_UINT16)) {
176 // Short (may also by an nvidia half)
177 //kdDebug(DBG_AREA_CMS) << "Channel (short): " << ch->name() << ": " << TQString().setNum(*((const TQ_UINT16 *)(m_data+ch->pos()))) << "\n";
178 }
179 else if (ch->size() == sizeof(TQ_UINT32)) {
180 // Integer (may also be float... Find out how to distinguish these!)
181 //kdDebug(DBG_AREA_CMS) << "Channel (int): " << ch->name() << ": " << TQString().setNum(*((const TQ_UINT32 *)(m_data+ch->pos()))) << "\n";
182 }
183 }
184
185}
This class gives some basic information about a channel, that is, one of the components that makes up...
TQ_INT32 size() const
returns the number of bytes this channel takes
A colorspace is the definition of a certain color model in Chalk.
virtual void toTQColor(const TQ_UINT8 *src, TQColor *c, KisProfile *profile=0)=0
The toTQColor methods take a byte array that is at least pixelSize() long and converts the contents t...
virtual void fromTQColor(const TQColor &c, TQ_UINT8 *dst, KisProfile *profile=0)=0
The fromTQColor methods take a given color defined as an RGB TQColor and fills a byte array with the ...
virtual bool convertPixelsTo(const TQ_UINT8 *src, TQ_UINT8 *dst, KisColorSpace *dstColorSpace, TQ_UINT32 numPixels, TQ_INT32 renderingIntent=INTENT_PERCEPTUAL)=0
Convert a byte array of srcLen pixels *src to the specified color space and put the converted bytes i...
virtual TQValueVector< KisChannelInfo * > channels() const =0
Return a vector describing all the channels this color model has.
virtual TQ_UINT32 pixelSize() const =0
The size in bytes of a single pixel in this color model.
A KisColor describes a color in a certain colorspace.
Definition kis_color.h:33
void toTQColor(TQColor *c) const
To save the user the trouble of doing color->colorSpace()->toTQColor(color->data(),...
KisColor & operator=(const KisColor &)
Effective C++, item 11.
Definition kis_color.cpp:91
KisColor()
Create an empty KisColor. It will be valid, but also black and transparent.
Definition kis_color.cpp:27
void convertTo(KisColorSpace *cs)
Convert this KisColor to the specified colorspace.
void setColor(TQ_UINT8 *data, KisColorSpace *colorSpace=0)
Replace the existing color data, and colorspace with the specified data.
TQ_UINT8 * data() const
For easy memcpy'ing etc.
Definition kis_color.h:62