| 1 | // Copyright (C) 2018 The Qt Company Ltd. | 
|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only | 
|---|
| 3 |  | 
|---|
| 4 | #ifndef QCOLORTRANSFERFUNCTION_P_H | 
|---|
| 5 | #define QCOLORTRANSFERFUNCTION_P_H | 
|---|
| 6 |  | 
|---|
| 7 | // | 
|---|
| 8 | //  W A R N I N G | 
|---|
| 9 | //  ------------- | 
|---|
| 10 | // | 
|---|
| 11 | // This file is not part of the Qt API.  It exists purely as an | 
|---|
| 12 | // implementation detail.  This header file may change from version to | 
|---|
| 13 | // version without notice, or even be removed. | 
|---|
| 14 | // | 
|---|
| 15 | // We mean it. | 
|---|
| 16 | // | 
|---|
| 17 |  | 
|---|
| 18 | #include <QtGui/private/qtguiglobal_p.h> | 
|---|
| 19 | #include <QtCore/QFlags> | 
|---|
| 20 |  | 
|---|
| 21 | #include <cmath> | 
|---|
| 22 |  | 
|---|
| 23 | QT_BEGIN_NAMESPACE | 
|---|
| 24 |  | 
|---|
| 25 | // Defines a ICC parametric curve type 4 | 
|---|
| 26 | class QColorTransferFunction | 
|---|
| 27 | { | 
|---|
| 28 | public: | 
|---|
| 29 | QColorTransferFunction() noexcept | 
|---|
| 30 | : m_a(1.0f), m_b(0.0f), m_c(1.0f), m_d(0.0f), m_e(0.0f), m_f(0.0f), m_g(1.0f) | 
|---|
| 31 | , m_flags(Hints(Hint::Calculated) | Hint::IsGamma | Hint::IsIdentity) | 
|---|
| 32 | { } | 
|---|
| 33 |  | 
|---|
| 34 | QColorTransferFunction(float a, float b, float c, float d, float e, float f, float g) noexcept | 
|---|
| 35 | : m_a(a), m_b(b), m_c(c), m_d(d), m_e(e), m_f(f), m_g(g), m_flags() | 
|---|
| 36 | { } | 
|---|
| 37 |  | 
|---|
| 38 | bool isGamma() const | 
|---|
| 39 | { | 
|---|
| 40 | updateHints(); | 
|---|
| 41 | return m_flags & Hint::IsGamma; | 
|---|
| 42 | } | 
|---|
| 43 | bool isIdentity() const | 
|---|
| 44 | { | 
|---|
| 45 | updateHints(); | 
|---|
| 46 | return m_flags & Hint::IsIdentity; | 
|---|
| 47 | } | 
|---|
| 48 | bool isSRgb() const | 
|---|
| 49 | { | 
|---|
| 50 | updateHints(); | 
|---|
| 51 | return m_flags & Hint::IsSRgb; | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | float apply(float x) const | 
|---|
| 55 | { | 
|---|
| 56 | if (x < m_d) | 
|---|
| 57 | return m_c * x + m_f; | 
|---|
| 58 | float t = std::pow(x: m_a * x + m_b, y: m_g); | 
|---|
| 59 | if (std::isfinite(x: t)) | 
|---|
| 60 | return t + m_e; | 
|---|
| 61 | if (t > 0.f) | 
|---|
| 62 | return 1.f; | 
|---|
| 63 | else | 
|---|
| 64 | return 0.f; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | QColorTransferFunction inverted() const | 
|---|
| 68 | { | 
|---|
| 69 | float a, b, c, d, e, f, g; | 
|---|
| 70 |  | 
|---|
| 71 | d = m_c * m_d + m_f; | 
|---|
| 72 |  | 
|---|
| 73 | if (std::isnormal(x: m_c)) { | 
|---|
| 74 | c = 1.0f / m_c; | 
|---|
| 75 | f = -m_f / m_c; | 
|---|
| 76 | } else { | 
|---|
| 77 | c = 0.0f; | 
|---|
| 78 | f = 0.0f; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | bool valid_abeg = std::isnormal(x: m_a) && std::isnormal(x: m_g); | 
|---|
| 82 | if (valid_abeg) | 
|---|
| 83 | a = std::pow(x: 1.0f / m_a, y: m_g); | 
|---|
| 84 | if (valid_abeg && !std::isfinite(x: a)) | 
|---|
| 85 | valid_abeg = false; | 
|---|
| 86 | if (valid_abeg) { | 
|---|
| 87 | b = -a * m_e; | 
|---|
| 88 | e = -m_b / m_a; | 
|---|
| 89 | g = 1.0f / m_g; | 
|---|
| 90 | } else { | 
|---|
| 91 | a = 0.0f; | 
|---|
| 92 | b = 0.0f; | 
|---|
| 93 | e = 1.0f; | 
|---|
| 94 | g = 1.0f; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | return QColorTransferFunction(a, b, c, d, e, f, g); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | // A few predefined curves: | 
|---|
| 101 | static QColorTransferFunction fromGamma(float gamma) | 
|---|
| 102 | { | 
|---|
| 103 | return QColorTransferFunction(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, gamma, | 
|---|
| 104 | Hints(Hint::Calculated) | Hint::IsGamma | | 
|---|
| 105 | (paramCompare(p1: gamma, p2: 1.0f) ? Hint::IsIdentity : Hint::NoHint)); | 
|---|
| 106 | } | 
|---|
| 107 | static QColorTransferFunction fromSRgb() | 
|---|
| 108 | { | 
|---|
| 109 | return QColorTransferFunction(1.0f / 1.055f, 0.055f / 1.055f, 1.0f / 12.92f, 0.04045f, 0.0f, 0.0f, 2.4f, | 
|---|
| 110 | Hints(Hint::Calculated) | Hint::IsSRgb); | 
|---|
| 111 | } | 
|---|
| 112 | static QColorTransferFunction fromProPhotoRgb() | 
|---|
| 113 | { | 
|---|
| 114 | return QColorTransferFunction(1.0f, 0.0f, 1.0f / 16.0f, 16.0f / 512.0f, 0.0f, 0.0f, 1.8f, | 
|---|
| 115 | Hints(Hint::Calculated)); | 
|---|
| 116 | } | 
|---|
| 117 | static QColorTransferFunction fromBt2020() | 
|---|
| 118 | { | 
|---|
| 119 | return QColorTransferFunction(1.0f / 1.0993f, 0.0993f / 1.0993f, 1.0f / 4.5f, 0.08145f, 0.0f, 0.0f, 2.2f, | 
|---|
| 120 | Hints(Hint::Calculated)); | 
|---|
| 121 | } | 
|---|
| 122 | bool matches(const QColorTransferFunction &o) const | 
|---|
| 123 | { | 
|---|
| 124 | return paramCompare(p1: m_a, p2: o.m_a) && paramCompare(p1: m_b, p2: o.m_b) | 
|---|
| 125 | && paramCompare(p1: m_c, p2: o.m_c) && paramCompare(p1: m_d, p2: o.m_d) | 
|---|
| 126 | && paramCompare(p1: m_e, p2: o.m_e) && paramCompare(p1: m_f, p2: o.m_f) | 
|---|
| 127 | && paramCompare(p1: m_g, p2: o.m_g); | 
|---|
| 128 | } | 
|---|
| 129 | friend inline bool operator==(const QColorTransferFunction &f1, const QColorTransferFunction &f2); | 
|---|
| 130 | friend inline bool operator!=(const QColorTransferFunction &f1, const QColorTransferFunction &f2); | 
|---|
| 131 |  | 
|---|
| 132 | float m_a; | 
|---|
| 133 | float m_b; | 
|---|
| 134 | float m_c; | 
|---|
| 135 | float m_d; | 
|---|
| 136 | float m_e; | 
|---|
| 137 | float m_f; | 
|---|
| 138 | float m_g; | 
|---|
| 139 |  | 
|---|
| 140 | enum class Hint : quint32 { | 
|---|
| 141 | NoHint = 0, | 
|---|
| 142 | Calculated = 1, | 
|---|
| 143 | IsGamma = 2, | 
|---|
| 144 | IsIdentity = 4, | 
|---|
| 145 | IsSRgb = 8 | 
|---|
| 146 | }; | 
|---|
| 147 |  | 
|---|
| 148 | Q_DECLARE_FLAGS(Hints, Hint); | 
|---|
| 149 |  | 
|---|
| 150 | private: | 
|---|
| 151 | QColorTransferFunction(float a, float b, float c, float d, float e, float f, float g, Hints flags) noexcept | 
|---|
| 152 | : m_a(a), m_b(b), m_c(c), m_d(d), m_e(e), m_f(f), m_g(g), m_flags(flags) | 
|---|
| 153 | { } | 
|---|
| 154 | static inline bool paramCompare(float p1, float p2) | 
|---|
| 155 | { | 
|---|
| 156 | // Much fuzzier than fuzzy compare. | 
|---|
| 157 | // It tries match parameters that has been passed through a 8.8 | 
|---|
| 158 | // fixed point form. | 
|---|
| 159 | return (qAbs(t: p1 - p2) <= (1.0f / 512.0f)); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | void updateHints() const | 
|---|
| 163 | { | 
|---|
| 164 | if (m_flags & Hint::Calculated) | 
|---|
| 165 | return; | 
|---|
| 166 | // We do not consider the case with m_d = 1.0f linear or simple, | 
|---|
| 167 | // since it wouldn't be linear for applyExtended(). | 
|---|
| 168 | bool simple = paramCompare(p1: m_a, p2: 1.0f) && paramCompare(p1: m_b, p2: 0.0f) | 
|---|
| 169 | && paramCompare(p1: m_d, p2: 0.0f) | 
|---|
| 170 | && paramCompare(p1: m_e, p2: 0.0f); | 
|---|
| 171 | if (simple) { | 
|---|
| 172 | m_flags |= Hint::IsGamma; | 
|---|
| 173 | if (qFuzzyCompare(p1: m_g, p2: 1.0f)) | 
|---|
| 174 | m_flags |= Hint::IsIdentity; | 
|---|
| 175 | } else { | 
|---|
| 176 | if (*this == fromSRgb()) | 
|---|
| 177 | m_flags |= Hint::IsSRgb; | 
|---|
| 178 | } | 
|---|
| 179 | m_flags |= Hint::Calculated; | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | mutable Hints m_flags; | 
|---|
| 183 | }; | 
|---|
| 184 |  | 
|---|
| 185 | Q_DECLARE_OPERATORS_FOR_FLAGS(QColorTransferFunction::Hints); | 
|---|
| 186 |  | 
|---|
| 187 | inline bool operator==(const QColorTransferFunction &f1, const QColorTransferFunction &f2) | 
|---|
| 188 | { | 
|---|
| 189 | return f1.matches(o: f2); | 
|---|
| 190 | } | 
|---|
| 191 | inline bool operator!=(const QColorTransferFunction &f1, const QColorTransferFunction &f2) | 
|---|
| 192 | { | 
|---|
| 193 | return !f1.matches(o: f2); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | QT_END_NAMESPACE | 
|---|
| 197 |  | 
|---|
| 198 | #endif // QCOLORTRANSFERFUNCTION_P_H | 
|---|
| 199 |  | 
|---|