File indexing completed on 2024-05-12 16:34:29
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 #include "ImageEffect.h" 0021 0022 #include "KoFilterEffectRenderContext.h" 0023 #include "KoFilterEffectLoadingContext.h" 0024 #include "KoViewConverter.h" 0025 #include "KoXmlWriter.h" 0026 #include "KoXmlReader.h" 0027 0028 #include <QMimeDatabase> 0029 #include <QMimeType> 0030 #include <QBuffer> 0031 #include <QPainter> 0032 #include <QDebug> 0033 0034 #include <klocalizedstring.h> 0035 0036 0037 ImageEffect::ImageEffect() 0038 : KoFilterEffect(ImageEffectId, i18n("Image")) 0039 { 0040 setRequiredInputCount(0); 0041 setMaximalInputCount(0); 0042 } 0043 0044 QImage ImageEffect::image() const 0045 { 0046 return m_image; 0047 } 0048 0049 void ImageEffect::setImage(const QImage &image) 0050 { 0051 m_image = image; 0052 } 0053 0054 QImage ImageEffect::processImage(const QImage &image, const KoFilterEffectRenderContext &context) const 0055 { 0056 QImage result(image.size(), QImage::Format_ARGB32_Premultiplied); 0057 result.fill(qRgba(0, 0, 0, 0)); 0058 0059 QPainter p(&result); 0060 p.drawImage(context.filterRegion(), m_image); 0061 return result; 0062 } 0063 0064 bool ImageEffect::load(const KoXmlElement &element, const KoFilterEffectLoadingContext &context) 0065 { 0066 if (element.tagName() != id()) 0067 return false; 0068 0069 QString href = element.attribute("xlink:href"); 0070 if (href.startsWith(QLatin1String("data:"))) { 0071 int start = href.indexOf("base64,"); 0072 if (start <= 0 || !m_image.loadFromData(QByteArray::fromBase64(href.mid(start + 7).toLatin1()))) 0073 return false; 0074 } else if (!m_image.load(context.pathFromHref(href))) { 0075 return false; 0076 } 0077 0078 return true; 0079 } 0080 0081 void ImageEffect::save(KoXmlWriter &writer) 0082 { 0083 writer.startElement(ImageEffectId); 0084 0085 saveCommonAttributes(writer); 0086 0087 QByteArray ba; 0088 QBuffer buffer(&ba); 0089 buffer.open(QIODevice::WriteOnly); 0090 if (m_image.save(&buffer, "PNG")) { 0091 QMimeDatabase db; 0092 const QString mimeType(db.mimeTypeForData(ba).name()); 0093 writer.addAttribute("xlink:href", "data:" + mimeType + ";base64," + ba.toBase64()); 0094 } 0095 0096 writer.endElement(); 0097 }