diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2024-04-09 17:57:29 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2024-04-26 16:19:36 +0000 |
commit | eccaf0abec8e1e9c055d409575e5a6c894879a06 (patch) | |
tree | 4bf91424612ec16ee7ee9de58636360c55ad353e /src/gui | |
parent | 02174b9680339c08937ea50c743abcdfbe40e3d1 (diff) |
QColorTransferTable: de-duplicate inverse lookup code
As a drive-by, drop the unnecessary floor() and just truncate. These
operations are the same for positive numbers, and by the time we reach
the code, we have already checked and dealt with negative numbers.
Change-Id: I3e99438f11e3a6c7862b8592b7bb4ddda1b33666
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/painting/qcolortransfertable_p.h | 54 |
1 files changed, 23 insertions, 31 deletions
diff --git a/src/gui/painting/qcolortransfertable_p.h b/src/gui/painting/qcolortransfertable_p.h index 51c9cff6d6..ce6ad0c4b2 100644 --- a/src/gui/painting/qcolortransfertable_p.h +++ b/src/gui/painting/qcolortransfertable_p.h @@ -101,7 +101,7 @@ public: 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 lo = static_cast<uint32_t>(x); const uint32_t hi = std::min(lo + 1, m_tableSize - 1); const float frac = x - lo; if (!m_table16.isEmpty()) @@ -120,36 +120,10 @@ public: return 0.0f; if (x >= 1.0f) return 1.0f; - if (!m_table16.isEmpty()) { - const float v = x * 65535.0f; - 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]; - const float y2 = m_table16[i]; - 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]; - const float y2 = m_table8[i]; - Q_ASSERT(v >= y1 && v <= y2); - const float fr = (v - y1) / (y2 - y1); - return (i + fr) * (1.0f / (m_tableSize - 1)); - } + if (!m_table16.isEmpty()) + return inverseLookup(x * 65535.0f, resultLargerThan, m_table16, m_tableSize - 1); + if (!m_table8.isEmpty()) + return inverseLookup(x * 255.0f, resultLargerThan, m_table8, m_tableSize - 1); return x; } @@ -213,6 +187,24 @@ public: uint32_t m_tableSize = 0; QList<uint8_t> m_table8; QList<uint16_t> m_table16; +private: + template<typename T> + static float inverseLookup(float needle, float resultLargerThan, const QList<T> &table, quint32 tableMax) + { + uint32_t i = static_cast<uint32_t>(resultLargerThan * tableMax); + auto it = std::lower_bound(table.cbegin() + i, table.cend(), needle); + i = it - table.cbegin(); + if (i == 0) + return 0.0f; + if (i >= tableMax) + return 1.0f; + const float y1 = table[i - 1]; + const float y2 = table[i]; + Q_ASSERT(needle >= y1 && needle <= y2); + const float fr = (needle - y1) / (y2 - y1); + return (i + fr) * (1.0f / tableMax); + } + }; inline bool operator!=(const QColorTransferTable &t1, const QColorTransferTable &t2) |