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 * Copyright (C) 2010 Zoltan Herczeg <zherczeg@webkit.org>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB.  If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23#ifndef FEConvolveMatrix_h
24#define FEConvolveMatrix_h
25
26#if ENABLE(FILTERS)
27#include "FilterEffect.h"
28#include "FloatPoint.h"
29#include "FloatSize.h"
30#include "Filter.h"
31#include <wtf/Vector.h>
32
33namespace WebCore {
34
35enum EdgeModeType {
36    EDGEMODE_UNKNOWN   = 0,
37    EDGEMODE_DUPLICATE = 1,
38    EDGEMODE_WRAP      = 2,
39    EDGEMODE_NONE      = 3
40};
41
42class FEConvolveMatrix : public FilterEffect {
43public:
44    static PassRefPtr<FEConvolveMatrix> create(Filter*, const IntSize&,
45            float, float, const IntPoint&, EdgeModeType, const FloatPoint&,
46            bool, const Vector<float>&);
47
48    IntSize kernelSize() const;
49    void setKernelSize(const IntSize&);
50
51    const Vector<float>& kernel() const;
52    void setKernel(const Vector<float>&);
53
54    float divisor() const;
55    bool setDivisor(float);
56
57    float bias() const;
58    bool setBias(float);
59
60    IntPoint targetOffset() const;
61    bool setTargetOffset(const IntPoint&);
62
63    EdgeModeType edgeMode() const;
64    bool setEdgeMode(EdgeModeType);
65
66    FloatPoint kernelUnitLength() const;
67    bool setKernelUnitLength(const FloatPoint&);
68
69    bool preserveAlpha() const;
70    bool setPreserveAlpha(bool);
71
72    virtual void platformApplySoftware();
73    virtual void dump();
74
75    virtual void determineAbsolutePaintRect() { setAbsolutePaintRect(enclosingIntRect(maxEffectRect())); }
76
77    virtual TextStream& externalRepresentation(TextStream&, int indention) const;
78
79private:
80
81    struct PaintingData {
82        Uint8ClampedArray* srcPixelArray;
83        Uint8ClampedArray* dstPixelArray;
84        int width;
85        int height;
86        float bias;
87    };
88
89    FEConvolveMatrix(Filter*, const IntSize&, float, float,
90            const IntPoint&, EdgeModeType, const FloatPoint&, bool, const Vector<float>&);
91
92    template<bool preserveAlphaValues>
93    ALWAYS_INLINE void fastSetInteriorPixels(PaintingData&, int clipRight, int clipBottom, int yStart, int yEnd);
94
95    ALWAYS_INLINE int getPixelValue(PaintingData&, int x, int y);
96
97    template<bool preserveAlphaValues>
98    void fastSetOuterPixels(PaintingData&, int x1, int y1, int x2, int y2);
99
100    // Wrapper functions
101    ALWAYS_INLINE void setInteriorPixels(PaintingData&, int clipRight, int clipBottom, int yStart, int yEnd);
102    ALWAYS_INLINE void setOuterPixels(PaintingData&, int x1, int y1, int x2, int y2);
103
104    // Parallelization parts
105    static const int s_minimalRectDimension = (100 * 100); // Empirical data limit for parallel jobs
106
107    template<typename Type>
108    friend class ParallelJobs;
109
110    struct InteriorPixelParameters {
111        FEConvolveMatrix* filter;
112        PaintingData* paintingData;
113        int clipBottom;
114        int clipRight;
115        int yStart;
116        int yEnd;
117    };
118
119    static void setInteriorPixelsWorker(InteriorPixelParameters*);
120
121    IntSize m_kernelSize;
122    float m_divisor;
123    float m_bias;
124    IntPoint m_targetOffset;
125    EdgeModeType m_edgeMode;
126    FloatPoint m_kernelUnitLength;
127    bool m_preserveAlpha;
128    Vector<float> m_kernelMatrix;
129};
130
131} // namespace WebCore
132
133#endif // ENABLE(FILTERS)
134
135#endif // FEConvolveMatrix_h
136