1 /*
2  *  Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
3  *  Copyright (c) 2020 L. E. Segovia <amy@amyspark.me>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19 */
20 
21 #include "LabF32ColorSpace.h"
22 
23 #include <QDomElement>
24 
25 #include <klocalizedstring.h>
26 
27 #include "../compositeops/KoCompositeOps.h"
28 #include <KoColorConversions.h>
29 #include <kis_dom_utils.h>
30 
LabF32ColorSpace(const QString & name,KoColorProfile * p)31 LabF32ColorSpace::LabF32ColorSpace(const QString &name, KoColorProfile *p)
32     : LcmsColorSpace<KoLabF32Traits>(colorSpaceId(), name, TYPE_LabA_FLT, cmsSigLabData, p)
33 {
34     const IccColorProfile *icc_p = dynamic_cast<const IccColorProfile *>(p);
35     Q_ASSERT(icc_p);
36     QVector<KoChannelInfo::DoubleRange> uiRanges(icc_p->getFloatUIMinMax());
37     Q_ASSERT(uiRanges.size() == 3);
38 
39     addChannel(new KoChannelInfo(i18nc("Lightness value in Lab color model", "Lightness"), 0 * sizeof(float), 0, KoChannelInfo::COLOR, KoChannelInfo::FLOAT32, sizeof(float), QColor(100, 100, 100), uiRanges[0]));
40     addChannel(new KoChannelInfo(i18n("a*"),        1 * sizeof(float), 1, KoChannelInfo::COLOR, KoChannelInfo::FLOAT32, sizeof(float), QColor(150, 150, 150), uiRanges[1]));
41     addChannel(new KoChannelInfo(i18n("b*"),        2 * sizeof(float), 2, KoChannelInfo::COLOR, KoChannelInfo::FLOAT32, sizeof(float), QColor(200, 200, 200), uiRanges[2]));
42     addChannel(new KoChannelInfo(i18n("Alpha"),     3 * sizeof(float), 3, KoChannelInfo::ALPHA, KoChannelInfo::FLOAT32, sizeof(float)));
43 
44     init();
45 
46     addStandardCompositeOps<KoLabF32Traits>(this);
47 
48     dbgPlugins << "La*b* (float) channel bounds for: " << icc_p->name();
49     dbgPlugins << "L: " << uiRanges[0].minVal << uiRanges[0].maxVal;
50     dbgPlugins << "a: " << uiRanges[1].minVal << uiRanges[1].maxVal;
51     dbgPlugins << "b: " << uiRanges[2].minVal << uiRanges[2].maxVal;
52 }
53 
willDegrade(ColorSpaceIndependence independence) const54 bool LabF32ColorSpace::willDegrade(ColorSpaceIndependence independence) const
55 {
56     if (independence == TO_RGBA16) {
57         return true;
58     } else {
59         return false;
60     }
61 }
62 
clone() const63 KoColorSpace *LabF32ColorSpace::clone() const
64 {
65     return new LabF32ColorSpace(name(), profile()->clone());
66 }
67 
colorToXML(const quint8 * pixel,QDomDocument & doc,QDomElement & colorElt) const68 void LabF32ColorSpace::colorToXML(const quint8 *pixel, QDomDocument &doc, QDomElement &colorElt) const
69 {
70     const KoLabF32Traits::Pixel *p = reinterpret_cast<const KoLabF32Traits::Pixel *>(pixel);
71     QDomElement labElt = doc.createElement("Lab");
72 
73     // XML expects 0-1, we need 0-100, -128-+127
74     // Get the bounds from the channels and adjust the calculations
75     labElt.setAttribute("L", KisDomUtils::toString(KoColorSpaceMaths< KoLabF32Traits::channels_type, qreal>::scaleToA(1.f / this->channels()[0]->getUIUnitValue() * (p->L - this->channels()[0]->getUIMin()))));
76     labElt.setAttribute("a", KisDomUtils::toString(KoColorSpaceMaths< KoLabF32Traits::channels_type, qreal>::scaleToA(1.f / this->channels()[1]->getUIUnitValue() * (p->a - this->channels()[1]->getUIMin()))));
77     labElt.setAttribute("b", KisDomUtils::toString(KoColorSpaceMaths< KoLabF32Traits::channels_type, qreal>::scaleToA(1.f / this->channels()[2]->getUIUnitValue() * (p->b - this->channels()[2]->getUIMin()))));
78     labElt.setAttribute("space", profile()->name());
79     colorElt.appendChild(labElt);
80 }
81 
colorFromXML(quint8 * pixel,const QDomElement & elt) const82 void LabF32ColorSpace::colorFromXML(quint8 *pixel, const QDomElement &elt) const
83 {
84     KoLabF32Traits::Pixel *p = reinterpret_cast<KoLabF32Traits::Pixel *>(pixel);
85     p->L = this->channels()[0]->getUIMin() + KoColorSpaceMaths< qreal, KoLabF32Traits::channels_type >::scaleToA(KisDomUtils::toDouble(elt.attribute("L"))) * this->channels()[0]->getUIUnitValue();
86     p->a = this->channels()[1]->getUIMin() + KoColorSpaceMaths< qreal, KoLabF32Traits::channels_type >::scaleToA(KisDomUtils::toDouble(elt.attribute("a"))) * this->channels()[1]->getUIUnitValue();
87     p->b = this->channels()[2]->getUIMin() + KoColorSpaceMaths< qreal, KoLabF32Traits::channels_type >::scaleToA(KisDomUtils::toDouble(elt.attribute("b"))) * this->channels()[2]->getUIUnitValue();
88     p->alpha = 1.0;
89 }
90 
toHSY(const QVector<double> & channelValues,qreal * hue,qreal * sat,qreal * luma) const91 void LabF32ColorSpace::toHSY(const QVector<double> &channelValues, qreal *hue, qreal *sat, qreal *luma) const
92 {
93     LabToLCH(channelValues[0],channelValues[1],channelValues[2], luma, sat, hue);
94 }
95 
fromHSY(qreal * hue,qreal * sat,qreal * luma) const96 QVector <double> LabF32ColorSpace::fromHSY(qreal *hue, qreal *sat, qreal *luma) const
97 {
98     QVector <double> channelValues(4);
99     LCHToLab(*luma, *sat, *hue, &channelValues[0],&channelValues[1],&channelValues[2]);
100     channelValues[3]=1.0;
101     return channelValues;
102 }
toYUV(const QVector<double> & channelValues,qreal * y,qreal * u,qreal * v) const103 void LabF32ColorSpace::toYUV(const QVector<double> &channelValues, qreal *y, qreal *u, qreal *v) const
104 {
105     *y =channelValues[0];
106     *u=channelValues[1];
107     *v=channelValues[2];
108 }
109 
fromYUV(qreal * y,qreal * u,qreal * v) const110 QVector <double> LabF32ColorSpace::fromYUV(qreal *y, qreal *u, qreal *v) const
111 {
112     QVector <double> channelValues(4);
113     channelValues[0]=*y;
114     channelValues[1]=*u;
115     channelValues[2]=*v;
116     channelValues[3]=1.0;
117     return channelValues;
118 }
119 
scaleToU8(const quint8 * srcPixel,qint32 channelIndex) const120 quint8 LabF32ColorSpace::scaleToU8(const quint8 *srcPixel, qint32 channelIndex) const
121 {
122     typename ColorSpaceTraits::channels_type c = ColorSpaceTraits::nativeArray(srcPixel)[channelIndex];
123     qreal b = 0;
124     switch (channelIndex) {
125     case ColorSpaceTraits::L_pos:
126         b = ((qreal)c) / ColorSpaceTraits::math_trait::unitValueL;
127         break;
128     case ColorSpaceTraits::a_pos:
129     case ColorSpaceTraits::b_pos:
130         if (c <= ColorSpaceTraits::math_trait::halfValueAB) {
131             b = ((qreal)c - ColorSpaceTraits::math_trait::zeroValueAB) / (2.0 * (ColorSpaceTraits::math_trait::halfValueAB - ColorSpaceTraits::math_trait::zeroValueAB));
132         } else {
133             b = 0.5 + ((qreal)c - ColorSpaceTraits::math_trait::halfValueAB) / (2.0 * (ColorSpaceTraits::math_trait::unitValueAB - ColorSpaceTraits::math_trait::halfValueAB));
134         }
135         break;
136     default:
137         b = ((qreal)c) / ColorSpaceTraits::math_trait::unitValue;
138         break;
139     }
140 
141     return KoColorSpaceMaths<qreal, quint8>::scaleToA(b);
142 }
143 
convertChannelToVisualRepresentation(const quint8 * src,quint8 * dst,quint32 nPixels,const qint32 selectedChannelIndex) const144 void LabF32ColorSpace::convertChannelToVisualRepresentation(const quint8 *src, quint8 *dst, quint32 nPixels, const qint32 selectedChannelIndex) const
145 {
146     for (uint pixelIndex = 0; pixelIndex < nPixels; ++pixelIndex) {
147         for (uint channelIndex = 0; channelIndex < this->channelCount(); ++channelIndex) {
148             KoChannelInfo *channel = this->channels().at(channelIndex);
149             qint32 channelSize = channel->size();
150             if (channel->channelType() == KoChannelInfo::COLOR) {
151                 if (channelIndex == ColorSpaceTraits::L_pos) {
152                     ColorSpaceTraits::channels_type c = ColorSpaceTraits::parent::nativeArray((src + (pixelIndex * ColorSpaceTraits::pixelSize)))[selectedChannelIndex];
153                     switch (selectedChannelIndex) {
154                     case ColorSpaceTraits::L_pos:
155                         break;
156                     case ColorSpaceTraits::a_pos:
157                     case ColorSpaceTraits::b_pos:
158                         if (c <= ColorSpaceTraits::math_trait::halfValueAB) {
159                             c = ColorSpaceTraits::math_trait::unitValueL * (((qreal)c - ColorSpaceTraits::math_trait::zeroValueAB) / (2.0 * (ColorSpaceTraits::math_trait::halfValueAB - ColorSpaceTraits::math_trait::zeroValueAB)));
160                         } else {
161                             c = ColorSpaceTraits::math_trait::unitValueL * (0.5 + ((qreal)c - ColorSpaceTraits::math_trait::halfValueAB) / (2.0 * (ColorSpaceTraits::math_trait::unitValueAB - ColorSpaceTraits::math_trait::halfValueAB)));
162                         }
163                         break;
164                     // As per KoChannelInfo alpha channels are [0..1]
165                     default:
166                         c = ColorSpaceTraits::math_trait::unitValueL * (qreal)c / ColorSpaceTraits::math_trait::unitValue;
167                         break;
168                     }
169                     ColorSpaceTraits::parent::nativeArray(dst + (pixelIndex * ColorSpaceTraits::pixelSize))[channelIndex] = c;
170                 } else {
171                     ColorSpaceTraits::parent::nativeArray(dst + (pixelIndex * ColorSpaceTraits::pixelSize))[channelIndex] = ColorSpaceTraits::math_trait::halfValueAB;
172                 }
173             } else if (channel->channelType() == KoChannelInfo::ALPHA) {
174                 memcpy(dst + (pixelIndex * ColorSpaceTraits::pixelSize) + (channelIndex * channelSize), src + (pixelIndex * ColorSpaceTraits::pixelSize) + (channelIndex * channelSize), channelSize);
175             }
176         }
177     }
178 }
179 
convertChannelToVisualRepresentation(const quint8 * src,quint8 * dst,quint32 nPixels,const QBitArray selectedChannels) const180 void LabF32ColorSpace::convertChannelToVisualRepresentation(const quint8 *src, quint8 *dst, quint32 nPixels, const QBitArray selectedChannels) const
181 {
182     for (uint pixelIndex = 0; pixelIndex < nPixels; ++pixelIndex) {
183         for (uint channelIndex = 0; channelIndex < this->channelCount(); ++channelIndex) {
184             KoChannelInfo *channel = this->channels().at(channelIndex);
185             qint32 channelSize = channel->size();
186             if (selectedChannels.testBit(channelIndex)) {
187                 memcpy(dst + (pixelIndex * ColorSpaceTraits::pixelSize) + (channelIndex * channelSize), src + (pixelIndex * ColorSpaceTraits::pixelSize) + (channelIndex * channelSize), channelSize);
188             } else {
189                 ColorSpaceTraits::channels_type v;
190                 switch (channelIndex) {
191                 case ColorSpaceTraits::L_pos:
192                     v = ColorSpaceTraits::math_trait::halfValueL;
193                     break;
194                 case ColorSpaceTraits::a_pos:
195                 case ColorSpaceTraits::b_pos:
196                     v = ColorSpaceTraits::math_trait::halfValueAB;
197                     break;
198                 default:
199                     v = ColorSpaceTraits::math_trait::zeroValue;
200                     break;
201                 }
202                 reinterpret_cast<ColorSpaceTraits::channels_type *>(dst + (pixelIndex * ColorSpaceTraits::pixelSize) + (channelIndex * channelSize))[0] = v;
203             }
204         }
205     }
206 }
207