Krita Source Code Documentation
Loading...
Searching...
No Matches
KoColorTransferFunctions.h File Reference
#include <cmath>
#include <QtGlobal>
#include <KoAlwaysInline.h>
#include <KoMultiArchBuildSupport.h>
#include <KoStreamedMath.h>

Go to the source code of this file.

Classes

struct  KoColorTransferFunctions< Arch >
 

Enumerations

enum class  ConversionPolicy { KeepTheSame , ApplyPQ , ApplyHLG , ApplySMPTE428 }
 
enum class  LinearizePolicy { KeepTheSame , LinearFromPQ , LinearFromHLG , LinearFromSMPTE428 }
 The KoColorTransferFunctions class. More...
 

Functions

ALWAYS_INLINE float applyHLGCurve (float x) noexcept
 
ALWAYS_INLINE void applyHLGOOTF (float *rgb, const double *lumaCoefficients, float gamma=1.2f) noexcept
 
ALWAYS_INLINE float applySmpte2048Curve (float x, float refWhite=203.0) noexcept
 
ALWAYS_INLINE float applySMPTE_ST_428Curve (float x) noexcept
 
ALWAYS_INLINE float HLGOOTFGamma (float nominalPeak) noexcept
 
ALWAYS_INLINE float removeHLGCurve (float x) noexcept
 
ALWAYS_INLINE void removeHLGOOTF (float *rgb, const double *lumaCoefficients, float gamma=1.2f) noexcept
 
ALWAYS_INLINE float removeSmpte2048Curve (float x, float refWhite=203.0) noexcept
 
ALWAYS_INLINE float removeSMPTE_ST_428Curve (float x) noexcept
 

Variables

static constexpr uint16_t max12bit = 4095.f
 
static constexpr float max16bit = 65535.0f
 
static constexpr float multiplier10bit = 1.0f / 1023.0f
 
static constexpr float multiplier12bit = 1.0f / 4095.0f
 
static constexpr float multiplier16bit = 1.0f / max16bit
 

Enumeration Type Documentation

◆ ConversionPolicy

enum class ConversionPolicy
strong
Enumerator
KeepTheSame 
ApplyPQ 
ApplyHLG 
ApplySMPTE428 

Definition at line 36 of file KoColorTransferFunctions.h.

◆ LinearizePolicy

enum class LinearizePolicy
strong

The KoColorTransferFunctions class.

A number of often used transferFunctions.

These functions can, at the time of writing, not be implemented in ICC profiles, so instead, we apply or remove the curve as necessary.

Enumerator
KeepTheSame 
LinearFromPQ 
LinearFromHLG 
LinearFromSMPTE428 

Definition at line 28 of file KoColorTransferFunctions.h.

Function Documentation

◆ applyHLGCurve()

ALWAYS_INLINE float applyHLGCurve ( float x)
noexcept

This scales everything so that diffuse white is at 75~%, as per BT.2408, pg 4-5, and BT. 2390 pg. 52-53.

Definition at line 104 of file KoColorTransferFunctions.h.

105{
106 const float a = 0.17883277f;
107 const float b = 0.28466892f;
108 const float c = 0.55991073f;
109
112 const float scale = (1.0f / 3.7743f);
113
114 float val = x * scale;
115
116 if (val > 1.0f / 12.0f) {
117 val = (a * logf(12.0f * (val) - b) + c) ;
118 } else {
119 val = (sqrtf(3.0f * (val)));
120 }
121 return qBound(0.0f, val, 1.0f);
122}

◆ applyHLGOOTF()

ALWAYS_INLINE void applyHLGOOTF ( float * rgb,
const double * lumaCoefficients,
float gamma = 1.2f )
noexcept

Definition at line 75 of file KoColorTransferFunctions.h.

78{
79 const float luma = (rgb[0] * static_cast<float>(lumaCoefficients[0]))
80 + (rgb[1] * static_cast<float>(lumaCoefficients[1]))
81 + (rgb[2] * static_cast<float>(lumaCoefficients[2]));
82 const float a = powf(luma, gamma);
83 rgb[0] *= a;
84 rgb[1] *= a;
85 rgb[2] *= a;
86}

◆ applySmpte2048Curve()

ALWAYS_INLINE float applySmpte2048Curve ( float x,
float refWhite = 203.0 )
noexcept

Definition at line 38 of file KoColorTransferFunctions.h.

39{
40 const float m1 = 2610.0f / 4096.0f / 4.0f;
41 const float m2 = 2523.0f / 4096.0f * 128.0f;
42 const float a1 = 3424.0f / 4096.0f;
43 const float c2 = 2413.0f / 4096.0f * 32.0f;
44 const float c3 = 2392.0f / 4096.0f * 32.0f;
45 const float a4 = 1.0f;
46 const float x_p = powf((refWhite/10000.0f) * std::max(0.0f, x), m1);
47 const float res = powf((a1 + c2 * x_p) / (a4 + c3 * x_p), m2);
48 return res;
49}

◆ applySMPTE_ST_428Curve()

ALWAYS_INLINE float applySMPTE_ST_428Curve ( float x)
noexcept

Definition at line 140 of file KoColorTransferFunctions.h.

141{
142 return powf(48.0f * x * (1.f / 52.37f), (1.f / 2.6f));
143}

◆ HLGOOTFGamma()

ALWAYS_INLINE float HLGOOTFGamma ( float nominalPeak)
noexcept

Definition at line 66 of file KoColorTransferFunctions.h.

67{
68 const float k = 1.111f;
69 return 1.2f * powf(k, log2f(nominalPeak * (1.f / 1000.0f)));
70}

◆ removeHLGCurve()

ALWAYS_INLINE float removeHLGCurve ( float x)
noexcept

Definition at line 124 of file KoColorTransferFunctions.h.

125{
126 const float a = 0.17883277f;
127 const float b = 0.28466892f;
128 const float c = 0.55991073f;
129
130 const float scale = 3.7743f;
131
132 if (x <= 0.5f) {
133 // return (powf(x, 2.0) / 3.0);
134 return ((x * x) / 3.0f) * scale;
135 } else {
136 return ((expf((x - c) * (1.f / a)) + b) * (1.f / 12.0f)) * scale;
137 }
138}

◆ removeHLGOOTF()

ALWAYS_INLINE void removeHLGOOTF ( float * rgb,
const double * lumaCoefficients,
float gamma = 1.2f )
noexcept

Definition at line 91 of file KoColorTransferFunctions.h.

94{
95 const float luma = (rgb[0] * static_cast<float>(lumaCoefficients[0]))
96 + (rgb[1] * static_cast<float>(lumaCoefficients[1]))
97 + (rgb[2] * static_cast<float>(lumaCoefficients[2]));
98 const float multiplier = powf(luma, 1.0 / gamma) ;
99 rgb[0] *= multiplier;
100 rgb[1] *= multiplier;
101 rgb[2] *= multiplier;
102}

◆ removeSmpte2048Curve()

ALWAYS_INLINE float removeSmpte2048Curve ( float x,
float refWhite = 203.0 )
noexcept

Definition at line 51 of file KoColorTransferFunctions.h.

52{
53 const float m1_r = 4096.0f * 4.0f / 2610.0f;
54 const float m2_r = 4096.0f / 2523.0f / 128.0f;
55 const float a1 = 3424.0f / 4096.0f;
56 const float c2 = 2413.0f / 4096.0f * 32.0f;
57 const float c3 = 2392.0f / 4096.0f * 32.0f;
58
59 const float x_p = powf(x, m2_r);
60 const float res = powf(qMax(0.0f, x_p - a1) / (c2 - c3 * x_p), m1_r);
61 return res * (10000.0f/refWhite);
62}

◆ removeSMPTE_ST_428Curve()

ALWAYS_INLINE float removeSMPTE_ST_428Curve ( float x)
noexcept

Definition at line 145 of file KoColorTransferFunctions.h.

146{
147 return (52.37f / 48.0f) * powf(x, 2.6f);
148}

Variable Documentation

◆ max12bit

constexpr uint16_t max12bit = 4095.f
staticconstexpr

Definition at line 30 of file KoColorTransferFunctions.h.

◆ max16bit

constexpr float max16bit = 65535.0f
staticconstexpr

Definition at line 31 of file KoColorTransferFunctions.h.

◆ multiplier10bit

constexpr float multiplier10bit = 1.0f / 1023.0f
staticconstexpr

Definition at line 32 of file KoColorTransferFunctions.h.

◆ multiplier12bit

constexpr float multiplier12bit = 1.0f / 4095.0f
staticconstexpr

Definition at line 33 of file KoColorTransferFunctions.h.

◆ multiplier16bit

constexpr float multiplier16bit = 1.0f / max16bit
staticconstexpr

Definition at line 34 of file KoColorTransferFunctions.h.