diff options
Diffstat (limited to 'src/gui/painting/qcolortransfertable_p.h')
-rw-r--r-- | src/gui/painting/qcolortransfertable_p.h | 60 |
1 files changed, 45 insertions, 15 deletions
diff --git a/src/gui/painting/qcolortransfertable_p.h b/src/gui/painting/qcolortransfertable_p.h index ac1f843912..51c9cff6d6 100644 --- a/src/gui/painting/qcolortransfertable_p.h +++ b/src/gui/painting/qcolortransfertable_p.h @@ -29,25 +29,38 @@ QT_BEGIN_NAMESPACE class Q_GUI_EXPORT QColorTransferTable { public: - QColorTransferTable() noexcept - : m_tableSize(0) - { } - QColorTransferTable(uint32_t size, const QList<uint8_t> &table) noexcept - : m_tableSize(size), m_table8(table) + enum Type : uint8_t { + TwoWay = 0, + OneWay, + }; + QColorTransferTable() noexcept = default; + QColorTransferTable(uint32_t size, const QList<uint8_t> &table, Type type = TwoWay) noexcept + : m_type(type), m_tableSize(size), m_table8(table) { - Q_ASSERT(qsizetype(size) <= table.count()); + Q_ASSERT(qsizetype(size) <= table.size()); } - QColorTransferTable(uint32_t size, const QList<uint16_t> &table) noexcept - : m_tableSize(size), m_table16(table) + QColorTransferTable(uint32_t size, const QList<uint16_t> &table, Type type = TwoWay) noexcept + : m_type(type), m_tableSize(size), m_table16(table) { - Q_ASSERT(qsizetype(size) <= table.count()); + Q_ASSERT(qsizetype(size) <= table.size()); } - bool isEmpty() const + bool isEmpty() const noexcept { return m_tableSize == 0; } + bool isIdentity() const + { + if (isEmpty()) + return true; + if (m_tableSize != 2) + return false; + if (!m_table8.isEmpty()) + return m_table8[0] == 0 && m_table8[1] == 255; + return m_table16[0] == 0 && m_table16[1] == 65535; + } + bool checkValidity() const { if (isEmpty()) @@ -58,7 +71,11 @@ public: // At least 2 elements if (m_tableSize < 2) return false; - // The table must describe an injective curve: + return (m_type == OneWay) || checkInvertibility(); + } + bool checkInvertibility() const + { + // The two-way tables must describe an injective curve: if (!m_table8.isEmpty()) { uint8_t val = 0; for (uint i = 0; i < m_tableSize; ++i) { @@ -80,15 +97,17 @@ public: float apply(float x) const { + if (isEmpty()) + return x; x = std::clamp(x, 0.0f, 1.0f); x *= m_tableSize - 1; const uint32_t lo = static_cast<uint32_t>(std::floor(x)); const uint32_t hi = std::min(lo + 1, m_tableSize - 1); const float frac = x - lo; if (!m_table16.isEmpty()) - return (m_table16[lo] * (1.0f - frac) + m_table16[hi] * frac) * (1.0f/65535.0f); + return (m_table16[lo] + (m_table16[hi] - m_table16[lo]) * frac) * (1.0f/65535.0f); if (!m_table8.isEmpty()) - return (m_table8[lo] * (1.0f - frac) + m_table8[hi] * frac) * (1.0f/255.0f); + return (m_table8[lo] + (m_table8[hi] - m_table8[lo]) * frac) * (1.0f/255.0f); return x; } @@ -96,6 +115,7 @@ public: float applyInverse(float x, float resultLargerThan = 0.0f) const { Q_ASSERT(resultLargerThan >= 0.0f && resultLargerThan <= 1.0f); + Q_ASSERT(m_type == TwoWay); if (x <= 0.0f) return 0.0f; if (x >= 1.0f) @@ -105,6 +125,8 @@ public: uint32_t i = static_cast<uint32_t>(std::floor(resultLargerThan * (m_tableSize - 1))); auto it = std::lower_bound(m_table16.cbegin() + i, m_table16.cend(), v); i = it - m_table16.cbegin(); + if (i == 0) + return 0.0f; if (i >= m_tableSize - 1) return 1.0f; const float y1 = m_table16[i - 1]; @@ -112,13 +134,14 @@ public: Q_ASSERT(v >= y1 && v <= y2); const float fr = (v - y1) / (y2 - y1); return (i + fr) * (1.0f / (m_tableSize - 1)); - } if (!m_table8.isEmpty()) { const float v = x * 255.0f; uint32_t i = static_cast<uint32_t>(std::floor(resultLargerThan * (m_tableSize - 1))); auto it = std::lower_bound(m_table8.cbegin() + i, m_table8.cend(), v); i = it - m_table8.cbegin(); + if (i == 0) + return 0.0f; if (i >= m_tableSize - 1) return 1.0f; const float y1 = m_table8[i - 1]; @@ -133,6 +156,10 @@ public: bool asColorTransferFunction(QColorTransferFunction *transferFn) { Q_ASSERT(transferFn); + if (isEmpty()) { + *transferFn = QColorTransferFunction(); + return true; + } if (m_tableSize < 2) return false; if (!m_table8.isEmpty() && (m_table8[0] != 0 || m_table8[m_tableSize - 1] != 255)) @@ -182,7 +209,8 @@ public: friend inline bool operator!=(const QColorTransferTable &t1, const QColorTransferTable &t2); friend inline bool operator==(const QColorTransferTable &t1, const QColorTransferTable &t2); - uint32_t m_tableSize; + Type m_type = TwoWay; + uint32_t m_tableSize = 0; QList<uint8_t> m_table8; QList<uint16_t> m_table16; }; @@ -191,6 +219,8 @@ inline bool operator!=(const QColorTransferTable &t1, const QColorTransferTable { if (t1.m_tableSize != t2.m_tableSize) return true; + if (t1.m_type != t2.m_type) + return true; if (t1.m_table8.isEmpty() != t2.m_table8.isEmpty()) return true; if (t1.m_table16.isEmpty() != t2.m_table16.isEmpty()) |