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 "LabU8ColorSpace.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 
LabU8ColorSpace(const QString & name,KoColorProfile * p)30 LabU8ColorSpace::LabU8ColorSpace(const QString &name, KoColorProfile *p) :
31     LcmsColorSpace<KoLabU8Traits>(colorSpaceId(), name, TYPE_LABA_8, cmsSigLabData, p)
32 {
33     addChannel(new KoChannelInfo(i18nc("Lightness value in Lab color model", "Lightness"), 0 * sizeof(quint8), 0, KoChannelInfo::COLOR, KoChannelInfo::UINT8, sizeof(quint8), QColor(100, 100, 100)));
34     addChannel(new KoChannelInfo(i18n("a*"),        1 * sizeof(quint8), 1, KoChannelInfo::COLOR, KoChannelInfo::UINT8, sizeof(quint8), QColor(150, 150, 150)));
35     addChannel(new KoChannelInfo(i18n("b*"),        2 * sizeof(quint8), 2, KoChannelInfo::COLOR, KoChannelInfo::UINT8, sizeof(quint8), QColor(200, 200, 200)));
36     addChannel(new KoChannelInfo(i18n("Alpha"),     3 * sizeof(quint8), 3, KoChannelInfo::ALPHA, KoChannelInfo::UINT8, sizeof(quint8)));
37     init();
38     addStandardCompositeOps<KoLabU8Traits>(this);
39 }
40 
willDegrade(ColorSpaceIndependence) const41 bool LabU8ColorSpace::willDegrade(ColorSpaceIndependence /*independence*/) const
42 {
43     return false;
44 }
45 
clone() const46 KoColorSpace *LabU8ColorSpace::clone() const
47 {
48     return new LabU8ColorSpace(name(), profile()->clone());
49 }
50 
colorToXML(const quint8 * pixel,QDomDocument & doc,QDomElement & colorElt) const51 void LabU8ColorSpace::colorToXML(const quint8 *pixel, QDomDocument &doc, QDomElement &colorElt) const
52 {
53     const KoLabU8Traits::Pixel *p = reinterpret_cast<const KoLabU8Traits::Pixel *>(pixel);
54     QDomElement labElt = doc.createElement("Lab");
55 
56     double a, b;
57 
58     if (p->a <= KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::halfValueAB) {
59         a = (p->a - KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::zeroValueAB) /
60             (2.0 * (KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::halfValueAB - KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::zeroValueAB));
61     } else {
62         a = 0.5 +
63             (p->a - KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::halfValueAB) /
64                 (2.0 * (KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::unitValueAB - KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::halfValueAB));
65     }
66 
67     if (p->b <= KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::halfValueAB) {
68         b = (p->b - KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::zeroValueAB) /
69             (2.0 * (KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::halfValueAB - KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::zeroValueAB));
70     } else {
71         b = 0.5 +
72             (p->b - KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::halfValueAB) /
73                 (2.0 * (KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::unitValueAB - KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::halfValueAB));
74     }
75 
76     labElt.setAttribute("L", KisDomUtils::toString(KoColorSpaceMaths<KoLabU8Traits::channels_type, qreal>::scaleToA(p->L)));
77     labElt.setAttribute("a", KisDomUtils::toString(a));
78     labElt.setAttribute("b", KisDomUtils::toString(b));
79     labElt.setAttribute("space", profile()->name());
80     colorElt.appendChild(labElt);
81 }
82 
colorFromXML(quint8 * pixel,const QDomElement & elt) const83 void LabU8ColorSpace::colorFromXML(quint8 *pixel, const QDomElement &elt) const
84 {
85     KoLabU8Traits::Pixel *p = reinterpret_cast<KoLabU8Traits::Pixel *>(pixel);
86 
87     double a = KisDomUtils::toDouble(elt.attribute("a"));
88     double b = KisDomUtils::toDouble(elt.attribute("b"));
89 
90     p->L = KoColorSpaceMaths<qreal, KoLabU8Traits::channels_type>::scaleToA(KisDomUtils::toDouble(elt.attribute("L")));
91 
92     if (a <= 0.5) {
93         p->a =
94             KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::zeroValueAB + 2.0 * a * (KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::halfValueAB - KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::zeroValueAB);
95     } else {
96         p->a = (KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::halfValueAB +
97                 2.0 * (a - 0.5) * (KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::unitValueAB - KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::halfValueAB));
98     }
99 
100     if (b <= 0.5) {
101         p->b =
102             KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::zeroValueAB + 2.0 * b * (KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::halfValueAB - KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::zeroValueAB);
103     } else {
104         p->b = (KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::halfValueAB +
105                 2.0 * (b - 0.5) * (KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::unitValueAB - KoLabColorSpaceMathsTraits<KoLabU8Traits::channels_type>::halfValueAB));
106     }
107 
108     p->alpha = KoColorSpaceMathsTraits<quint8>::max;
109 }
110 
toHSY(const QVector<double> & channelValues,qreal * hue,qreal * sat,qreal * luma) const111 void LabU8ColorSpace::toHSY(const QVector<double> &channelValues, qreal *hue, qreal *sat, qreal *luma) const
112 {
113     LabToLCH(channelValues[0],channelValues[1],channelValues[2], luma, sat, hue);
114 }
115 
fromHSY(qreal * hue,qreal * sat,qreal * luma) const116 QVector <double> LabU8ColorSpace::fromHSY(qreal *hue, qreal *sat, qreal *luma) const
117 {
118     QVector <double> channelValues(4);
119     LCHToLab(*luma, *sat, *hue, &channelValues[0],&channelValues[1],&channelValues[2]);
120     channelValues[3]=1.0;
121     return channelValues;
122 }
123 
toYUV(const QVector<double> & channelValues,qreal * y,qreal * u,qreal * v) const124 void LabU8ColorSpace::toYUV(const QVector<double> &channelValues, qreal *y, qreal *u, qreal *v) const
125 {
126     *y =channelValues[0];
127     *u=channelValues[1];
128     *v=channelValues[2];
129 }
130 
fromYUV(qreal * y,qreal * u,qreal * v) const131 QVector <double> LabU8ColorSpace::fromYUV(qreal *y, qreal *u, qreal *v) const
132 {
133     QVector <double> channelValues(4);
134     channelValues[0]=*y;
135     channelValues[1]=*u;
136     channelValues[2]=*v;
137     channelValues[3]=1.0;
138     return channelValues;
139 }
140 
scaleToU8(const quint8 * srcPixel,qint32 channelIndex) const141 quint8 LabU8ColorSpace::scaleToU8(const quint8 *srcPixel, qint32 channelIndex) const
142 {
143     typename ColorSpaceTraits::channels_type c = ColorSpaceTraits::nativeArray(srcPixel)[channelIndex];
144     qreal b = 0;
145     switch (channelIndex) {
146     case ColorSpaceTraits::L_pos:
147         b = ((qreal)c) / ColorSpaceTraits::math_trait::unitValueL;
148         break;
149     case ColorSpaceTraits::a_pos:
150     case ColorSpaceTraits::b_pos:
151         if (c <= ColorSpaceTraits::math_trait::halfValueAB) {
152             b = ((qreal)c - ColorSpaceTraits::math_trait::zeroValueAB) / (2.0 * (ColorSpaceTraits::math_trait::halfValueAB - ColorSpaceTraits::math_trait::zeroValueAB));
153         } else {
154             b = 0.5 + ((qreal)c - ColorSpaceTraits::math_trait::halfValueAB) / (2.0 * (ColorSpaceTraits::math_trait::unitValueAB - ColorSpaceTraits::math_trait::halfValueAB));
155         }
156         break;
157     default:
158         b = ((qreal)c) / ColorSpaceTraits::math_trait::unitValue;
159         break;
160     }
161 
162     return KoColorSpaceMaths<qreal, quint8>::scaleToA(b);
163 }
164 
convertChannelToVisualRepresentation(const quint8 * src,quint8 * dst,quint32 nPixels,const qint32 selectedChannelIndex) const165 void LabU8ColorSpace::convertChannelToVisualRepresentation(const quint8 *src, quint8 *dst, quint32 nPixels, const qint32 selectedChannelIndex) const
166 {
167     for (uint pixelIndex = 0; pixelIndex < nPixels; ++pixelIndex) {
168         for (uint channelIndex = 0; channelIndex < this->channelCount(); ++channelIndex) {
169             KoChannelInfo *channel = this->channels().at(channelIndex);
170             qint32 channelSize = channel->size();
171             if (channel->channelType() == KoChannelInfo::COLOR) {
172                 if (channelIndex == ColorSpaceTraits::L_pos) {
173                     ColorSpaceTraits::channels_type c = ColorSpaceTraits::parent::nativeArray((src + (pixelIndex * ColorSpaceTraits::pixelSize)))[selectedChannelIndex];
174                     switch (selectedChannelIndex) {
175                     case ColorSpaceTraits::L_pos:
176                         break;
177                     case ColorSpaceTraits::a_pos:
178                     case ColorSpaceTraits::b_pos:
179                         if (c <= ColorSpaceTraits::math_trait::halfValueAB) {
180                             c = ColorSpaceTraits::math_trait::unitValueL * (((qreal)c - ColorSpaceTraits::math_trait::zeroValueAB) / (2.0 * (ColorSpaceTraits::math_trait::halfValueAB - ColorSpaceTraits::math_trait::zeroValueAB)));
181                         } else {
182                             c = ColorSpaceTraits::math_trait::unitValueL * (0.5 + ((qreal)c - ColorSpaceTraits::math_trait::halfValueAB) / (2.0 * (ColorSpaceTraits::math_trait::unitValueAB - ColorSpaceTraits::math_trait::halfValueAB)));
183                         }
184                         break;
185                     // As per KoChannelInfo alpha channels are [0..1]
186                     default:
187                         c = ColorSpaceTraits::math_trait::unitValueL * (qreal)c / ColorSpaceTraits::math_trait::unitValue;
188                         break;
189                     }
190                     ColorSpaceTraits::parent::nativeArray(dst + (pixelIndex * ColorSpaceTraits::pixelSize))[channelIndex] = c;
191                 } else {
192                     ColorSpaceTraits::parent::nativeArray(dst + (pixelIndex * ColorSpaceTraits::pixelSize))[channelIndex] = ColorSpaceTraits::math_trait::halfValueAB;
193                 }
194             } else if (channel->channelType() == KoChannelInfo::ALPHA) {
195                 memcpy(dst + (pixelIndex * ColorSpaceTraits::pixelSize) + (channelIndex * channelSize), src + (pixelIndex * ColorSpaceTraits::pixelSize) + (channelIndex * channelSize), channelSize);
196             }
197         }
198     }
199 }
200 
convertChannelToVisualRepresentation(const quint8 * src,quint8 * dst,quint32 nPixels,const QBitArray selectedChannels) const201 void LabU8ColorSpace::convertChannelToVisualRepresentation(const quint8 *src, quint8 *dst, quint32 nPixels, const QBitArray selectedChannels) const
202 {
203     for (uint pixelIndex = 0; pixelIndex < nPixels; ++pixelIndex) {
204         for (uint channelIndex = 0; channelIndex < this->channelCount(); ++channelIndex) {
205             KoChannelInfo *channel = this->channels().at(channelIndex);
206             qint32 channelSize = channel->size();
207             if (selectedChannels.testBit(channelIndex)) {
208                 memcpy(dst + (pixelIndex * ColorSpaceTraits::pixelSize) + (channelIndex * channelSize), src + (pixelIndex * ColorSpaceTraits::pixelSize) + (channelIndex * channelSize), channelSize);
209             } else {
210                 ColorSpaceTraits::channels_type v;
211                 switch (channelIndex) {
212                 case ColorSpaceTraits::L_pos:
213                     v = ColorSpaceTraits::math_trait::halfValueL;
214                     break;
215                 case ColorSpaceTraits::a_pos:
216                 case ColorSpaceTraits::b_pos:
217                     v = ColorSpaceTraits::math_trait::halfValueAB;
218                     break;
219                 default:
220                     v = ColorSpaceTraits::math_trait::zeroValue;
221                     break;
222                 }
223                 reinterpret_cast<ColorSpaceTraits::channels_type *>(dst + (pixelIndex * ColorSpaceTraits::pixelSize) + (channelIndex * channelSize))[0] = v;
224             }
225         }
226     }
227 }
228