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#ifndef FEColorMatrix_h
23#define FEColorMatrix_h
24
25#if ENABLE(FILTERS)
26#include "FilterEffect.h"
27
28#include "Filter.h"
29#include <wtf/Vector.h>
30
31namespace WebCore {
32
33enum ColorMatrixType {
34    FECOLORMATRIX_TYPE_UNKNOWN          = 0,
35    FECOLORMATRIX_TYPE_MATRIX           = 1,
36    FECOLORMATRIX_TYPE_SATURATE         = 2,
37    FECOLORMATRIX_TYPE_HUEROTATE        = 3,
38    FECOLORMATRIX_TYPE_LUMINANCETOALPHA = 4
39};
40
41class FEColorMatrix : public FilterEffect {
42public:
43    static PassRefPtr<FEColorMatrix> create(Filter*, ColorMatrixType, const Vector<float>&);
44
45    ColorMatrixType type() const;
46    bool setType(ColorMatrixType);
47
48    const Vector<float>& values() const;
49    bool setValues(const Vector<float>&);
50
51    virtual void platformApplySoftware();
52#if ENABLE(OPENCL)
53    virtual bool platformApplyOpenCL();
54#endif
55    virtual void dump();
56
57    virtual TextStream& externalRepresentation(TextStream&, int indention) const;
58
59    static inline void calculateSaturateComponents(float* components, float value);
60    static inline void calculateHueRotateComponents(float* components, float value);
61
62private:
63    FEColorMatrix(Filter*, ColorMatrixType, const Vector<float>&);
64
65    ColorMatrixType m_type;
66    Vector<float> m_values;
67};
68
69inline void FEColorMatrix::calculateSaturateComponents(float* components, float value)
70{
71    components[0] = (0.213 + 0.787 * value);
72    components[1] = (0.715 - 0.715 * value);
73    components[2] = (0.072 - 0.072 * value);
74    components[3] = (0.213 - 0.213 * value);
75    components[4] = (0.715 + 0.285 * value);
76    components[5] = (0.072 - 0.072 * value);
77    components[6] = (0.213 - 0.213 * value);
78    components[7] = (0.715 - 0.715 * value);
79    components[8] = (0.072 + 0.928 * value);
80}
81
82inline void FEColorMatrix::calculateHueRotateComponents(float* components, float value)
83{
84    float cosHue = cos(value * piFloat / 180);
85    float sinHue = sin(value * piFloat / 180);
86    components[0] = 0.213 + cosHue * 0.787 - sinHue * 0.213;
87    components[1] = 0.715 - cosHue * 0.715 - sinHue * 0.715;
88    components[2] = 0.072 - cosHue * 0.072 + sinHue * 0.928;
89    components[3] = 0.213 - cosHue * 0.213 + sinHue * 0.143;
90    components[4] = 0.715 + cosHue * 0.285 + sinHue * 0.140;
91    components[5] = 0.072 - cosHue * 0.072 - sinHue * 0.283;
92    components[6] = 0.213 - cosHue * 0.213 - sinHue * 0.787;
93    components[7] = 0.715 - cosHue * 0.715 + sinHue * 0.715;
94    components[8] = 0.072 + cosHue * 0.928 + sinHue * 0.072;
95}
96
97
98} // namespace WebCore
99
100#endif // ENABLE(FILTERS)
101
102#endif // FEColorMatrix_h
103