📄 qimagereader.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************///#define QIMAGEREADER_DEBUG/*! \class QImageReader \brief The QImageReader class provides a format independent interface for reading images from files or other devices. \reentrant \ingroup multimedia \ingroup io The most common way to read images is through QImage and QPixmap's constructors, or by calling QImage::load() and QPixmap::load(). QImageReader is a specialized class which gives you more control when reading images. For example, you can read an image into a specific size by calling setScaledSize(), and you can select a clip rect, effectively loading only parts of an image, by calling setClipRect(). Depending on the underlying support in the image format, this can save memory and speed up loading of images. To read an image, you start by constructing a QImageReader object. Pass either a file name or a device pointer, and the image format to QImageReader's constructor. You can then set several options, such as the clip rect (by calling setClipRect()) and scaled size (by calling setScaledSize()). canRead() returns the image if the QImageReader can read the image (i.e., the image format is supported and the device is open for reading). Call read() to read the image. If any error occurs when reading the image, read() will return a null QImage. You can then call error() to find the type of error that occurred, or errorString() to get a human readable description of what went wrong. Call supportedImageFormats() for a list of formats that QImageReader can read. QImageReader supports all built-in image formats, in addition to any image format plugins that support reading. \sa QImageWriter, QImageIOHandler, QImageIOPlugin*//*! \enum QImageReader::ImageReaderError This enum describes the different types of errors that can occur when reading images with QImageReader. \value FileNotFoundError QImageReader was used with a file name, but not file was found with that name. This can also happen if the file name contained no extension, and the file with the correct extension is not supported by Qt. \value DeviceError QImageReader encountered a device error when reading the image. You can consult your particular device for more details on what went wrong. \value UnsupportedFormatError Qt does not support the requested image format. \value InvalidDataError The image data was invalid, and QImageReader was unable to read an image from it. The can happen if the image file is damaged. \value UnknownError An unknown error occurred. If you get this value after calling read(), it is most likely caused by a bug in QImageReader.*/#include "qimagereader.h"#include <qbytearray.h>#ifdef QIMAGEREADER_DEBUG#include <qdebug.h>#endif#include <qfile.h>#include <qfileinfo.h>#include <qimage.h>#include <qimageiohandler.h>#include <qlist.h>#include <qrect.h>#include <qset.h>#include <qsize.h>#include <qcolor.h>#include <qvariant.h>// factory loader#include <qcoreapplication.h>#include <private/qfactoryloader_p.h>// image handlers#include <private/qbmphandler_p.h>#include <private/qppmhandler_p.h>#include <private/qxbmhandler_p.h>#include <private/qxpmhandler_p.h>#ifndef QT_NO_IMAGEFORMAT_PNG#include <private/qpnghandler_p.h>#endif#ifndef QT_NO_LIBRARYQ_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader, (QImageIOHandlerFactoryInterface_iid, QCoreApplication::libraryPaths(), QLatin1String("/imageformats")))#endifenum _qt_BuiltInFormatType {#ifndef QT_NO_IMAGEFORMAT_PNG _qt_PngFormat,#endif _qt_BmpFormat,#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};struct _qt_BuiltInFormatStruct{ _qt_BuiltInFormatType type; const char *extension;};static const _qt_BuiltInFormatStruct _qt_BuiltInFormats[] = {#ifndef QT_NO_IMAGEFORMAT_PNG {_qt_PngFormat, "png"},#endif {_qt_BmpFormat, "bmp"},#ifndef QT_NO_IMAGEFORMAT_PPM {_qt_PpmFormat, "ppm"}, {_qt_PgmFormat, "pgm"}, {_qt_PbmFormat, "pbm"},#endif#ifndef QT_NO_IMAGEFORMAT_XBM {_qt_XbmFormat, "xbm"},#endif#ifndef QT_NO_IMAGEFORMAT_XPM {_qt_XpmFormat, "xpm"},#endif {_qt_NoFormat, ""}};static QImageIOHandler *createReadHandler(QIODevice *device, const QByteArray &format){ QByteArray form = format.toLower(); QImageIOHandler *handler = 0;#ifndef QT_NO_LIBRARY // check if we have plugins that support the image format QFactoryLoader *l = loader(); QStringList keys = l->keys();#endif QByteArray suffix;#ifdef QIMAGEREADER_DEBUG qDebug() << "QImageReader::createReadHandler( device =" << (void *)device << ", format =" << format << ")," << keys.size() << "plugins available: " << keys;#endif#ifndef QT_NO_LIBRARY int suffixPluginIndex = -1; if (device && format.isEmpty()) { // if there's no format, see if \a device is a file, and if so, find // the file suffix and find support for that format among our plugins. // this allows plugins to override our built-in handlers. if (QFile *file = qobject_cast<QFile *>(device)) {#ifdef QIMAGEREADER_DEBUG qDebug() << "QImageReader::createReadHandler: device is a file:" << file->fileName();#endif if (!(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()) { int index = keys.indexOf(QString::fromLatin1(suffix)); if (index != -1) {#ifdef QIMAGEREADER_DEBUG qDebug() << "QImageReader::createReadHandler: suffix recognized; the" << suffix << "plugin might be able to read this";#endif suffixPluginIndex = index; } } } }#endif // QT_NO_LIBRARY QByteArray testFormat = !form.isEmpty() ? form : suffix;#ifndef QT_NO_LIBRARY if (suffixPluginIndex != -1) { // check if the plugin that claims support for this format can load // from this device with this format. const qint64 pos = device ? device->pos() : 0; QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(QString::fromLatin1(suffix))); if (plugin && plugin->capabilities(device, testFormat) & QImageIOPlugin::CanRead) { handler = plugin->create(device, testFormat);#ifdef QIMAGEREADER_DEBUG qDebug() << "QImageReader::createReadHandler: using the" << suffix << "plugin";#endif } if (device && !device->isSequential()) device->seek(pos); } if (!handler && !testFormat.isEmpty()) { // check if any plugin supports the format (they are not allowed to // read from the device yet). const qint64 pos = device ? device->pos() : 0; for (int i = 0; i < keys.size(); ++i) { if (i != suffixPluginIndex) { QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i))); if (plugin && plugin->capabilities(device, testFormat) & QImageIOPlugin::CanRead) {#ifdef QIMAGEREADER_DEBUG qDebug() << "QImageReader::createReadHandler: the" << keys.at(i) << "plugin can read this format";#endif handler = plugin->create(device, testFormat); break; } } } if (device && !device->isSequential()) device->seek(pos); }#endif // QT_NO_LIBRARY // if we don't have a handler yet, check if we have built-in support for // the format if (!handler && !testFormat.isEmpty()) { if (false) {#ifndef QT_NO_IMAGEFORMAT_PNG } else if (testFormat == "png") { handler = new QPngHandler;#endif#ifndef QT_NO_IMAGEFORMAT_BMP } else if (testFormat == "bmp") { handler = new QBmpHandler;#endif#ifndef QT_NO_IMAGEFORMAT_XPM } else if (testFormat == "xpm") { handler = new QXpmHandler;#endif#ifndef QT_NO_IMAGEFORMAT_XBM } else if (testFormat == "xbm") { handler = new QXbmHandler; handler->setOption(QImageIOHandler::SubType, testFormat);#endif#ifndef QT_NO_IMAGEFORMAT_PPM } else if (testFormat == "pbm" || testFormat == "pbmraw" || testFormat == "pgm" || testFormat == "pgmraw" || testFormat == "ppm" || testFormat == "ppmraw") { handler = new QPpmHandler; handler->setOption(QImageIOHandler::SubType, testFormat);#endif }#ifdef QIMAGEREADER_DEBUG if (handler) qDebug() << "QImageReader::createReadHandler: using the built-in handler for" << testFormat;#endif }#ifndef QT_NO_LIBRARY if (!handler) { // check if any of our plugins recognize the file from its contents. const qint64 pos = device ? device->pos() : 0; for (int i = 0; i < keys.size(); ++i) { if (i != suffixPluginIndex) { QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i))); if (plugin && plugin->capabilities(device, QByteArray()) & QImageIOPlugin::CanRead) { handler = plugin->create(device, testFormat);#ifdef QIMAGEREADER_DEBUG qDebug() << "QImageReader::createReadHandler: the" << keys.at(i) << "plugin can read this data";#endif break; } } } if (device && !device->isSequential()) device->seek(pos); }#endif if (!handler) { // check if any of our built-in handlers recognize the file from its // contents. int currentFormat = 0; if (!suffix.isEmpty()) { // If reading from a file with a suffix, start testing our // built-in handler for that suffix first. for (int i = 0; i < _qt_NumFormats; ++i) { if (_qt_BuiltInFormats[i].extension == suffix) { currentFormat = i; break; } } } QByteArray subType; int numFormats = _qt_NumFormats; while (device && numFormats >= 0) { const _qt_BuiltInFormatStruct *formatStruct = &_qt_BuiltInFormats[currentFormat]; const qint64 pos = device->pos(); switch (formatStruct->type) {#ifndef QT_NO_IMAGEFORMAT_PNG case _qt_PngFormat: if (QPngHandler::canRead(device)) handler = new QPngHandler; break;#endif#ifndef QT_NO_IMAGEFORMAT_BMP case _qt_BmpFormat: if (QBmpHandler::canRead(device)) handler = new QBmpHandler; break;#endif#ifndef QT_NO_IMAGEFORMAT_XPM case _qt_XpmFormat: if (QXpmHandler::canRead(device)) handler = new QXpmHandler; break;#endif#ifndef QT_NO_IMAGEFORMAT_PPM case _qt_PbmFormat: case _qt_PgmFormat: case _qt_PpmFormat: if (QPpmHandler::canRead(device, &subType)) { handler = new QPpmHandler; handler->setOption(QImageIOHandler::SubType, subType); } break;#endif#ifndef QT_NO_IMAGEFORMAT_XBM case _qt_XbmFormat: if (QXbmHandler::canRead(device)) handler = new QXbmHandler; break;#endif default: break; } if (!device->isSequential()) device->seek(pos); if (handler) {#ifdef QIMAGEREADER_DEBUG qDebug() << "QImageReader::createReadHandler: the" << formatStruct->extension << "built-in handler can read this data";#endif break; } --numFormats; ++currentFormat; currentFormat %= _qt_NumFormats; } } if (!handler) {#ifdef QIMAGEREADER_DEBUG qDebug() << "QImageReader::createReadHandler: no handlers found. giving up.";#endif // no handler: give up. return 0; } handler->setDevice(device); if (!form.isEmpty()) handler->setFormat(form); return handler;}class QImageReaderPrivate{public: QImageReaderPrivate(QImageReader *qq); ~QImageReaderPrivate(); // device QByteArray format; QIODevice *device; bool deleteDevice; QImageIOHandler *handler; bool initHandler(); // image options QRect clipRect; QSize scaledSize; QRect scaledClipRect; int quality; QMap<QString, QString> text;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -