diff options
Diffstat (limited to 'src/gui/painting/qcolortransfertable_p.h')
-rw-r--r-- | src/gui/painting/qcolortransfertable_p.h | 146 |
1 files changed, 66 insertions, 80 deletions
diff --git a/src/gui/painting/qcolortransfertable_p.h b/src/gui/painting/qcolortransfertable_p.h index bbc9f08dcc..ce6ad0c4b2 100644 --- a/src/gui/painting/qcolortransfertable_p.h +++ b/src/gui/painting/qcolortransfertable_p.h @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2018 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtGui module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2018 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QCOLORTRANSFERTABLE_P_H #define QCOLORTRANSFERTABLE_P_H @@ -65,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()) @@ -94,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) { @@ -116,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 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()) - 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; } @@ -132,43 +115,25 @@ 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) 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 >= 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 >= 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; } 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)) @@ -218,15 +183,36 @@ 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; +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) { 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()) |