QtBase  v6.3.1
qimagereaderwriterhelpers.cpp
Go to the documentation of this file.
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 <qcborarray.h>
43 #include <qmutex.h>
44 #include <private/qfactoryloader_p.h>
45 
47 
49 
50 #ifndef QT_NO_IMAGEFORMATPLUGIN
51 
54 Q_GLOBAL_STATIC(QMutex, loaderMutex)
55 
56 static void appendImagePluginFormats(QFactoryLoader *loader,
59 {
60  typedef QMultiMap<int, QString> PluginKeyMap;
62 
63  const PluginKeyMap keyMap = loader->keyMap();
65  int i = -1;
66  QImageIOPlugin *plugin = nullptr;
67  result->reserve(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 *>(loader->instance(i));
72  }
73  const QByteArray key = it.value().toLatin1();
74  if (plugin && (plugin->capabilities(nullptr, key) & cap) != 0)
75  result->append(key);
76  }
77 }
78 
79 static void appendImagePluginMimeTypes(QFactoryLoader *loader,
82  QList<QByteArray> *resultKeys = nullptr)
83 {
84  QList<QPluginParsedMetaData> metaDataList = loader->metaData();
85  const int pluginCount = metaDataList.size();
86  for (int i = 0; i < pluginCount; ++i) {
87  const QCborMap metaData = metaDataList.at(i).value(QtPluginMetaDataKeys::MetaData).toMap();
88  const QCborArray keys = metaData.value(QLatin1String("Keys")).toArray();
89  const QCborArray mimeTypes = metaData.value(QLatin1String("MimeTypes")).toArray();
90  QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i));
91  const int keyCount = keys.size();
92  for (int k = 0; k < keyCount; ++k) {
93  const QByteArray key = keys.at(k).toString().toLatin1();
94  if (plugin && (plugin->capabilities(nullptr, key) & cap) != 0) {
95  result->append(mimeTypes.at(k).toString().toLatin1());
96  if (resultKeys)
97  resultKeys->append(key);
98  }
99  }
100  }
101 }
102 
104 {
105  loaderMutex()->lock();
106  return QSharedPointer<QFactoryLoader>(loader(), [](QFactoryLoader *) {
107  loaderMutex()->unlock();
108  });
109 }
110 
111 static inline QImageIOPlugin::Capability pluginCapability(Capability cap)
112 {
114 }
115 
116 #endif // QT_NO_IMAGEFORMATPLUGIN
117 
119 {
120  QList<QByteArray> formats;
121  formats.reserve(_qt_NumFormats);
122  for (int i = 0; i < _qt_NumFormats; ++i)
123  formats << _qt_BuiltInFormats[i].extension;
124 
125 #ifndef QT_NO_IMAGEFORMATPLUGIN
126  appendImagePluginFormats(loader(), pluginCapability(cap), &formats);
127 #endif // QT_NO_IMAGEFORMATPLUGIN
128 
129  std::sort(formats.begin(), formats.end());
130  formats.erase(std::unique(formats.begin(), formats.end()), formats.end());
131  return formats;
132 }
133 
135 {
136  QList<QByteArray> mimeTypes;
137  mimeTypes.reserve(_qt_NumFormats);
138  for (const auto &fmt : _qt_BuiltInFormats)
139  mimeTypes.append(QByteArrayLiteral("image/") + fmt.mimeType);
140 
141 #ifndef QT_NO_IMAGEFORMATPLUGIN
142  appendImagePluginMimeTypes(loader(), pluginCapability(cap), &mimeTypes);
143 #endif // QT_NO_IMAGEFORMATPLUGIN
144 
145  std::sort(mimeTypes.begin(), mimeTypes.end());
146  mimeTypes.erase(std::unique(mimeTypes.begin(), mimeTypes.end()), mimeTypes.end());
147  return mimeTypes;
148 }
149 
151 {
152  QList<QByteArray> formats;
153  if (mimeType.startsWith("image/")) {
154  const QByteArray type = mimeType.mid(sizeof("image/") - 1);
155  for (const auto &fmt : _qt_BuiltInFormats) {
156  if (fmt.mimeType == type && !formats.contains(fmt.extension))
157  formats << fmt.extension;
158  }
159  }
160 
161 #ifndef QT_NO_IMAGEFORMATPLUGIN
162  QList<QByteArray> mimeTypes;
164  appendImagePluginMimeTypes(loader(), pluginCapability(cap), &mimeTypes, &keys);
165  for (int i = 0; i < mimeTypes.size(); ++i) {
166  if (mimeTypes.at(i) == mimeType) {
167  const auto &key = keys.at(i);
168  if (!formats.contains(key))
169  formats << key;
170  }
171  }
172 #endif // QT_NO_IMAGEFORMATPLUGIN
173 
174  return formats;
175 }
176 
177 } // QImageReaderWriterHelpers
178 
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:85
bool startsWith(QByteArrayView bv) const
Definition: qbytearray.h:192
QByteArray mid(qsizetype index, qsizetype len=-1) const
The QCborArray class is used to hold an array of CBOR elements.
Definition: qcborarray.h:56
QCborValue at(qsizetype i) const
Definition: qcborarray.cpp:219
The QCborMap class is used to hold an associative container representable in CBOR.
Definition: qcbormap.h:57
QCborValue value(qint64 key) const
Definition: qcbormap.h:191
QString toString(const QString &defaultValue={}) const
QCborArray toArray() const
MetaDataList metaData() const
QObject * instance(int index) const
The QImageIOPlugin class defines an interface for writing an image format plugin. \reentrant.
virtual Capabilities capabilities(QIODevice *device, const QByteArray &format) const =0
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition: qstring.h:84
Definition: qlist.h:108
qsizetype size() const noexcept
Definition: qlist.h:414
iterator erase(const_iterator begin, const_iterator end)
Definition: qlist.h:893
iterator end()
Definition: qlist.h:624
const_reference at(qsizetype i) const noexcept
Definition: qlist.h:457
iterator begin()
Definition: qlist.h:623
void reserve(qsizetype size)
Definition: qlist.h:757
void append(parameter_type t)
Definition: qlist.h:469
The QMutex class provides access serialization between threads.
Definition: qmutex.h:285
The QSharedPointer class holds a strong reference to a shared pointer.
QByteArray toLatin1() const &
Definition: qstring.h:745
typename C::const_iterator const_iterator
PluginKeyMap::const_iterator PluginKeyMapConstIterator
Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,(QImageIOHandlerFactoryInterface_iid, QLatin1String("/imageformats"))) static void appendImagePluginFormats(QFactoryLoader *loader
QList< QByteArray > imageFormatsForMimeType(const QByteArray &mimeType, Capability cap)
QSharedPointer< QFactoryLoader > pluginLoader()
QList< QByteArray > supportedImageFormats(Capability cap)
const PluginKeyMapConstIterator cend
QList< QByteArray > supportedMimeTypes(Capability cap)
#define QByteArrayLiteral(str)
Definition: qbytearray.h:80
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
#define QImageIOHandlerFactoryInterface_iid
GLenum type
Definition: qopengl.h:270
GLuint64 key
GLuint64EXT * result
[6]
Definition: qopenglext.h:10932
GLenum cap
Definition: qopenglext.h:8893
QStringList keys
QStringList::Iterator it
bool contains(const AT &t) const noexcept
Definition: qlist.h:78