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

0001 /* This file is part of the KDE project
0002  * Copyright (c) 2009 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 #include "MergeEffect.h"
0021 #include "KoViewConverter.h"
0022 #include "KoXmlWriter.h"
0023 #include "KoXmlReader.h"
0024 #include <klocalizedstring.h>
0025 #include <QPainter>
0026 #include <limits.h>
0027 
0028 MergeEffect::MergeEffect()
0029         : KoFilterEffect(MergeEffectId, i18n("Merge"))
0030 {
0031     setRequiredInputCount(2);
0032     setMaximalInputCount(INT_MAX);
0033 }
0034 
0035 QImage MergeEffect::processImage(const QImage &image, const KoFilterEffectRenderContext &/*context*/) const
0036 {
0037     Q_UNUSED(image);
0038 
0039     return image;
0040 }
0041 
0042 QImage MergeEffect::processImages(const QVector<QImage> &images, const KoFilterEffectRenderContext &/*context*/) const
0043 {
0044     int imageCount = images.count();
0045     if (!imageCount)
0046         return QImage();
0047 
0048     QImage result = images[0];
0049     if (imageCount == 1)
0050         return result;
0051 
0052     QPainter p(&result);
0053 
0054     for (int i = 1; i < imageCount; ++i) {
0055         p.drawImage(QPoint(), images[i]);
0056     }
0057 
0058     return result;
0059 }
0060 
0061 bool MergeEffect::load(const KoXmlElement &element, const KoFilterEffectLoadingContext &)
0062 {
0063     if (element.tagName() != id())
0064         return false;
0065 
0066     int inputCount = inputs().count();
0067     int inputIndex = 0;
0068     for (KoXmlNode n = element.firstChild(); !n.isNull(); n = n.nextSibling()) {
0069         KoXmlElement node = n.toElement();
0070         if (node.tagName() == "feMergeNode") {
0071             if (node.hasAttribute("in")) {
0072                 if (inputIndex < inputCount)
0073                     setInput(inputIndex, node.attribute("in"));
0074                 else
0075                     addInput(node.attribute("in"));
0076                 inputIndex++;
0077             }
0078         }
0079     }
0080 
0081     return true;
0082 }
0083 
0084 void MergeEffect::save(KoXmlWriter &writer)
0085 {
0086     writer.startElement(MergeEffectId);
0087 
0088     saveCommonAttributes(writer);
0089 
0090     foreach(const QString &input, inputs()) {
0091         writer.startElement("feMergeNode");
0092         writer.addAttribute("in", input);
0093         writer.endElement();
0094     }
0095 
0096     writer.endElement();
0097 }