Krita Source Code Documentation
Loading...
Searching...
No Matches
KoGamutMappingUtils Namespace Reference

Classes

struct  PiecewiseFilmicCurve
 The piecewiseFilmicCurve class Implement's Hable's piecewise filmic curve. https://filmicworlds.com/blog/filmic-tonemapping-with-piecewise-power-curves/ License: CC-0. More...
 

Enumerations

enum  CurveType { Reinhard , PiecewiseFilmic }
 
enum  MappingModel { LAB , ICtCp , OkLAB }
 The MappingModel enum This represents the preferred model to do gamut and/or tone mapping in. More...
 

Functions

QPointF KRITAPIGMENT_EXPORT cuspForHue (const QMap< float, QPointF > &gamutCuspDescription, const float hue)
 
float KRITAPIGMENT_EXPORT deltaE76 (const QVector< float > &a, const QVector< float > &b)
 deltaE76 calculates the difference between two colors, represented as normalized channels.
 
QMap< float, QPointF > KRITAPIGMENT_EXPORT getGamutCuspDescription (const KoColorProfile *profile, KoGamutMappingUtils::MappingModel model, const int samplesPerChannel=5)
 getGamutCuspDescription Generates a hexagonal gamut cusp description from a RGB or CMYK profile.
 
QVector4D KRITAPIGMENT_EXPORT ICtCpToXYZ (const QVector4D &iCtCp)
 
QVector4D KRITAPIGMENT_EXPORT OkLabToXYZ (const QVector4D &okLab)
 
float KRITAPIGMENT_EXPORT reinhardToneMapping (const float linearY, const float maxY)
 reinhardToneMapping Implements 'extended' reinhard tonemapping.
 
QVector4D KRITAPIGMENT_EXPORT XYZToICtCp (const QVector4D &xyz)
 XYZToICtCp Ad-hoc implementation of ICtCp for use in filters. Unlike the official YCbCr-compatible standard, this one doesn't have a 0.5 offset in the Ct and Cp channels.
 
QVector4D KRITAPIGMENT_EXPORT XYZToOkLab (const QVector4D &xyz)
 XYZToOkLab Ad-hoc implementation of OkLAB. This takes a QVector4D and transforms it to the OkLAB model.
 

Enumeration Type Documentation

◆ CurveType

Enumerator
Reinhard 
PiecewiseFilmic 

Definition at line 66 of file KoGamutMappingUtils.h.

◆ MappingModel

The MappingModel enum This represents the preferred model to do gamut and/or tone mapping in.

Here to keep the filter and the transform in sync.

Enumerator
LAB 
ICtCp 
OkLAB 

Definition at line 50 of file KoGamutMappingUtils.h.

Function Documentation

◆ cuspForHue()

QPointF KoGamutMappingUtils::cuspForHue ( const QMap< float, QPointF > & gamutCuspDescription,
const float hue )

Definition at line 339 of file KoGamutMappingUtils.cpp.

340{
341 if (gamutCuspDescription.isEmpty()) return QPointF();
342
343 float firstAngle = 0.0;
344 float secondAngle = 0.0;
345
346 auto it = gamutCuspDescription.upperBound(hue);
347 if (it == gamutCuspDescription.begin()) {
348 firstAngle = gamutCuspDescription.lastKey();
349 secondAngle = gamutCuspDescription.firstKey();
350 } else if (it == gamutCuspDescription.end()) {
351 firstAngle = gamutCuspDescription.lastKey();
352 secondAngle = gamutCuspDescription.firstKey();
353 } else {
354 firstAngle = std::prev(it).key();
355 secondAngle = it.key();
356 }
357
358 const float angle = firstAngle;
359 const float nextAngle = secondAngle;
360
361 QPointF lch1 = gamutCuspDescription.value(angle);
362 QPointF lch2 = gamutCuspDescription.value(nextAngle);
363 const float t = hue < angle
364 ? (hue - angle + 1.0) / (nextAngle + 1.0 - angle)
365 : (hue - angle) / (nextAngle - angle);
366 return QLineF(lch1, lch2).pointAt(t);
367}

◆ deltaE76()

float KoGamutMappingUtils::deltaE76 ( const QVector< float > & a,
const QVector< float > & b )

deltaE76 calculates the difference between two colors, represented as normalized channels.

Returns
The difference. Given that the input is scaled to [0 1], the output will also be [0 1]

Definition at line 17 of file KoGamutMappingUtils.cpp.

17 {
18 //This is E76, E2000 implementation is too complicated and buggy.
19 float dL = a[0] - b[0];
20 float dA = a[1] - b[1];
21 float dB = a[2] - b[2];
22 return qSqrt(dL * dL + dA * dA + dB * dB);
23}

◆ getGamutCuspDescription()

QMap< float, QPointF > KoGamutMappingUtils::getGamutCuspDescription ( const KoColorProfile * profile,
KoGamutMappingUtils::MappingModel model,
const int samplesPerChannel = 5 )

getGamutCuspDescription Generates a hexagonal gamut cusp description from a RGB or CMYK profile.

Parameters
profilecmyk or RGB profile to use.
Returns
gamut cusp description, consisting of hue and the XYZ of the maximum chroma.

Definition at line 249 of file KoGamutMappingUtils.cpp.

250{
251 QMap<float, QPointF> bounds;
255 QVector<float> xyzChannels(xyzCs->channelCount());
256 QVector<float> labChannels(xyzCs->channelCount());
257
258 QVector<QVector<float>> colorList;
259 if (profile->colorModelID() == RGBAColorModelID.id()) {
260 for (int i = 0; i < samplesPerChannel; i++) {
261 const float ch = float(i)/float(samplesPerChannel-1);
262 QVector<float> c = {ch, 1.0, 0.0, 1.0};
263 colorList << c;
264 QVector<float> c2 = {ch, 0.0, 1.0, 1.0};
265 colorList << c2;
266 }
267 for (int i = 0; i < samplesPerChannel; i++) {
268 const float ch = float(i)/float(samplesPerChannel-1);
269 QVector<float> c = {1.0, ch, 0.0, 1.0};
270 colorList << c;
271 QVector<float> c2 = {0.0, ch, 1.0, 1.0};
272 colorList << c2;
273 }
274 for (int i = 0; i < samplesPerChannel; i++) {
275 const float ch = float(i)/float(samplesPerChannel-1);
276 QVector<float> c = {1.0, 0.0, ch, 1.0};
277 colorList << c;
278 QVector<float> c2 = {0.0, 1.0, ch, 1.0};
279 colorList << c2;
280 }
281 } else if (profile->colorModelID() == CMYKAColorModelID.id()) {
282 for (int i = 0; i < samplesPerChannel; i++) {
283 const float ch = float(i)/float(samplesPerChannel-1);
284 QVector<float> c = {ch, 1.0, 0.0, 0.0, 1.0};
285 colorList << c;
286 QVector<float> c2 = {ch, 0.0, 1.0, 0.0, 1.0};
287 colorList << c2;
288 }
289 for (int i = 0; i < samplesPerChannel; i++) {
290 const float ch = float(i)/float(samplesPerChannel-1);
291 QVector<float> c = {1.0, ch, 0.0, 0.0, 1.0};
292 colorList << c;
293 QVector<float> c2 = {0.0, ch, 1.0, 0.0, 1.0};
294 colorList << c2;
295 }
296 for (int i = 0; i < samplesPerChannel; i++) {
297 const float ch = float(i)/float(samplesPerChannel-1);
298 QVector<float> c = {1.0, 0.0, ch, 0.0, 1.0};
299 colorList << c;
300 QVector<float> c2 = {0.0, 1.0, ch, 0.0, 1.0};
301 colorList << c2;
302 }
303 }
304 KoColor c(cs);
305
306 Q_FOREACH( const QVector<float> channels, colorList) {
307 cs->fromNormalisedChannelsValue(c.data(), channels);
308 std::array<qreal, 3> lch;
309 if (model == LAB) {
310 KoColor labC;
311 cs->convertPixelsTo(c.data(), labC.data(), labCs, 1,
314 labCs->normalisedChannelsValue(labC.data(), labChannels);
315 LabToLCH(labChannels[0], labChannels[1], labChannels[2], &lch[0], &lch[1], &lch[2]);
316 } else {
317
318 KoColor xyzC;
319 cs->convertPixelsTo(c.data(), xyzC.data(), xyzCs, 1,
322 xyzCs->normalisedChannelsValue(xyzC.data(), xyzChannels);
323
324 if (model == ICtCp) {
325 QVector4D ictcp = KoGamutMappingUtils::XYZToICtCp(QVector4D (xyzChannels[0], xyzChannels[1], xyzChannels[2], 0));
326 LabToLCH(ictcp.x(), ictcp.y()+0.5, ictcp.z()+0.5, &lch[0], &lch[1], &lch[2]);
327 } else {
328 QVector4D okLab = KoGamutMappingUtils::XYZToOkLab(QVector4D (xyzChannels[0], xyzChannels[1], xyzChannels[2], 0));
329 LabToLCH(okLab.x(), okLab.y()+0.5, okLab.z()+0.5, &lch[0], &lch[1], &lch[2]);
330 }
331 }
332
333 bounds.insert(lch[2], QPointF(lch[1], lch[0]));
334 }
335
336 return bounds;
337}
void LabToLCH(const qreal l, const qreal a, const qreal b, qreal *L, qreal *C, qreal *H)
const KoID Float32BitsColorDepthID("F32", ki18n("32-bit float/channel"))
const KoID XYZAColorModelID("XYZA", ki18n("XYZ/Alpha"))
const KoID Integer8BitsColorDepthID("U8", ki18n("8-bit integer/channel"))
const KoID CMYKAColorModelID("CMYKA", ki18n("CMYK/Alpha"))
const KoID LABAColorModelID("LABA", ki18n("L*a*b*/Alpha"))
const KoID RGBAColorModelID("RGBA", ki18n("RGB/Alpha"))
virtual quint32 channelCount() const =0
virtual void normalisedChannelsValue(const quint8 *pixel, QVector< float > &channels) const =0
virtual void fromNormalisedChannelsValue(quint8 *pixel, const QVector< float > &values) const =0
virtual bool convertPixelsTo(const quint8 *src, quint8 *dst, const KoColorSpace *dstColorSpace, quint32 numPixels, KoColorConversionTransformation::Intent renderingIntent, KoColorConversionTransformation::ConversionFlags conversionFlags) const
quint8 * data()
Definition KoColor.h:144
QString id() const
Definition KoID.cpp:63
#define bounds(x, a, b)
QVector4D KRITAPIGMENT_EXPORT XYZToICtCp(const QVector4D &xyz)
XYZToICtCp Ad-hoc implementation of ICtCp for use in filters. Unlike the official YCbCr-compatible st...
QVector4D KRITAPIGMENT_EXPORT XYZToOkLab(const QVector4D &xyz)
XYZToOkLab Ad-hoc implementation of OkLAB. This takes a QVector4D and transforms it to the OkLAB mode...
virtual QString colorModelID() const
const KoColorSpace * colorSpace(const QString &colorModelId, const QString &colorDepthId, const KoColorProfile *profile)
static KoColorSpaceRegistry * instance()

References KoColorConversionTransformation::adjustmentConversionFlags(), bounds, KoColorSpace::channelCount(), CMYKAColorModelID, KoColorProfile::colorModelID(), KoColorSpaceRegistry::colorSpace(), KoColorSpace::convertPixelsTo(), KoColor::data(), Float32BitsColorDepthID, KoColorSpace::fromNormalisedChannelsValue(), ICtCp, KoID::id(), KoColorSpaceRegistry::instance(), Integer8BitsColorDepthID, KoColorConversionTransformation::IntentRelativeColorimetric, LAB, LABAColorModelID, LabToLCH(), KoColorSpace::normalisedChannelsValue(), RGBAColorModelID, XYZAColorModelID, XYZToICtCp(), and XYZToOkLab().

◆ ICtCpToXYZ()

QVector4D KoGamutMappingUtils::ICtCpToXYZ ( const QVector4D & iCtCp)

Definition at line 240 of file KoGamutMappingUtils.cpp.

241{
242 const QVector4D lmsPrime = lmsToICtCp_inv * iCtCp;
243 const QVector4D lms = QVector4D(removeSmpte2048Curve(lmsPrime.x()),
244 removeSmpte2048Curve(lmsPrime.y()),
245 removeSmpte2048Curve(lmsPrime.z()), 0.0);
246 return xyz50To65_inv * (XYZd65toDolbyLMS_inv * lms);
247}
ALWAYS_INLINE float removeSmpte2048Curve(float x, float refWhite=203.0) noexcept
const QMatrix4x4 xyz50To65_inv
const QMatrix4x4 XYZd65toDolbyLMS_inv
const QMatrix4x4 lmsToICtCp_inv

References lmsToICtCp_inv, removeSmpte2048Curve(), xyz50To65_inv, and XYZd65toDolbyLMS_inv.

◆ OkLabToXYZ()

QVector4D KoGamutMappingUtils::OkLabToXYZ ( const QVector4D & okLab)

Definition at line 221 of file KoGamutMappingUtils.cpp.

221 {
222 const QVector4D lmsPrime = lmsToOkLab_inv * okLab;
223 const QVector4D lms = QVector4D(powf(lmsPrime.x(), 3.0),
224 powf(lmsPrime.y(), 3.0),
225 powf(lmsPrime.z(), 3.0), 0.0);
226 return xyz50To65_inv * (XYZtoOkLABLMS_inv * lms);
227}
const QMatrix4x4 XYZtoOkLABLMS_inv
const QMatrix4x4 lmsToOkLab_inv

References lmsToOkLab_inv, xyz50To65_inv, and XYZtoOkLABLMS_inv.

◆ reinhardToneMapping()

float KoGamutMappingUtils::reinhardToneMapping ( const float linearY,
const float maxY )

reinhardToneMapping Implements 'extended' reinhard tonemapping.

Parameters
linearY– luminance, linear.
maxWhite– maximum white to scale down, also linear
Returns
tone mapped value

Definition at line 25 of file KoGamutMappingUtils.cpp.

26{
27 const float num = linearY * (1.0f + (linearY/(maxY*maxY)));
28 return num / (linearY + 1.0f);
29}

◆ XYZToICtCp()

QVector4D KoGamutMappingUtils::XYZToICtCp ( const QVector4D & xyz)

XYZToICtCp Ad-hoc implementation of ICtCp for use in filters. Unlike the official YCbCr-compatible standard, this one doesn't have a 0.5 offset in the Ct and Cp channels.

Parameters
xyz
Returns

Definition at line 229 of file KoGamutMappingUtils.cpp.

230{
231 const QVector4D lms = XYZd65toDolbyLMS * (xyz50To65 * xyz);
232
233 const QVector4D lmsPrime = QVector4D(applySmpte2048Curve(lms.x()),
234 applySmpte2048Curve(lms.y()),
235 applySmpte2048Curve(lms.z()), 0.0);
236
237 return lmsToICtCp * lmsPrime;
238}
ALWAYS_INLINE float applySmpte2048Curve(float x, float refWhite=203.0) noexcept
const QMatrix4x4 XYZd65toDolbyLMS
const QMatrix4x4 xyz50To65
const QMatrix4x4 lmsToICtCp

References applySmpte2048Curve(), lmsToICtCp, xyz50To65, and XYZd65toDolbyLMS.

◆ XYZToOkLab()

QVector4D KoGamutMappingUtils::XYZToOkLab ( const QVector4D & xyz)

XYZToOkLab Ad-hoc implementation of OkLAB. This takes a QVector4D and transforms it to the OkLAB model.

Parameters
xyz
Returns
Oklab color.

Definition at line 211 of file KoGamutMappingUtils.cpp.

211 {
212 const QVector4D lms = XYZtoOkLABLMS * (xyz50To65 * xyz);
213
214 const QVector4D lmsPrime = QVector4D(cbrtf(lms.x()),
215 cbrtf(lms.y()),
216 cbrtf(lms.z()), 0.0);
217
218 return lmsToOkLab * lmsPrime;
219}
const QMatrix4x4 XYZtoOkLABLMS
const QMatrix4x4 lmsToOkLab

References lmsToOkLab, xyz50To65, and XYZtoOkLABLMS.