QImage plugins should report supported mime types

Introduces the methods QImageReader::supportedMimeTypes and
QImageWriter::supportedMimeTypes which corresponds to the similar
supportedImageFormats methods, except they return lists of MIME types.

Task-number: QTBUG-28177
Change-Id: Ibb0e264a12eaf972a8bfd6bd891dcd9f89efd085
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
This commit is contained in:
Allan Sandfeld Jensen 2012-11-26 11:57:47 +01:00 committed by The Qt Project
parent 2da24ac2b9
commit 06e4b1cff4
9 changed files with 175 additions and 38 deletions

View File

@ -186,31 +186,32 @@ struct _qt_BuiltInFormatStruct
{
_qt_BuiltInFormatType type;
const char *extension;
const char *mimeType;
};
static const _qt_BuiltInFormatStruct _qt_BuiltInFormats[] = {
#ifndef QT_NO_IMAGEFORMAT_PNG
{_qt_PngFormat, "png"},
{_qt_PngFormat, "png", "image/png"},
#endif
#ifndef QT_NO_IMAGEFORMAT_JPEG
{_qt_JpgFormat, "jpg"},
{_qt_JpgFormat, "jpg", "image/jpeg"},
#endif
#ifdef QT_BUILTIN_GIF_READER
{_qt_GifFormat, "gif"},
{_qt_GifFormat, "gif", "image/gif"},
#endif
{_qt_BmpFormat, "bmp"},
{_qt_BmpFormat, "bmp", "image/bmp"},
#ifndef QT_NO_IMAGEFORMAT_PPM
{_qt_PpmFormat, "ppm"},
{_qt_PgmFormat, "pgm"},
{_qt_PbmFormat, "pbm"},
{_qt_PpmFormat, "ppm", "image/x-portable-pixmap"},
{_qt_PgmFormat, "pgm", "image/x-portable-graymap"},
{_qt_PbmFormat, "pbm", "image/x-portable-bitmap"},
#endif
#ifndef QT_NO_IMAGEFORMAT_XBM
{_qt_XbmFormat, "xbm"},
{_qt_XbmFormat, "xbm", "image/x-xbitmap"},
#endif
#ifndef QT_NO_IMAGEFORMAT_XPM
{_qt_XpmFormat, "xpm"},
{_qt_XpmFormat, "xpm", "image/x-xpixmap"},
#endif
{_qt_NoFormat, ""}
{_qt_NoFormat, "", ""}
};
static QImageIOHandler *createReadHandlerHelper(QIODevice *device,
@ -1434,6 +1435,10 @@ QByteArray QImageReader::imageFormat(QIODevice *device)
void supportedImageHandlerFormats(QFactoryLoader *loader,
QImageIOPlugin::Capability cap,
QSet<QByteArray> *result);
void supportedImageHandlerMimeTypes(QFactoryLoader *loader,
QImageIOPlugin::Capability cap,
QSet<QByteArray> *result);
#endif
/*!
@ -1442,18 +1447,17 @@ void supportedImageHandlerFormats(QFactoryLoader *loader,
By default, Qt can read the following formats:
\table
\header \li Format \li Description
\row \li BMP \li Windows Bitmap
\row \li GIF \li Graphic Interchange Format (optional)
\row \li JPG \li Joint Photographic Experts Group
\row \li JPEG \li Joint Photographic Experts Group
\row \li PNG \li Portable Network Graphics
\row \li PBM \li Portable Bitmap
\row \li PGM \li Portable Graymap
\row \li PPM \li Portable Pixmap
\row \li XBM \li X11 Bitmap
\row \li XPM \li X11 Pixmap
\row \li SVG \li Scalable Vector Graphics
\header \li Format \li MIME type \li Description
\row \li BMP \li image/bmp \li Windows Bitmap
\row \li GIF \li image/gif \li Graphic Interchange Format (optional)
\row \li JPG \li image/jpeg \li Joint Photographic Experts Group
\row \li PNG \li image/png \li Portable Network Graphics
\row \li PBM \li image/x-portable-bitmap \li Portable Bitmap
\row \li PGM \li image/x-portable-graymap \li Portable Graymap
\row \li PPM \li image/x-portable-pixmap \li Portable Pixmap
\row \li XBM \li image/x-xbitmap \li X11 Bitmap
\row \li XPM \li image/x-xpixmap \li X11 Pixmap
\row \li SVG \li image/svg+xml \li Scalable Vector Graphics
\endtable
Reading and writing SVG files is supported through Qt's
@ -1484,4 +1488,31 @@ QList<QByteArray> QImageReader::supportedImageFormats()
return sortedFormats;
}
/*!
Returns the list of MIME types supported by QImageReader.
Note that the QApplication instance must be created before this function is
called.
\sa supportedImageFormats(), QImageWriter::supportedMimeTypes()
*/
QList<QByteArray> QImageReader::supportedMimeTypes()
{
QSet<QByteArray> mimeTypes;
for (int i = 0; i < _qt_NumFormats; ++i)
mimeTypes << _qt_BuiltInFormats[i].mimeType;
#ifndef QT_NO_LIBRARY
supportedImageHandlerMimeTypes(loader(), QImageIOPlugin::CanRead, &mimeTypes);
#endif // QT_NO_LIBRARY
QList<QByteArray> sortedMimeTypes;
for (QSet<QByteArray>::ConstIterator it = mimeTypes.constBegin(); it != mimeTypes.constEnd(); ++it)
sortedMimeTypes << *it;
qSort(sortedMimeTypes);
return sortedMimeTypes;
}
QT_END_NAMESPACE

View File

@ -133,6 +133,7 @@ public:
static QByteArray imageFormat(const QString &fileName);
static QByteArray imageFormat(QIODevice *device);
static QList<QByteArray> supportedImageFormats();
static QList<QByteArray> supportedMimeTypes();
private:
Q_DISABLE_COPY(QImageReader)

View File

@ -100,6 +100,7 @@
#include <qfile.h>
#include <qfileinfo.h>
#include <qimageiohandler.h>
#include <qjsonarray.h>
#include <qset.h>
#include <qvariant.h>
@ -677,6 +678,26 @@ void supportedImageHandlerFormats(QFactoryLoader *loader,
result->insert(key);
}
}
void supportedImageHandlerMimeTypes(QFactoryLoader *loader,
QImageIOPlugin::Capability cap,
QSet<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(QStringLiteral("MetaData")).toObject();
const QJsonArray keys = metaData.value(QStringLiteral("Keys")).toArray();
const QJsonArray mimeTypes = metaData.value(QStringLiteral("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->insert(mimeTypes.at(k).toString().toLatin1());
}
}
}
#endif // QT_NO_IMAGEFORMATPLUGIN
/*!
@ -685,16 +706,15 @@ void supportedImageHandlerFormats(QFactoryLoader *loader,
By default, Qt can write the following formats:
\table
\header \li Format \li Description
\row \li BMP \li Windows Bitmap
\row \li JPG \li Joint Photographic Experts Group
\row \li JPEG \li Joint Photographic Experts Group
\row \li PNG \li Portable Network Graphics
\row \li PBM \li Portable Bitmap
\row \li PGM \li Portable Graymap
\row \li PPM \li Portable Pixmap
\row \li XBM \li X11 Bitmap
\row \li XPM \li X11 Pixmap
\header \li Format \li MIME type \li Description
\row \li BMP \li image/bmp \li Windows Bitmap
\row \li JPG \li image/jpeg \li Joint Photographic Experts Group
\row \li PNG \li image/png \li Portable Network Graphics
\row \li PBM \li image/x-portable-bitmap \li Portable Bitmap
\row \li PGM \li image/x-portable-graymap \li Portable Graymap
\row \li PPM \li image/x-portable-pixmap \li Portable Pixmap
\row \li XBM \li image/x-xbitmap \li X11 Bitmap
\row \li XPM \li image/x-xpixmap \li X11 Pixmap
\endtable
Reading and writing SVG files is supported through Qt's
@ -725,9 +745,6 @@ QList<QByteArray> QImageWriter::supportedImageFormats()
#ifndef QT_NO_IMAGEFORMAT_JPEG
formats << "jpg" << "jpeg";
#endif
#ifdef QT_BUILTIN_GIF_READER
formats << "gif";
#endif
#ifndef QT_NO_IMAGEFORMATPLUGIN
supportedImageHandlerFormats(loader(), QImageIOPlugin::CanWrite, &formats);
@ -741,4 +758,46 @@ QList<QByteArray> QImageWriter::supportedImageFormats()
return sortedFormats;
}
/*!
Returns the list of MIME types supported by QImageWriter.
Note that the QApplication instance must be created before this function is
called.
\sa supportedImageFormats(), QImageReader::supportedMimeTypes()
*/
QList<QByteArray> QImageWriter::supportedMimeTypes()
{
QSet<QByteArray> mimeTypes;
mimeTypes << "image/bmp";
#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_IMAGEFORMAT_JPEG
mimeTypes << "image/jpeg";
#endif
#ifndef QT_NO_LIBRARY
supportedImageHandlerMimeTypes(loader(), QImageIOPlugin::CanWrite, &mimeTypes);
#endif // QT_NO_LIBRARY
QList<QByteArray> sortedMimeTypes;
for (QSet<QByteArray>::ConstIterator it = mimeTypes.constBegin(); it != mimeTypes.constEnd(); ++it)
sortedMimeTypes << *it;
qSort(sortedMimeTypes);
return sortedMimeTypes;
}
QT_END_NAMESPACE

View File

@ -102,6 +102,7 @@ public:
bool supportsOption(QImageIOHandler::ImageOption option) const;
static QList<QByteArray> supportedImageFormats();
static QList<QByteArray> supportedMimeTypes();
private:
Q_DISABLE_COPY(QImageWriter)

View File

@ -1,3 +1,4 @@
{
"Keys": [ "gif" ]
"Keys": [ "gif" ],
"MimeTypes": [ "image/gif" ]
}

View File

@ -1,3 +1,4 @@
{
"Keys": [ "ico" ]
"Keys": [ "ico" ],
"MimeTypes": [ "image/vnd.microsoft.icon" ]
}

View File

@ -1,3 +1,4 @@
{
"Keys": [ "jpg", "jpeg" ]
"Keys": [ "jpg", "jpeg" ],
"MimeTypes": [ "image/jpeg", "image/jpeg" ]
}

View File

@ -94,6 +94,7 @@ private slots:
void multiWordNamedColorXPM();
void supportedFormats();
void supportedMimeTypes();
void readFromDevice_data();
void readFromDevice();
@ -570,6 +571,26 @@ void tst_QImageReader::supportedFormats()
QCOMPARE(formatSet.size(), formats.size());
}
void tst_QImageReader::supportedMimeTypes()
{
QList<QByteArray> mimeTypes = QImageReader::supportedMimeTypes();
QList<QByteArray> sortedMimeTypes = mimeTypes;
qSort(sortedMimeTypes);
// check that the list is sorted
QCOMPARE(mimeTypes, sortedMimeTypes);
QSet<QByteArray> mimeTypeSet;
foreach (QByteArray mimeType, mimeTypes)
mimeTypeSet << mimeType;
// check the list as a minimum contains image/bmp
QVERIFY(mimeTypeSet.contains("image/bmp"));
// check that the list does not contain duplicates
QCOMPARE(mimeTypeSet.size(), mimeTypes.size());
}
void tst_QImageReader::setBackgroundColor_data()
{
QTest::addColumn<QString>("fileName");

View File

@ -81,6 +81,7 @@ private slots:
void writeImage2_data();
void writeImage2();
void supportedFormats();
void supportedMimeTypes();
void writeToInvalidDevice();
@ -352,6 +353,26 @@ void tst_QImageWriter::supportedFormats()
QCOMPARE(formatSet.size(), formats.size());
}
void tst_QImageWriter::supportedMimeTypes()
{
QList<QByteArray> mimeTypes = QImageWriter::supportedMimeTypes();
QList<QByteArray> sortedMimeTypes = mimeTypes;
qSort(sortedMimeTypes);
// check that the list is sorted
QCOMPARE(mimeTypes, sortedMimeTypes);
QSet<QByteArray> mimeTypeSet;
foreach (QByteArray mimeType, mimeTypes)
mimeTypeSet << mimeType;
// check the list as a minimum contains image/bmp
QVERIFY(mimeTypeSet.contains("image/bmp"));
// check that the list does not contain duplicates
QCOMPARE(mimeTypeSet.size(), mimeTypes.size());
}
void tst_QImageWriter::writeToInvalidDevice()
{
QLatin1String fileName("/these/directories/do/not/exist/001.png");