image/qimagereader.cpp

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

Generated by Squish Coco Non-Commercial