Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcolortrclut.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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// Qt-Security score:significant reason:default
4
9#include "qcolortrc_p.h"
10#include <qmath.h>
11
13std::shared_ptr<QColorTrcLut> QColorTrcLut::create()
14{
15 struct Access : QColorTrcLut {};
16 return std::make_shared<Access>();
17}
18
19std::shared_ptr<QColorTrcLut> QColorTrcLut::fromGamma(float gamma, Direction dir)
20{
21 auto cp = create();
22 cp->setFromGamma(gamma, dir);
23 return cp;
24}
25
26std::shared_ptr<QColorTrcLut> QColorTrcLut::fromTrc(const QColorTrc &trc, Direction dir)
27{
28 if (!trc.isValid())
29 return nullptr;
30 auto cp = create();
31 cp->setFromTrc(trc, dir);
32 return cp;
33}
34
35void QColorTrcLut::setFromGamma(float gamma, Direction dir)
36{
37 constexpr float iRes = 1.f / float(Resolution);
38 if (dir & ToLinear) {
39 if (!m_toLinear)
40 m_toLinear.reset(new ushort[Resolution + 1]);
41 for (int i = 0; i <= Resolution; ++i) {
42 const int val = qRound(qPow(i * iRes, gamma) * (255 * 256));
43 m_toLinear[i] = qBound(0, val, 65280);
44 }
45 }
46
47 if (dir & FromLinear) {
48 const float iGamma = 1.f / gamma;
49 if (!m_fromLinear)
50 m_fromLinear.reset(new ushort[Resolution + 1]);
51 for (int i = 0; i <= Resolution; ++i)
52 m_fromLinear[i] = ushort(qRound(qBound(0.f, qPow(i * iRes, iGamma), 1.f) * (255 * 256)));
53 }
54}
55
56void QColorTrcLut::setFromTransferFunction(const QColorTransferFunction &fun, Direction dir)
57{
58 constexpr float iRes = 1.f / float(Resolution);
59 if (dir & ToLinear) {
60 if (!m_toLinear)
61 m_toLinear.reset(new ushort[Resolution + 1]);
62 for (int i = 0; i <= Resolution; ++i) {
63 const int val = qRound(fun.apply(i * iRes)* (255 * 256));
64 if (val > 65280 && i < m_unclampedToLinear)
65 m_unclampedToLinear = i;
66 m_toLinear[i] = qBound(0, val, 65280);
67 }
68 }
69
70 if (dir & FromLinear) {
71 if (!m_fromLinear)
72 m_fromLinear.reset(new ushort[Resolution + 1]);
73 QColorTransferFunction inv = fun.inverted();
74 for (int i = 0; i <= Resolution; ++i)
75 m_fromLinear[i] = ushort(qRound(qBound(0.f, inv.apply(i * iRes), 1.f) * (255 * 256)));
76 }
77}
78
79void QColorTrcLut::setFromTransferGenericFunction(const QColorTransferGenericFunction &fun, Direction dir)
80{
81 constexpr float iRes = 1.f / float(Resolution);
82 if (dir & ToLinear) {
83 if (!m_toLinear)
84 m_toLinear.reset(new ushort[Resolution + 1]);
85 for (int i = 0; i <= Resolution; ++i) {
86 const int val = qRound(fun.apply(i * iRes) * (255 * 256));
87 if (val > 65280 && i < m_unclampedToLinear)
88 m_unclampedToLinear = i;
89 m_toLinear[i] = qBound(0, val, 65280);
90 }
91 }
92
93 if (dir & FromLinear) {
94 if (!m_fromLinear)
95 m_fromLinear.reset(new ushort[Resolution + 1]);
96 for (int i = 0; i <= Resolution; ++i)
97 m_fromLinear[i] = ushort(qRound(qBound(0.f, fun.applyInverse(i * iRes), 1.f) * (255 * 256)));
98 }
99}
100
101void QColorTrcLut::setFromTransferTable(const QColorTransferTable &table, Direction dir)
102{
103 constexpr float iRes = 1.f / float(Resolution);
104 if (dir & ToLinear) {
105 if (!m_toLinear)
106 m_toLinear.reset(new ushort[Resolution + 1]);
107 for (int i = 0; i <= Resolution; ++i)
108 m_toLinear[i] = ushort(qRound(table.apply(i * iRes) * (255 * 256)));
109 }
110
111 if (dir & FromLinear) {
112 if (!m_fromLinear)
113 m_fromLinear.reset(new ushort[Resolution + 1]);
114 float minInverse = 0.0f;
115 for (int i = 0; i <= Resolution; ++i) {
116 minInverse = table.applyInverse(i * iRes, minInverse);
117 m_fromLinear[i] = ushort(qRound(minInverse * (255 * 256)));
118 }
119 }
120}
121
122void QColorTrcLut::setFromTrc(const QColorTrc &trc, Direction dir)
123{
124 switch (trc.m_type) {
125 case QColorTrc::Type::ParameterizedFunction:
126 return setFromTransferFunction(trc.fun(), dir);
127 case QColorTrc::Type::Table:
128 return setFromTransferTable(trc.table(), dir);
129 case QColorTrc::Type::GenericFunction:
130 return setFromTransferGenericFunction(trc.hdr(), dir);
131 case QColorTrc::Type::Uninitialized:
132 break;
133 }
134}
135
136QT_END_NAMESPACE
Combined button and popup list for selecting options.