1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "private/qimagereaderwriterhelpers_p.h"
5
6#include <qcborarray.h>
7#include <qmutex.h>
8#include <private/qfactoryloader_p.h>
9
10QT_BEGIN_NAMESPACE
11
12using namespace Qt::StringLiterals;
13
14namespace QImageReaderWriterHelpers {
15
16#ifndef QT_NO_IMAGEFORMATPLUGIN
17
18Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, irhLoader,
19 (QImageIOHandlerFactoryInterface_iid, "/imageformats"_L1))
20Q_GLOBAL_STATIC(QMutex, irhLoaderMutex)
21
22static void appendImagePluginFormats(QFactoryLoader *loader,
23 QImageIOPlugin::Capability cap,
24 QList<QByteArray> *result)
25{
26 typedef QMultiMap<int, QString> PluginKeyMap;
27 typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator;
28
29 const PluginKeyMap keyMap = loader->keyMap();
30 const PluginKeyMapConstIterator cend = keyMap.constEnd();
31 int i = -1;
32 QImageIOPlugin *plugin = nullptr;
33 result->reserve(asize: result->size() + keyMap.size());
34 for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it) {
35 if (it.key() != i) {
36 i = it.key();
37 plugin = qobject_cast<QImageIOPlugin *>(object: loader->instance(index: i));
38 }
39 const QByteArray key = it.value().toLatin1();
40 if (plugin && (plugin->capabilities(device: nullptr, format: key) & cap) != 0)
41 result->append(t: key);
42 }
43}
44
45static void appendImagePluginMimeTypes(QFactoryLoader *loader,
46 QImageIOPlugin::Capability cap,
47 QList<QByteArray> *result,
48 QList<QByteArray> *resultKeys = nullptr)
49{
50 QList<QPluginParsedMetaData> metaDataList = loader->metaData();
51 const int pluginCount = metaDataList.size();
52 for (int i = 0; i < pluginCount; ++i) {
53 const QCborMap metaData = metaDataList.at(i).value(k: QtPluginMetaDataKeys::MetaData).toMap();
54 const QCborArray keys = metaData.value(key: "Keys"_L1).toArray();
55 const QCborArray mimeTypes = metaData.value(key: "MimeTypes"_L1).toArray();
56 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(object: loader->instance(index: i));
57 const int keyCount = keys.size();
58 for (int k = 0; k < keyCount; ++k) {
59 const QByteArray key = keys.at(i: k).toString().toLatin1();
60 if (plugin && (plugin->capabilities(device: nullptr, format: key) & cap) != 0) {
61 result->append(t: mimeTypes.at(i: k).toString().toLatin1());
62 if (resultKeys)
63 resultKeys->append(t: key);
64 }
65 }
66 }
67}
68
69QSharedPointer<QFactoryLoader> pluginLoader()
70{
71 irhLoaderMutex()->lock();
72 return QSharedPointer<QFactoryLoader>(irhLoader(), [](QFactoryLoader *) {
73 irhLoaderMutex()->unlock();
74 });
75}
76
77static inline QImageIOPlugin::Capability pluginCapability(Capability cap)
78{
79 return cap == CanRead ? QImageIOPlugin::CanRead : QImageIOPlugin::CanWrite;
80}
81
82#endif // QT_NO_IMAGEFORMATPLUGIN
83
84QList<QByteArray> supportedImageFormats(Capability cap)
85{
86 QList<QByteArray> formats;
87 formats.reserve(asize: _qt_NumFormats);
88 for (int i = 0; i < _qt_NumFormats; ++i)
89 formats << _qt_BuiltInFormats[i].extension;
90
91#ifndef QT_NO_IMAGEFORMATPLUGIN
92 appendImagePluginFormats(loader: irhLoader(), cap: pluginCapability(cap), result: &formats);
93#endif // QT_NO_IMAGEFORMATPLUGIN
94
95 std::sort(first: formats.begin(), last: formats.end());
96 formats.erase(abegin: std::unique(first: formats.begin(), last: formats.end()), aend: formats.end());
97 return formats;
98}
99
100QList<QByteArray> supportedMimeTypes(Capability cap)
101{
102 QList<QByteArray> mimeTypes;
103 mimeTypes.reserve(asize: _qt_NumFormats);
104 for (const auto &fmt : _qt_BuiltInFormats)
105 mimeTypes.append(QByteArrayLiteral("image/") + fmt.mimeType);
106
107#ifndef QT_NO_IMAGEFORMATPLUGIN
108 appendImagePluginMimeTypes(loader: irhLoader(), cap: pluginCapability(cap), result: &mimeTypes);
109#endif // QT_NO_IMAGEFORMATPLUGIN
110
111 std::sort(first: mimeTypes.begin(), last: mimeTypes.end());
112 mimeTypes.erase(abegin: std::unique(first: mimeTypes.begin(), last: mimeTypes.end()), aend: mimeTypes.end());
113 return mimeTypes;
114}
115
116QList<QByteArray> imageFormatsForMimeType(const QByteArray &mimeType, Capability cap)
117{
118 QList<QByteArray> formats;
119 if (mimeType.startsWith(bv: "image/")) {
120 const QByteArray type = mimeType.mid(index: sizeof("image/") - 1);
121 for (const auto &fmt : _qt_BuiltInFormats) {
122 if (fmt.mimeType == type && !formats.contains(t: fmt.extension))
123 formats << fmt.extension;
124 }
125 }
126
127#ifndef QT_NO_IMAGEFORMATPLUGIN
128 QList<QByteArray> mimeTypes;
129 QList<QByteArray> keys;
130 appendImagePluginMimeTypes(loader: irhLoader(), cap: pluginCapability(cap), result: &mimeTypes, resultKeys: &keys);
131 for (int i = 0; i < mimeTypes.size(); ++i) {
132 if (mimeTypes.at(i) == mimeType) {
133 const auto &key = keys.at(i);
134 if (!formats.contains(t: key))
135 formats << key;
136 }
137 }
138#endif // QT_NO_IMAGEFORMATPLUGIN
139
140 return formats;
141}
142
143} // QImageReaderWriterHelpers
144
145QT_END_NAMESPACE
146

source code of qtbase/src/gui/image/qimagereaderwriterhelpers.cpp