1/****************************************************************************
2**
3** Copyright (C) 2018 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "private/qimagereaderwriterhelpers_p.h"
41
42#include <qjsonarray.h>
43#include <qmutex.h>
44#include <private/qfactoryloader_p.h>
45
46QT_BEGIN_NAMESPACE
47
48namespace QImageReaderWriterHelpers {
49
50#ifndef QT_NO_IMAGEFORMATPLUGIN
51
52Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
53 (QImageIOHandlerFactoryInterface_iid, QLatin1String("/imageformats")))
54Q_GLOBAL_STATIC(QMutex, loaderMutex)
55
56static void appendImagePluginFormats(QFactoryLoader *loader,
57 QImageIOPlugin::Capability cap,
58 QList<QByteArray> *result)
59{
60 typedef QMultiMap<int, QString> PluginKeyMap;
61 typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator;
62
63 const PluginKeyMap keyMap = loader->keyMap();
64 const PluginKeyMapConstIterator cend = keyMap.constEnd();
65 int i = -1;
66 QImageIOPlugin *plugin = nullptr;
67 result->reserve(alloc: result->size() + keyMap.size());
68 for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it) {
69 if (it.key() != i) {
70 i = it.key();
71 plugin = qobject_cast<QImageIOPlugin *>(object: loader->instance(index: i));
72 }
73 const QByteArray key = it.value().toLatin1();
74 if (plugin && (plugin->capabilities(device: nullptr, format: key) & cap) != 0)
75 result->append(t: key);
76 }
77}
78
79static void appendImagePluginMimeTypes(QFactoryLoader *loader,
80 QImageIOPlugin::Capability cap,
81 QList<QByteArray> *result,
82 QList<QByteArray> *resultKeys = nullptr)
83{
84 QList<QJsonObject> metaDataList = loader->metaData();
85
86 const int pluginCount = metaDataList.size();
87 for (int i = 0; i < pluginCount; ++i) {
88 const QJsonObject metaData = metaDataList.at(i).value(key: QLatin1String("MetaData")).toObject();
89 const QJsonArray keys = metaData.value(key: QLatin1String("Keys")).toArray();
90 const QJsonArray mimeTypes = metaData.value(key: QLatin1String("MimeTypes")).toArray();
91 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(object: loader->instance(index: i));
92 const int keyCount = keys.size();
93 for (int k = 0; k < keyCount; ++k) {
94 const QByteArray key = keys.at(i: k).toString().toLatin1();
95 if (plugin && (plugin->capabilities(device: nullptr, format: key) & cap) != 0) {
96 result->append(t: mimeTypes.at(i: k).toString().toLatin1());
97 if (resultKeys)
98 resultKeys->append(t: key);
99 }
100 }
101 }
102}
103
104QSharedPointer<QFactoryLoader> pluginLoader()
105{
106 loaderMutex()->lock();
107 return QSharedPointer<QFactoryLoader>(loader(), [](QFactoryLoader *) {
108 loaderMutex()->unlock();
109 });
110}
111
112static inline QImageIOPlugin::Capability pluginCapability(Capability cap)
113{
114 return cap == CanRead ? QImageIOPlugin::CanRead : QImageIOPlugin::CanWrite;
115}
116
117#endif // QT_NO_IMAGEFORMATPLUGIN
118
119QList<QByteArray> supportedImageFormats(Capability cap)
120{
121 QList<QByteArray> formats;
122 formats.reserve(alloc: _qt_NumFormats);
123 for (int i = 0; i < _qt_NumFormats; ++i)
124 formats << _qt_BuiltInFormats[i].extension;
125
126#ifndef QT_NO_IMAGEFORMATPLUGIN
127 appendImagePluginFormats(loader: loader(), cap: pluginCapability(cap), result: &formats);
128#endif // QT_NO_IMAGEFORMATPLUGIN
129
130 std::sort(first: formats.begin(), last: formats.end());
131 formats.erase(afirst: std::unique(first: formats.begin(), last: formats.end()), alast: formats.end());
132 return formats;
133}
134
135QList<QByteArray> supportedMimeTypes(Capability cap)
136{
137 QList<QByteArray> mimeTypes;
138 mimeTypes.reserve(alloc: _qt_NumFormats);
139 for (const auto &fmt : _qt_BuiltInFormats)
140 mimeTypes.append(QByteArrayLiteral("image/") + fmt.mimeType);
141
142#ifndef QT_NO_IMAGEFORMATPLUGIN
143 appendImagePluginMimeTypes(loader: loader(), cap: pluginCapability(cap), result: &mimeTypes);
144#endif // QT_NO_IMAGEFORMATPLUGIN
145
146 std::sort(first: mimeTypes.begin(), last: mimeTypes.end());
147 mimeTypes.erase(afirst: std::unique(first: mimeTypes.begin(), last: mimeTypes.end()), alast: mimeTypes.end());
148 return mimeTypes;
149}
150
151QList<QByteArray> imageFormatsForMimeType(const QByteArray &mimeType, Capability cap)
152{
153 QList<QByteArray> formats;
154 if (mimeType.startsWith(c: "image/")) {
155 const QByteArray type = mimeType.mid(index: sizeof("image/") - 1);
156 for (const auto &fmt : _qt_BuiltInFormats) {
157 if (fmt.mimeType == type && !formats.contains(t: fmt.extension))
158 formats << fmt.extension;
159 }
160 }
161
162#ifndef QT_NO_IMAGEFORMATPLUGIN
163 QList<QByteArray> mimeTypes;
164 QList<QByteArray> keys;
165 appendImagePluginMimeTypes(loader: loader(), cap: pluginCapability(cap), result: &mimeTypes, resultKeys: &keys);
166 for (int i = 0; i < mimeTypes.size(); ++i) {
167 if (mimeTypes.at(i) == mimeType) {
168 const auto &key = keys.at(i);
169 if (!formats.contains(t: key))
170 formats << key;
171 }
172 }
173#endif // QT_NO_IMAGEFORMATPLUGIN
174
175 return formats;
176}
177
178} // QImageReaderWriterHelpers
179
180QT_END_NAMESPACE
181

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