1 /*
2 * Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
3 *
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 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "LabColorSpace.h"
21
22 #include <QDomElement>
23
24 #include <klocalizedstring.h>
25
26 #include "../compositeops/KoCompositeOps.h"
27 #include <KoColorConversions.h>
28 #include <kis_dom_utils.h>
29
LabU16ColorSpace(const QString & name,KoColorProfile * p)30 LabU16ColorSpace::LabU16ColorSpace(const QString &name, KoColorProfile *p)
31 : LcmsColorSpace<KoLabU16Traits>(colorSpaceId(), name, TYPE_LABA_16, cmsSigLabData, p)
32 {
33 addChannel(new KoChannelInfo(i18nc("Lightness value in Lab color model", "Lightness"), 0 * sizeof(quint16), 0, KoChannelInfo::COLOR, KoChannelInfo::UINT16, sizeof(quint16), QColor(100, 100, 100)));
34 addChannel(new KoChannelInfo(i18n("a*"), 1 * sizeof(quint16), 1, KoChannelInfo::COLOR, KoChannelInfo::UINT16, sizeof(quint16), QColor(150, 150, 150)));
35 addChannel(new KoChannelInfo(i18n("b*"), 2 * sizeof(quint16), 2, KoChannelInfo::COLOR, KoChannelInfo::UINT16, sizeof(quint16), QColor(200, 200, 200)));
36 addChannel(new KoChannelInfo(i18n("Alpha"), 3 * sizeof(quint16), 3, KoChannelInfo::ALPHA, KoChannelInfo::UINT16, sizeof(quint16)));
37
38 init();
39
40 addStandardCompositeOps<KoLabU16Traits>(this);
41 }
42
willDegrade(ColorSpaceIndependence independence) const43 bool LabU16ColorSpace::willDegrade(ColorSpaceIndependence independence) const
44 {
45 if (independence == TO_RGBA8) {
46 return true;
47 } else {
48 return false;
49 }
50 }
51
clone() const52 KoColorSpace *LabU16ColorSpace::clone() const
53 {
54 return new LabU16ColorSpace(name(), profile()->clone());
55 }
56
colorToXML(const quint8 * pixel,QDomDocument & doc,QDomElement & colorElt) const57 void LabU16ColorSpace::colorToXML(const quint8 *pixel, QDomDocument &doc, QDomElement &colorElt) const
58 {
59 const KoLabU16Traits::Pixel *p = reinterpret_cast<const KoLabU16Traits::Pixel *>(pixel);
60 QDomElement labElt = doc.createElement("Lab");
61
62 qreal a, b;
63
64 if (p->a <= KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::halfValueAB) {
65 a = (p->a - KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::zeroValueAB) /
66 (2.0 * (KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::halfValueAB - KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::zeroValueAB));
67 } else {
68 a = 0.5 +
69 (p->a - KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::halfValueAB) /
70 (2.0 * (KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::unitValueAB - KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::halfValueAB));
71 }
72
73 if (p->b <= KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::halfValueAB) {
74 b = (p->b - KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::zeroValueAB) /
75 (2.0 * (KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::halfValueAB - KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::zeroValueAB));
76 } else {
77 b = 0.5 +
78 (p->b - KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::halfValueAB) /
79 (2.0 * (KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::unitValueAB - KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::halfValueAB));
80 }
81
82 labElt.setAttribute("L", KisDomUtils::toString(KoColorSpaceMaths<KoLabU16Traits::channels_type, qreal>::scaleToA(p->L)));
83 labElt.setAttribute("a", KisDomUtils::toString(a));
84 labElt.setAttribute("b", KisDomUtils::toString(b));
85 labElt.setAttribute("space", profile()->name());
86 colorElt.appendChild(labElt);
87 }
88
colorFromXML(quint8 * pixel,const QDomElement & elt) const89 void LabU16ColorSpace::colorFromXML(quint8 *pixel, const QDomElement &elt) const
90 {
91 KoLabU16Traits::Pixel *p = reinterpret_cast<KoLabU16Traits::Pixel *>(pixel);
92
93 double a = KisDomUtils::toDouble(elt.attribute("a"));
94 double b = KisDomUtils::toDouble(elt.attribute("b"));
95
96 p->L = KoColorSpaceMaths<qreal, KoLabU16Traits::channels_type>::scaleToA(KisDomUtils::toDouble(elt.attribute("L")));
97
98 if (a <= 0.5) {
99 p->a = KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::zeroValueAB +
100 2.0 * a * (KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::halfValueAB - KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::zeroValueAB);
101 } else {
102 p->a = (KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::halfValueAB +
103 2.0 * (a - 0.5) * (KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::unitValueAB - KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::halfValueAB));
104 }
105
106 if (b <= 0.5) {
107 p->b = KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::zeroValueAB +
108 2.0 * b * (KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::halfValueAB - KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::zeroValueAB);
109 } else {
110 p->b = (KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::halfValueAB +
111 2.0 * (b - 0.5) * (KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::unitValueAB - KoLabColorSpaceMathsTraits<KoLabU16Traits::channels_type>::halfValueAB));
112 }
113
114 p->alpha = KoColorSpaceMathsTraits<quint16>::max;
115 }
toHSY(const QVector<double> & channelValues,qreal * hue,qreal * sat,qreal * luma) const116 void LabU16ColorSpace::toHSY(const QVector<double> &channelValues, qreal *hue, qreal *sat, qreal *luma) const
117 {
118 LabToLCH(channelValues[0],channelValues[1],channelValues[2], luma, sat, hue);
119 }
120
fromHSY(qreal * hue,qreal * sat,qreal * luma) const121 QVector <double> LabU16ColorSpace::fromHSY(qreal *hue, qreal *sat, qreal *luma) const
122 {
123 QVector <double> channelValues(4);
124 LCHToLab(*luma, *sat, *hue, &channelValues[0],&channelValues[1],&channelValues[2]);
125 channelValues[3]=1.0;
126 return channelValues;
127 }
128
toYUV(const QVector<double> & channelValues,qreal * y,qreal * u,qreal * v) const129 void LabU16ColorSpace::toYUV(const QVector<double> &channelValues, qreal *y, qreal *u, qreal *v) const
130 {
131 *y =channelValues[0];
132 *u=channelValues[1];
133 *v=channelValues[2];
134 }
135
fromYUV(qreal * y,qreal * u,qreal * v) const136 QVector <double> LabU16ColorSpace::fromYUV(qreal *y, qreal *u, qreal *v) const
137 {
138 QVector <double> channelValues(4);
139 channelValues[0]=*y;
140 channelValues[1]=*u;
141 channelValues[2]=*v;
142 channelValues[3]=1.0;
143 return channelValues;
144 }
145
scaleToU8(const quint8 * srcPixel,qint32 channelIndex) const146 quint8 LabU16ColorSpace::scaleToU8(const quint8 *srcPixel, qint32 channelIndex) const
147 {
148 typename ColorSpaceTraits::channels_type c = ColorSpaceTraits::nativeArray(srcPixel)[channelIndex];
149 qreal b = 0;
150 switch (channelIndex) {
151 case ColorSpaceTraits::L_pos:
152 b = ((qreal)c) / ColorSpaceTraits::math_trait::unitValueL;
153 break;
154 case ColorSpaceTraits::a_pos:
155 case ColorSpaceTraits::b_pos:
156 if (c <= ColorSpaceTraits::math_trait::halfValueAB) {
157 b = ((qreal)c - ColorSpaceTraits::math_trait::zeroValueAB) / (2.0 * (ColorSpaceTraits::math_trait::halfValueAB - ColorSpaceTraits::math_trait::zeroValueAB));
158 } else {
159 b = 0.5 + ((qreal)c - ColorSpaceTraits::math_trait::halfValueAB) / (2.0 * (ColorSpaceTraits::math_trait::unitValueAB - ColorSpaceTraits::math_trait::halfValueAB));
160 }
161 break;
162 default:
163 b = ((qreal)c) / ColorSpaceTraits::math_trait::unitValue;
164 break;
165 }
166
167 return KoColorSpaceMaths<qreal, quint8>::scaleToA(b);
168 }
169
convertChannelToVisualRepresentation(const quint8 * src,quint8 * dst,quint32 nPixels,const qint32 selectedChannelIndex) const170 void LabU16ColorSpace::convertChannelToVisualRepresentation(const quint8 *src, quint8 *dst, quint32 nPixels, const qint32 selectedChannelIndex) const
171 {
172 for (uint pixelIndex = 0; pixelIndex < nPixels; ++pixelIndex) {
173 for (uint channelIndex = 0; channelIndex < this->channelCount(); ++channelIndex) {
174 KoChannelInfo *channel = this->channels().at(channelIndex);
175 qint32 channelSize = channel->size();
176 if (channel->channelType() == KoChannelInfo::COLOR) {
177 if (channelIndex == ColorSpaceTraits::L_pos) {
178 ColorSpaceTraits::channels_type c = ColorSpaceTraits::parent::nativeArray((src + (pixelIndex * ColorSpaceTraits::pixelSize)))[selectedChannelIndex];
179 switch (selectedChannelIndex) {
180 case ColorSpaceTraits::L_pos:
181 break;
182 case ColorSpaceTraits::a_pos:
183 case ColorSpaceTraits::b_pos:
184 if (c <= ColorSpaceTraits::math_trait::halfValueAB) {
185 c = ColorSpaceTraits::math_trait::unitValueL * (((qreal)c - ColorSpaceTraits::math_trait::zeroValueAB) / (2.0 * (ColorSpaceTraits::math_trait::halfValueAB - ColorSpaceTraits::math_trait::zeroValueAB)));
186 } else {
187 c = ColorSpaceTraits::math_trait::unitValueL * (0.5 + ((qreal)c - ColorSpaceTraits::math_trait::halfValueAB) / (2.0 * (ColorSpaceTraits::math_trait::unitValueAB - ColorSpaceTraits::math_trait::halfValueAB)));
188 }
189 break;
190 // As per KoChannelInfo alpha channels are [0..1]
191 default:
192 c = ColorSpaceTraits::math_trait::unitValueL * (qreal)c / ColorSpaceTraits::math_trait::unitValue;
193 break;
194 }
195 ColorSpaceTraits::parent::nativeArray(dst + (pixelIndex * ColorSpaceTraits::pixelSize))[channelIndex] = c;
196 } else {
197 ColorSpaceTraits::parent::nativeArray(dst + (pixelIndex * ColorSpaceTraits::pixelSize))[channelIndex] = ColorSpaceTraits::math_trait::halfValueAB;
198 }
199 } else if (channel->channelType() == KoChannelInfo::ALPHA) {
200 memcpy(dst + (pixelIndex * ColorSpaceTraits::pixelSize) + (channelIndex * channelSize), src + (pixelIndex * ColorSpaceTraits::pixelSize) + (channelIndex * channelSize), channelSize);
201 }
202 }
203 }
204 }
205
convertChannelToVisualRepresentation(const quint8 * src,quint8 * dst,quint32 nPixels,const QBitArray selectedChannels) const206 void LabU16ColorSpace::convertChannelToVisualRepresentation(const quint8 *src, quint8 *dst, quint32 nPixels, const QBitArray selectedChannels) const
207 {
208 for (uint pixelIndex = 0; pixelIndex < nPixels; ++pixelIndex) {
209 for (uint channelIndex = 0; channelIndex < this->channelCount(); ++channelIndex) {
210 KoChannelInfo *channel = this->channels().at(channelIndex);
211 qint32 channelSize = channel->size();
212 if (selectedChannels.testBit(channelIndex)) {
213 memcpy(dst + (pixelIndex * ColorSpaceTraits::pixelSize) + (channelIndex * channelSize), src + (pixelIndex * ColorSpaceTraits::pixelSize) + (channelIndex * channelSize), channelSize);
214 } else {
215 ColorSpaceTraits::channels_type v;
216 switch (channelIndex) {
217 case ColorSpaceTraits::L_pos:
218 v = ColorSpaceTraits::math_trait::halfValueL;
219 break;
220 case ColorSpaceTraits::a_pos:
221 case ColorSpaceTraits::b_pos:
222 v = ColorSpaceTraits::math_trait::halfValueAB;
223 break;
224 default:
225 v = ColorSpaceTraits::math_trait::zeroValue;
226 break;
227 }
228 reinterpret_cast<ColorSpaceTraits::channels_type *>(dst + (pixelIndex * ColorSpaceTraits::pixelSize) + (channelIndex * channelSize))[0] = v;
229 }
230 }
231 }
232 }
233