[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
[561] | 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
[2] | 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at qt-info@nokia.com.
|
---|
[2] | 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | //#define QIMAGEREADER_DEBUG
|
---|
| 43 |
|
---|
| 44 | /*!
|
---|
| 45 | \class QImageReader
|
---|
| 46 | \brief The QImageReader class provides a format independent interface
|
---|
| 47 | for reading images from files or other devices.
|
---|
| 48 |
|
---|
| 49 | \reentrant
|
---|
[561] | 50 | \ingroup painting
|
---|
[2] | 51 | \ingroup io
|
---|
| 52 |
|
---|
| 53 | The most common way to read images is through QImage and QPixmap's
|
---|
| 54 | constructors, or by calling QImage::load() and
|
---|
| 55 | QPixmap::load(). QImageReader is a specialized class which gives
|
---|
| 56 | you more control when reading images. For example, you can read an
|
---|
| 57 | image into a specific size by calling setScaledSize(), and you can
|
---|
| 58 | select a clip rect, effectively loading only parts of an image, by
|
---|
| 59 | calling setClipRect(). Depending on the underlying support in the
|
---|
| 60 | image format, this can save memory and speed up loading of images.
|
---|
| 61 |
|
---|
| 62 | To read an image, you start by constructing a QImageReader object.
|
---|
| 63 | Pass either a file name or a device pointer, and the image format
|
---|
| 64 | to QImageReader's constructor. You can then set several options,
|
---|
| 65 | such as the clip rect (by calling setClipRect()) and scaled size
|
---|
| 66 | (by calling setScaledSize()). canRead() returns the image if the
|
---|
| 67 | QImageReader can read the image (i.e., the image format is
|
---|
| 68 | supported and the device is open for reading). Call read() to read
|
---|
| 69 | the image.
|
---|
| 70 |
|
---|
| 71 | If any error occurs when reading the image, read() will return a
|
---|
| 72 | null QImage. You can then call error() to find the type of error
|
---|
| 73 | that occurred, or errorString() to get a human readable
|
---|
| 74 | description of what went wrong.
|
---|
| 75 |
|
---|
| 76 | Call supportedImageFormats() for a list of formats that
|
---|
| 77 | QImageReader can read. QImageReader supports all built-in image
|
---|
| 78 | formats, in addition to any image format plugins that support
|
---|
| 79 | reading.
|
---|
| 80 |
|
---|
| 81 | QImageReader autodetects the image format by default, by looking at the
|
---|
| 82 | provided (optional) format string, the file name suffix, and the data
|
---|
| 83 | stream contents. You can enable or disable this feature, by calling
|
---|
| 84 | setAutoDetectImageFormat().
|
---|
| 85 |
|
---|
| 86 | \sa QImageWriter, QImageIOHandler, QImageIOPlugin
|
---|
| 87 | */
|
---|
| 88 |
|
---|
| 89 | /*!
|
---|
| 90 | \enum QImageReader::ImageReaderError
|
---|
| 91 |
|
---|
| 92 | This enum describes the different types of errors that can occur
|
---|
| 93 | when reading images with QImageReader.
|
---|
| 94 |
|
---|
| 95 | \value FileNotFoundError QImageReader was used with a file name,
|
---|
| 96 | but not file was found with that name. This can also happen if the
|
---|
| 97 | file name contained no extension, and the file with the correct
|
---|
| 98 | extension is not supported by Qt.
|
---|
| 99 |
|
---|
| 100 | \value DeviceError QImageReader encountered a device error when
|
---|
| 101 | reading the image. You can consult your particular device for more
|
---|
| 102 | details on what went wrong.
|
---|
| 103 |
|
---|
| 104 | \value UnsupportedFormatError Qt does not support the requested
|
---|
| 105 | image format.
|
---|
| 106 |
|
---|
| 107 | \value InvalidDataError The image data was invalid, and
|
---|
| 108 | QImageReader was unable to read an image from it. The can happen
|
---|
| 109 | if the image file is damaged.
|
---|
| 110 |
|
---|
| 111 | \value UnknownError An unknown error occurred. If you get this
|
---|
| 112 | value after calling read(), it is most likely caused by a bug in
|
---|
| 113 | QImageReader.
|
---|
| 114 | */
|
---|
| 115 | #include "qimagereader.h"
|
---|
| 116 |
|
---|
| 117 | #include <qbytearray.h>
|
---|
| 118 | #ifdef QIMAGEREADER_DEBUG
|
---|
| 119 | #include <qdebug.h>
|
---|
| 120 | #endif
|
---|
| 121 | #include <qfile.h>
|
---|
| 122 | #include <qfileinfo.h>
|
---|
| 123 | #include <qimage.h>
|
---|
| 124 | #include <qimageiohandler.h>
|
---|
| 125 | #include <qlist.h>
|
---|
| 126 | #include <qrect.h>
|
---|
| 127 | #include <qset.h>
|
---|
| 128 | #include <qsize.h>
|
---|
| 129 | #include <qcolor.h>
|
---|
| 130 | #include <qvariant.h>
|
---|
| 131 |
|
---|
| 132 | // factory loader
|
---|
| 133 | #include <qcoreapplication.h>
|
---|
| 134 | #include <private/qfactoryloader_p.h>
|
---|
| 135 |
|
---|
| 136 | // image handlers
|
---|
| 137 | #include <private/qbmphandler_p.h>
|
---|
| 138 | #include <private/qppmhandler_p.h>
|
---|
| 139 | #include <private/qxbmhandler_p.h>
|
---|
| 140 | #include <private/qxpmhandler_p.h>
|
---|
| 141 | #ifndef QT_NO_IMAGEFORMAT_PNG
|
---|
| 142 | #include <private/qpnghandler_p.h>
|
---|
| 143 | #endif
|
---|
[846] | 144 | #ifndef QT_NO_IMAGEFORMAT_JPEG
|
---|
| 145 | #include <private/qjpeghandler_p.h>
|
---|
| 146 | #endif
|
---|
| 147 | #ifndef QT_NO_IMAGEFORMAT_MNG
|
---|
| 148 | #include <private/qmnghandler_p.h>
|
---|
| 149 | #endif
|
---|
| 150 | #ifndef QT_NO_IMAGEFORMAT_TIFF
|
---|
| 151 | #include <private/qtiffhandler_p.h>
|
---|
| 152 | #endif
|
---|
| 153 | #ifdef QT_BUILTIN_GIF_READER
|
---|
| 154 | #include <private/qgifhandler_p.h>
|
---|
| 155 | #endif
|
---|
[2] | 156 |
|
---|
| 157 | QT_BEGIN_NAMESPACE
|
---|
| 158 |
|
---|
[846] | 159 | #ifndef QT_NO_LIBRARY
|
---|
[2] | 160 | Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
|
---|
| 161 | (QImageIOHandlerFactoryInterface_iid, QLatin1String("/imageformats")))
|
---|
| 162 | #endif
|
---|
| 163 |
|
---|
| 164 | enum _qt_BuiltInFormatType {
|
---|
| 165 | #ifndef QT_NO_IMAGEFORMAT_PNG
|
---|
| 166 | _qt_PngFormat,
|
---|
| 167 | #endif
|
---|
[846] | 168 | #ifndef QT_NO_IMAGEFORMAT_JPEG
|
---|
| 169 | _qt_JpgFormat,
|
---|
| 170 | #endif
|
---|
| 171 | #ifndef QT_NO_IMAGEFORMAT_MNG
|
---|
| 172 | _qt_MngFormat,
|
---|
| 173 | #endif
|
---|
| 174 | #ifndef QT_NO_IMAGEFORMAT_TIFF
|
---|
| 175 | _qt_TifFormat,
|
---|
| 176 | #endif
|
---|
| 177 | #ifdef QT_BUILTIN_GIF_READER
|
---|
| 178 | _qt_GifFormat,
|
---|
| 179 | #endif
|
---|
[2] | 180 | _qt_BmpFormat,
|
---|
| 181 | #ifndef QT_NO_IMAGEFORMAT_PPM
|
---|
| 182 | _qt_PpmFormat,
|
---|
| 183 | _qt_PgmFormat,
|
---|
| 184 | _qt_PbmFormat,
|
---|
| 185 | #endif
|
---|
| 186 | #ifndef QT_NO_IMAGEFORMAT_XBM
|
---|
| 187 | _qt_XbmFormat,
|
---|
| 188 | #endif
|
---|
| 189 | #ifndef QT_NO_IMAGEFORMAT_XPM
|
---|
| 190 | _qt_XpmFormat,
|
---|
| 191 | #endif
|
---|
| 192 | _qt_NumFormats,
|
---|
| 193 | _qt_NoFormat = -1
|
---|
| 194 | };
|
---|
| 195 |
|
---|
| 196 | struct _qt_BuiltInFormatStruct
|
---|
| 197 | {
|
---|
| 198 | _qt_BuiltInFormatType type;
|
---|
| 199 | const char *extension;
|
---|
| 200 | };
|
---|
| 201 |
|
---|
| 202 | static const _qt_BuiltInFormatStruct _qt_BuiltInFormats[] = {
|
---|
| 203 | #ifndef QT_NO_IMAGEFORMAT_PNG
|
---|
| 204 | {_qt_PngFormat, "png"},
|
---|
| 205 | #endif
|
---|
[846] | 206 | #ifndef QT_NO_IMAGEFORMAT_JPEG
|
---|
| 207 | {_qt_JpgFormat, "jpg"},
|
---|
| 208 | #endif
|
---|
| 209 | #ifndef QT_NO_IMAGEFORMAT_MNG
|
---|
| 210 | {_qt_MngFormat, "mng"},
|
---|
| 211 | #endif
|
---|
| 212 | #ifndef QT_NO_IMAGEFORMAT_TIFF
|
---|
| 213 | {_qt_TifFormat, "tif"},
|
---|
| 214 | #endif
|
---|
| 215 | #ifdef QT_BUILTIN_GIF_READER
|
---|
| 216 | {_qt_GifFormat, "gif"},
|
---|
| 217 | #endif
|
---|
[2] | 218 | {_qt_BmpFormat, "bmp"},
|
---|
| 219 | #ifndef QT_NO_IMAGEFORMAT_PPM
|
---|
| 220 | {_qt_PpmFormat, "ppm"},
|
---|
| 221 | {_qt_PgmFormat, "pgm"},
|
---|
| 222 | {_qt_PbmFormat, "pbm"},
|
---|
| 223 | #endif
|
---|
| 224 | #ifndef QT_NO_IMAGEFORMAT_XBM
|
---|
| 225 | {_qt_XbmFormat, "xbm"},
|
---|
| 226 | #endif
|
---|
| 227 | #ifndef QT_NO_IMAGEFORMAT_XPM
|
---|
| 228 | {_qt_XpmFormat, "xpm"},
|
---|
| 229 | #endif
|
---|
| 230 | {_qt_NoFormat, ""}
|
---|
| 231 | };
|
---|
| 232 |
|
---|
| 233 | static QImageIOHandler *createReadHandlerHelper(QIODevice *device,
|
---|
[561] | 234 | const QByteArray &format,
|
---|
| 235 | bool autoDetectImageFormat,
|
---|
| 236 | bool ignoresFormatAndExtension)
|
---|
[2] | 237 | {
|
---|
| 238 | if (!autoDetectImageFormat && format.isEmpty())
|
---|
| 239 | return 0;
|
---|
| 240 |
|
---|
| 241 | QByteArray form = format.toLower();
|
---|
| 242 | QImageIOHandler *handler = 0;
|
---|
| 243 |
|
---|
[846] | 244 | #ifndef QT_NO_LIBRARY
|
---|
[2] | 245 | // check if we have plugins that support the image format
|
---|
| 246 | QFactoryLoader *l = loader();
|
---|
| 247 | QStringList keys = l->keys();
|
---|
| 248 | #endif
|
---|
| 249 | QByteArray suffix;
|
---|
| 250 |
|
---|
| 251 | #ifdef QIMAGEREADER_DEBUG
|
---|
| 252 | qDebug() << "QImageReader::createReadHandler( device =" << (void *)device << ", format =" << format << "),"
|
---|
| 253 | << keys.size() << "plugins available: " << keys;
|
---|
| 254 | #endif
|
---|
| 255 |
|
---|
[846] | 256 | #ifndef QT_NO_LIBRARY
|
---|
[2] | 257 | int suffixPluginIndex = -1;
|
---|
[561] | 258 | if (device && format.isEmpty() && autoDetectImageFormat && !ignoresFormatAndExtension) {
|
---|
[2] | 259 | // if there's no format, see if \a device is a file, and if so, find
|
---|
| 260 | // the file suffix and find support for that format among our plugins.
|
---|
| 261 | // this allows plugins to override our built-in handlers.
|
---|
| 262 | if (QFile *file = qobject_cast<QFile *>(device)) {
|
---|
| 263 | #ifdef QIMAGEREADER_DEBUG
|
---|
| 264 | qDebug() << "QImageReader::createReadHandler: device is a file:" << file->fileName();
|
---|
| 265 | #endif
|
---|
| 266 | if (!(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()) {
|
---|
| 267 | int index = keys.indexOf(QString::fromLatin1(suffix));
|
---|
| 268 | if (index != -1) {
|
---|
| 269 | #ifdef QIMAGEREADER_DEBUG
|
---|
| 270 | qDebug() << "QImageReader::createReadHandler: suffix recognized; the"
|
---|
| 271 | << suffix << "plugin might be able to read this";
|
---|
| 272 | #endif
|
---|
| 273 | suffixPluginIndex = index;
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 | #endif // QT_NO_LIBRARY
|
---|
| 279 |
|
---|
| 280 | QByteArray testFormat = !form.isEmpty() ? form : suffix;
|
---|
| 281 |
|
---|
[561] | 282 | if (ignoresFormatAndExtension)
|
---|
| 283 | testFormat = QByteArray();
|
---|
| 284 |
|
---|
[846] | 285 | #ifndef QT_NO_LIBRARY
|
---|
[2] | 286 | if (suffixPluginIndex != -1) {
|
---|
| 287 | // check if the plugin that claims support for this format can load
|
---|
| 288 | // from this device with this format.
|
---|
| 289 | const qint64 pos = device ? device->pos() : 0;
|
---|
| 290 | QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(QString::fromLatin1(suffix)));
|
---|
| 291 | if (plugin && plugin->capabilities(device, testFormat) & QImageIOPlugin::CanRead) {
|
---|
| 292 | handler = plugin->create(device, testFormat);
|
---|
| 293 | #ifdef QIMAGEREADER_DEBUG
|
---|
| 294 | qDebug() << "QImageReader::createReadHandler: using the" << suffix
|
---|
| 295 | << "plugin";
|
---|
| 296 | #endif
|
---|
| 297 | }
|
---|
| 298 | if (device && !device->isSequential())
|
---|
| 299 | device->seek(pos);
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[769] | 302 | if (!handler && !testFormat.isEmpty() && !ignoresFormatAndExtension) {
|
---|
[2] | 303 | // check if any plugin supports the format (they are not allowed to
|
---|
| 304 | // read from the device yet).
|
---|
| 305 | const qint64 pos = device ? device->pos() : 0;
|
---|
[769] | 306 |
|
---|
| 307 | if (autoDetectImageFormat) {
|
---|
| 308 | for (int i = 0; i < keys.size(); ++i) {
|
---|
| 309 | if (i != suffixPluginIndex) {
|
---|
| 310 | QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i)));
|
---|
| 311 | if (plugin && plugin->capabilities(device, testFormat) & QImageIOPlugin::CanRead) {
|
---|
[2] | 312 | #ifdef QIMAGEREADER_DEBUG
|
---|
[769] | 313 | qDebug() << "QImageReader::createReadHandler: the" << keys.at(i) << "plugin can read this format";
|
---|
[2] | 314 | #endif
|
---|
[769] | 315 | handler = plugin->create(device, testFormat);
|
---|
| 316 | break;
|
---|
| 317 | }
|
---|
[2] | 318 | }
|
---|
| 319 | }
|
---|
[769] | 320 | } else {
|
---|
| 321 | QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(QLatin1String(testFormat)));
|
---|
| 322 | if (plugin && plugin->capabilities(device, testFormat) & QImageIOPlugin::CanRead) {
|
---|
| 323 | #ifdef QIMAGEREADER_DEBUG
|
---|
| 324 | qDebug() << "QImageReader::createReadHandler: the" << testFormat << "plugin can read this format";
|
---|
| 325 | #endif
|
---|
| 326 | handler = plugin->create(device, testFormat);
|
---|
| 327 | }
|
---|
[2] | 328 | }
|
---|
| 329 | if (device && !device->isSequential())
|
---|
| 330 | device->seek(pos);
|
---|
| 331 | }
|
---|
[769] | 332 |
|
---|
[2] | 333 | #endif // QT_NO_LIBRARY
|
---|
| 334 |
|
---|
| 335 | // if we don't have a handler yet, check if we have built-in support for
|
---|
| 336 | // the format
|
---|
| 337 | if (!handler && !testFormat.isEmpty()) {
|
---|
| 338 | if (false) {
|
---|
| 339 | #ifndef QT_NO_IMAGEFORMAT_PNG
|
---|
[846] | 340 | } else if (testFormat == "png") {
|
---|
[2] | 341 | handler = new QPngHandler;
|
---|
| 342 | #endif
|
---|
[846] | 343 | #ifndef QT_NO_IMAGEFORMAT_JPEG
|
---|
| 344 | } else if (testFormat == "jpg" || testFormat == "jpeg") {
|
---|
| 345 | handler = new QJpegHandler;
|
---|
| 346 | #endif
|
---|
| 347 | #ifndef QT_NO_IMAGEFORMAT_MNG
|
---|
| 348 | } else if (testFormat == "mng") {
|
---|
| 349 | handler = new QMngHandler;
|
---|
| 350 | #endif
|
---|
| 351 | #ifndef QT_NO_IMAGEFORMAT_TIFF
|
---|
| 352 | } else if (testFormat == "tif" || testFormat == "tiff") {
|
---|
| 353 | handler = new QTiffHandler;
|
---|
| 354 | #endif
|
---|
| 355 | #ifdef QT_BUILTIN_GIF_READER
|
---|
| 356 | } else if (testFormat == "gif") {
|
---|
| 357 | handler = new QGifHandler;
|
---|
| 358 | #endif
|
---|
[2] | 359 | #ifndef QT_NO_IMAGEFORMAT_BMP
|
---|
| 360 | } else if (testFormat == "bmp") {
|
---|
| 361 | handler = new QBmpHandler;
|
---|
| 362 | #endif
|
---|
| 363 | #ifndef QT_NO_IMAGEFORMAT_XPM
|
---|
| 364 | } else if (testFormat == "xpm") {
|
---|
| 365 | handler = new QXpmHandler;
|
---|
| 366 | #endif
|
---|
| 367 | #ifndef QT_NO_IMAGEFORMAT_XBM
|
---|
| 368 | } else if (testFormat == "xbm") {
|
---|
| 369 | handler = new QXbmHandler;
|
---|
| 370 | handler->setOption(QImageIOHandler::SubType, testFormat);
|
---|
| 371 | #endif
|
---|
| 372 | #ifndef QT_NO_IMAGEFORMAT_PPM
|
---|
| 373 | } else if (testFormat == "pbm" || testFormat == "pbmraw" || testFormat == "pgm"
|
---|
| 374 | || testFormat == "pgmraw" || testFormat == "ppm" || testFormat == "ppmraw") {
|
---|
| 375 | handler = new QPpmHandler;
|
---|
| 376 | handler->setOption(QImageIOHandler::SubType, testFormat);
|
---|
| 377 | #endif
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | #ifdef QIMAGEREADER_DEBUG
|
---|
| 381 | if (handler)
|
---|
| 382 | qDebug() << "QImageReader::createReadHandler: using the built-in handler for" << testFormat;
|
---|
| 383 | #endif
|
---|
| 384 | }
|
---|
| 385 |
|
---|
[846] | 386 | #ifndef QT_NO_LIBRARY
|
---|
[561] | 387 | if (!handler && (autoDetectImageFormat || ignoresFormatAndExtension)) {
|
---|
[2] | 388 | // check if any of our plugins recognize the file from its contents.
|
---|
| 389 | const qint64 pos = device ? device->pos() : 0;
|
---|
| 390 | for (int i = 0; i < keys.size(); ++i) {
|
---|
| 391 | if (i != suffixPluginIndex) {
|
---|
| 392 | QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i)));
|
---|
| 393 | if (plugin && plugin->capabilities(device, QByteArray()) & QImageIOPlugin::CanRead) {
|
---|
| 394 | handler = plugin->create(device, testFormat);
|
---|
| 395 | #ifdef QIMAGEREADER_DEBUG
|
---|
| 396 | qDebug() << "QImageReader::createReadHandler: the" << keys.at(i) << "plugin can read this data";
|
---|
| 397 | #endif
|
---|
| 398 | break;
|
---|
| 399 | }
|
---|
| 400 | }
|
---|
| 401 | }
|
---|
| 402 | if (device && !device->isSequential())
|
---|
| 403 | device->seek(pos);
|
---|
| 404 | }
|
---|
[846] | 405 | #endif // QT_NO_LIBRARY
|
---|
[2] | 406 |
|
---|
[561] | 407 | if (!handler && (autoDetectImageFormat || ignoresFormatAndExtension)) {
|
---|
[2] | 408 | // check if any of our built-in handlers recognize the file from its
|
---|
| 409 | // contents.
|
---|
| 410 | int currentFormat = 0;
|
---|
| 411 | if (!suffix.isEmpty()) {
|
---|
| 412 | // If reading from a file with a suffix, start testing our
|
---|
| 413 | // built-in handler for that suffix first.
|
---|
| 414 | for (int i = 0; i < _qt_NumFormats; ++i) {
|
---|
| 415 | if (_qt_BuiltInFormats[i].extension == suffix) {
|
---|
| 416 | currentFormat = i;
|
---|
| 417 | break;
|
---|
| 418 | }
|
---|
| 419 | }
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | QByteArray subType;
|
---|
| 423 | int numFormats = _qt_NumFormats;
|
---|
| 424 | while (device && numFormats >= 0) {
|
---|
| 425 | const _qt_BuiltInFormatStruct *formatStruct = &_qt_BuiltInFormats[currentFormat];
|
---|
| 426 |
|
---|
| 427 | const qint64 pos = device->pos();
|
---|
| 428 | switch (formatStruct->type) {
|
---|
| 429 | #ifndef QT_NO_IMAGEFORMAT_PNG
|
---|
| 430 | case _qt_PngFormat:
|
---|
| 431 | if (QPngHandler::canRead(device))
|
---|
| 432 | handler = new QPngHandler;
|
---|
| 433 | break;
|
---|
| 434 | #endif
|
---|
[846] | 435 | #ifndef QT_NO_IMAGEFORMAT_JPEG
|
---|
| 436 | case _qt_JpgFormat:
|
---|
| 437 | if (QJpegHandler::canRead(device))
|
---|
| 438 | handler = new QJpegHandler;
|
---|
| 439 | break;
|
---|
| 440 | #endif
|
---|
| 441 | #ifndef QT_NO_IMAGEFORMAT_MNG
|
---|
| 442 | case _qt_MngFormat:
|
---|
| 443 | if (QMngHandler::canRead(device))
|
---|
| 444 | handler = new QMngHandler;
|
---|
| 445 | break;
|
---|
| 446 | #endif
|
---|
| 447 | #ifndef QT_NO_IMAGEFORMAT_TIFF
|
---|
| 448 | case _qt_TifFormat:
|
---|
| 449 | if (QTiffHandler::canRead(device))
|
---|
| 450 | handler = new QTiffHandler;
|
---|
| 451 | break;
|
---|
| 452 | #endif
|
---|
| 453 | #ifdef QT_BUILTIN_GIF_READER
|
---|
| 454 | case _qt_GifFormat:
|
---|
| 455 | if (QGifHandler::canRead(device))
|
---|
| 456 | handler = new QGifHandler;
|
---|
| 457 | break;
|
---|
| 458 | #endif
|
---|
[2] | 459 | #ifndef QT_NO_IMAGEFORMAT_BMP
|
---|
| 460 | case _qt_BmpFormat:
|
---|
| 461 | if (QBmpHandler::canRead(device))
|
---|
| 462 | handler = new QBmpHandler;
|
---|
| 463 | break;
|
---|
| 464 | #endif
|
---|
| 465 | #ifndef QT_NO_IMAGEFORMAT_XPM
|
---|
| 466 | case _qt_XpmFormat:
|
---|
| 467 | if (QXpmHandler::canRead(device))
|
---|
| 468 | handler = new QXpmHandler;
|
---|
| 469 | break;
|
---|
| 470 | #endif
|
---|
| 471 | #ifndef QT_NO_IMAGEFORMAT_PPM
|
---|
| 472 | case _qt_PbmFormat:
|
---|
| 473 | case _qt_PgmFormat:
|
---|
| 474 | case _qt_PpmFormat:
|
---|
| 475 | if (QPpmHandler::canRead(device, &subType)) {
|
---|
| 476 | handler = new QPpmHandler;
|
---|
| 477 | handler->setOption(QImageIOHandler::SubType, subType);
|
---|
| 478 | }
|
---|
| 479 | break;
|
---|
| 480 | #endif
|
---|
| 481 | #ifndef QT_NO_IMAGEFORMAT_XBM
|
---|
| 482 | case _qt_XbmFormat:
|
---|
| 483 | if (QXbmHandler::canRead(device))
|
---|
| 484 | handler = new QXbmHandler;
|
---|
| 485 | break;
|
---|
| 486 | #endif
|
---|
| 487 | default:
|
---|
| 488 | break;
|
---|
| 489 | }
|
---|
| 490 | if (!device->isSequential())
|
---|
| 491 | device->seek(pos);
|
---|
| 492 |
|
---|
| 493 | if (handler) {
|
---|
| 494 | #ifdef QIMAGEREADER_DEBUG
|
---|
| 495 | qDebug() << "QImageReader::createReadHandler: the" << formatStruct->extension
|
---|
| 496 | << "built-in handler can read this data";
|
---|
| 497 | #endif
|
---|
| 498 | break;
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 | --numFormats;
|
---|
| 502 | ++currentFormat;
|
---|
| 503 | currentFormat %= _qt_NumFormats;
|
---|
| 504 | }
|
---|
| 505 | }
|
---|
| 506 |
|
---|
| 507 | if (!handler) {
|
---|
| 508 | #ifdef QIMAGEREADER_DEBUG
|
---|
| 509 | qDebug() << "QImageReader::createReadHandler: no handlers found. giving up.";
|
---|
| 510 | #endif
|
---|
| 511 | // no handler: give up.
|
---|
| 512 | return 0;
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 | handler->setDevice(device);
|
---|
| 516 | if (!form.isEmpty())
|
---|
| 517 | handler->setFormat(form);
|
---|
| 518 | return handler;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | class QImageReaderPrivate
|
---|
| 522 | {
|
---|
| 523 | public:
|
---|
| 524 | QImageReaderPrivate(QImageReader *qq);
|
---|
| 525 | ~QImageReaderPrivate();
|
---|
| 526 |
|
---|
| 527 | // device
|
---|
| 528 | QByteArray format;
|
---|
| 529 | bool autoDetectImageFormat;
|
---|
[561] | 530 | bool ignoresFormatAndExtension;
|
---|
[2] | 531 | QIODevice *device;
|
---|
| 532 | bool deleteDevice;
|
---|
| 533 | QImageIOHandler *handler;
|
---|
| 534 | bool initHandler();
|
---|
| 535 |
|
---|
| 536 | // image options
|
---|
| 537 | QRect clipRect;
|
---|
| 538 | QSize scaledSize;
|
---|
| 539 | QRect scaledClipRect;
|
---|
| 540 | int quality;
|
---|
| 541 | QMap<QString, QString> text;
|
---|
| 542 | void getText();
|
---|
| 543 |
|
---|
| 544 | // error
|
---|
| 545 | QImageReader::ImageReaderError imageReaderError;
|
---|
| 546 | QString errorString;
|
---|
| 547 |
|
---|
| 548 | QImageReader *q;
|
---|
| 549 | };
|
---|
| 550 |
|
---|
| 551 | /*!
|
---|
| 552 | \internal
|
---|
| 553 | */
|
---|
| 554 | QImageReaderPrivate::QImageReaderPrivate(QImageReader *qq)
|
---|
[561] | 555 | : autoDetectImageFormat(true), ignoresFormatAndExtension(false)
|
---|
[2] | 556 | {
|
---|
| 557 | device = 0;
|
---|
| 558 | deleteDevice = false;
|
---|
| 559 | handler = 0;
|
---|
| 560 | quality = -1;
|
---|
| 561 | imageReaderError = QImageReader::UnknownError;
|
---|
| 562 |
|
---|
| 563 | q = qq;
|
---|
| 564 | }
|
---|
| 565 |
|
---|
| 566 | /*!
|
---|
| 567 | \internal
|
---|
| 568 | */
|
---|
| 569 | QImageReaderPrivate::~QImageReaderPrivate()
|
---|
| 570 | {
|
---|
| 571 | if (deleteDevice)
|
---|
| 572 | delete device;
|
---|
| 573 | delete handler;
|
---|
| 574 | }
|
---|
| 575 |
|
---|
| 576 | /*!
|
---|
| 577 | \internal
|
---|
| 578 | */
|
---|
| 579 | bool QImageReaderPrivate::initHandler()
|
---|
| 580 | {
|
---|
| 581 | // check some preconditions
|
---|
[846] | 582 | if (!device || (!deleteDevice && !device->isOpen() && !device->open(QIODevice::ReadOnly))) {
|
---|
[2] | 583 | imageReaderError = QImageReader::DeviceError;
|
---|
| 584 | errorString = QLatin1String(QT_TRANSLATE_NOOP(QImageReader, "Invalid device"));
|
---|
| 585 | return false;
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 | // probe the file extension
|
---|
| 589 | if (deleteDevice && !device->isOpen() && !device->open(QIODevice::ReadOnly) && autoDetectImageFormat) {
|
---|
| 590 | QList<QByteArray> extensions = QImageReader::supportedImageFormats();
|
---|
| 591 | if (!format.isEmpty()) {
|
---|
| 592 | // Try the most probable extension first
|
---|
| 593 | int currentFormatIndex = extensions.indexOf(format.toLower());
|
---|
| 594 | if (currentFormatIndex > 0)
|
---|
| 595 | extensions.swap(0, currentFormatIndex);
|
---|
| 596 | }
|
---|
| 597 |
|
---|
| 598 | int currentExtension = 0;
|
---|
| 599 |
|
---|
| 600 | QFile *file = static_cast<QFile *>(device);
|
---|
| 601 | QString fileName = file->fileName();
|
---|
| 602 |
|
---|
| 603 | do {
|
---|
| 604 | file->setFileName(fileName + QLatin1Char('.')
|
---|
| 605 | + QString::fromLatin1(extensions.at(currentExtension++).constData()));
|
---|
| 606 | file->open(QIODevice::ReadOnly);
|
---|
| 607 | } while (!file->isOpen() && currentExtension < extensions.size());
|
---|
| 608 |
|
---|
| 609 | if (!device->isOpen()) {
|
---|
| 610 | imageReaderError = QImageReader::FileNotFoundError;
|
---|
| 611 | errorString = QLatin1String(QT_TRANSLATE_NOOP(QImageReader, "File not found"));
|
---|
| 612 | file->setFileName(fileName); // restore the old file name
|
---|
| 613 | return false;
|
---|
| 614 | }
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 | // assign a handler
|
---|
[561] | 618 | if (!handler && (handler = createReadHandlerHelper(device, format, autoDetectImageFormat, ignoresFormatAndExtension)) == 0) {
|
---|
[2] | 619 | imageReaderError = QImageReader::UnsupportedFormatError;
|
---|
| 620 | errorString = QLatin1String(QT_TRANSLATE_NOOP(QImageReader, "Unsupported image format"));
|
---|
| 621 | return false;
|
---|
| 622 | }
|
---|
| 623 | return true;
|
---|
| 624 | }
|
---|
| 625 |
|
---|
| 626 | /*!
|
---|
| 627 | \internal
|
---|
| 628 | */
|
---|
| 629 | void QImageReaderPrivate::getText()
|
---|
| 630 | {
|
---|
| 631 | if (!text.isEmpty() || (!handler && !initHandler()) || !handler->supportsOption(QImageIOHandler::Description))
|
---|
| 632 | return;
|
---|
| 633 | foreach (QString pair, handler->option(QImageIOHandler::Description).toString().split(
|
---|
| 634 | QLatin1String("\n\n"))) {
|
---|
| 635 | int index = pair.indexOf(QLatin1Char(':'));
|
---|
| 636 | if (index >= 0 && pair.indexOf(QLatin1Char(' ')) < index) {
|
---|
| 637 | text.insert(QLatin1String("Description"), pair.simplified());
|
---|
| 638 | } else {
|
---|
| 639 | QString key = pair.left(index);
|
---|
| 640 | text.insert(key, pair.mid(index + 2).simplified());
|
---|
| 641 | }
|
---|
| 642 | }
|
---|
| 643 | }
|
---|
| 644 |
|
---|
| 645 | /*!
|
---|
| 646 | Constructs an empty QImageReader object. Before reading an image,
|
---|
| 647 | call setDevice() or setFileName().
|
---|
| 648 | */
|
---|
| 649 | QImageReader::QImageReader()
|
---|
| 650 | : d(new QImageReaderPrivate(this))
|
---|
| 651 | {
|
---|
| 652 | }
|
---|
| 653 |
|
---|
| 654 | /*!
|
---|
| 655 | Constructs a QImageReader object with the device \a device and the
|
---|
| 656 | image format \a format.
|
---|
| 657 | */
|
---|
| 658 | QImageReader::QImageReader(QIODevice *device, const QByteArray &format)
|
---|
| 659 | : d(new QImageReaderPrivate(this))
|
---|
| 660 | {
|
---|
| 661 | d->device = device;
|
---|
| 662 | d->format = format;
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | /*!
|
---|
| 666 | Constructs a QImageReader object with the file name \a fileName
|
---|
| 667 | and the image format \a format.
|
---|
| 668 |
|
---|
| 669 | \sa setFileName()
|
---|
| 670 | */
|
---|
| 671 | QImageReader::QImageReader(const QString &fileName, const QByteArray &format)
|
---|
| 672 | : d(new QImageReaderPrivate(this))
|
---|
| 673 | {
|
---|
| 674 | QFile *file = new QFile(fileName);
|
---|
| 675 | d->device = file;
|
---|
| 676 | d->deleteDevice = true;
|
---|
| 677 | d->format = format;
|
---|
| 678 | }
|
---|
| 679 |
|
---|
| 680 | /*!
|
---|
| 681 | Destructs the QImageReader object.
|
---|
| 682 | */
|
---|
| 683 | QImageReader::~QImageReader()
|
---|
| 684 | {
|
---|
| 685 | delete d;
|
---|
| 686 | }
|
---|
| 687 |
|
---|
| 688 | /*!
|
---|
| 689 | Sets the format QImageReader will use when reading images, to \a
|
---|
| 690 | format. \a format is a case insensitive text string. Example:
|
---|
| 691 |
|
---|
| 692 | \snippet doc/src/snippets/code/src_gui_image_qimagereader.cpp 0
|
---|
| 693 |
|
---|
| 694 | You can call supportedImageFormats() for the full list of formats
|
---|
| 695 | QImageReader supports.
|
---|
| 696 |
|
---|
| 697 | \sa format()
|
---|
| 698 | */
|
---|
| 699 | void QImageReader::setFormat(const QByteArray &format)
|
---|
| 700 | {
|
---|
| 701 | d->format = format;
|
---|
| 702 | }
|
---|
| 703 |
|
---|
| 704 | /*!
|
---|
| 705 | Returns the format QImageReader uses for reading images.
|
---|
| 706 |
|
---|
| 707 | You can call this function after assigning a device to the
|
---|
| 708 | reader to determine the format of the device. For example:
|
---|
| 709 |
|
---|
| 710 | \snippet doc/src/snippets/code/src_gui_image_qimagereader.cpp 1
|
---|
| 711 |
|
---|
| 712 | If the reader cannot read any image from the device (e.g., there is no
|
---|
| 713 | image there, or the image has already been read), or if the format is
|
---|
| 714 | unsupported, this function returns an empty QByteArray().
|
---|
| 715 |
|
---|
| 716 | \sa setFormat(), supportedImageFormats()
|
---|
| 717 | */
|
---|
| 718 | QByteArray QImageReader::format() const
|
---|
| 719 | {
|
---|
| 720 | if (d->format.isEmpty()) {
|
---|
| 721 | if (!d->initHandler())
|
---|
| 722 | return QByteArray();
|
---|
| 723 | return d->handler->canRead() ? d->handler->format() : QByteArray();
|
---|
| 724 | }
|
---|
| 725 |
|
---|
| 726 | return d->format;
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 | /*!
|
---|
| 730 | If \a enabled is true, image format autodetection is enabled; otherwise,
|
---|
| 731 | it is disabled. By default, autodetection is enabled.
|
---|
| 732 |
|
---|
| 733 | QImageReader uses an extensive approach to detecting the image format;
|
---|
| 734 | firstly, if you pass a file name to QImageReader, it will attempt to
|
---|
| 735 | detect the file extension if the given file name does not point to an
|
---|
| 736 | existing file, by appending supported default extensions to the given file
|
---|
| 737 | name, one at a time. It then uses the following approach to detect the
|
---|
| 738 | image format:
|
---|
| 739 |
|
---|
| 740 | \list
|
---|
| 741 |
|
---|
| 742 | \o Image plugins are queried first, based on either the optional format
|
---|
| 743 | string, or the file name suffix (if the source device is a file). No
|
---|
| 744 | content detection is done at this stage. QImageReader will choose the
|
---|
| 745 | first plugin that supports reading for this format.
|
---|
| 746 |
|
---|
| 747 | \o If no plugin supports the image format, Qt's built-in handlers are
|
---|
| 748 | checked based on either the optional format string, or the file name
|
---|
| 749 | suffix.
|
---|
| 750 |
|
---|
| 751 | \o If no capable plugins or built-in handlers are found, each plugin is
|
---|
| 752 | tested by inspecting the content of the data stream.
|
---|
| 753 |
|
---|
| 754 | \o If no plugins could detect the image format based on data contents,
|
---|
| 755 | each built-in image handler is tested by inspecting the contents.
|
---|
| 756 |
|
---|
| 757 | \o Finally, if all above approaches fail, QImageReader will report failure
|
---|
| 758 | when trying to read the image.
|
---|
| 759 |
|
---|
[561] | 760 | \endlist
|
---|
[2] | 761 |
|
---|
| 762 | By disabling image format autodetection, QImageReader will only query the
|
---|
| 763 | plugins and built-in handlers based on the format string (i.e., no file
|
---|
| 764 | name extensions are tested).
|
---|
| 765 |
|
---|
| 766 | \sa QImageIOHandler::canRead(), QImageIOPlugin::capabilities()
|
---|
| 767 | */
|
---|
| 768 | void QImageReader::setAutoDetectImageFormat(bool enabled)
|
---|
| 769 | {
|
---|
| 770 | d->autoDetectImageFormat = enabled;
|
---|
| 771 | }
|
---|
| 772 |
|
---|
| 773 | /*!
|
---|
| 774 | Returns true if image format autodetection is enabled on this image
|
---|
| 775 | reader; otherwise returns false. By default, autodetection is enabled.
|
---|
| 776 |
|
---|
[561] | 777 | \sa setAutoDetectImageFormat()
|
---|
[2] | 778 | */
|
---|
| 779 | bool QImageReader::autoDetectImageFormat() const
|
---|
| 780 | {
|
---|
| 781 | return d->autoDetectImageFormat;
|
---|
| 782 | }
|
---|
| 783 |
|
---|
[561] | 784 |
|
---|
[2] | 785 | /*!
|
---|
[561] | 786 | If \a ignored is set to true, then the image reader will ignore
|
---|
| 787 | specified formats or file extensions and decide which plugin to
|
---|
| 788 | use only based on the contents in the datastream.
|
---|
| 789 |
|
---|
| 790 | Setting this flag means that all image plugins gets loaded. Each
|
---|
| 791 | plugin will read the first bytes in the image data and decide if
|
---|
| 792 | the plugin is compatible or not.
|
---|
| 793 |
|
---|
| 794 | This also disables auto detecting the image format.
|
---|
| 795 |
|
---|
| 796 | \sa decideFormatFromContent()
|
---|
| 797 | */
|
---|
| 798 |
|
---|
| 799 | void QImageReader::setDecideFormatFromContent(bool ignored)
|
---|
| 800 | {
|
---|
| 801 | d->ignoresFormatAndExtension = ignored;
|
---|
| 802 | }
|
---|
| 803 |
|
---|
| 804 |
|
---|
| 805 | /*!
|
---|
| 806 | Returns whether the image reader should decide which plugin to use
|
---|
| 807 | only based on the contents of the datastream rather than on the file
|
---|
| 808 | extension.
|
---|
| 809 |
|
---|
| 810 | \sa setDecideFormatFromContent()
|
---|
| 811 | */
|
---|
| 812 |
|
---|
| 813 | bool QImageReader::decideFormatFromContent() const
|
---|
| 814 | {
|
---|
| 815 | return d->ignoresFormatAndExtension;
|
---|
| 816 | }
|
---|
| 817 |
|
---|
| 818 |
|
---|
| 819 | /*!
|
---|
[2] | 820 | Sets QImageReader's device to \a device. If a device has already
|
---|
| 821 | been set, the old device is removed from QImageReader and is
|
---|
| 822 | otherwise left unchanged.
|
---|
| 823 |
|
---|
| 824 | If the device is not already open, QImageReader will attempt to
|
---|
| 825 | open the device in \l QIODevice::ReadOnly mode by calling
|
---|
| 826 | open(). Note that this does not work for certain devices, such as
|
---|
| 827 | QProcess, QTcpSocket and QUdpSocket, where more logic is required
|
---|
| 828 | to open the device.
|
---|
| 829 |
|
---|
| 830 | \sa device(), setFileName()
|
---|
| 831 | */
|
---|
| 832 | void QImageReader::setDevice(QIODevice *device)
|
---|
| 833 | {
|
---|
| 834 | if (d->device && d->deleteDevice)
|
---|
| 835 | delete d->device;
|
---|
| 836 | d->device = device;
|
---|
| 837 | d->deleteDevice = false;
|
---|
| 838 | delete d->handler;
|
---|
| 839 | d->handler = 0;
|
---|
| 840 | d->text.clear();
|
---|
| 841 | }
|
---|
| 842 |
|
---|
| 843 | /*!
|
---|
| 844 | Returns the device currently assigned to QImageReader, or 0 if no
|
---|
| 845 | device has been assigned.
|
---|
| 846 | */
|
---|
| 847 | QIODevice *QImageReader::device() const
|
---|
| 848 | {
|
---|
| 849 | return d->device;
|
---|
| 850 | }
|
---|
| 851 |
|
---|
| 852 | /*!
|
---|
| 853 | Sets the file name of QImageReader to \a fileName. Internally,
|
---|
| 854 | QImageReader will create a QFile object and open it in \l
|
---|
| 855 | QIODevice::ReadOnly mode, and use this when reading images.
|
---|
| 856 |
|
---|
| 857 | If \a fileName does not include a file extension (e.g., .png or .bmp),
|
---|
| 858 | QImageReader will cycle through all supported extensions until it finds
|
---|
| 859 | a matching file.
|
---|
| 860 |
|
---|
| 861 | \sa fileName(), setDevice(), supportedImageFormats()
|
---|
| 862 | */
|
---|
| 863 | void QImageReader::setFileName(const QString &fileName)
|
---|
| 864 | {
|
---|
| 865 | setDevice(new QFile(fileName));
|
---|
| 866 | d->deleteDevice = true;
|
---|
| 867 | }
|
---|
| 868 |
|
---|
| 869 | /*!
|
---|
| 870 | If the currently assigned device is a QFile, or if setFileName()
|
---|
| 871 | has been called, this function returns the name of the file
|
---|
| 872 | QImageReader reads from. Otherwise (i.e., if no device has been
|
---|
| 873 | assigned or the device is not a QFile), an empty QString is
|
---|
| 874 | returned.
|
---|
| 875 |
|
---|
| 876 | \sa setFileName(), setDevice()
|
---|
| 877 | */
|
---|
| 878 | QString QImageReader::fileName() const
|
---|
| 879 | {
|
---|
| 880 | QFile *file = qobject_cast<QFile *>(d->device);
|
---|
| 881 | return file ? file->fileName() : QString();
|
---|
| 882 | }
|
---|
| 883 |
|
---|
| 884 | /*!
|
---|
| 885 | \since 4.2
|
---|
| 886 |
|
---|
| 887 | This is an image format specific function that sets the quality
|
---|
| 888 | level of the image to \a quality. For image formats that do not
|
---|
| 889 | support setting the quality, this value is ignored.
|
---|
| 890 |
|
---|
| 891 | The value range of \a quality depends on the image format. For
|
---|
| 892 | example, the "jpeg" format supports a quality range from 0 (low
|
---|
| 893 | quality, high compression) to 100 (high quality, low compression).
|
---|
| 894 |
|
---|
| 895 | \sa quality()
|
---|
| 896 | */
|
---|
| 897 | void QImageReader::setQuality(int quality)
|
---|
| 898 | {
|
---|
| 899 | d->quality = quality;
|
---|
| 900 | }
|
---|
| 901 |
|
---|
| 902 | /*!
|
---|
| 903 | \since 4.2
|
---|
| 904 |
|
---|
| 905 | Returns the quality level of the image.
|
---|
| 906 |
|
---|
| 907 | \sa setQuality()
|
---|
| 908 | */
|
---|
| 909 | int QImageReader::quality() const
|
---|
| 910 | {
|
---|
| 911 | return d->quality;
|
---|
| 912 | }
|
---|
| 913 |
|
---|
| 914 |
|
---|
| 915 | /*!
|
---|
| 916 | Returns the size of the image, without actually reading the image
|
---|
| 917 | contents.
|
---|
| 918 |
|
---|
| 919 | If the image format does not support this feature, this function returns
|
---|
| 920 | an invalid size. Qt's built-in image handlers all support this feature,
|
---|
| 921 | but custom image format plugins are not required to do so.
|
---|
| 922 |
|
---|
| 923 | \sa QImageIOHandler::ImageOption, QImageIOHandler::option(), QImageIOHandler::supportsOption()
|
---|
| 924 | */
|
---|
| 925 | QSize QImageReader::size() const
|
---|
| 926 | {
|
---|
| 927 | if (!d->initHandler())
|
---|
| 928 | return QSize();
|
---|
| 929 |
|
---|
| 930 | if (d->handler->supportsOption(QImageIOHandler::Size))
|
---|
| 931 | return d->handler->option(QImageIOHandler::Size).toSize();
|
---|
| 932 |
|
---|
| 933 | return QSize();
|
---|
| 934 | }
|
---|
| 935 |
|
---|
| 936 | /*!
|
---|
| 937 | \since 4.5
|
---|
| 938 |
|
---|
| 939 | Returns the format of the image, without actually reading the image
|
---|
| 940 | contents. The format describes the image format \l QImageReader::read()
|
---|
| 941 | returns, not the format of the actual image.
|
---|
| 942 |
|
---|
| 943 | If the image format does not support this feature, this function returns
|
---|
| 944 | an invalid format.
|
---|
| 945 |
|
---|
| 946 | \sa QImageIOHandler::ImageOption, QImageIOHandler::option(), QImageIOHandler::supportsOption()
|
---|
| 947 | */
|
---|
| 948 | QImage::Format QImageReader::imageFormat() const
|
---|
| 949 | {
|
---|
| 950 | if (!d->initHandler())
|
---|
| 951 | return QImage::Format_Invalid;
|
---|
| 952 |
|
---|
| 953 | if (d->handler->supportsOption(QImageIOHandler::ImageFormat))
|
---|
| 954 | return (QImage::Format)d->handler->option(QImageIOHandler::ImageFormat).toInt();
|
---|
| 955 |
|
---|
| 956 | return QImage::Format_Invalid;
|
---|
| 957 | }
|
---|
| 958 |
|
---|
| 959 | /*!
|
---|
| 960 | \since 4.1
|
---|
| 961 |
|
---|
| 962 | Returns the text keys for this image. You can use
|
---|
| 963 | these keys with text() to list the image text for
|
---|
| 964 | a certain key.
|
---|
| 965 |
|
---|
| 966 | Support for this option is implemented through
|
---|
| 967 | QImageIOHandler::Description.
|
---|
| 968 |
|
---|
| 969 | \sa text(), QImageWriter::setText(), QImage::textKeys()
|
---|
| 970 | */
|
---|
| 971 | QStringList QImageReader::textKeys() const
|
---|
| 972 | {
|
---|
| 973 | d->getText();
|
---|
| 974 | return d->text.keys();
|
---|
| 975 | }
|
---|
| 976 |
|
---|
| 977 | /*!
|
---|
| 978 | \since 4.1
|
---|
| 979 |
|
---|
| 980 | Returns the image text associated with \a key.
|
---|
| 981 |
|
---|
| 982 | Support for this option is implemented through
|
---|
| 983 | QImageIOHandler::Description.
|
---|
| 984 |
|
---|
| 985 | \sa textKeys(), QImageWriter::setText()
|
---|
| 986 | */
|
---|
| 987 | QString QImageReader::text(const QString &key) const
|
---|
| 988 | {
|
---|
| 989 | d->getText();
|
---|
| 990 | return d->text.value(key);
|
---|
| 991 | }
|
---|
| 992 |
|
---|
| 993 | /*!
|
---|
| 994 | Sets the image clip rect (also known as the ROI, or Region Of
|
---|
| 995 | Interest) to \a rect. The coordinates of \a rect are relative to
|
---|
| 996 | the untransformed image size, as returned by size().
|
---|
| 997 |
|
---|
| 998 | \sa clipRect(), setScaledSize(), setScaledClipRect()
|
---|
| 999 | */
|
---|
| 1000 | void QImageReader::setClipRect(const QRect &rect)
|
---|
| 1001 | {
|
---|
| 1002 | d->clipRect = rect;
|
---|
| 1003 | }
|
---|
| 1004 |
|
---|
| 1005 | /*!
|
---|
| 1006 | Returns the clip rect (also known as the ROI, or Region Of
|
---|
| 1007 | Interest) of the image. If no clip rect has been set, an invalid
|
---|
| 1008 | QRect is returned.
|
---|
| 1009 |
|
---|
| 1010 | \sa setClipRect()
|
---|
| 1011 | */
|
---|
| 1012 | QRect QImageReader::clipRect() const
|
---|
| 1013 | {
|
---|
| 1014 | return d->clipRect;
|
---|
| 1015 | }
|
---|
| 1016 |
|
---|
| 1017 | /*!
|
---|
| 1018 | Sets the scaled size of the image to \a size. The scaling is
|
---|
| 1019 | performed after the initial clip rect, but before the scaled clip
|
---|
| 1020 | rect is applied. The algorithm used for scaling depends on the
|
---|
| 1021 | image format. By default (i.e., if the image format does not
|
---|
| 1022 | support scaling), QImageReader will use QImage::scale() with
|
---|
| 1023 | Qt::SmoothScaling.
|
---|
| 1024 |
|
---|
| 1025 | \sa scaledSize(), setClipRect(), setScaledClipRect()
|
---|
| 1026 | */
|
---|
| 1027 | void QImageReader::setScaledSize(const QSize &size)
|
---|
| 1028 | {
|
---|
| 1029 | d->scaledSize = size;
|
---|
| 1030 | }
|
---|
| 1031 |
|
---|
| 1032 | /*!
|
---|
| 1033 | Returns the scaled size of the image.
|
---|
| 1034 |
|
---|
| 1035 | \sa setScaledSize()
|
---|
| 1036 | */
|
---|
| 1037 | QSize QImageReader::scaledSize() const
|
---|
| 1038 | {
|
---|
| 1039 | return d->scaledSize;
|
---|
| 1040 | }
|
---|
| 1041 |
|
---|
| 1042 | /*!
|
---|
| 1043 | Sets the scaled clip rect to \a rect. The scaled clip rect is the
|
---|
| 1044 | clip rect (also known as ROI, or Region Of Interest) that is
|
---|
| 1045 | applied after the image has been scaled.
|
---|
| 1046 |
|
---|
| 1047 | \sa scaledClipRect(), setScaledSize()
|
---|
| 1048 | */
|
---|
| 1049 | void QImageReader::setScaledClipRect(const QRect &rect)
|
---|
| 1050 | {
|
---|
| 1051 | d->scaledClipRect = rect;
|
---|
| 1052 | }
|
---|
| 1053 |
|
---|
| 1054 | /*!
|
---|
| 1055 | Returns the scaled clip rect of the image.
|
---|
| 1056 |
|
---|
| 1057 | \sa setScaledClipRect()
|
---|
| 1058 | */
|
---|
| 1059 | QRect QImageReader::scaledClipRect() const
|
---|
| 1060 | {
|
---|
| 1061 | return d->scaledClipRect;
|
---|
| 1062 | }
|
---|
| 1063 |
|
---|
| 1064 | /*!
|
---|
| 1065 | \since 4.1
|
---|
| 1066 |
|
---|
| 1067 | Sets the background color to \a color.
|
---|
| 1068 | Image formats that support this operation are expected to
|
---|
| 1069 | initialize the background to \a color before reading an image.
|
---|
| 1070 |
|
---|
| 1071 | \sa backgroundColor(), read()
|
---|
| 1072 | */
|
---|
| 1073 | void QImageReader::setBackgroundColor(const QColor &color)
|
---|
| 1074 | {
|
---|
| 1075 | if (!d->initHandler())
|
---|
| 1076 | return;
|
---|
| 1077 | if (d->handler->supportsOption(QImageIOHandler::BackgroundColor))
|
---|
| 1078 | d->handler->setOption(QImageIOHandler::BackgroundColor, color);
|
---|
| 1079 | }
|
---|
| 1080 |
|
---|
| 1081 | /*!
|
---|
| 1082 | \since 4.1
|
---|
| 1083 |
|
---|
| 1084 | Returns the background color that's used when reading an image.
|
---|
| 1085 | If the image format does not support setting the background color
|
---|
| 1086 | an invalid color is returned.
|
---|
| 1087 |
|
---|
| 1088 | \sa setBackgroundColor(), read()
|
---|
| 1089 | */
|
---|
| 1090 | QColor QImageReader::backgroundColor() const
|
---|
| 1091 | {
|
---|
| 1092 | if (!d->initHandler())
|
---|
| 1093 | return QColor();
|
---|
| 1094 | if (d->handler->supportsOption(QImageIOHandler::BackgroundColor))
|
---|
| 1095 | return qVariantValue<QColor>(d->handler->option(QImageIOHandler::BackgroundColor));
|
---|
| 1096 | return QColor();
|
---|
| 1097 | }
|
---|
| 1098 |
|
---|
| 1099 | /*!
|
---|
| 1100 | \since 4.1
|
---|
| 1101 |
|
---|
| 1102 | Returns true if the image format supports animation;
|
---|
| 1103 | otherwise, false is returned.
|
---|
| 1104 |
|
---|
| 1105 | \sa QMovie::supportedFormats()
|
---|
| 1106 | */
|
---|
| 1107 | bool QImageReader::supportsAnimation() const
|
---|
| 1108 | {
|
---|
| 1109 | if (!d->initHandler())
|
---|
| 1110 | return false;
|
---|
| 1111 | if (d->handler->supportsOption(QImageIOHandler::Animation))
|
---|
| 1112 | return d->handler->option(QImageIOHandler::Animation).toBool();
|
---|
| 1113 | return false;
|
---|
| 1114 | }
|
---|
| 1115 |
|
---|
| 1116 | /*!
|
---|
| 1117 | Returns true if an image can be read for the device (i.e., the
|
---|
| 1118 | image format is supported, and the device seems to contain valid
|
---|
| 1119 | data); otherwise returns false.
|
---|
| 1120 |
|
---|
| 1121 | canRead() is a lightweight function that only does a quick test to
|
---|
| 1122 | see if the image data is valid. read() may still return false
|
---|
| 1123 | after canRead() returns true, if the image data is corrupt.
|
---|
| 1124 |
|
---|
| 1125 | For images that support animation, canRead() returns false when
|
---|
| 1126 | all frames have been read.
|
---|
| 1127 |
|
---|
| 1128 | \sa read(), supportedImageFormats()
|
---|
| 1129 | */
|
---|
| 1130 | bool QImageReader::canRead() const
|
---|
| 1131 | {
|
---|
| 1132 | if (!d->initHandler())
|
---|
| 1133 | return false;
|
---|
| 1134 |
|
---|
| 1135 | return d->handler->canRead();
|
---|
| 1136 | }
|
---|
| 1137 |
|
---|
| 1138 | /*!
|
---|
| 1139 | Reads an image from the device. On success, the image that was
|
---|
| 1140 | read is returned; otherwise, a null QImage is returned. You can
|
---|
| 1141 | then call error() to find the type of error that occurred, or
|
---|
| 1142 | errorString() to get a human readable description of the error.
|
---|
| 1143 |
|
---|
| 1144 | For image formats that support animation, calling read()
|
---|
| 1145 | repeatedly will return the next frame. When all frames have been
|
---|
| 1146 | read, a null image will be returned.
|
---|
| 1147 |
|
---|
| 1148 | \sa canRead(), supportedImageFormats(), supportsAnimation(), QMovie
|
---|
| 1149 | */
|
---|
| 1150 | QImage QImageReader::read()
|
---|
| 1151 | {
|
---|
| 1152 | // Because failed image reading might have side effects, we explicitly
|
---|
| 1153 | // return a null image instead of the image we've just created.
|
---|
| 1154 | QImage image;
|
---|
| 1155 | return read(&image) ? image : QImage();
|
---|
| 1156 | }
|
---|
| 1157 |
|
---|
| 1158 | /*!
|
---|
| 1159 | \overload
|
---|
| 1160 |
|
---|
| 1161 | Reads an image from the device into \a image, which must point to a
|
---|
| 1162 | QImage. Returns true on success; otherwise, returns false.
|
---|
| 1163 |
|
---|
| 1164 | If \a image has same format and size as the image data that is about to be
|
---|
| 1165 | read, this function may not need to allocate a new image before
|
---|
| 1166 | reading. Because of this, it can be faster than the other read() overload,
|
---|
| 1167 | which always constructs a new image; especially when reading several
|
---|
| 1168 | images with the same format and size.
|
---|
| 1169 |
|
---|
| 1170 | \snippet doc/src/snippets/code/src_gui_image_qimagereader.cpp 2
|
---|
| 1171 |
|
---|
| 1172 | For image formats that support animation, calling read() repeatedly will
|
---|
| 1173 | return the next frame. When all frames have been read, a null image will
|
---|
| 1174 | be returned.
|
---|
| 1175 |
|
---|
| 1176 | \sa canRead(), supportedImageFormats(), supportsAnimation(), QMovie
|
---|
| 1177 | */
|
---|
| 1178 | bool QImageReader::read(QImage *image)
|
---|
| 1179 | {
|
---|
| 1180 | if (!image) {
|
---|
| 1181 | qWarning("QImageReader::read: cannot read into null pointer");
|
---|
| 1182 | return false;
|
---|
| 1183 | }
|
---|
| 1184 |
|
---|
| 1185 | if (!d->handler && !d->initHandler())
|
---|
| 1186 | return false;
|
---|
| 1187 |
|
---|
| 1188 | // set the handler specific options.
|
---|
| 1189 | if (d->handler->supportsOption(QImageIOHandler::ScaledSize) && d->scaledSize.isValid()) {
|
---|
| 1190 | if ((d->handler->supportsOption(QImageIOHandler::ClipRect) && !d->clipRect.isNull())
|
---|
| 1191 | || d->clipRect.isNull()) {
|
---|
| 1192 | // Only enable the ScaledSize option if there is no clip rect, or
|
---|
| 1193 | // if the handler also supports ClipRect.
|
---|
| 1194 | d->handler->setOption(QImageIOHandler::ScaledSize, d->scaledSize);
|
---|
| 1195 | }
|
---|
| 1196 | }
|
---|
| 1197 | if (d->handler->supportsOption(QImageIOHandler::ClipRect) && !d->clipRect.isNull())
|
---|
| 1198 | d->handler->setOption(QImageIOHandler::ClipRect, d->clipRect);
|
---|
| 1199 | if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull())
|
---|
| 1200 | d->handler->setOption(QImageIOHandler::ScaledClipRect, d->scaledClipRect);
|
---|
| 1201 | if (d->handler->supportsOption(QImageIOHandler::Quality))
|
---|
| 1202 | d->handler->setOption(QImageIOHandler::Quality, d->quality);
|
---|
| 1203 |
|
---|
| 1204 | // read the image
|
---|
| 1205 | if (!d->handler->read(image)) {
|
---|
| 1206 | d->imageReaderError = InvalidDataError;
|
---|
| 1207 | d->errorString = QLatin1String(QT_TRANSLATE_NOOP(QImageReader, "Unable to read image data"));
|
---|
| 1208 | return false;
|
---|
| 1209 | }
|
---|
| 1210 |
|
---|
| 1211 | // provide default implementations for any unsupported image
|
---|
| 1212 | // options
|
---|
| 1213 | if (d->handler->supportsOption(QImageIOHandler::ClipRect) && !d->clipRect.isNull()) {
|
---|
| 1214 | if (d->handler->supportsOption(QImageIOHandler::ScaledSize) && d->scaledSize.isValid()) {
|
---|
| 1215 | if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull()) {
|
---|
| 1216 | // all features are supported by the handler; nothing to do.
|
---|
| 1217 | } else {
|
---|
| 1218 | // the image is already scaled, so apply scaled clipping.
|
---|
| 1219 | if (!d->scaledClipRect.isNull())
|
---|
| 1220 | *image = image->copy(d->scaledClipRect);
|
---|
| 1221 | }
|
---|
| 1222 | } else {
|
---|
| 1223 | if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull()) {
|
---|
| 1224 | // supports scaled clipping but not scaling, most
|
---|
| 1225 | // likely a broken handler.
|
---|
| 1226 | } else {
|
---|
| 1227 | if (d->scaledSize.isValid()) {
|
---|
| 1228 | *image = image->scaled(d->scaledSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
---|
| 1229 | }
|
---|
| 1230 | if (d->scaledClipRect.isValid()) {
|
---|
| 1231 | *image = image->copy(d->scaledClipRect);
|
---|
| 1232 | }
|
---|
| 1233 | }
|
---|
| 1234 | }
|
---|
| 1235 | } else {
|
---|
| 1236 | if (d->handler->supportsOption(QImageIOHandler::ScaledSize) && d->scaledSize.isValid()) {
|
---|
| 1237 | // in this case, there's nothing we can do. if the
|
---|
| 1238 | // plugin supports scaled size but not ClipRect, then
|
---|
| 1239 | // we have to ignore ClipRect."
|
---|
| 1240 |
|
---|
| 1241 | if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull()) {
|
---|
| 1242 | // nothing to do (ClipRect is ignored!)
|
---|
| 1243 | } else {
|
---|
| 1244 | // provide all workarounds.
|
---|
| 1245 | if (d->scaledClipRect.isValid()) {
|
---|
| 1246 | *image = image->copy(d->scaledClipRect);
|
---|
| 1247 | }
|
---|
| 1248 | }
|
---|
| 1249 | } else {
|
---|
| 1250 | if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull()) {
|
---|
| 1251 | // this makes no sense; a handler that supports
|
---|
| 1252 | // ScaledClipRect but not ScaledSize is broken, and we
|
---|
| 1253 | // can't work around it.
|
---|
| 1254 | } else {
|
---|
| 1255 | // provide all workarounds.
|
---|
| 1256 | if (d->clipRect.isValid())
|
---|
| 1257 | *image = image->copy(d->clipRect);
|
---|
| 1258 | if (d->scaledSize.isValid())
|
---|
| 1259 | *image = image->scaled(d->scaledSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
---|
| 1260 | if (d->scaledClipRect.isValid())
|
---|
| 1261 | *image = image->copy(d->scaledClipRect);
|
---|
| 1262 | }
|
---|
| 1263 | }
|
---|
| 1264 | }
|
---|
| 1265 |
|
---|
| 1266 | return true;
|
---|
| 1267 | }
|
---|
| 1268 |
|
---|
| 1269 | /*!
|
---|
| 1270 | For image formats that support animation, this function steps over the
|
---|
| 1271 | current image, returning true if successful or false if there is no
|
---|
| 1272 | following image in the animation.
|
---|
| 1273 |
|
---|
| 1274 | The default implementation calls read(), then discards the resulting
|
---|
| 1275 | image, but the image handler may have a more efficient way of implementing
|
---|
| 1276 | this operation.
|
---|
| 1277 |
|
---|
| 1278 | \sa jumpToImage(), QImageIOHandler::jumpToNextImage()
|
---|
| 1279 | */
|
---|
| 1280 | bool QImageReader::jumpToNextImage()
|
---|
| 1281 | {
|
---|
| 1282 | if (!d->initHandler())
|
---|
| 1283 | return false;
|
---|
| 1284 | return d->handler->jumpToNextImage();
|
---|
| 1285 | }
|
---|
| 1286 |
|
---|
| 1287 | /*!
|
---|
| 1288 | For image formats that support animation, this function skips to the image
|
---|
| 1289 | whose sequence number is \a imageNumber, returning true if successful
|
---|
| 1290 | or false if the corresponding image cannot be found.
|
---|
| 1291 |
|
---|
| 1292 | The next call to read() will attempt to read this image.
|
---|
| 1293 |
|
---|
| 1294 | \sa jumpToNextImage(), QImageIOHandler::jumpToImage()
|
---|
| 1295 | */
|
---|
| 1296 | bool QImageReader::jumpToImage(int imageNumber)
|
---|
| 1297 | {
|
---|
| 1298 | if (!d->initHandler())
|
---|
| 1299 | return false;
|
---|
| 1300 | return d->handler->jumpToImage(imageNumber);
|
---|
| 1301 | }
|
---|
| 1302 |
|
---|
| 1303 | /*!
|
---|
[561] | 1304 | For image formats that support animation, this function returns the number
|
---|
| 1305 | of times the animation should loop. If this function returns -1, it can
|
---|
| 1306 | either mean the animation should loop forever, or that an error occurred.
|
---|
| 1307 | If an error occurred, canRead() will return false.
|
---|
[2] | 1308 |
|
---|
[561] | 1309 | \sa supportsAnimation(), QImageIOHandler::loopCount(), canRead()
|
---|
[2] | 1310 | */
|
---|
| 1311 | int QImageReader::loopCount() const
|
---|
| 1312 | {
|
---|
| 1313 | if (!d->initHandler())
|
---|
| 1314 | return -1;
|
---|
| 1315 | return d->handler->loopCount();
|
---|
| 1316 | }
|
---|
| 1317 |
|
---|
| 1318 | /*!
|
---|
[561] | 1319 | For image formats that support animation, this function returns the total
|
---|
| 1320 | number of images in the animation. If the format does not support
|
---|
| 1321 | animation, 0 is returned.
|
---|
[2] | 1322 |
|
---|
[561] | 1323 | This function returns -1 if an error occurred.
|
---|
[2] | 1324 |
|
---|
[561] | 1325 | \sa supportsAnimation(), QImageIOHandler::imageCount(), canRead()
|
---|
[2] | 1326 | */
|
---|
| 1327 | int QImageReader::imageCount() const
|
---|
| 1328 | {
|
---|
| 1329 | if (!d->initHandler())
|
---|
| 1330 | return -1;
|
---|
| 1331 | return d->handler->imageCount();
|
---|
| 1332 | }
|
---|
| 1333 |
|
---|
| 1334 | /*!
|
---|
[561] | 1335 | For image formats that support animation, this function returns the number
|
---|
| 1336 | of milliseconds to wait until displaying the next frame in the animation.
|
---|
| 1337 | If the image format doesn't support animation, 0 is returned.
|
---|
[2] | 1338 |
|
---|
[561] | 1339 | This function returns -1 if an error occurred.
|
---|
| 1340 |
|
---|
| 1341 | \sa supportsAnimation(), QImageIOHandler::nextImageDelay(), canRead()
|
---|
[2] | 1342 | */
|
---|
| 1343 | int QImageReader::nextImageDelay() const
|
---|
| 1344 | {
|
---|
| 1345 | if (!d->initHandler())
|
---|
| 1346 | return -1;
|
---|
| 1347 | return d->handler->nextImageDelay();
|
---|
| 1348 | }
|
---|
| 1349 |
|
---|
| 1350 | /*!
|
---|
[561] | 1351 | For image formats that support animation, this function returns the
|
---|
| 1352 | sequence number of the current frame. If the image format doesn't support
|
---|
| 1353 | animation, 0 is returned.
|
---|
[2] | 1354 |
|
---|
[561] | 1355 | This function returns -1 if an error occurred.
|
---|
| 1356 |
|
---|
| 1357 | \sa supportsAnimation(), QImageIOHandler::currentImageNumber(), canRead()
|
---|
[2] | 1358 | */
|
---|
| 1359 | int QImageReader::currentImageNumber() const
|
---|
| 1360 | {
|
---|
| 1361 | if (!d->initHandler())
|
---|
| 1362 | return -1;
|
---|
| 1363 | return d->handler->currentImageNumber();
|
---|
| 1364 | }
|
---|
| 1365 |
|
---|
| 1366 | /*!
|
---|
| 1367 | For image formats that support animation, this function returns
|
---|
| 1368 | the rect for the current frame. Otherwise, a null rect is returned.
|
---|
| 1369 |
|
---|
| 1370 | \sa supportsAnimation(), QImageIOHandler::currentImageRect()
|
---|
| 1371 | */
|
---|
| 1372 | QRect QImageReader::currentImageRect() const
|
---|
| 1373 | {
|
---|
| 1374 | if (!d->initHandler())
|
---|
| 1375 | return QRect();
|
---|
| 1376 | return d->handler->currentImageRect();
|
---|
| 1377 | }
|
---|
| 1378 |
|
---|
| 1379 | /*!
|
---|
| 1380 | Returns the type of error that occurred last.
|
---|
| 1381 |
|
---|
| 1382 | \sa ImageReaderError, errorString()
|
---|
| 1383 | */
|
---|
| 1384 | QImageReader::ImageReaderError QImageReader::error() const
|
---|
| 1385 | {
|
---|
| 1386 | return d->imageReaderError;
|
---|
| 1387 | }
|
---|
| 1388 |
|
---|
| 1389 | /*!
|
---|
| 1390 | Returns a human readable description of the last error that
|
---|
| 1391 | occurred.
|
---|
| 1392 |
|
---|
| 1393 | \sa error()
|
---|
| 1394 | */
|
---|
| 1395 | QString QImageReader::errorString() const
|
---|
| 1396 | {
|
---|
[561] | 1397 | if (d->errorString.isEmpty())
|
---|
| 1398 | return QLatin1String(QT_TRANSLATE_NOOP(QImageReader, "Unknown error"));
|
---|
[2] | 1399 | return d->errorString;
|
---|
| 1400 | }
|
---|
| 1401 |
|
---|
| 1402 | /*!
|
---|
| 1403 | \since 4.2
|
---|
| 1404 |
|
---|
| 1405 | Returns true if the reader supports \a option; otherwise returns
|
---|
| 1406 | false.
|
---|
| 1407 |
|
---|
| 1408 | Different image formats support different options. Call this function to
|
---|
| 1409 | determine whether a certain option is supported by the current format. For
|
---|
| 1410 | example, the PNG format allows you to embed text into the image's metadata
|
---|
| 1411 | (see text()), and the BMP format allows you to determine the image's size
|
---|
| 1412 | without loading the whole image into memory (see size()).
|
---|
| 1413 |
|
---|
| 1414 | \snippet doc/src/snippets/code/src_gui_image_qimagereader.cpp 3
|
---|
| 1415 |
|
---|
| 1416 | \sa QImageWriter::supportsOption()
|
---|
| 1417 | */
|
---|
| 1418 | bool QImageReader::supportsOption(QImageIOHandler::ImageOption option) const
|
---|
| 1419 | {
|
---|
| 1420 | if (!d->initHandler())
|
---|
| 1421 | return false;
|
---|
| 1422 | return d->handler->supportsOption(option);
|
---|
| 1423 | }
|
---|
| 1424 |
|
---|
| 1425 | /*!
|
---|
| 1426 | If supported, this function returns the image format of the file
|
---|
| 1427 | \a fileName. Otherwise, an empty string is returned.
|
---|
| 1428 | */
|
---|
| 1429 | QByteArray QImageReader::imageFormat(const QString &fileName)
|
---|
| 1430 | {
|
---|
| 1431 | QFile file(fileName);
|
---|
| 1432 | if (!file.open(QFile::ReadOnly))
|
---|
| 1433 | return QByteArray();
|
---|
| 1434 |
|
---|
| 1435 | return imageFormat(&file);
|
---|
| 1436 | }
|
---|
| 1437 |
|
---|
| 1438 | /*!
|
---|
| 1439 | If supported, this function returns the image format of the device
|
---|
| 1440 | \a device. Otherwise, an empty string is returned.
|
---|
| 1441 |
|
---|
| 1442 | \sa QImageReader::autoDetectImageFormat()
|
---|
| 1443 | */
|
---|
| 1444 | QByteArray QImageReader::imageFormat(QIODevice *device)
|
---|
| 1445 | {
|
---|
| 1446 | QByteArray format;
|
---|
[561] | 1447 | QImageIOHandler *handler = createReadHandlerHelper(device, format, /* autoDetectImageFormat = */ true, false);
|
---|
[2] | 1448 | if (handler) {
|
---|
| 1449 | if (handler->canRead())
|
---|
| 1450 | format = handler->format();
|
---|
| 1451 | delete handler;
|
---|
| 1452 | }
|
---|
| 1453 | return format;
|
---|
| 1454 | }
|
---|
| 1455 |
|
---|
| 1456 | /*!
|
---|
| 1457 | Returns the list of image formats supported by QImageReader.
|
---|
| 1458 |
|
---|
| 1459 | By default, Qt can read the following formats:
|
---|
| 1460 |
|
---|
| 1461 | \table
|
---|
| 1462 | \header \o Format \o Description
|
---|
| 1463 | \row \o BMP \o Windows Bitmap
|
---|
| 1464 | \row \o GIF \o Graphic Interchange Format (optional)
|
---|
| 1465 | \row \o JPG \o Joint Photographic Experts Group
|
---|
| 1466 | \row \o JPEG \o Joint Photographic Experts Group
|
---|
| 1467 | \row \o MNG \o Multiple-image Network Graphics
|
---|
| 1468 | \row \o PNG \o Portable Network Graphics
|
---|
| 1469 | \row \o PBM \o Portable Bitmap
|
---|
| 1470 | \row \o PGM \o Portable Graymap
|
---|
| 1471 | \row \o PPM \o Portable Pixmap
|
---|
| 1472 | \row \o TIFF \o Tagged Image File Format
|
---|
| 1473 | \row \o XBM \o X11 Bitmap
|
---|
| 1474 | \row \o XPM \o X11 Pixmap
|
---|
[561] | 1475 | \row \o SVG \o Scalable Vector Graphics
|
---|
[2] | 1476 | \endtable
|
---|
| 1477 |
|
---|
| 1478 | Reading and writing SVG files is supported through Qt's
|
---|
| 1479 | \l{QtSvg Module}{SVG Module}.
|
---|
| 1480 |
|
---|
| 1481 | To configure Qt with GIF support, pass \c -qt-gif to the \c
|
---|
| 1482 | configure script or check the appropriate option in the graphical
|
---|
| 1483 | installer.
|
---|
| 1484 |
|
---|
| 1485 | \sa setFormat(), QImageWriter::supportedImageFormats(), QImageIOPlugin
|
---|
| 1486 | */
|
---|
| 1487 | QList<QByteArray> QImageReader::supportedImageFormats()
|
---|
| 1488 | {
|
---|
| 1489 | QSet<QByteArray> formats;
|
---|
| 1490 | for (int i = 0; i < _qt_NumFormats; ++i)
|
---|
| 1491 | formats << _qt_BuiltInFormats[i].extension;
|
---|
| 1492 |
|
---|
[846] | 1493 | #ifndef QT_NO_LIBRARY
|
---|
[2] | 1494 | QFactoryLoader *l = loader();
|
---|
| 1495 | QStringList keys = l->keys();
|
---|
| 1496 |
|
---|
| 1497 | for (int i = 0; i < keys.count(); ++i) {
|
---|
| 1498 | QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i)));
|
---|
| 1499 | if (plugin && plugin->capabilities(0, keys.at(i).toLatin1()) & QImageIOPlugin::CanRead)
|
---|
| 1500 | formats << keys.at(i).toLatin1();
|
---|
| 1501 | }
|
---|
| 1502 | #endif // QT_NO_LIBRARY
|
---|
| 1503 |
|
---|
| 1504 | QList<QByteArray> sortedFormats;
|
---|
| 1505 | for (QSet<QByteArray>::ConstIterator it = formats.constBegin(); it != formats.constEnd(); ++it)
|
---|
| 1506 | sortedFormats << *it;
|
---|
| 1507 |
|
---|
| 1508 | qSort(sortedFormats);
|
---|
| 1509 | return sortedFormats;
|
---|
| 1510 | }
|
---|
| 1511 |
|
---|
| 1512 | QT_END_NAMESPACE
|
---|