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
4#include "qcolortrclut_p.h"
5#include "qcolortransferfunction_p.h"
6#include "qcolortransfertable_p.h"
7#include <qmath.h>
8
9QT_BEGIN_NAMESPACE
10std::shared_ptr<QColorTrcLut> QColorTrcLut::create()
11{
12 struct Access : QColorTrcLut {};
13 return std::make_shared<Access>();
14}
15
16std::shared_ptr<QColorTrcLut> QColorTrcLut::fromGamma(qreal gamma)
17{
18 auto cp = create();
19
20 for (int i = 0; i <= (255 * 16); ++i) {
21 cp->m_toLinear[i] = ushort(qRound(d: qPow(x: i / qreal(255 * 16), y: gamma) * (255 * 256)));
22 cp->m_fromLinear[i] = ushort(qRound(d: qPow(x: i / qreal(255 * 16), y: qreal(1) / gamma) * (255 * 256)));
23 }
24
25 return cp;
26}
27
28std::shared_ptr<QColorTrcLut> QColorTrcLut::fromTransferFunction(const QColorTransferFunction &fun)
29{
30 auto cp = create();
31 QColorTransferFunction inv = fun.inverted();
32
33 for (int i = 0; i <= (255 * 16); ++i) {
34 cp->m_toLinear[i] = ushort(qRound(f: fun.apply(x: i / qreal(255 * 16)) * (255 * 256)));
35 cp->m_fromLinear[i] = ushort(qRound(f: inv.apply(x: i / qreal(255 * 16)) * (255 * 256)));
36 }
37
38 return cp;
39}
40
41std::shared_ptr<QColorTrcLut> QColorTrcLut::fromTransferTable(const QColorTransferTable &table)
42{
43 auto cp = create();
44
45 float minInverse = 0.0f;
46 for (int i = 0; i <= (255 * 16); ++i) {
47 cp->m_toLinear[i] = ushort(qBound(min: 0, val: qRound(f: table.apply(x: i / qreal(255 * 16)) * (255 * 256)), max: 65280));
48 minInverse = table.applyInverse(x: i / qreal(255 * 16), resultLargerThan: minInverse);
49 cp->m_fromLinear[i] = ushort(qBound(min: 0, val: qRound(f: minInverse * (255 * 256)), max: 65280));
50 }
51
52 return cp;
53}
54
55QT_END_NAMESPACE
56

source code of qtbase/src/gui/painting/qcolortrclut.cpp