summaryrefslogtreecommitdiffstats
path: root/src/gui/painting/qcolortrclut.cpp
blob: 8a7673bc009ba5ad89960d356e1fbfd35f8a7296 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (C) 2016 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

#include "qcolortrclut_p.h"
#include "qcolortransferfunction_p.h"
#include "qcolortransfertable_p.h"
#include <qmath.h>

QT_BEGIN_NAMESPACE
std::shared_ptr<QColorTrcLut> QColorTrcLut::create()
{
    struct Access : QColorTrcLut {};
    return std::make_shared<Access>();
}

std::shared_ptr<QColorTrcLut> QColorTrcLut::fromGamma(qreal gamma, Direction dir)
{
    auto cp = create();
    cp->setFromGamma(gamma, dir);
    return cp;
}

std::shared_ptr<QColorTrcLut> QColorTrcLut::fromTransferFunction(const QColorTransferFunction &fun, Direction dir)
{
    auto cp = create();
    cp->setFromTransferFunction(fun, dir);
    return cp;
}

std::shared_ptr<QColorTrcLut> QColorTrcLut::fromTransferTable(const QColorTransferTable &table, Direction dir)
{
    auto cp = create();
    cp->setFromTransferTable(table, dir);
    return cp;
}

void QColorTrcLut::setFromGamma(qreal gamma, Direction dir)
{
    if (dir & ToLinear) {
        if (!m_toLinear)
            m_toLinear.reset(new ushort[Resolution + 1]);
        for (int i = 0; i <= Resolution; ++i)
            m_toLinear[i] = ushort(qRound(qPow(i / qreal(Resolution), gamma) * (255 * 256)));
    }

    if (dir & FromLinear) {
        if (!m_fromLinear)
            m_fromLinear.reset(new ushort[Resolution + 1]);
        for (int i = 0; i <= Resolution; ++i)
            m_fromLinear[i] = ushort(qRound(qPow(i / qreal(Resolution), qreal(1) / gamma) * (255 * 256)));
    }
}

void QColorTrcLut::setFromTransferFunction(const QColorTransferFunction &fun, Direction dir)
{
    if (dir & ToLinear) {
        if (!m_toLinear)
            m_toLinear.reset(new ushort[Resolution + 1]);
        for (int i = 0; i <= Resolution; ++i)
            m_toLinear[i] = ushort(qRound(fun.apply(i / qreal(Resolution)) * (255 * 256)));
    }

    if (dir & FromLinear) {
        if (!m_fromLinear)
            m_fromLinear.reset(new ushort[Resolution + 1]);
        QColorTransferFunction inv = fun.inverted();
        for (int i = 0; i <= Resolution; ++i)
            m_fromLinear[i] = ushort(qRound(inv.apply(i / qreal(Resolution)) * (255 * 256)));
    }
}

void QColorTrcLut::setFromTransferTable(const QColorTransferTable &table, Direction dir)
{
    if (dir & ToLinear) {
        if (!m_toLinear)
            m_toLinear.reset(new ushort[Resolution + 1]);
        for (int i = 0; i <= Resolution; ++i)
            m_toLinear[i] = ushort(qBound(0, qRound(table.apply(i / qreal(Resolution)) * (255 * 256)), 65280));
    }

    if (dir & FromLinear) {
        if (!m_fromLinear)
            m_fromLinear.reset(new ushort[Resolution + 1]);
        float minInverse = 0.0f;
        for (int i = 0; i <= Resolution; ++i) {
            minInverse = table.applyInverse(i / qreal(Resolution), minInverse);
            m_fromLinear[i] = ushort(qBound(0, qRound(minInverse * (255 * 256)), 65280));
        }
    }
}

QT_END_NAMESPACE