Krita Source Code Documentation
Loading...
Searching...
No Matches
LabU8ColorSpace.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006 Cyrille Berger <cberger@cberger.net>
3 * SPDX-FileCopyrightText: 2021 L. E. Segovia <amy@amyspark.me>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7
8#include "LabU8ColorSpace.h"
9
10#include <QDomElement>
11
12#include <klocalizedstring.h>
13
14#include "../compositeops/KoCompositeOps.h"
16#include <KoColorConversions.h>
17#include <kis_dom_utils.h>
18
20 LcmsColorSpace<KoLabU8Traits>(colorSpaceId(), name, TYPE_LABA_8, cmsSigLabData, p)
21{
22 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)));
23 addChannel(new KoChannelInfo(i18n("a*"), 1 * sizeof(quint8), 1, KoChannelInfo::COLOR, KoChannelInfo::UINT8, sizeof(quint8), QColor(150, 150, 150)));
24 addChannel(new KoChannelInfo(i18n("b*"), 2 * sizeof(quint8), 2, KoChannelInfo::COLOR, KoChannelInfo::UINT8, sizeof(quint8), QColor(200, 200, 200)));
25 addChannel(new KoChannelInfo(i18n("Alpha"), 3 * sizeof(quint8), 3, KoChannelInfo::ALPHA, KoChannelInfo::UINT8, sizeof(quint8)));
26 init();
27 addStandardCompositeOps<KoLabU8Traits>(this);
28 addStandardDitherOps<KoLabU8Traits>(this);
29}
30
32{
33 return false;
34}
35
37{
38 return new LabU8ColorSpace(name(), profile()->clone());
39}
40
41void LabU8ColorSpace::colorToXML(const quint8 *pixel, QDomDocument &doc, QDomElement &colorElt) const
42{
43 const KoLabU8Traits::Pixel *p = reinterpret_cast<const KoLabU8Traits::Pixel *>(pixel);
44 QDomElement labElt = doc.createElement("Lab");
45
46 double a, b;
48
49 if (p->a <= halfValue) {
50 a = double(double(halfValue - p->a) / halfValue);
51 a = a * -128.0;
52 } else {
53 a = double(double(p->a - halfValue) / halfValue);
54 a = 127.0 * a;
55 }
56
57 if (p->b <= halfValue) {
58 b = double(double(halfValue - p->b) / halfValue);
59 b = b * -128.0;
60 } else {
61 b = double(double(p->b - halfValue) / halfValue);
62 b = 127.0 * b;
63 }
64
66 labElt.setAttribute("a", KisDomUtils::toString(a));
67 labElt.setAttribute("b", KisDomUtils::toString(b));
68 labElt.setAttribute("space", profile()->name());
69 colorElt.appendChild(labElt);
70}
71
72void LabU8ColorSpace::colorFromXML(quint8 *pixel, const QDomElement &elt) const
73{
74 KoLabU8Traits::Pixel *p = reinterpret_cast<KoLabU8Traits::Pixel *>(pixel);
75
76 double a = KisDomUtils::toDouble(elt.attribute("a"));
77 double b = KisDomUtils::toDouble(elt.attribute("b"));
78
79 // L will go from 0 to 100
80
82
84
85 // a goes from -128 to 127
86 if(a <= 0) {
87 a = (a/-128.0);
88 p->a = halfValue - (a * halfValue);
89 } else {
90 a = fabs(a/127.0);
91 p->a = (a * halfValue) + halfValue;
92 }
93
94 if(b <= 0) {
95 b = (b/-128.0);
96 p->b = halfValue - (b * halfValue);
97 } else {
98 b = fabs(b/127.0);
99 p->b = (b * halfValue) + halfValue;
100 }
101
103}
104
105void LabU8ColorSpace::toHSY(const QVector<double> &channelValues, qreal *hue, qreal *sat, qreal *luma) const
106{
107 LabToLCH(channelValues[0],channelValues[1],channelValues[2], luma, sat, hue);
108}
109
110QVector <double> LabU8ColorSpace::fromHSY(qreal *hue, qreal *sat, qreal *luma) const
111{
112 QVector <double> channelValues(4);
113 LCHToLab(*luma, *sat, *hue, &channelValues[0],&channelValues[1],&channelValues[2]);
114 channelValues[3]=1.0;
115 return channelValues;
116}
117
118void LabU8ColorSpace::toYUV(const QVector<double> &channelValues, qreal *y, qreal *u, qreal *v) const
119{
120 *y =channelValues[0];
121 *u=channelValues[1];
122 *v=channelValues[2];
123}
124
125QVector <double> LabU8ColorSpace::fromYUV(qreal *y, qreal *u, qreal *v) const
126{
127 QVector <double> channelValues(4);
128 channelValues[0]=*y;
129 channelValues[1]=*u;
130 channelValues[2]=*v;
131 channelValues[3]=1.0;
132 return channelValues;
133}
134
135quint8 LabU8ColorSpace::scaleToU8(const quint8 *srcPixel, qint32 channelIndex) const
136{
137 typename ColorSpaceTraits::channels_type c = ColorSpaceTraits::nativeArray(srcPixel)[channelIndex];
138 qreal b = 0;
139 switch (channelIndex) {
140 case ColorSpaceTraits::L_pos:
141 b = ((qreal)c) / ColorSpaceTraits::math_trait::unitValueL;
142 break;
143 case ColorSpaceTraits::a_pos:
144 case ColorSpaceTraits::b_pos:
145 if (c <= ColorSpaceTraits::math_trait::halfValueAB) {
146 b = ((qreal)c - ColorSpaceTraits::math_trait::zeroValueAB) / (2.0 * (ColorSpaceTraits::math_trait::halfValueAB - ColorSpaceTraits::math_trait::zeroValueAB));
147 } else {
148 b = 0.5 + ((qreal)c - ColorSpaceTraits::math_trait::halfValueAB) / (2.0 * (ColorSpaceTraits::math_trait::unitValueAB - ColorSpaceTraits::math_trait::halfValueAB));
149 }
150 break;
151 default:
152 b = ((qreal)c) / ColorSpaceTraits::math_trait::unitValue;
153 break;
154 }
155
157}
158
159void LabU8ColorSpace::convertChannelToVisualRepresentation(const quint8 *src, quint8 *dst, quint32 nPixels, const qint32 selectedChannelIndex) const
160{
161 for (uint pixelIndex = 0; pixelIndex < nPixels; ++pixelIndex) {
162 for (uint channelIndex = 0; channelIndex < ColorSpaceTraits::channels_nb; ++channelIndex) {
163 if (channelIndex != ColorSpaceTraits::alpha_pos) {
164 if (channelIndex == ColorSpaceTraits::L_pos) {
165 ColorSpaceTraits::channels_type c = ColorSpaceTraits::nativeArray((src + (pixelIndex * ColorSpaceTraits::pixelSize)))[selectedChannelIndex];
166 switch (selectedChannelIndex) {
167 case ColorSpaceTraits::L_pos:
168 break;
169 case ColorSpaceTraits::a_pos:
170 case ColorSpaceTraits::b_pos:
171 if (c <= ColorSpaceTraits::math_trait::halfValueAB) {
172 c = ColorSpaceTraits::math_trait::unitValueL * (((qreal)c - ColorSpaceTraits::math_trait::zeroValueAB) / (2.0 * (ColorSpaceTraits::math_trait::halfValueAB - ColorSpaceTraits::math_trait::zeroValueAB)));
173 } else {
174 c = ColorSpaceTraits::math_trait::unitValueL * (0.5 + ((qreal)c - ColorSpaceTraits::math_trait::halfValueAB) / (2.0 * (ColorSpaceTraits::math_trait::unitValueAB - ColorSpaceTraits::math_trait::halfValueAB)));
175 }
176 break;
177 // As per KoChannelInfo alpha channels are [0..1]
178 default:
179 c = ColorSpaceTraits::math_trait::unitValueL * (qreal)c / ColorSpaceTraits::math_trait::unitValue;
180 break;
181 }
182 ColorSpaceTraits::nativeArray(dst + (pixelIndex * ColorSpaceTraits::pixelSize))[channelIndex] = c;
183 } else {
184 ColorSpaceTraits::nativeArray(dst + (pixelIndex * ColorSpaceTraits::pixelSize))[channelIndex] = ColorSpaceTraits::math_trait::halfValueAB;
185 }
186 } else {
187 ColorSpaceTraits::nativeArray((dst + (pixelIndex * ColorSpaceTraits::pixelSize)))[channelIndex] =
188 ColorSpaceTraits::nativeArray((src + (pixelIndex * ColorSpaceTraits::pixelSize)))[channelIndex];
189 }
190 }
191 }
192}
193
194void LabU8ColorSpace::convertChannelToVisualRepresentation(const quint8 *src, quint8 *dst, quint32 nPixels, const QBitArray selectedChannels) const
195{
196 for (uint pixelIndex = 0; pixelIndex < nPixels; ++pixelIndex) {
197 for (uint channelIndex = 0; channelIndex < ColorSpaceTraits::channels_nb; ++channelIndex) {
198 if (selectedChannels.testBit(channelIndex)) {
199 ColorSpaceTraits::nativeArray((dst + (pixelIndex * ColorSpaceTraits::pixelSize)))[channelIndex] =
200 ColorSpaceTraits::nativeArray((src + (pixelIndex * ColorSpaceTraits::pixelSize)))[channelIndex];
201 } else {
202 ColorSpaceTraits::channels_type v;
203 switch (channelIndex) {
204 case ColorSpaceTraits::L_pos:
205 v = ColorSpaceTraits::math_trait::halfValueL;
206 break;
207 case ColorSpaceTraits::a_pos:
208 case ColorSpaceTraits::b_pos:
209 v = ColorSpaceTraits::math_trait::halfValueAB;
210 break;
211 default:
212 v = ColorSpaceTraits::math_trait::zeroValue;
213 break;
214 }
215 ColorSpaceTraits::nativeArray((dst + (pixelIndex * ColorSpaceTraits::pixelSize)))[channelIndex] = v;
216 }
217 }
218 }
219}
const Params2D p
qreal v
qreal u
void LabToLCH(const qreal l, const qreal a, const qreal b, qreal *L, qreal *C, qreal *H)
void LCHToLab(const qreal L, const qreal C, const qreal H, qreal *l, qreal *a, qreal *b)
ColorSpaceIndependence
unsigned int uint
#define TYPE_LABA_8
@ ALPHA
The channel represents the opacity of a pixel.
@ COLOR
The channel represents a color.
@ UINT8
use this for an unsigned integer 8bits channel
static _Tdst scaleToA(_T a)
virtual void addChannel(KoChannelInfo *ci)
quint8 scaleToU8(const quint8 *srcPixel, qint32 channelIndex) const override
QVector< double > fromHSY(qreal *hue, qreal *sat, qreal *luma) const override
void toYUV(const QVector< double > &channelValues, qreal *y, qreal *u, qreal *v) const override
bool willDegrade(ColorSpaceIndependence independence) const override
QVector< double > fromYUV(qreal *y, qreal *u, qreal *v) const override
void convertChannelToVisualRepresentation(const quint8 *src, quint8 *dst, quint32 nPixels, const qint32 selectedChannelIndex) const override
void colorToXML(const quint8 *pixel, QDomDocument &doc, QDomElement &colorElt) const override
void toHSY(const QVector< double > &channelValues, qreal *hue, qreal *sat, qreal *luma) const override
void colorFromXML(quint8 *pixel, const QDomElement &elt) const override
virtual KoColorSpace * clone() const
LabU8ColorSpace(const QString &name, KoColorProfile *p)
const KoColorProfile * profile() const override
double toDouble(const QString &str, bool *ok=nullptr)
QString toString(const QString &value)
_channels_type_ channels_type
the type of the value of the channels of this color space