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