image/qimagewriter.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
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/*! -
43 \class QImageWriter -
44 \brief The QImageWriter class provides a format independent interface -
45 for writing images to files or other devices. -
46 -
47 \inmodule QtGui -
48 \reentrant -
49 \ingroup painting -
50 \ingroup io -
51 -
52 QImageWriter supports setting format specific options, such as the -
53 gamma level, compression level and quality, prior to storing the -
54 image. If you do not need such options, you can use QImage::save() -
55 or QPixmap::save() instead. -
56 -
57 To store an image, you start by constructing a QImageWriter -
58 object. Pass either a file name or a device pointer, and the -
59 image format to QImageWriter's constructor. You can then set -
60 several options, such as the gamma level (by calling setGamma()) -
61 and quality (by calling setQuality()). canWrite() returns true if -
62 QImageWriter can write the image (i.e., the image format is -
63 supported and the device is open for writing). Call write() to -
64 write the image to the device. -
65 -
66 If any error occurs when writing the image, write() will return -
67 false. You can then call error() to find the type of error that -
68 occurred, or errorString() to get a human readable description of -
69 what went wrong. -
70 -
71 Call supportedImageFormats() for a list of formats that -
72 QImageWriter can write. QImageWriter supports all built-in image -
73 formats, in addition to any image format plugins that support -
74 writing. -
75 -
76 \sa QImageReader, QImageIOHandler, QImageIOPlugin -
77*/ -
78 -
79/*! -
80 \enum QImageWriter::ImageWriterError -
81 -
82 This enum describes errors that can occur when writing images with -
83 QImageWriter. -
84 -
85 \value DeviceError QImageWriter encountered a device error when -
86 writing the image data. Consult your device for more details on -
87 what went wrong. -
88 -
89 \value UnsupportedFormatError Qt does not support the requested -
90 image format. -
91 -
92 \value UnknownError An unknown error occurred. If you get this -
93 value after calling write(), it is most likely caused by a bug in -
94 QImageWriter. -
95*/ -
96 -
97#include "qimagewriter.h" -
98 -
99#include <qbytearray.h> -
100#include <qfile.h> -
101#include <qfileinfo.h> -
102#include <qimageiohandler.h> -
103#include <qset.h> -
104#include <qvariant.h> -
105 -
106// factory loader -
107#include <qcoreapplication.h> -
108#include <private/qfactoryloader_p.h> -
109 -
110// image handlers -
111#include <private/qbmphandler_p.h> -
112#include <private/qppmhandler_p.h> -
113#include <private/qxbmhandler_p.h> -
114#include <private/qxpmhandler_p.h> -
115#ifndef QT_NO_IMAGEFORMAT_PNG -
116#include <private/qpnghandler_p.h> -
117#endif -
118#ifndef QT_NO_IMAGEFORMAT_JPEG -
119#include <private/qjpeghandler_p.h> -
120#endif -
121#ifdef QT_BUILTIN_GIF_READER -
122#include <private/qgifhandler_p.h> -
123#endif -
124 -
125QT_BEGIN_NAMESPACE -
126 -
127#ifndef QT_NO_IMAGEFORMATPLUGIN -
128Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
never executed: delete x;
executed: return thisGlobalStatic.pointer.load();
Execution Count:528
partially evaluated: !thisGlobalStatic.pointer.testAndSetOrdered(0, x)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:12
evaluated: !thisGlobalStatic.pointer.load()
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:516
partially evaluated: !thisGlobalStatic.destroyed
TRUEFALSE
yes
Evaluation Count:12
no
Evaluation Count:0
0-528
129 (QImageIOHandlerFactoryInterface_iid, QLatin1String("/imageformats"))) -
130#endif -
131 -
132static QImageIOHandler *createWriteHandlerHelper(QIODevice *device, -
133 const QByteArray &format) -
134{ -
135 QByteArray form = format.toLower();
executed (the execution status of this line is deduced): QByteArray form = format.toLower();
-
136 QByteArray suffix;
executed (the execution status of this line is deduced): QByteArray suffix;
-
137 QImageIOHandler *handler = 0;
executed (the execution status of this line is deduced): QImageIOHandler *handler = 0;
-
138 -
139#ifndef QT_NO_IMAGEFORMATPLUGIN -
140 typedef QMultiMap<int, QString> PluginKeyMap;
executed (the execution status of this line is deduced): typedef QMultiMap<int, QString> PluginKeyMap;
-
141 -
142 // check if any plugins can write the image -
143 QFactoryLoader *l = loader();
executed (the execution status of this line is deduced): QFactoryLoader *l = loader();
-
144 const PluginKeyMap keyMap = l->keyMap();
executed (the execution status of this line is deduced): const PluginKeyMap keyMap = l->keyMap();
-
145 int suffixPluginIndex = -1;
executed (the execution status of this line is deduced): int suffixPluginIndex = -1;
-
146#endif -
147 -
148 if (device && format.isEmpty()) {
partially evaluated: device
TRUEFALSE
yes
Evaluation Count:511
no
Evaluation Count:0
evaluated: format.isEmpty()
TRUEFALSE
yes
Evaluation Count:18
yes
Evaluation Count:493
0-511
149 // if there's no format, see if \a device is a file, and if so, find -
150 // the file suffix and find support for that format among our plugins. -
151 // this allows plugins to override our built-in handlers. -
152 if (QFile *file = qobject_cast<QFile *>(device)) {
partially evaluated: QFile *file = qobject_cast<QFile *>(device)
TRUEFALSE
yes
Evaluation Count:18
no
Evaluation Count:0
0-18
153 if (!(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()) {
partially evaluated: !(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()
TRUEFALSE
yes
Evaluation Count:18
no
Evaluation Count:0
0-18
154#ifndef QT_NO_IMAGEFORMATPLUGIN -
155 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);
-
156 if (index != -1)
partially evaluated: index != -1
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:18
0-18
157 suffixPluginIndex = index;
never executed: suffixPluginIndex = index;
0
158#endif -
159 }
executed: }
Execution Count:18
18
160 }
executed: }
Execution Count:18
18
161 }
executed: }
Execution Count:18
18
162 -
163 QByteArray testFormat = !form.isEmpty() ? form : suffix;
evaluated: !form.isEmpty()
TRUEFALSE
yes
Evaluation Count:493
yes
Evaluation Count:18
18-493
164 -
165#ifndef QT_NO_IMAGEFORMATPLUGIN -
166 if (suffixPluginIndex != -1) {
partially evaluated: suffixPluginIndex != -1
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:511
0-511
167 // when format is missing, check if we can find a plugin for the -
168 // suffix. -
169 const int index = keyMap.key(QString::fromLatin1(suffix), -1);
never executed (the execution status of this line is deduced): const int index = keyMap.key(QString::fromLatin1(suffix), -1);
-
170 if (index != -1) {
never evaluated: index != -1
0
171 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(index));
never executed (the execution status of this line is deduced): QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(index));
-
172 if (plugin && (plugin->capabilities(device, suffix) & QImageIOPlugin::CanWrite))
never evaluated: plugin
never evaluated: (plugin->capabilities(device, suffix) & QImageIOPlugin::CanWrite)
0
173 handler = plugin->create(device, suffix);
never executed: handler = plugin->create(device, suffix);
0
174 }
never executed: }
0
175 }
never executed: }
0
176#endif // QT_NO_IMAGEFORMATPLUGIN -
177 -
178 // check if any built-in handlers can write the image -
179 if (!handler && !testFormat.isEmpty()) {
partially evaluated: !handler
TRUEFALSE
yes
Evaluation Count:511
no
Evaluation Count:0
partially evaluated: !testFormat.isEmpty()
TRUEFALSE
yes
Evaluation Count:511
no
Evaluation Count:0
0-511
180 if (false) {
partially evaluated: false
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:511
0-511
181#ifndef QT_NO_IMAGEFORMAT_PNG -
182 } else if (testFormat == "png") {
never executed: }
evaluated: testFormat == "png"
TRUEFALSE
yes
Evaluation Count:129
yes
Evaluation Count:382
0-382
183 handler = new QPngHandler;
executed (the execution status of this line is deduced): handler = new QPngHandler;
-
184#endif -
185#ifndef QT_NO_IMAGEFORMAT_JPEG -
186 } else if (testFormat == "jpg" || testFormat == "jpeg") { -
187 handler = new QJpegHandler; -
188#endif -
189#ifdef QT_BUILTIN_GIF_READER -
190 } else if (testFormat == "gif") { -
191 handler = new QGifHandler; -
192#endif -
193#ifndef QT_NO_IMAGEFORMAT_BMP -
194 } else if (testFormat == "bmp") {
executed: }
Execution Count:129
evaluated: testFormat == "bmp"
TRUEFALSE
yes
Evaluation Count:34
yes
Evaluation Count:348
34-348
195 handler = new QBmpHandler;
executed (the execution status of this line is deduced): handler = new QBmpHandler;
-
196 } else if (testFormat == "dib") {
executed: }
Execution Count:34
partially evaluated: testFormat == "dib"
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:348
0-348
197 handler = new QBmpHandler(QBmpHandler::DibFormat);
never executed (the execution status of this line is deduced): handler = new QBmpHandler(QBmpHandler::DibFormat);
-
198#endif -
199#ifndef QT_NO_IMAGEFORMAT_XPM -
200 } else if (testFormat == "xpm") {
never executed: }
evaluated: testFormat == "xpm"
TRUEFALSE
yes
Evaluation Count:291
yes
Evaluation Count:57
0-291
201 handler = new QXpmHandler;
executed (the execution status of this line is deduced): handler = new QXpmHandler;
-
202#endif -
203#ifndef QT_NO_IMAGEFORMAT_XBM -
204 } else if (testFormat == "xbm") {
executed: }
Execution Count:291
evaluated: testFormat == "xbm"
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:51
6-291
205 handler = new QXbmHandler;
executed (the execution status of this line is deduced): handler = new QXbmHandler;
-
206 handler->setOption(QImageIOHandler::SubType, testFormat);
executed (the execution status of this line is deduced): handler->setOption(QImageIOHandler::SubType, testFormat);
-
207#endif -
208#ifndef QT_NO_IMAGEFORMAT_PPM -
209 } else if (testFormat == "pbm" || testFormat == "pbmraw" || testFormat == "pgm"
executed: }
Execution Count:6
evaluated: testFormat == "pbm"
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:47
partially evaluated: testFormat == "pbmraw"
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:47
evaluated: testFormat == "pgm"
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:46
0-47
210 || testFormat == "pgmraw" || testFormat == "ppm" || testFormat == "ppmraw") {
partially evaluated: testFormat == "pgmraw"
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:46
evaluated: testFormat == "ppm"
TRUEFALSE
yes
Evaluation Count:22
yes
Evaluation Count:24
partially evaluated: testFormat == "ppmraw"
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:24
0-46
211 handler = new QPpmHandler;
executed (the execution status of this line is deduced): handler = new QPpmHandler;
-
212 handler->setOption(QImageIOHandler::SubType, testFormat);
executed (the execution status of this line is deduced): handler->setOption(QImageIOHandler::SubType, testFormat);
-
213#endif -
214 }
executed: }
Execution Count:27
27
215 } -
216 -
217#ifndef QT_NO_IMAGEFORMATPLUGIN -
218 if (!testFormat.isEmpty()) {
partially evaluated: !testFormat.isEmpty()
TRUEFALSE
yes
Evaluation Count:511
no
Evaluation Count:0
0-511
219 const int keyCount = keyMap.keys().size();
executed (the execution status of this line is deduced): const int keyCount = keyMap.keys().size();
-
220 for (int i = 0; i < keyCount; ++i) {
evaluated: i < keyCount
TRUEFALSE
yes
Evaluation Count:2021
yes
Evaluation Count:488
488-2021
221 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));
-
222 if (plugin && (plugin->capabilities(device, testFormat) & QImageIOPlugin::CanWrite)) {
evaluated: plugin
TRUEFALSE
yes
Evaluation Count:1533
yes
Evaluation Count:488
evaluated: (plugin->capabilities(device, testFormat) & QImageIOPlugin::CanWrite)
TRUEFALSE
yes
Evaluation Count:23
yes
Evaluation Count:1510
23-1533
223 delete handler;
executed (the execution status of this line is deduced): delete handler;
-
224 handler = plugin->create(device, testFormat);
executed (the execution status of this line is deduced): handler = plugin->create(device, testFormat);
-
225 break;
executed: break;
Execution Count:23
23
226 } -
227 }
executed: }
Execution Count:1998
1998
228 }
executed: }
Execution Count:511
511
229#endif // QT_NO_IMAGEFORMATPLUGIN -
230 -
231 if (!handler)
evaluated: !handler
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:510
1-510
232 return 0;
executed: return 0;
Execution Count:1
1
233 -
234 handler->setDevice(device);
executed (the execution status of this line is deduced): handler->setDevice(device);
-
235 if (!testFormat.isEmpty())
partially evaluated: !testFormat.isEmpty()
TRUEFALSE
yes
Evaluation Count:510
no
Evaluation Count:0
0-510
236 handler->setFormat(testFormat);
executed: handler->setFormat(testFormat);
Execution Count:510
510
237 return handler;
executed: return handler;
Execution Count:510
510
238} -
239 -
240class QImageWriterPrivate -
241{ -
242public: -
243 QImageWriterPrivate(QImageWriter *qq); -
244 -
245 // device -
246 QByteArray format; -
247 QIODevice *device; -
248 bool deleteDevice; -
249 QImageIOHandler *handler; -
250 -
251 // image options -
252 int quality; -
253 int compression; -
254 float gamma; -
255 QString description; -
256 QString text; -
257 -
258 // error -
259 QImageWriter::ImageWriterError imageWriterError; -
260 QString errorString; -
261 -
262 QImageWriter *q; -
263}; -
264 -
265/*! -
266 \internal -
267*/ -
268QImageWriterPrivate::QImageWriterPrivate(QImageWriter *qq) -
269{ -
270 device = 0;
executed (the execution status of this line is deduced): device = 0;
-
271 deleteDevice = false;
executed (the execution status of this line is deduced): deleteDevice = false;
-
272 handler = 0;
executed (the execution status of this line is deduced): handler = 0;
-
273 quality = -1;
executed (the execution status of this line is deduced): quality = -1;
-
274 compression = 0;
executed (the execution status of this line is deduced): compression = 0;
-
275 gamma = 0.0;
executed (the execution status of this line is deduced): gamma = 0.0;
-
276 imageWriterError = QImageWriter::UnknownError;
executed (the execution status of this line is deduced): imageWriterError = QImageWriter::UnknownError;
-
277 errorString = QT_TRANSLATE_NOOP(QImageWriter, QLatin1String("Unknown error"));
executed (the execution status of this line is deduced): errorString = QLatin1String("Unknown error");
-
278 -
279 q = qq;
executed (the execution status of this line is deduced): q = qq;
-
280}
executed: }
Execution Count:512
512
281 -
282/*! -
283 Constructs an empty QImageWriter object. Before writing, you must -
284 call setFormat() to set an image format, then setDevice() or -
285 setFileName(). -
286*/ -
287QImageWriter::QImageWriter() -
288 : d(new QImageWriterPrivate(this)) -
289{ -
290}
executed: }
Execution Count:1
1
291 -
292/*! -
293 Constructs a QImageWriter object using the device \a device and -
294 image format \a format. -
295*/ -
296QImageWriter::QImageWriter(QIODevice *device, const QByteArray &format) -
297 : d(new QImageWriterPrivate(this)) -
298{ -
299 d->device = device;
executed (the execution status of this line is deduced): d->device = device;
-
300 d->format = format;
executed (the execution status of this line is deduced): d->format = format;
-
301}
executed: }
Execution Count:145
145
302 -
303/*! -
304 Constructs a QImageWriter objects that will write to a file with -
305 the name \a fileName, using the image format \a format. If \a -
306 format is not provided, QImageWriter will detect the image format -
307 by inspecting the extension of \a fileName. -
308*/ -
309QImageWriter::QImageWriter(const QString &fileName, const QByteArray &format) -
310 : d(new QImageWriterPrivate(this)) -
311{ -
312 QFile *file = new QFile(fileName);
executed (the execution status of this line is deduced): QFile *file = new QFile(fileName);
-
313 d->device = file;
executed (the execution status of this line is deduced): d->device = file;
-
314 d->deleteDevice = true;
executed (the execution status of this line is deduced): d->deleteDevice = true;
-
315 d->format = format;
executed (the execution status of this line is deduced): d->format = format;
-
316}
executed: }
Execution Count:366
366
317 -
318/*! -
319 Destructs the QImageWriter object. -
320*/ -
321QImageWriter::~QImageWriter() -
322{ -
323 if (d->deleteDevice)
evaluated: d->deleteDevice
TRUEFALSE
yes
Evaluation Count:366
yes
Evaluation Count:146
146-366
324 delete d->device;
executed: delete d->device;
Execution Count:366
366
325 delete d->handler;
executed (the execution status of this line is deduced): delete d->handler;
-
326 delete d;
executed (the execution status of this line is deduced): delete d;
-
327}
executed: }
Execution Count:512
512
328 -
329/*! -
330 Sets the format QImageWriter will use when writing images, to \a -
331 format. \a format is a case insensitive text string. Example: -
332 -
333 \snippet code/src_gui_image_qimagewriter.cpp 0 -
334 -
335 You can call supportedImageFormats() for the full list of formats -
336 QImageWriter supports. -
337 -
338 \sa format() -
339*/ -
340void QImageWriter::setFormat(const QByteArray &format) -
341{ -
342 d->format = format;
executed (the execution status of this line is deduced): d->format = format;
-
343}
executed: }
Execution Count:2
2
344 -
345/*! -
346 Returns the format QImageWriter uses for writing images. -
347 -
348 \sa setFormat() -
349*/ -
350QByteArray QImageWriter::format() const -
351{ -
352 return d->format;
never executed: return d->format;
0
353} -
354 -
355/*! -
356 Sets QImageWriter's device to \a device. If a device has already -
357 been set, the old device is removed from QImageWriter and is -
358 otherwise left unchanged. -
359 -
360 If the device is not already open, QImageWriter will attempt to -
361 open the device in \l QIODevice::WriteOnly mode by calling -
362 open(). Note that this does not work for certain devices, such as -
363 QProcess, QTcpSocket and QUdpSocket, where more logic is required -
364 to open the device. -
365 -
366 \sa device(), setFileName() -
367*/ -
368void QImageWriter::setDevice(QIODevice *device) -
369{ -
370 if (d->device && d->deleteDevice)
evaluated: d->device
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:1
partially evaluated: d->deleteDevice
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-1
371 delete d->device;
never executed: delete d->device;
0
372 -
373 d->device = device;
executed (the execution status of this line is deduced): d->device = device;
-
374 d->deleteDevice = false;
executed (the execution status of this line is deduced): d->deleteDevice = false;
-
375 delete d->handler;
executed (the execution status of this line is deduced): delete d->handler;
-
376 d->handler = 0;
executed (the execution status of this line is deduced): d->handler = 0;
-
377}
executed: }
Execution Count:2
2
378 -
379/*! -
380 Returns the device currently assigned to QImageWriter, or 0 if no -
381 device has been assigned. -
382*/ -
383QIODevice *QImageWriter::device() const -
384{ -
385 return d->device;
executed: return d->device;
Execution Count:2
2
386} -
387 -
388/*! -
389 Sets the file name of QImageWriter to \a fileName. Internally, -
390 QImageWriter will create a QFile and open it in \l -
391 QIODevice::WriteOnly mode, and use this file when writing images. -
392 -
393 \sa fileName(), setDevice() -
394*/ -
395void QImageWriter::setFileName(const QString &fileName) -
396{ -
397 setDevice(new QFile(fileName));
never executed (the execution status of this line is deduced): setDevice(new QFile(fileName));
-
398 d->deleteDevice = true;
never executed (the execution status of this line is deduced): d->deleteDevice = true;
-
399}
never executed: }
0
400 -
401/*! -
402 If the currently assigned device is a QFile, or if setFileName() -
403 has been called, this function returns the name of the file -
404 QImageWriter writes to. Otherwise (i.e., if no device has been -
405 assigned or the device is not a QFile), an empty QString is -
406 returned. -
407 -
408 \sa setFileName(), setDevice() -
409*/ -
410QString QImageWriter::fileName() const -
411{ -
412 QFile *file = qobject_cast<QFile *>(d->device);
executed (the execution status of this line is deduced): QFile *file = qobject_cast<QFile *>(d->device);
-
413 return file ? file->fileName() : QString();
executed: return file ? file->fileName() : QString();
Execution Count:2
2
414} -
415 -
416/*! -
417 This is an image format specific function that sets the quality -
418 level of the image to \a quality. For image formats that do not -
419 support setting the quality, this value is ignored. -
420 -
421 The value range of \a quality depends on the image format. For -
422 example, the "jpeg" format supports a quality range from 0 (low -
423 quality, high compression) to 100 (high quality, low compression). -
424 -
425 \sa quality() -
426*/ -
427void QImageWriter::setQuality(int quality) -
428{ -
429 d->quality = quality;
executed (the execution status of this line is deduced): d->quality = quality;
-
430}
executed: }
Execution Count:3
3
431 -
432/*! -
433 Returns the quality level of the image. -
434 -
435 \sa setQuality() -
436*/ -
437int QImageWriter::quality() const -
438{ -
439 return d->quality;
executed: return d->quality;
Execution Count:3
3
440} -
441 -
442/*! -
443 This is an image format specific function that set the compression -
444 of an image. For image formats that do not support setting the -
445 compression, this value is ignored. -
446 -
447 The value range of \a compression depends on the image format. For -
448 example, the "tiff" format supports two values, 0(no compression) and -
449 1(LZW-compression). -
450 -
451 \sa compression() -
452*/ -
453void QImageWriter::setCompression(int compression) -
454{ -
455 d->compression = compression;
executed (the execution status of this line is deduced): d->compression = compression;
-
456}
executed: }
Execution Count:3
3
457 -
458/*! -
459 Returns the compression of the image. -
460 -
461 \sa setCompression() -
462*/ -
463int QImageWriter::compression() const -
464{ -
465 return d->compression;
executed: return d->compression;
Execution Count:3
3
466} -
467 -
468/*! -
469 This is an image format specific function that sets the gamma -
470 level of the image to \a gamma. For image formats that do not -
471 support setting the gamma level, this value is ignored. -
472 -
473 The value range of \a gamma depends on the image format. For -
474 example, the "png" format supports a gamma range from 0.0 to 1.0. -
475 -
476 \sa quality() -
477*/ -
478void QImageWriter::setGamma(float gamma) -
479{ -
480 d->gamma = gamma;
executed (the execution status of this line is deduced): d->gamma = gamma;
-
481}
executed: }
Execution Count:2
2
482 -
483/*! -
484 Returns the gamma level of the image. -
485 -
486 \sa setGamma() -
487*/ -
488float QImageWriter::gamma() const -
489{ -
490 return d->gamma;
executed: return d->gamma;
Execution Count:2
2
491} -
492 -
493/*! -
494 \obsolete -
495 -
496 Use setText() instead. -
497 -
498 This is an image format specific function that sets the -
499 description of the image to \a description. For image formats that -
500 do not support setting the description, this value is ignored. -
501 -
502 The contents of \a description depends on the image format. -
503 -
504 \sa description() -
505*/ -
506void QImageWriter::setDescription(const QString &description) -
507{ -
508 d->description = description;
never executed (the execution status of this line is deduced): d->description = description;
-
509}
never executed: }
0
510 -
511/*! -
512 \obsolete -
513 -
514 Use QImageReader::text() instead. -
515 -
516 Returns the description of the image. -
517 -
518 \sa setDescription() -
519*/ -
520QString QImageWriter::description() const -
521{ -
522 return d->description;
never executed: return d->description;
0
523} -
524 -
525/*! -
526 \since 4.1 -
527 -
528 Sets the image text associated with the key \a key to -
529 \a text. This is useful for storing copyright information -
530 or other information about the image. Example: -
531 -
532 \snippet code/src_gui_image_qimagewriter.cpp 1 -
533 -
534 If you want to store a single block of data -
535 (e.g., a comment), you can pass an empty key, or use -
536 a generic key like "Description". -
537 -
538 The key and text will be embedded into the -
539 image data after calling write(). -
540 -
541 Support for this option is implemented through -
542 QImageIOHandler::Description. -
543 -
544 \sa QImage::setText(), QImageReader::text() -
545*/ -
546void QImageWriter::setText(const QString &key, const QString &text) -
547{ -
548 if (!d->description.isEmpty())
evaluated: !d->description.isEmpty()
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:12
12
549 d->description += QLatin1String("\n\n");
executed: d->description += QLatin1String("\n\n");
Execution Count:12
12
550 d->description += key.simplified() + QLatin1String(": ") + text.simplified();
executed (the execution status of this line is deduced): d->description += key.simplified() + QLatin1String(": ") + text.simplified();
-
551}
executed: }
Execution Count:24
24
552 -
553/*! -
554 Returns true if QImageWriter can write the image; i.e., the image -
555 format is supported and the assigned device is open for reading. -
556 -
557 \sa write(), setDevice(), setFormat() -
558*/ -
559bool QImageWriter::canWrite() const -
560{ -
561 if (d->device && !d->handler && (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0) {
partially evaluated: d->device
TRUEFALSE
yes
Evaluation Count:511
no
Evaluation Count:0
evaluated: !d->handler
TRUEFALSE
yes
Evaluation Count:510
yes
Evaluation Count:1
evaluated: (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:509
0-511
562 d->imageWriterError = QImageWriter::UnsupportedFormatError;
executed (the execution status of this line is deduced): d->imageWriterError = QImageWriter::UnsupportedFormatError;
-
563 d->errorString = QT_TRANSLATE_NOOP(QImageWriter,
executed (the execution status of this line is deduced): d->errorString = QLatin1String("Unsupported image format");
-
564 QLatin1String("Unsupported image format")); -
565 return false;
executed: return false;
Execution Count:1
1
566 } -
567 if (d->device && !d->device->isOpen())
partially evaluated: d->device
TRUEFALSE
yes
Evaluation Count:510
no
Evaluation Count:0
evaluated: !d->device->isOpen()
TRUEFALSE
yes
Evaluation Count:376
yes
Evaluation Count:134
0-510
568 d->device->open(QIODevice::WriteOnly);
executed: d->device->open(QIODevice::WriteOnly);
Execution Count:376
376
569 if (!d->device || !d->device->isWritable()) {
partially evaluated: !d->device
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:510
evaluated: !d->device->isWritable()
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:498
0-510
570 d->imageWriterError = QImageWriter::DeviceError;
executed (the execution status of this line is deduced): d->imageWriterError = QImageWriter::DeviceError;
-
571 d->errorString = QT_TRANSLATE_NOOP(QImageWriter,
executed (the execution status of this line is deduced): d->errorString = QLatin1String("Device not writable");
-
572 QLatin1String("Device not writable")); -
573 return false;
executed: return false;
Execution Count:12
12
574 } -
575 return true;
executed: return true;
Execution Count:498
498
576} -
577 -
578/*! -
579 Writes the image \a image to the assigned device or file -
580 name. Returns true on success; otherwise returns false. If the -
581 operation fails, you can call error() to find the type of error -
582 that occurred, or errorString() to get a human readable -
583 description of the error. -
584 -
585 \sa canWrite(), error(), errorString() -
586*/ -
587bool QImageWriter::write(const QImage &image) -
588{ -
589 if (!canWrite())
evaluated: !canWrite()
TRUEFALSE
yes
Evaluation Count:11
yes
Evaluation Count:497
11-497
590 return false;
executed: return false;
Execution Count:11
11
591 -
592 if (d->handler->supportsOption(QImageIOHandler::Quality))
evaluated: d->handler->supportsOption(QImageIOHandler::Quality)
TRUEFALSE
yes
Evaluation Count:145
yes
Evaluation Count:352
145-352
593 d->handler->setOption(QImageIOHandler::Quality, d->quality);
executed: d->handler->setOption(QImageIOHandler::Quality, d->quality);
Execution Count:145
145
594 if (d->handler->supportsOption(QImageIOHandler::CompressionRatio))
partially evaluated: d->handler->supportsOption(QImageIOHandler::CompressionRatio)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:497
0-497
595 d->handler->setOption(QImageIOHandler::CompressionRatio, d->compression);
never executed: d->handler->setOption(QImageIOHandler::CompressionRatio, d->compression);
0
596 if (d->handler->supportsOption(QImageIOHandler::Gamma))
evaluated: d->handler->supportsOption(QImageIOHandler::Gamma)
TRUEFALSE
yes
Evaluation Count:123
yes
Evaluation Count:374
123-374
597 d->handler->setOption(QImageIOHandler::Gamma, d->gamma);
executed: d->handler->setOption(QImageIOHandler::Gamma, d->gamma);
Execution Count:123
123
598 if (!d->description.isEmpty() && d->handler->supportsOption(QImageIOHandler::Description))
evaluated: !d->description.isEmpty()
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:485
partially evaluated: d->handler->supportsOption(QImageIOHandler::Description)
TRUEFALSE
yes
Evaluation Count:12
no
Evaluation Count:0
0-485
599 d->handler->setOption(QImageIOHandler::Description, d->description);
executed: d->handler->setOption(QImageIOHandler::Description, d->description);
Execution Count:12
12
600 -
601 if (!d->handler->write(image))
partially evaluated: !d->handler->write(image)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:497
0-497
602 return false;
never executed: return false;
0
603 if (QFile *file = qobject_cast<QFile *>(d->device))
evaluated: QFile *file = qobject_cast<QFile *>(d->device)
TRUEFALSE
yes
Evaluation Count:408
yes
Evaluation Count:89
89-408
604 file->flush();
executed: file->flush();
Execution Count:408
408
605 return true;
executed: return true;
Execution Count:497
497
606} -
607 -
608/*! -
609 Returns the type of error that last occurred. -
610 -
611 \sa ImageWriterError, errorString() -
612*/ -
613QImageWriter::ImageWriterError QImageWriter::error() const -
614{ -
615 return d->imageWriterError;
executed: return d->imageWriterError;
Execution Count:5
5
616} -
617 -
618/*! -
619 Returns a human readable description of the last error that occurred. -
620 -
621 \sa error() -
622*/ -
623QString QImageWriter::errorString() const -
624{ -
625 return d->errorString;
executed: return d->errorString;
Execution Count:6
6
626} -
627 -
628/*! -
629 \since 4.2 -
630 -
631 Returns true if the writer supports \a option; otherwise returns -
632 false. -
633 -
634 Different image formats support different options. Call this function to -
635 determine whether a certain option is supported by the current format. For -
636 example, the PNG format allows you to embed text into the image's metadata -
637 (see text()). -
638 -
639 \snippet code/src_gui_image_qimagewriter.cpp 2 -
640 -
641 Options can be tested after the writer has been associated with a format. -
642 -
643 \sa QImageReader::supportsOption(), setFormat() -
644*/ -
645bool QImageWriter::supportsOption(QImageIOHandler::ImageOption option) const -
646{ -
647 if (!d->handler && (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0) {
evaluated: !d->handler
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:13
partially evaluated: (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-13
648 d->imageWriterError = QImageWriter::UnsupportedFormatError;
never executed (the execution status of this line is deduced): d->imageWriterError = QImageWriter::UnsupportedFormatError;
-
649 d->errorString = QT_TRANSLATE_NOOP(QImageWriter,
never executed (the execution status of this line is deduced): d->errorString = QLatin1String("Unsupported image format");
-
650 QLatin1String("Unsupported image format")); -
651 return false;
never executed: return false;
0
652 } -
653 -
654 return d->handler->supportsOption(option);
executed: return d->handler->supportsOption(option);
Execution Count:14
14
655} -
656 -
657 -
658#ifndef QT_NO_IMAGEFORMATPLUGIN -
659void supportedImageHandlerFormats(QFactoryLoader *loader, -
660 QImageIOPlugin::Capability cap, -
661 QSet<QByteArray> *result) -
662{ -
663 typedef QMultiMap<int, QString> PluginKeyMap;
executed (the execution status of this line is deduced): typedef QMultiMap<int, QString> PluginKeyMap;
-
664 typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator;
executed (the execution status of this line is deduced): typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator;
-
665 -
666 const PluginKeyMap keyMap = loader->keyMap();
executed (the execution status of this line is deduced): const PluginKeyMap keyMap = loader->keyMap();
-
667 const PluginKeyMapConstIterator cend = keyMap.constEnd();
executed (the execution status of this line is deduced): const PluginKeyMapConstIterator cend = keyMap.constEnd();
-
668 int i = -1;
executed (the execution status of this line is deduced): int i = -1;
-
669 QImageIOPlugin *plugin = 0;
executed (the execution status of this line is deduced): QImageIOPlugin *plugin = 0;
-
670 for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it) {
evaluated: it != cend
TRUEFALSE
yes
Evaluation Count:2740
yes
Evaluation Count:685
685-2740
671 if (it.key() != i) {
evaluated: it.key() != i
TRUEFALSE
yes
Evaluation Count:2055
yes
Evaluation Count:685
685-2055
672 i = it.key();
executed (the execution status of this line is deduced): i = it.key();
-
673 plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i));
executed (the execution status of this line is deduced): plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i));
-
674 }
executed: }
Execution Count:2055
2055
675 const QByteArray key = it.value().toLatin1();
executed (the execution status of this line is deduced): const QByteArray key = it.value().toLatin1();
-
676 if (plugin && (plugin->capabilities(0, key) & cap) != 0)
partially evaluated: plugin
TRUEFALSE
yes
Evaluation Count:2740
no
Evaluation Count:0
evaluated: (plugin->capabilities(0, key) & cap) != 0
TRUEFALSE
yes
Evaluation Count:2723
yes
Evaluation Count:17
0-2740
677 result->insert(key);
executed: result->insert(key);
Execution Count:2723
2723
678 }
executed: }
Execution Count:2740
2740
679}
executed: }
Execution Count:685
685
680#endif // QT_NO_IMAGEFORMATPLUGIN -
681 -
682/*! -
683 Returns the list of image formats supported by QImageWriter. -
684 -
685 By default, Qt can write the following formats: -
686 -
687 \table -
688 \header \li Format \li Description -
689 \row \li BMP \li Windows Bitmap -
690 \row \li JPG \li Joint Photographic Experts Group -
691 \row \li JPEG \li Joint Photographic Experts Group -
692 \row \li PNG \li Portable Network Graphics -
693 \row \li PBM \li Portable Bitmap -
694 \row \li PGM \li Portable Graymap -
695 \row \li PPM \li Portable Pixmap -
696 \row \li XBM \li X11 Bitmap -
697 \row \li XPM \li X11 Pixmap -
698 \endtable -
699 -
700 Reading and writing SVG files is supported through the \l{Qt SVG} module. -
701 The \l{Qt Image Formats} module provides support for additional image formats. -
702 -
703 Note that the QApplication instance must be created before this function is -
704 called. -
705 -
706 \sa setFormat(), QImageReader::supportedImageFormats(), QImageIOPlugin -
707*/ -
708QList<QByteArray> QImageWriter::supportedImageFormats() -
709{ -
710 QSet<QByteArray> formats;
executed (the execution status of this line is deduced): QSet<QByteArray> formats;
-
711#ifndef QT_NO_IMAGEFORMAT_BMP -
712 formats << "bmp";
executed (the execution status of this line is deduced): formats << "bmp";
-
713#endif -
714#ifndef QT_NO_IMAGEFORMAT_PPM -
715 formats << "pbm" << "pgm" << "ppm";
executed (the execution status of this line is deduced): formats << "pbm" << "pgm" << "ppm";
-
716#endif -
717#ifndef QT_NO_IMAGEFORMAT_XBM -
718 formats << "xbm";
executed (the execution status of this line is deduced): formats << "xbm";
-
719#endif -
720#ifndef QT_NO_IMAGEFORMAT_XPM -
721 formats << "xpm";
executed (the execution status of this line is deduced): formats << "xpm";
-
722#endif -
723#ifndef QT_NO_IMAGEFORMAT_PNG -
724 formats << "png";
executed (the execution status of this line is deduced): formats << "png";
-
725#endif -
726#ifndef QT_NO_IMAGEFORMAT_JPEG -
727 formats << "jpg" << "jpeg"; -
728#endif -
729#ifdef QT_BUILTIN_GIF_READER -
730 formats << "gif"; -
731#endif -
732 -
733#ifndef QT_NO_IMAGEFORMATPLUGIN -
734 supportedImageHandlerFormats(loader(), QImageIOPlugin::CanWrite, &formats);
executed (the execution status of this line is deduced): supportedImageHandlerFormats(loader(), QImageIOPlugin::CanWrite, &formats);
-
735#endif // QT_NO_IMAGEFORMATPLUGIN -
736 -
737 QList<QByteArray> sortedFormats;
executed (the execution status of this line is deduced): QList<QByteArray> sortedFormats;
-
738 for (QSet<QByteArray>::ConstIterator it = formats.constBegin(); it != formats.constEnd(); ++it)
evaluated: it != formats.constEnd()
TRUEFALSE
yes
Evaluation Count:170
yes
Evaluation Count:17
17-170
739 sortedFormats << *it;
executed: sortedFormats << *it;
Execution Count:170
170
740 -
741 qSort(sortedFormats);
executed (the execution status of this line is deduced): qSort(sortedFormats);
-
742 return sortedFormats;
executed: return sortedFormats;
Execution Count:17
17
743} -
744 -
745QT_END_NAMESPACE -
746 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial