Avoid code duplication in QImage{Reader,Writer}

Extract code related to supportedImageFormats() and
supportedMimeTypes() into QImageReaderWriterHelpers
namespace.

Change-Id: If0f56682072859be1b6f07afd7737431325827fc
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
bb10
Alexander Volkov 2018-01-25 14:38:25 +03:00
parent bcd6de15ac
commit f104d52737
5 changed files with 299 additions and 201 deletions

View File

@ -9,6 +9,7 @@ HEADERS += \
image/qimage_p.h \
image/qimageiohandler.h \
image/qimagereader.h \
image/qimagereaderwriterhelpers_p.h \
image/qimagewriter.h \
image/qpaintengine_pic_p.h \
image/qpicture.h \
@ -33,6 +34,7 @@ SOURCES += \
image/qimage_conversions.cpp \
image/qimageiohandler.cpp \
image/qimagereader.cpp \
image/qimagereaderwriterhelpers.cpp \
image/qimagewriter.cpp \
image/qpaintengine_pic.cpp \
image/qpicture.cpp \

View File

@ -164,73 +164,13 @@
#include <private/qpnghandler_p.h>
#endif
#include <private/qimagereaderwriterhelpers_p.h>
#include <algorithm>
QT_BEGIN_NAMESPACE
#ifndef QT_NO_IMAGEFORMATPLUGIN
Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
(QImageIOHandlerFactoryInterface_iid, QLatin1String("/imageformats")))
#endif
enum _qt_BuiltInFormatType {
#ifndef QT_NO_IMAGEFORMAT_PNG
_qt_PngFormat,
#endif
#ifndef QT_NO_IMAGEFORMAT_BMP
_qt_BmpFormat,
#endif
#ifndef QT_NO_IMAGEFORMAT_PPM
_qt_PpmFormat,
_qt_PgmFormat,
_qt_PbmFormat,
#endif
#ifndef QT_NO_IMAGEFORMAT_XBM
_qt_XbmFormat,
#endif
#ifndef QT_NO_IMAGEFORMAT_XPM
_qt_XpmFormat,
#endif
_qt_NumFormats,
_qt_NoFormat = -1
};
#if !defined(QT_NO_IMAGEFORMAT_PPM)
# define MAX_MT_SIZE 20
#elif !defined(QT_NO_IMAGEFORMAT_XBM) || !defined(QT_NO_IMAGEFORMAT_XPM)
# define MAX_MT_SIZE 10
#else
# define MAX_MT_SIZE 4
#endif
struct _qt_BuiltInFormatStruct
{
char extension[4];
char mimeType[MAX_MT_SIZE];
};
#undef MAX_MT_SIZE
static const _qt_BuiltInFormatStruct _qt_BuiltInFormats[] = {
#ifndef QT_NO_IMAGEFORMAT_PNG
{"png", "png"},
#endif
#ifndef QT_NO_IMAGEFORMAT_BMP
{"bmp", "bmp"},
#endif
#ifndef QT_NO_IMAGEFORMAT_PPM
{"ppm", "x-portable-pixmap"},
{"pgm", "x-portable-graymap"},
{"pbm", "x-portable-bitmap"},
#endif
#ifndef QT_NO_IMAGEFORMAT_XBM
{"xbm", "x-xbitmap"},
#endif
#ifndef QT_NO_IMAGEFORMAT_XPM
{"xpm", "x-xpixmap"},
#endif
};
Q_STATIC_ASSERT(_qt_NumFormats == sizeof _qt_BuiltInFormats / sizeof *_qt_BuiltInFormats);
using namespace QImageReaderWriterHelpers;
static QImageIOHandler *createReadHandlerHelper(QIODevice *device,
const QByteArray &format,
@ -251,7 +191,7 @@ static QImageIOHandler *createReadHandlerHelper(QIODevice *device,
typedef QMultiMap<int, QString> PluginKeyMap;
// check if we have plugins that support the image format
QFactoryLoader *l = loader();
auto l = QImageReaderWriterHelpers::pluginLoader();
const PluginKeyMap keyMap = l->keyMap();
#ifdef QIMAGEREADER_DEBUG
@ -1565,16 +1505,6 @@ QByteArray QImageReader::imageFormat(QIODevice *device)
return format;
}
#ifndef QT_NO_IMAGEFORMATPLUGIN
void supportedImageHandlerFormats(QFactoryLoader *loader,
QImageIOPlugin::Capability cap,
QList<QByteArray> *result);
void supportedImageHandlerMimeTypes(QFactoryLoader *loader,
QImageIOPlugin::Capability cap,
QList<QByteArray> *result);
#endif
/*!
Returns the list of image formats supported by QImageReader.
@ -1605,18 +1535,7 @@ void supportedImageHandlerMimeTypes(QFactoryLoader *loader,
QList<QByteArray> QImageReader::supportedImageFormats()
{
QList<QByteArray> formats;
formats.reserve(_qt_NumFormats);
for (int i = 0; i < _qt_NumFormats; ++i)
formats << _qt_BuiltInFormats[i].extension;
#ifndef QT_NO_IMAGEFORMATPLUGIN
supportedImageHandlerFormats(loader(), QImageIOPlugin::CanRead, &formats);
#endif // QT_NO_IMAGEFORMATPLUGIN
std::sort(formats.begin(), formats.end());
formats.erase(std::unique(formats.begin(), formats.end()), formats.end());
return formats;
return QImageReaderWriterHelpers::supportedImageFormats(QImageReaderWriterHelpers::CanRead);
}
/*!
@ -1630,18 +1549,7 @@ QList<QByteArray> QImageReader::supportedImageFormats()
QList<QByteArray> QImageReader::supportedMimeTypes()
{
QList<QByteArray> mimeTypes;
mimeTypes.reserve(_qt_NumFormats);
for (const auto &fmt : _qt_BuiltInFormats)
mimeTypes.append(QByteArrayLiteral("image/") + fmt.mimeType);
#ifndef QT_NO_IMAGEFORMATPLUGIN
supportedImageHandlerMimeTypes(loader(), QImageIOPlugin::CanRead, &mimeTypes);
#endif // QT_NO_IMAGEFORMATPLUGIN
std::sort(mimeTypes.begin(), mimeTypes.end());
mimeTypes.erase(std::unique(mimeTypes.begin(), mimeTypes.end()), mimeTypes.end());
return mimeTypes;
return QImageReaderWriterHelpers::supportedMimeTypes(QImageReaderWriterHelpers::CanRead);
}
QT_END_NAMESPACE

View File

@ -0,0 +1,148 @@
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "private/qimagereaderwriterhelpers_p.h"
#include <qjsonarray.h>
#include <qmutex.h>
#include <private/qfactoryloader_p.h>
QT_BEGIN_NAMESPACE
namespace QImageReaderWriterHelpers {
#ifndef QT_NO_IMAGEFORMATPLUGIN
Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
(QImageIOHandlerFactoryInterface_iid, QLatin1String("/imageformats")))
Q_GLOBAL_STATIC(QMutex, loaderMutex)
static void appendImagePluginFormats(QFactoryLoader *loader,
QImageIOPlugin::Capability cap,
QList<QByteArray> *result)
{
typedef QMultiMap<int, QString> PluginKeyMap;
typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator;
const PluginKeyMap keyMap = loader->keyMap();
const PluginKeyMapConstIterator cend = keyMap.constEnd();
int i = -1;
QImageIOPlugin *plugin = 0;
result->reserve(result->size() + keyMap.size());
for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it) {
if (it.key() != i) {
i = it.key();
plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i));
}
const QByteArray key = it.value().toLatin1();
if (plugin && (plugin->capabilities(0, key) & cap) != 0)
result->append(key);
}
}
static void appendImagePluginMimeTypes(QFactoryLoader *loader,
QImageIOPlugin::Capability cap,
QList<QByteArray> *result)
{
QList<QJsonObject> metaDataList = loader->metaData();
const int pluginCount = metaDataList.size();
for (int i = 0; i < pluginCount; ++i) {
const QJsonObject metaData = metaDataList.at(i).value(QLatin1String("MetaData")).toObject();
const QJsonArray keys = metaData.value(QLatin1String("Keys")).toArray();
const QJsonArray mimeTypes = metaData.value(QLatin1String("MimeTypes")).toArray();
QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i));
const int keyCount = keys.size();
for (int k = 0; k < keyCount; ++k) {
if (plugin && (plugin->capabilities(0, keys.at(k).toString().toLatin1()) & cap) != 0)
result->append(mimeTypes.at(k).toString().toLatin1());
}
}
}
QSharedPointer<QFactoryLoader> pluginLoader()
{
loaderMutex()->lock();
return QSharedPointer<QFactoryLoader>(loader(), [](QFactoryLoader *) {
loaderMutex()->unlock();
});
}
static inline QImageIOPlugin::Capability pluginCapability(Capability cap)
{
return cap == CanRead ? QImageIOPlugin::CanRead : QImageIOPlugin::CanWrite;
}
#endif // QT_NO_IMAGEFORMATPLUGIN
QList<QByteArray> supportedImageFormats(Capability cap)
{
QList<QByteArray> formats;
formats.reserve(_qt_NumFormats);
for (int i = 0; i < _qt_NumFormats; ++i)
formats << _qt_BuiltInFormats[i].extension;
#ifndef QT_NO_IMAGEFORMATPLUGIN
appendImagePluginFormats(loader(), pluginCapability(cap), &formats);
#endif // QT_NO_IMAGEFORMATPLUGIN
std::sort(formats.begin(), formats.end());
formats.erase(std::unique(formats.begin(), formats.end()), formats.end());
return formats;
}
QList<QByteArray> supportedMimeTypes(Capability cap)
{
QList<QByteArray> mimeTypes;
mimeTypes.reserve(_qt_NumFormats);
for (const auto &fmt : _qt_BuiltInFormats)
mimeTypes.append(QByteArrayLiteral("image/") + fmt.mimeType);
#ifndef QT_NO_IMAGEFORMATPLUGIN
appendImagePluginMimeTypes(loader(), pluginCapability(cap), &mimeTypes);
#endif // QT_NO_IMAGEFORMATPLUGIN
std::sort(mimeTypes.begin(), mimeTypes.end());
mimeTypes.erase(std::unique(mimeTypes.begin(), mimeTypes.end()), mimeTypes.end());
return mimeTypes;
}
} // QImageReaderWriterHelpers
QT_END_NAMESPACE

View File

@ -0,0 +1,138 @@
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QIMAGEREADERWRITERHELPERS_P_H
#define QIMAGEREADERWRITERHELPERS_P_H
#include <QtGui/private/qtguiglobal_p.h>
#include <qsharedpointer.h>
#include "qimageiohandler.h"
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
QT_BEGIN_NAMESPACE
class QFactoryLoader;
namespace QImageReaderWriterHelpers {
enum _qt_BuiltInFormatType {
#ifndef QT_NO_IMAGEFORMAT_PNG
_qt_PngFormat,
#endif
#ifndef QT_NO_IMAGEFORMAT_BMP
_qt_BmpFormat,
#endif
#ifndef QT_NO_IMAGEFORMAT_PPM
_qt_PpmFormat,
_qt_PgmFormat,
_qt_PbmFormat,
#endif
#ifndef QT_NO_IMAGEFORMAT_XBM
_qt_XbmFormat,
#endif
#ifndef QT_NO_IMAGEFORMAT_XPM
_qt_XpmFormat,
#endif
_qt_NumFormats,
_qt_NoFormat = -1
};
#if !defined(QT_NO_IMAGEFORMAT_PPM)
# define MAX_MT_SIZE 20
#elif !defined(QT_NO_IMAGEFORMAT_XBM) || !defined(QT_NO_IMAGEFORMAT_XPM)
# define MAX_MT_SIZE 10
#else
# define MAX_MT_SIZE 4
#endif
struct _qt_BuiltInFormatStruct
{
char extension[4];
char mimeType[MAX_MT_SIZE];
};
#undef MAX_MT_SIZE
static const _qt_BuiltInFormatStruct _qt_BuiltInFormats[] = {
#ifndef QT_NO_IMAGEFORMAT_PNG
{"png", "png"},
#endif
#ifndef QT_NO_IMAGEFORMAT_BMP
{"bmp", "bmp"},
#endif
#ifndef QT_NO_IMAGEFORMAT_PPM
{"ppm", "x-portable-pixmap"},
{"pgm", "x-portable-graymap"},
{"pbm", "x-portable-bitmap"},
#endif
#ifndef QT_NO_IMAGEFORMAT_XBM
{"xbm", "x-xbitmap"},
#endif
#ifndef QT_NO_IMAGEFORMAT_XPM
{"xpm", "x-xpixmap"},
#endif
};
Q_STATIC_ASSERT(_qt_NumFormats == sizeof _qt_BuiltInFormats / sizeof *_qt_BuiltInFormats);
#ifndef QT_NO_IMAGEFORMATPLUGIN
QSharedPointer<QFactoryLoader> pluginLoader();
#endif
enum Capability {
CanRead,
CanWrite
};
QList<QByteArray> supportedImageFormats(Capability cap);
QList<QByteArray> supportedMimeTypes(Capability cap);
}
QT_END_NAMESPACE
#endif // QIMAGEREADERWRITERHELPERS_P_H

View File

@ -102,7 +102,6 @@
#include <qfileinfo.h>
#include <qimage.h>
#include <qimageiohandler.h>
#include <qjsonarray.h>
#include <qset.h>
#include <qvariant.h>
@ -119,15 +118,12 @@
#include <private/qpnghandler_p.h>
#endif
#include <private/qimagereaderwriterhelpers_p.h>
#include <algorithm>
QT_BEGIN_NAMESPACE
#ifndef QT_NO_IMAGEFORMATPLUGIN
Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
(QImageIOHandlerFactoryInterface_iid, QLatin1String("/imageformats")))
#endif
static QImageIOHandler *createWriteHandlerHelper(QIODevice *device,
const QByteArray &format)
{
@ -139,7 +135,7 @@ static QImageIOHandler *createWriteHandlerHelper(QIODevice *device,
typedef QMultiMap<int, QString> PluginKeyMap;
// check if any plugins can write the image
QFactoryLoader *l = loader();
auto l = QImageReaderWriterHelpers::pluginLoader();
const PluginKeyMap keyMap = l->keyMap();
int suffixPluginIndex = -1;
#endif
@ -824,52 +820,6 @@ bool QImageWriter::supportsOption(QImageIOHandler::ImageOption option) const
return d->handler->supportsOption(option);
}
#ifndef QT_NO_IMAGEFORMATPLUGIN
void supportedImageHandlerFormats(QFactoryLoader *loader,
QImageIOPlugin::Capability cap,
QList<QByteArray> *result)
{
typedef QMultiMap<int, QString> PluginKeyMap;
typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator;
const PluginKeyMap keyMap = loader->keyMap();
const PluginKeyMapConstIterator cend = keyMap.constEnd();
int i = -1;
QImageIOPlugin *plugin = 0;
result->reserve(result->size() + keyMap.size());
for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it) {
if (it.key() != i) {
i = it.key();
plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i));
}
const QByteArray key = it.value().toLatin1();
if (plugin && (plugin->capabilities(0, key) & cap) != 0)
result->append(key);
}
}
void supportedImageHandlerMimeTypes(QFactoryLoader *loader,
QImageIOPlugin::Capability cap,
QList<QByteArray> *result)
{
QList<QJsonObject> metaDataList = loader->metaData();
const int pluginCount = metaDataList.size();
for (int i = 0; i < pluginCount; ++i) {
const QJsonObject metaData = metaDataList.at(i).value(QLatin1String("MetaData")).toObject();
const QJsonArray keys = metaData.value(QLatin1String("Keys")).toArray();
const QJsonArray mimeTypes = metaData.value(QLatin1String("MimeTypes")).toArray();
QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i));
const int keyCount = keys.size();
for (int k = 0; k < keyCount; ++k) {
if (plugin && (plugin->capabilities(0, keys.at(k).toString().toLatin1()) & cap) != 0)
result->append(mimeTypes.at(k).toString().toLatin1());
}
}
}
#endif // QT_NO_IMAGEFORMATPLUGIN
/*!
Returns the list of image formats supported by QImageWriter.
@ -897,30 +847,7 @@ void supportedImageHandlerMimeTypes(QFactoryLoader *loader,
*/
QList<QByteArray> QImageWriter::supportedImageFormats()
{
QList<QByteArray> formats;
#ifndef QT_NO_IMAGEFORMAT_BMP
formats << "bmp";
#endif
#ifndef QT_NO_IMAGEFORMAT_PPM
formats << "pbm" << "pgm" << "ppm";
#endif
#ifndef QT_NO_IMAGEFORMAT_XBM
formats << "xbm";
#endif
#ifndef QT_NO_IMAGEFORMAT_XPM
formats << "xpm";
#endif
#ifndef QT_NO_IMAGEFORMAT_PNG
formats << "png";
#endif
#ifndef QT_NO_IMAGEFORMATPLUGIN
supportedImageHandlerFormats(loader(), QImageIOPlugin::CanWrite, &formats);
#endif // QT_NO_IMAGEFORMATPLUGIN
std::sort(formats.begin(), formats.end());
formats.erase(std::unique(formats.begin(), formats.end()), formats.end());
return formats;
return QImageReaderWriterHelpers::supportedImageFormats(QImageReaderWriterHelpers::CanWrite);
}
/*!
@ -933,32 +860,7 @@ QList<QByteArray> QImageWriter::supportedImageFormats()
*/
QList<QByteArray> QImageWriter::supportedMimeTypes()
{
QList<QByteArray> mimeTypes;
#ifndef QT_NO_IMAGEFORMAT_BMP
mimeTypes << "image/bmp";
#endif
#ifndef QT_NO_IMAGEFORMAT_PPM
mimeTypes << "image/x-portable-bitmap";
mimeTypes << "image/x-portable-graymap";
mimeTypes << "image/x-portable-pixmap";
#endif
#ifndef QT_NO_IMAGEFORMAT_XBM
mimeTypes << "image/x-xbitmap";
#endif
#ifndef QT_NO_IMAGEFORMAT_XPM
mimeTypes << "image/x-xpixmap";
#endif
#ifndef QT_NO_IMAGEFORMAT_PNG
mimeTypes << "image/png";
#endif
#ifndef QT_NO_IMAGEFORMATPLUGIN
supportedImageHandlerMimeTypes(loader(), QImageIOPlugin::CanWrite, &mimeTypes);
#endif // QT_NO_IMAGEFORMATPLUGIN
std::sort(mimeTypes.begin(), mimeTypes.end());
mimeTypes.erase(std::unique(mimeTypes.begin(), mimeTypes.end()), mimeTypes.end());
return mimeTypes;
return QImageReaderWriterHelpers::supportedMimeTypes(QImageReaderWriterHelpers::CanWrite);
}
QT_END_NAMESPACE