File indexing completed on 2025-01-19 03:51:02

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2019-09-20
0007  * Description : QImage DImg plugin.
0008  *
0009  * SPDX-FileCopyrightText: 2020-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "dimgqimageplugin.h"
0016 
0017 // Qt includes
0018 
0019 #include <QImageReader>
0020 #include <QImageWriter>
0021 #include <QMimeDatabase>
0022 
0023 // KDE includes
0024 
0025 #include <klocalizedstring.h>
0026 
0027 // Local includes
0028 
0029 #include "digikam_debug.h"
0030 #include "digikam_globals.h"
0031 #include "dimgqimageloader.h"
0032 #include "dimgjxlexportsettings.h"
0033 #include "dimgavifexportsettings.h"
0034 #include "dimgwebpexportsettings.h"
0035 
0036 namespace DigikamQImageDImgPlugin
0037 {
0038 
0039 DImgQImagePlugin::DImgQImagePlugin(QObject* const parent)
0040     : DPluginDImg(parent)
0041 {
0042 }
0043 
0044 DImgQImagePlugin::~DImgQImagePlugin()
0045 {
0046 }
0047 
0048 QString DImgQImagePlugin::name() const
0049 {
0050     return i18nc("@title", "QImage loader");
0051 }
0052 
0053 QString DImgQImagePlugin::iid() const
0054 {
0055     return QLatin1String(DPLUGIN_IID);
0056 }
0057 
0058 QIcon DImgQImagePlugin::icon() const
0059 {
0060     return QIcon::fromTheme(QLatin1String("image-x-generic"));
0061 }
0062 
0063 QString DImgQImagePlugin::description() const
0064 {
0065     return i18nc("@info", "An image loader based on QImage plugins");
0066 }
0067 
0068 QString DImgQImagePlugin::details() const
0069 {
0070     return xi18nc("@info", "<para>This plugin allows users to load and save image using QImage plugins from Qt Framework.</para>"
0071                   "<para>See <a href='https://doc.qt.io/qt-5/qimage.html#reading-and-writing-image-files'>Qt Framework documentation</a> "
0072                   "for main native list of format supported.</para>"
0073                   "<para>See <a href='https://invent.kde.org/frameworks/kimageformats/-/blob/master/README.md'>"
0074                   "KDE Framework documentation</a> for extended list of formats supported.</para>"
0075     );
0076 }
0077 
0078 
0079 QString DImgQImagePlugin::handbookSection() const
0080 {
0081     return QLatin1String("supported_materials");
0082 }
0083 
0084 QString DImgQImagePlugin::handbookChapter() const
0085 {
0086     return QLatin1String("image_formats");
0087 }
0088 
0089 QString DImgQImagePlugin::handbookReference() const
0090 {
0091     return QLatin1String("image-others");
0092 }
0093 
0094 QList<DPluginAuthor> DImgQImagePlugin::authors() const
0095 {
0096     return QList<DPluginAuthor>()
0097             << DPluginAuthor(QString::fromUtf8("Renchi Raju"),
0098                              QString::fromUtf8("renchi dot raju at gmail dot com"),
0099                              QString::fromUtf8("(C) 2005"))
0100             << DPluginAuthor(QString::fromUtf8("Maik Qualmann"),
0101                              QString::fromUtf8("metzpinguin at gmail dot com"),
0102                              QString::fromUtf8("(C) 2019-2022"))
0103             << DPluginAuthor(QString::fromUtf8("Gilles Caulier"),
0104                              QString::fromUtf8("caulier dot gilles at gmail dot com"),
0105                              QString::fromUtf8("(C) 2006-2022"))
0106             ;
0107 }
0108 
0109 void DImgQImagePlugin::setup(QObject* const /*parent*/)
0110 {
0111     // Nothing to do
0112 }
0113 
0114 QString DImgQImagePlugin::loaderName() const
0115 {
0116     return QLatin1String("QIMAGE");
0117 }
0118 
0119 QString DImgQImagePlugin::typeMimes() const
0120 {
0121     QString ret;
0122 
0123     Q_FOREACH (const QByteArray& ba, QImageReader::supportedImageFormats())
0124     {
0125         ret += QString::fromUtf8("%1 ").arg(QString::fromUtf8(ba).toUpper());
0126     }
0127 
0128     return ret;
0129 }
0130 
0131 int DImgQImagePlugin::canRead(const QFileInfo& fileInfo, bool magic) const
0132 {
0133     QString filePath = fileInfo.filePath();
0134     QString format   = fileInfo.suffix().toUpper();
0135 
0136     QString mimeType(QMimeDatabase().mimeTypeForFile(filePath).name());
0137 
0138     // Ignore non image format.
0139 
0140     if (
0141         mimeType.startsWith(QLatin1String("video/")) ||
0142         mimeType.startsWith(QLatin1String("audio/"))
0143        )
0144     {
0145         return 0;
0146     }
0147 
0148     if (!magic)
0149     {
0150         Q_FOREACH (const QByteArray& ba, QImageReader::supportedImageFormats())
0151         {
0152             if (QString::fromUtf8(ba).toUpper() == format)
0153             {
0154                 return 80;
0155             }
0156         }
0157 
0158         return 0;
0159     }
0160 
0161     QImageReader reader(filePath);
0162     reader.setDecideFormatFromContent(true);
0163     QByteArray readFormat = reader.format();
0164 
0165     return (QImageReader::supportedImageFormats().contains(readFormat) ? 80 : 0);
0166 }
0167 
0168 int DImgQImagePlugin::canWrite(const QString& format) const
0169 {
0170     Q_FOREACH (const QByteArray& ba, QImageWriter::supportedImageFormats())
0171     {
0172         if (QString::fromUtf8(ba).toUpper() == format.toUpper())
0173         {
0174             return 80;
0175         }
0176     }
0177 
0178     return 0;
0179 }
0180 
0181 DImgLoader* DImgQImagePlugin::loader(DImg* const image, const DRawDecoding&) const
0182 {
0183     return new DImgQImageLoader(image);
0184 }
0185 
0186 DImgLoaderSettings* DImgQImagePlugin::exportWidget(const QString& format) const
0187 {
0188     if      (format.toUpper() == QLatin1String("JXL"))
0189     {
0190         return (new DImgJXLExportSettings());
0191     }
0192     else if (format.toUpper() == QLatin1String("AVIF"))
0193     {
0194         return (new DImgAVIFExportSettings());
0195     }
0196     else if (format.toUpper() == QLatin1String("WEBP"))
0197     {
0198         return (new DImgWEBPExportSettings());
0199     }
0200 
0201     return nullptr;
0202 }
0203 
0204 } // namespace DigikamQImageDImgPlugin
0205 
0206 #include "moc_dimgqimageplugin.cpp"