Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qimagereaderwriterhelpers.cpp
Go to the documentation of this file.
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
// Qt-Security score:significant reason:default
4
5
#
include
"private/qimagereaderwriterhelpers_p.h"
6
7
#
include
<
qcborarray
.
h
>
8
#
include
<
qmutex
.
h
>
9
#
include
<
private
/
qfactoryloader_p
.
h
>
10
11
QT_BEGIN_NAMESPACE
12
13
using
namespace
Qt::StringLiterals;
14
15
namespace
QImageReaderWriterHelpers
{
16
17
#
ifndef
QT_NO_IMAGEFORMATPLUGIN
18
19
Q_GLOBAL_STATIC_WITH_ARGS
(
QFactoryLoader
,
irhLoader
,
20
(
QImageIOHandlerFactoryInterface_iid
,
"/imageformats"_L1
))
21
Q_GLOBAL_STATIC
(
QMutex
,
irhLoaderMutex
)
22
23
static
void
appendImagePluginFormats
(
QFactoryLoader
*
loader
,
24
QImageIOPlugin
::
Capability
cap
,
25
QList
<
QByteArray
> *
result
)
26
{
27
typedef
QMultiMap
<
int
,
QString
>
PluginKeyMap
;
28
typedef
PluginKeyMap
::
const_iterator
PluginKeyMapConstIterator
;
29
30
const
PluginKeyMap
keyMap
=
loader
->
keyMap
();
31
const
PluginKeyMapConstIterator
cend
=
keyMap
.
constEnd
();
32
int
i
= -1;
33
QImageIOPlugin
*
plugin
=
nullptr
;
34
result
->
reserve
(
result
->
size
() +
keyMap
.
size
());
35
for
(
PluginKeyMapConstIterator
it
=
keyMap
.
constBegin
();
it
!=
cend
; ++
it
) {
36
if
(
it
.
key
() !=
i
) {
37
i
=
it
.
key
();
38
plugin
=
qobject_cast
<
QImageIOPlugin
*>(
loader
->
instance
(
i
));
39
}
40
const
QByteArray
key
=
it
.
value
().
toLatin1
();
41
if
(
plugin
&& (
plugin
->
capabilities
(
nullptr
,
key
) &
cap
) != 0)
42
result
->
append
(
key
);
43
}
44
}
45
46
static
void
appendImagePluginMimeTypes
(QFactoryLoader *loader,
47
QImageIOPlugin::Capability cap,
48
QList<QByteArray> *result,
49
QList<QByteArray> *resultKeys =
nullptr
)
50
{
51
QList<QPluginParsedMetaData> metaDataList = loader->metaData();
52
const
int
pluginCount = metaDataList.size();
53
for
(
int
i = 0; i < pluginCount; ++i) {
54
const
QCborMap metaData = metaDataList.at(i).value(QtPluginMetaDataKeys::MetaData).toMap();
55
const
QCborArray keys = metaData.value(
"Keys"_L1
).toArray();
56
const
QCborArray mimeTypes = metaData.value(
"MimeTypes"_L1
).toArray();
57
QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i));
58
const
int
keyCount = keys.size();
59
for
(
int
k = 0; k < keyCount; ++k) {
60
const
QByteArray key = keys.at(k).toString().toLatin1();
61
if
(plugin && (plugin->capabilities(
nullptr
, key) & cap) != 0) {
62
result->append(mimeTypes.at(k).toString().toLatin1());
63
if
(resultKeys)
64
resultKeys->append(key);
65
}
66
}
67
}
68
}
69
70
QSharedPointer
<
QFactoryLoader
>
pluginLoader
()
71
{
72
irhLoaderMutex()->lock();
73
return
QSharedPointer<QFactoryLoader>(irhLoader(), [](QFactoryLoader *) {
74
irhLoaderMutex()->unlock();
75
});
76
}
77
78
static
inline
QImageIOPlugin
::
Capability
pluginCapability
(Capability cap)
79
{
80
return
cap == CanRead ? QImageIOPlugin::CanRead : QImageIOPlugin::CanWrite;
81
}
82
83
#
endif
// QT_NO_IMAGEFORMATPLUGIN
84
85
QList
<
QByteArray
>
supportedImageFormats
(Capability cap)
86
{
87
QList<QByteArray> formats;
88
formats.reserve(_qt_NumFormats);
89
for
(
int
i = 0; i < _qt_NumFormats; ++i)
90
formats << _qt_BuiltInFormats[i].extension;
91
92
#
ifndef
QT_NO_IMAGEFORMATPLUGIN
93
appendImagePluginFormats(irhLoader(), pluginCapability(cap), &formats);
94
#
endif
// QT_NO_IMAGEFORMATPLUGIN
95
96
std::sort(formats.begin(), formats.end());
97
formats.erase(std::unique(formats.begin(), formats.end()), formats.end());
98
return
formats;
99
}
100
101
static
constexpr
QByteArrayView
imagePrefix
()
noexcept
{
return
"image/"
; }
102
103
QList
<
QByteArray
>
supportedMimeTypes
(Capability cap)
104
{
105
QList<QByteArray> mimeTypes;
106
mimeTypes.reserve(_qt_NumFormats);
107
for
(
const
auto
&fmt : _qt_BuiltInFormats)
108
mimeTypes.emplace_back(imagePrefix() + fmt.mimeType);
109
110
#
ifndef
QT_NO_IMAGEFORMATPLUGIN
111
appendImagePluginMimeTypes(irhLoader(), pluginCapability(cap), &mimeTypes);
112
#
endif
// QT_NO_IMAGEFORMATPLUGIN
113
114
std::sort(mimeTypes.begin(), mimeTypes.end());
115
mimeTypes.erase(std::unique(mimeTypes.begin(), mimeTypes.end()), mimeTypes.end());
116
return
mimeTypes;
117
}
118
119
QList
<
QByteArray
>
imageFormatsForMimeType
(QByteArrayView mimeType, Capability cap)
120
{
121
QList<QByteArray> formats;
122
if
(mimeType.startsWith(imagePrefix())) {
123
const
QByteArrayView type = mimeType.mid(imagePrefix().size());
124
for
(
const
auto
&fmt : _qt_BuiltInFormats) {
125
if
(fmt.mimeType == type && !formats.contains(fmt.extension))
126
formats << fmt.extension;
127
}
128
}
129
130
#
ifndef
QT_NO_IMAGEFORMATPLUGIN
131
QList<QByteArray> mimeTypes;
132
QList<QByteArray> keys;
133
appendImagePluginMimeTypes(irhLoader(), pluginCapability(cap), &mimeTypes, &keys);
134
for
(
int
i = 0; i < mimeTypes.size(); ++i) {
135
if
(mimeTypes.at(i) == mimeType) {
136
const
auto
&key = keys.at(i);
137
if
(!formats.contains(key))
138
formats << key;
139
}
140
}
141
#
endif
// QT_NO_IMAGEFORMATPLUGIN
142
143
return
formats;
144
}
145
146
}
// QImageReaderWriterHelpers
147
148
QT_END_NAMESPACE
QImageReaderWriterHelpers
Definition
qimagereaderwriterhelpers.cpp:15
QImageReaderWriterHelpers::pluginCapability
static QImageIOPlugin::Capability pluginCapability(Capability cap)
Definition
qimagereaderwriterhelpers.cpp:78
QImageReaderWriterHelpers::imageFormatsForMimeType
QList< QByteArray > imageFormatsForMimeType(QByteArrayView mimeType, Capability cap)
Definition
qimagereaderwriterhelpers.cpp:119
QImageReaderWriterHelpers::Q_GLOBAL_STATIC_WITH_ARGS
Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, irhLoader,(QImageIOHandlerFactoryInterface_iid, "/imageformats"_L1)) static void appendImagePluginFormats(QFactoryLoader *loader
QImageReaderWriterHelpers::appendImagePluginMimeTypes
static void appendImagePluginMimeTypes(QFactoryLoader *loader, QImageIOPlugin::Capability cap, QList< QByteArray > *result, QList< QByteArray > *resultKeys=nullptr)
Definition
qimagereaderwriterhelpers.cpp:46
QImageReaderWriterHelpers::imagePrefix
static constexpr QByteArrayView imagePrefix() noexcept
Definition
qimagereaderwriterhelpers.cpp:101
QImageReaderWriterHelpers::pluginLoader
QSharedPointer< QFactoryLoader > pluginLoader()
Definition
qimagereaderwriterhelpers.cpp:70
QImageReaderWriterHelpers::supportedImageFormats
QList< QByteArray > supportedImageFormats(Capability cap)
Definition
qimagereaderwriterhelpers.cpp:85
QImageReaderWriterHelpers::supportedMimeTypes
QList< QByteArray > supportedMimeTypes(Capability cap)
Definition
qimagereaderwriterhelpers.cpp:103
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtbase
src
gui
image
qimagereaderwriterhelpers.cpp
Generated on
for Qt by
1.18.0