File indexing completed on 2024-05-12 16:34:28

0001 /* This file is part of the KDE project
0002  * Copyright (c) 2010 Jan Hambrecht <jaham@gmx.net>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #ifndef CONVOLVEMATRIXEFFECT_H
0021 #define CONVOLVEMATRIXEFFECT_H
0022 
0023 #include "KoFilterEffect.h"
0024 #include <QPointF>
0025 #include <QVector>
0026 
0027 #define ConvolveMatrixEffectId "feConvolveMatrix"
0028 
0029 class KoFilterEffectLoadingContext;
0030 
0031 /// A convolve matrix effect
0032 class ConvolveMatrixEffect : public KoFilterEffect
0033 {
0034 public:
0035     /// Edge mode, i.e. how the kernel behaves at image edges
0036     enum EdgeMode {
0037         Duplicate, ///< duplicates colors at the edges
0038         Wrap,      ///< takes the colors at the opposite edge
0039         None       ///< uses values of zero for each color channel
0040     };
0041 
0042     ConvolveMatrixEffect();
0043 
0044     /// Returns the order of the kernel matrix
0045     QPoint order() const;
0046 
0047     /// Sets the order of the kernel matrix
0048     void setOrder(const QPoint &order);
0049 
0050     /// Returns the kernel matrix
0051     QVector<qreal> kernel() const;
0052 
0053     /// Sets the kernel matrix
0054     void setKernel(const QVector<qreal> &kernel);
0055 
0056     /// Returns the divisor
0057     qreal divisor() const;
0058 
0059     /// Sets the divisor
0060     void setDivisor(qreal divisor);
0061 
0062     /// Returns the bias
0063     qreal bias() const;
0064 
0065     /// Sets the bias
0066     void setBias(qreal bias);
0067 
0068     /// Returns the target cell within the kernel
0069     QPoint target() const;
0070 
0071     /// Sets the target cell within the kernel
0072     void setTarget(const QPoint &target);
0073 
0074     /// Returns edge mode
0075     EdgeMode edgeMode() const;
0076 
0077     /// Sets the edge mode
0078     void setEdgeMode(EdgeMode edgeMode);
0079 
0080     /// Returns if alpha values are preserved
0081     bool isPreserveAlphaEnabled() const;
0082 
0083     /// Enables/disables preserving alpha values
0084     void enablePreserveAlpha(bool on);
0085 
0086     /// reimplemented from KoFilterEffect
0087     QImage processImage(const QImage &image, const KoFilterEffectRenderContext &context) const override;
0088     /// reimplemented from KoFilterEffect
0089     bool load(const KoXmlElement &element, const KoFilterEffectLoadingContext &context) override;
0090     /// reimplemented from KoFilterEffect
0091     void save(KoXmlWriter &writer) override;
0092 
0093 private:
0094     void setDefaults();
0095 
0096     QPoint m_order;          ///< the dimension of the kernel
0097     QVector<qreal> m_kernel; ///< the kernel
0098     qreal m_divisor;         ///< the divisor
0099     qreal m_bias;            ///< the bias
0100     QPoint m_target;         ///< target cell within the kernel
0101     EdgeMode m_edgeMode;     ///< the edge mode
0102     QPointF m_kernelUnitLength; ///< the kernel unit length
0103     bool m_preserveAlpha;    ///< indicates if original alpha values are left intact
0104 };
0105 
0106 #endif // CONVOLVEMATRIXEFFECT_H