e93a3b62edb5fc8d31d022a4a140d9e0714f5018
[WebKit-https.git] / Source / WebCore / platform / graphics / filters / FEComponentTransfer.h
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
4  * Copyright (C) 2005 Eric Seidel <eric@webkit.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #pragma once
23
24 #include "FilterEffect.h"
25
26 #include "Filter.h"
27 #include <wtf/Vector.h>
28
29 namespace WebCore {
30
31 enum ComponentTransferType {
32     FECOMPONENTTRANSFER_TYPE_UNKNOWN  = 0,
33     FECOMPONENTTRANSFER_TYPE_IDENTITY = 1,
34     FECOMPONENTTRANSFER_TYPE_TABLE    = 2,
35     FECOMPONENTTRANSFER_TYPE_DISCRETE = 3,
36     FECOMPONENTTRANSFER_TYPE_LINEAR   = 4,
37     FECOMPONENTTRANSFER_TYPE_GAMMA    = 5
38 };
39
40 struct ComponentTransferFunction {
41     ComponentTransferFunction() = default;
42
43     ComponentTransferType type { FECOMPONENTTRANSFER_TYPE_UNKNOWN };
44
45     float slope { 0 };
46     float intercept { 0 };
47     float amplitude { 0 };
48     float exponent { 0 };
49     float offset { 0 };
50
51     Vector<float> tableValues;
52 };
53
54 class FEComponentTransfer : public FilterEffect {
55 public:
56     static Ref<FEComponentTransfer> create(Filter&, const ComponentTransferFunction& redFunc, const ComponentTransferFunction& greenFunc,
57                                            const ComponentTransferFunction& blueFunc, const ComponentTransferFunction& alphaFunc);
58
59     ComponentTransferFunction redFunction() const { return m_redFunction; }
60     ComponentTransferFunction greenFunction() const { return m_greenFunction; }
61     ComponentTransferFunction blueFunction() const { return m_blueFunction; }
62     ComponentTransferFunction alphaFunction() const { return m_alphaFunction; }
63
64 private:
65     FEComponentTransfer(Filter&, const ComponentTransferFunction& redFunc, const ComponentTransferFunction& greenFunc,
66                         const ComponentTransferFunction& blueFunc, const ComponentTransferFunction& alphaFunc);
67
68     using LookupTable = std::array<uint8_t, 256>;
69
70     static void computeIdentityTable(LookupTable&, const ComponentTransferFunction&);
71     static void computeTabularTable(LookupTable&, const ComponentTransferFunction&);
72     static void computeDiscreteTable(LookupTable&, const ComponentTransferFunction&);
73     static void computeLinearTable(LookupTable&, const ComponentTransferFunction&);
74     static void computeGammaTable(LookupTable&, const ComponentTransferFunction&);
75
76     void computeLookupTables(LookupTable& redTable, LookupTable& greenTable, LookupTable& blueTable, LookupTable& alphaTable);
77
78     void platformApplySoftware() override;
79
80     WTF::TextStream& externalRepresentation(WTF::TextStream&, int indention) const override;
81
82     ComponentTransferFunction m_redFunction;
83     ComponentTransferFunction m_greenFunction;
84     ComponentTransferFunction m_blueFunction;
85     ComponentTransferFunction m_alphaFunction;
86 };
87
88 } // namespace WebCore
89