qimagewriter.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/image/qimagewriter.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Contact: https://www.qt.io/licensing/-
5**-
6** This file is part of the QtGui module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see https://www.qt.io/terms-conditions. For further-
15** information use the contact form at https://www.qt.io/contact-us.-
16**-
17** GNU Lesser General Public License Usage-
18** Alternatively, this file may be used under the terms of the GNU Lesser-
19** General Public License version 3 as published by the Free Software-
20** Foundation and appearing in the file LICENSE.LGPL3 included in the-
21** packaging of this file. Please review the following information to-
22** ensure the GNU Lesser General Public License version 3 requirements-
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
24**-
25** GNU General Public License Usage-
26** Alternatively, this file may be used under the terms of the GNU-
27** General Public License version 2.0 or (at your option) the GNU General-
28** Public license version 3 or any later version approved by the KDE Free-
29** Qt Foundation. The licenses are as published by the Free Software-
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
31** included in the packaging of this file. Please review the following-
32** information to ensure the GNU General Public License requirements will-
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
34** https://www.gnu.org/licenses/gpl-3.0.html.-
35**-
36** $QT_END_LICENSE$-
37**-
38****************************************************************************/-
39-
40/*!-
41 \class QImageWriter-
42 \brief The QImageWriter class provides a format independent interface-
43 for writing images to files or other devices.-
44-
45 \inmodule QtGui-
46 \reentrant-
47 \ingroup painting-
48 \ingroup io-
49-
50 QImageWriter supports setting format specific options, such as the-
51 gamma level, compression level and quality, prior to storing the-
52 image. If you do not need such options, you can use QImage::save()-
53 or QPixmap::save() instead.-
54-
55 To store an image, you start by constructing a QImageWriter-
56 object. Pass either a file name or a device pointer, and the-
57 image format to QImageWriter's constructor. You can then set-
58 several options, such as the gamma level (by calling setGamma())-
59 and quality (by calling setQuality()). canWrite() returns \c true if-
60 QImageWriter can write the image (i.e., the image format is-
61 supported and the device is open for writing). Call write() to-
62 write the image to the device.-
63-
64 If any error occurs when writing the image, write() will return-
65 false. You can then call error() to find the type of error that-
66 occurred, or errorString() to get a human readable description of-
67 what went wrong.-
68-
69 Call supportedImageFormats() for a list of formats that-
70 QImageWriter can write. QImageWriter supports all built-in image-
71 formats, in addition to any image format plugins that support-
72 writing.-
73-
74 \sa QImageReader, QImageIOHandler, QImageIOPlugin-
75*/-
76-
77/*!-
78 \enum QImageWriter::ImageWriterError-
79-
80 This enum describes errors that can occur when writing images with-
81 QImageWriter.-
82-
83 \value DeviceError QImageWriter encountered a device error when-
84 writing the image data. Consult your device for more details on-
85 what went wrong.-
86-
87 \value UnsupportedFormatError Qt does not support the requested-
88 image format.-
89-
90 \value UnknownError An unknown error occurred. If you get this-
91 value after calling write(), it is most likely caused by a bug in-
92 QImageWriter.-
93*/-
94-
95#include "qimagewriter.h"-
96-
97#include <qbytearray.h>-
98#include <qfile.h>-
99#include <qfileinfo.h>-
100#include <qimage.h>-
101#include <qimageiohandler.h>-
102#include <qjsonarray.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-
125#include <algorithm>-
126-
127QT_BEGIN_NAMESPACE-
128-
129#ifndef QT_NO_IMAGEFORMATPLUGIN-
130Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
never executed: end of block
never executed: guard.store(QtGlobalStatic::Destroyed);
never executed: return &holder.value;
guard.load() =...c::InitializedDescription
TRUEnever evaluated
FALSEnever evaluated
0
131 (QImageIOHandlerFactoryInterface_iid, QLatin1String("/imageformats")))-
132#endif-
133-
134static QImageIOHandler *createWriteHandlerHelper(QIODevice *device,-
135 const QByteArray &format)-
136{-
137 QByteArray form = format.toLower();-
138 QByteArray suffix;-
139 QImageIOHandler *handler = 0;-
140-
141#ifndef QT_NO_IMAGEFORMATPLUGIN-
142 typedef QMultiMap<int, QString> PluginKeyMap;-
143-
144 // check if any plugins can write the image-
145 QFactoryLoader *l = loader();-
146 const PluginKeyMap keyMap = l->keyMap();-
147 int suffixPluginIndex = -1;-
148#endif-
149-
150 if (device && format.isEmpty()) {
deviceDescription
TRUEnever evaluated
FALSEnever evaluated
format.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
151 // if there's no format, see if \a device is a file, and if so, find-
152 // the file suffix and find support for that format among our plugins.-
153 // this allows plugins to override our built-in handlers.-
154 if (QFile *file = qobject_cast<QFile *>(device)) {
QFile *file = ...ile *>(device)Description
TRUEnever evaluated
FALSEnever evaluated
0
155 if (!(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()) {
!(suffix = QFi...1()).isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
156#ifndef QT_NO_IMAGEFORMATPLUGIN-
157 const int index = keyMap.key(QString::fromLatin1(suffix), -1);-
158 if (index != -1)
index != -1Description
TRUEnever evaluated
FALSEnever evaluated
0
159 suffixPluginIndex = index;
never executed: suffixPluginIndex = index;
0
160#endif-
161 }
never executed: end of block
0
162 }
never executed: end of block
0
163 }
never executed: end of block
0
164-
165 QByteArray testFormat = !form.isEmpty() ? form : suffix;
!form.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
166-
167#ifndef QT_NO_IMAGEFORMATPLUGIN-
168 if (suffixPluginIndex != -1) {
suffixPluginIndex != -1Description
TRUEnever evaluated
FALSEnever evaluated
0
169 // when format is missing, check if we can find a plugin for the-
170 // suffix.-
171 const int index = keyMap.key(QString::fromLatin1(suffix), -1);-
172 if (index != -1) {
index != -1Description
TRUEnever evaluated
FALSEnever evaluated
0
173 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(index));-
174 if (plugin && (plugin->capabilities(device, suffix) & QImageIOPlugin::CanWrite))
pluginDescription
TRUEnever evaluated
FALSEnever evaluated
(plugin->capab...gin::CanWrite)Description
TRUEnever evaluated
FALSEnever evaluated
0
175 handler = plugin->create(device, suffix);
never executed: handler = plugin->create(device, suffix);
0
176 }
never executed: end of block
0
177 }
never executed: end of block
0
178#endif // QT_NO_IMAGEFORMATPLUGIN-
179-
180 // check if any built-in handlers can write the image-
181 if (!handler && !testFormat.isEmpty()) {
!handlerDescription
TRUEnever evaluated
FALSEnever evaluated
!testFormat.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
182 if (false) {
dead code: { }
-
183#ifndef QT_NO_IMAGEFORMAT_PNG
dead code: { }
-
184 } else if (testFormat == "png") {
testFormat == "png"Description
TRUEnever evaluated
FALSEnever evaluated
dead code: { }
-
185 handler = new QPngHandler;-
186#endif-
187#ifndef QT_NO_IMAGEFORMAT_JPEG-
188 } else if (testFormat == "jpg" || testFormat == "jpeg") {-
189 handler = new QJpegHandler;-
190#endif-
191#ifdef QT_BUILTIN_GIF_READER-
192 } else if (testFormat == "gif") {-
193 handler = new QGifHandler;-
194#endif-
195#ifndef QT_NO_IMAGEFORMAT_BMP-
196 } else if (testFormat == "bmp") {
never executed: end of block
testFormat == "bmp"Description
TRUEnever evaluated
FALSEnever evaluated
0
197 handler = new QBmpHandler;-
198 } else if (testFormat == "dib") {
never executed: end of block
testFormat == "dib"Description
TRUEnever evaluated
FALSEnever evaluated
0
199 handler = new QBmpHandler(QBmpHandler::DibFormat);-
200#endif-
201#ifndef QT_NO_IMAGEFORMAT_XPM-
202 } else if (testFormat == "xpm") {
never executed: end of block
testFormat == "xpm"Description
TRUEnever evaluated
FALSEnever evaluated
0
203 handler = new QXpmHandler;-
204#endif-
205#ifndef QT_NO_IMAGEFORMAT_XBM-
206 } else if (testFormat == "xbm") {
never executed: end of block
testFormat == "xbm"Description
TRUEnever evaluated
FALSEnever evaluated
0
207 handler = new QXbmHandler;-
208 handler->setOption(QImageIOHandler::SubType, testFormat);-
209#endif-
210#ifndef QT_NO_IMAGEFORMAT_PPM-
211 } else if (testFormat == "pbm" || testFormat == "pbmraw" || testFormat == "pgm"
never executed: end of block
testFormat == "pbm"Description
TRUEnever evaluated
FALSEnever evaluated
testFormat == "pbmraw"Description
TRUEnever evaluated
FALSEnever evaluated
testFormat == "pgm"Description
TRUEnever evaluated
FALSEnever evaluated
0
212 || testFormat == "pgmraw" || testFormat == "ppm" || testFormat == "ppmraw") {
testFormat == "pgmraw"Description
TRUEnever evaluated
FALSEnever evaluated
testFormat == "ppm"Description
TRUEnever evaluated
FALSEnever evaluated
testFormat == "ppmraw"Description
TRUEnever evaluated
FALSEnever evaluated
0
213 handler = new QPpmHandler;-
214 handler->setOption(QImageIOHandler::SubType, testFormat);-
215#endif-
216 }
never executed: end of block
0
217 }
never executed: end of block
0
218-
219#ifndef QT_NO_IMAGEFORMATPLUGIN-
220 if (!testFormat.isEmpty()) {
!testFormat.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
221 const int keyCount = keyMap.size();-
222 for (int i = 0; i < keyCount; ++i) {
i < keyCountDescription
TRUEnever evaluated
FALSEnever evaluated
0
223 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(i));-
224 if (plugin && (plugin->capabilities(device, testFormat) & QImageIOPlugin::CanWrite)) {
pluginDescription
TRUEnever evaluated
FALSEnever evaluated
(plugin->capab...gin::CanWrite)Description
TRUEnever evaluated
FALSEnever evaluated
0
225 delete handler;-
226 handler = plugin->create(device, testFormat);-
227 break;
never executed: break;
0
228 }-
229 }
never executed: end of block
0
230 }
never executed: end of block
0
231#endif // QT_NO_IMAGEFORMATPLUGIN-
232-
233 if (!handler)
!handlerDescription
TRUEnever evaluated
FALSEnever evaluated
0
234 return 0;
never executed: return 0;
0
235-
236 handler->setDevice(device);-
237 if (!testFormat.isEmpty())
!testFormat.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
238 handler->setFormat(testFormat);
never executed: handler->setFormat(testFormat);
0
239 return handler;
never executed: return handler;
0
240}-
241-
242class QImageWriterPrivate-
243{-
244public:-
245 QImageWriterPrivate(QImageWriter *qq);-
246-
247 bool canWriteHelper();-
248-
249 // device-
250 QByteArray format;-
251 QIODevice *device;-
252 bool deleteDevice;-
253 QImageIOHandler *handler;-
254-
255 // image options-
256 int quality;-
257 int compression;-
258 float gamma;-
259 QString description;-
260 QString text;-
261 QByteArray subType;-
262 bool optimizedWrite;-
263 bool progressiveScanWrite;-
264 QImageIOHandler::Transformations transformation;-
265-
266 // error-
267 QImageWriter::ImageWriterError imageWriterError;-
268 QString errorString;-
269-
270 QImageWriter *q;-
271};-
272-
273/*!-
274 \internal-
275*/-
276QImageWriterPrivate::QImageWriterPrivate(QImageWriter *qq)-
277{-
278 device = 0;-
279 deleteDevice = false;-
280 handler = 0;-
281 quality = -1;-
282 compression = 0;-
283 gamma = 0.0;-
284 optimizedWrite = false;-
285 progressiveScanWrite = false;-
286 imageWriterError = QImageWriter::UnknownError;-
287 errorString = QImageWriter::tr("Unknown error");-
288 transformation = QImageIOHandler::TransformationNone;-
289-
290 q = qq;-
291}
never executed: end of block
0
292-
293bool QImageWriterPrivate::canWriteHelper()-
294{-
295 if (!device) {
!deviceDescription
TRUEnever evaluated
FALSEnever evaluated
0
296 imageWriterError = QImageWriter::DeviceError;-
297 errorString = QImageWriter::tr("Device is not set");-
298 return false;
never executed: return false;
0
299 }-
300 if (!device->isOpen())
!device->isOpen()Description
TRUEnever evaluated
FALSEnever evaluated
0
301 device->open(QIODevice::WriteOnly);
never executed: device->open(QIODevice::WriteOnly);
0
302 if (!device->isWritable()) {
!device->isWritable()Description
TRUEnever evaluated
FALSEnever evaluated
0
303 imageWriterError = QImageWriter::DeviceError;-
304 errorString = QImageWriter::tr("Device not writable");-
305 return false;
never executed: return false;
0
306 }-
307 if (!handler && (handler = createWriteHandlerHelper(device, format)) == 0) {
!handlerDescription
TRUEnever evaluated
FALSEnever evaluated
(handler = cre... format)) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
308 imageWriterError = QImageWriter::UnsupportedFormatError;-
309 errorString = QImageWriter::tr("Unsupported image format");-
310 return false;
never executed: return false;
0
311 }-
312 return true;
never executed: return true;
0
313}-
314-
315/*!-
316 Constructs an empty QImageWriter object. Before writing, you must-
317 call setFormat() to set an image format, then setDevice() or-
318 setFileName().-
319*/-
320QImageWriter::QImageWriter()-
321 : d(new QImageWriterPrivate(this))-
322{-
323}
never executed: end of block
0
324-
325/*!-
326 Constructs a QImageWriter object using the device \a device and-
327 image format \a format.-
328*/-
329QImageWriter::QImageWriter(QIODevice *device, const QByteArray &format)-
330 : d(new QImageWriterPrivate(this))-
331{-
332 d->device = device;-
333 d->format = format;-
334}
never executed: end of block
0
335-
336/*!-
337 Constructs a QImageWriter objects that will write to a file with-
338 the name \a fileName, using the image format \a format. If \a-
339 format is not provided, QImageWriter will detect the image format-
340 by inspecting the extension of \a fileName.-
341*/-
342QImageWriter::QImageWriter(const QString &fileName, const QByteArray &format)-
343 : d(new QImageWriterPrivate(this))-
344{-
345 QFile *file = new QFile(fileName);-
346 d->device = file;-
347 d->deleteDevice = true;-
348 d->format = format;-
349}
never executed: end of block
0
350-
351/*!-
352 Destructs the QImageWriter object.-
353*/-
354QImageWriter::~QImageWriter()-
355{-
356 if (d->deleteDevice)
d->deleteDeviceDescription
TRUEnever evaluated
FALSEnever evaluated
0
357 delete d->device;
never executed: delete d->device;
0
358 delete d->handler;-
359 delete d;-
360}
never executed: end of block
0
361-
362/*!-
363 Sets the format QImageWriter will use when writing images, to \a-
364 format. \a format is a case insensitive text string. Example:-
365-
366 \snippet code/src_gui_image_qimagewriter.cpp 0-
367-
368 You can call supportedImageFormats() for the full list of formats-
369 QImageWriter supports.-
370-
371 \sa format()-
372*/-
373void QImageWriter::setFormat(const QByteArray &format)-
374{-
375 d->format = format;-
376}
never executed: end of block
0
377-
378/*!-
379 Returns the format QImageWriter uses for writing images.-
380-
381 \sa setFormat()-
382*/-
383QByteArray QImageWriter::format() const-
384{-
385 return d->format;
never executed: return d->format;
0
386}-
387-
388/*!-
389 Sets QImageWriter's device to \a device. If a device has already-
390 been set, the old device is removed from QImageWriter and is-
391 otherwise left unchanged.-
392-
393 If the device is not already open, QImageWriter will attempt to-
394 open the device in \l QIODevice::WriteOnly mode by calling-
395 open(). Note that this does not work for certain devices, such as-
396 QProcess, QTcpSocket and QUdpSocket, where more logic is required-
397 to open the device.-
398-
399 \sa device(), setFileName()-
400*/-
401void QImageWriter::setDevice(QIODevice *device)-
402{-
403 if (d->device && d->deleteDevice)
d->deviceDescription
TRUEnever evaluated
FALSEnever evaluated
d->deleteDeviceDescription
TRUEnever evaluated
FALSEnever evaluated
0
404 delete d->device;
never executed: delete d->device;
0
405-
406 d->device = device;-
407 d->deleteDevice = false;-
408 delete d->handler;-
409 d->handler = 0;-
410}
never executed: end of block
0
411-
412/*!-
413 Returns the device currently assigned to QImageWriter, or 0 if no-
414 device has been assigned.-
415*/-
416QIODevice *QImageWriter::device() const-
417{-
418 return d->device;
never executed: return d->device;
0
419}-
420-
421/*!-
422 Sets the file name of QImageWriter to \a fileName. Internally,-
423 QImageWriter will create a QFile and open it in \l-
424 QIODevice::WriteOnly mode, and use this file when writing images.-
425-
426 \sa fileName(), setDevice()-
427*/-
428void QImageWriter::setFileName(const QString &fileName)-
429{-
430 setDevice(new QFile(fileName));-
431 d->deleteDevice = true;-
432}
never executed: end of block
0
433-
434/*!-
435 If the currently assigned device is a QFile, or if setFileName()-
436 has been called, this function returns the name of the file-
437 QImageWriter writes to. Otherwise (i.e., if no device has been-
438 assigned or the device is not a QFile), an empty QString is-
439 returned.-
440-
441 \sa setFileName(), setDevice()-
442*/-
443QString QImageWriter::fileName() const-
444{-
445 QFile *file = qobject_cast<QFile *>(d->device);-
446 return file ? file->fileName() : QString();
never executed: return file ? file->fileName() : QString();
0
447}-
448-
449/*!-
450 Sets the quality setting of the image format to \a quality.-
451-
452 Some image formats, in particular lossy ones, entail a tradeoff between a)-
453 visual quality of the resulting image, and b) encoding execution time and-
454 compression level. This function sets the level of that tradeoff for image-
455 formats that support it. For other formats, this value is ignored.-
456-
457 The value range of \a quality depends on the image format. For example,-
458 the "jpeg" format supports a quality range from 0 (low visual quality, high-
459 compression) to 100 (high visual quality, low compression).-
460-
461 \sa quality()-
462*/-
463void QImageWriter::setQuality(int quality)-
464{-
465 d->quality = quality;-
466}
never executed: end of block
0
467-
468/*!-
469 Returns the quality setting of the image format.-
470-
471 \sa setQuality()-
472*/-
473int QImageWriter::quality() const-
474{-
475 return d->quality;
never executed: return d->quality;
0
476}-
477-
478/*!-
479 This is an image format specific function that set the compression-
480 of an image. For image formats that do not support setting the-
481 compression, this value is ignored.-
482-
483 The value range of \a compression depends on the image format. For-
484 example, the "tiff" format supports two values, 0(no compression) and-
485 1(LZW-compression).-
486-
487 \sa compression()-
488*/-
489void QImageWriter::setCompression(int compression)-
490{-
491 d->compression = compression;-
492}
never executed: end of block
0
493-
494/*!-
495 Returns the compression of the image.-
496-
497 \sa setCompression()-
498*/-
499int QImageWriter::compression() const-
500{-
501 return d->compression;
never executed: return d->compression;
0
502}-
503-
504/*!-
505 This is an image format specific function that sets the gamma-
506 level of the image to \a gamma. For image formats that do not-
507 support setting the gamma level, this value is ignored.-
508-
509 The value range of \a gamma depends on the image format. For-
510 example, the "png" format supports a gamma range from 0.0 to 1.0.-
511-
512 \sa quality()-
513*/-
514void QImageWriter::setGamma(float gamma)-
515{-
516 d->gamma = gamma;-
517}
never executed: end of block
0
518-
519/*!-
520 Returns the gamma level of the image.-
521-
522 \sa setGamma()-
523*/-
524float QImageWriter::gamma() const-
525{-
526 return d->gamma;
never executed: return d->gamma;
0
527}-
528-
529/*!-
530 \since 5.4-
531-
532 This is an image format specific function that sets the-
533 subtype of the image to \a type. Subtype can be used by-
534 a handler to determine which format it should use while-
535 saving the image.-
536-
537 For example, saving an image in DDS format with A8R8G8R8 subtype:-
538-
539 \snippet code/src_gui_image_qimagewriter.cpp 3-
540*/-
541void QImageWriter::setSubType(const QByteArray &type)-
542{-
543 d->subType = type;-
544}
never executed: end of block
0
545-
546/*!-
547 \since 5.4-
548-
549 Returns the subtype of the image.-
550-
551 \sa setSubType()-
552*/-
553QByteArray QImageWriter::subType() const-
554{-
555 return d->subType;
never executed: return d->subType;
0
556}-
557-
558/*!-
559 \since 5.4-
560-
561 Returns the list of subtypes supported by an image.-
562*/-
563QList<QByteArray> QImageWriter::supportedSubTypes() const-
564{-
565 if (!supportsOption(QImageIOHandler::SupportedSubTypes))
!supportsOptio...ortedSubTypes)Description
TRUEnever evaluated
FALSEnever evaluated
0
566 return QList<QByteArray>();
never executed: return QList<QByteArray>();
0
567 return d->handler->option(QImageIOHandler::SupportedSubTypes).value< QList<QByteArray> >();
never executed: return d->handler->option(QImageIOHandler::SupportedSubTypes).value< QList<QByteArray> >();
0
568}-
569-
570/*!-
571 \since 5.5-
572-
573 This is an image format-specific function which sets the \a optimize flags when-
574 writing images. For image formats that do not support setting an \a optimize flag,-
575 this value is ignored.-
576-
577 The default is false.-
578-
579 \sa optimizedWrite()-
580*/-
581void QImageWriter::setOptimizedWrite(bool optimize)-
582{-
583 d->optimizedWrite = optimize;-
584}
never executed: end of block
0
585-
586/*!-
587 \since 5.5-
588-
589 Returns whether optimization has been turned on for writing the image.-
590-
591 \sa setOptimizedWrite()-
592*/-
593bool QImageWriter::optimizedWrite() const-
594{-
595 return d->optimizedWrite;
never executed: return d->optimizedWrite;
0
596}-
597-
598/*!-
599 \since 5.5-
600-
601 This is an image format-specific function which turns on \a progressive scanning-
602 when writing images. For image formats that do not support setting a \a progressive-
603 scan flag, this value is ignored.-
604-
605 The default is false.-
606-
607 \sa progressiveScanWrite()-
608*/-
609-
610void QImageWriter::setProgressiveScanWrite(bool progressive)-
611{-
612 d->progressiveScanWrite = progressive;-
613}
never executed: end of block
0
614-
615/*!-
616 \since 5.5-
617-
618 Returns whether the image should be written as a progressive image.-
619-
620 \sa setProgressiveScanWrite()-
621*/-
622bool QImageWriter::progressiveScanWrite() const-
623{-
624 return d->progressiveScanWrite;
never executed: return d->progressiveScanWrite;
0
625}-
626-
627/*!-
628 \since 5.5-
629-
630 Sets the image transformations metadata including orientation to \a transform.-
631-
632 If transformation metadata is not supported by the image format,-
633 the transform is applied before writing.-
634-
635 \sa transformation(), write()-
636*/-
637void QImageWriter::setTransformation(QImageIOHandler::Transformations transform)-
638{-
639 d->transformation = transform;-
640}
never executed: end of block
0
641-
642/*!-
643 \since 5.5-
644-
645 Returns the transformation and orientation the image has been set to written with.-
646-
647 \sa setTransformation()-
648*/-
649QImageIOHandler::Transformations QImageWriter::transformation() const-
650{-
651 return d->transformation;
never executed: return d->transformation;
0
652}-
653-
654/*!-
655 \obsolete-
656-
657 Use setText() instead.-
658-
659 This is an image format specific function that sets the-
660 description of the image to \a description. For image formats that-
661 do not support setting the description, this value is ignored.-
662-
663 The contents of \a description depends on the image format.-
664-
665 \sa description()-
666*/-
667void QImageWriter::setDescription(const QString &description)-
668{-
669 d->description = description;-
670}
never executed: end of block
0
671-
672/*!-
673 \obsolete-
674-
675 Use QImageReader::text() instead.-
676-
677 Returns the description of the image.-
678-
679 \sa setDescription()-
680*/-
681QString QImageWriter::description() const-
682{-
683 return d->description;
never executed: return d->description;
0
684}-
685-
686/*!-
687 \since 4.1-
688-
689 Sets the image text associated with the key \a key to-
690 \a text. This is useful for storing copyright information-
691 or other information about the image. Example:-
692-
693 \snippet code/src_gui_image_qimagewriter.cpp 1-
694-
695 If you want to store a single block of data-
696 (e.g., a comment), you can pass an empty key, or use-
697 a generic key like "Description".-
698-
699 The key and text will be embedded into the-
700 image data after calling write().-
701-
702 Support for this option is implemented through-
703 QImageIOHandler::Description.-
704-
705 \sa QImage::setText(), QImageReader::text()-
706*/-
707void QImageWriter::setText(const QString &key, const QString &text)-
708{-
709 if (!d->description.isEmpty())
!d->description.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
710 d->description += QLatin1String("\n\n");
never executed: d->description += QLatin1String("\n\n");
0
711 d->description += key.simplified() + QLatin1String(": ") + text.simplified();-
712}
never executed: end of block
0
713-
714/*!-
715 Returns \c true if QImageWriter can write the image; i.e., the image-
716 format is supported and the assigned device is open for reading.-
717-
718 \sa write(), setDevice(), setFormat()-
719*/-
720bool QImageWriter::canWrite() const-
721{-
722 if (QFile *file = qobject_cast<QFile *>(d->device)) {
QFile *file = ... *>(d->device)Description
TRUEnever evaluated
FALSEnever evaluated
0
723 const bool remove = !file->isOpen() && !file->exists();
!file->isOpen()Description
TRUEnever evaluated
FALSEnever evaluated
!file->exists()Description
TRUEnever evaluated
FALSEnever evaluated
0
724 const bool result = d->canWriteHelper();-
725 if (!result && remove)
!resultDescription
TRUEnever evaluated
FALSEnever evaluated
removeDescription
TRUEnever evaluated
FALSEnever evaluated
0
726 file->remove();
never executed: file->remove();
0
727 return result;
never executed: return result;
0
728 }-
729-
730 return d->canWriteHelper();
never executed: return d->canWriteHelper();
0
731}-
732-
733extern void qt_imageTransform(QImage &src, QImageIOHandler::Transformations orient);-
734-
735/*!-
736 Writes the image \a image to the assigned device or file-
737 name. Returns \c true on success; otherwise returns \c false. If the-
738 operation fails, you can call error() to find the type of error-
739 that occurred, or errorString() to get a human readable-
740 description of the error.-
741-
742 \sa canWrite(), error(), errorString()-
743*/-
744bool QImageWriter::write(const QImage &image)-
745{-
746 if (!canWrite())
!canWrite()Description
TRUEnever evaluated
FALSEnever evaluated
0
747 return false;
never executed: return false;
0
748-
749 QImage img = image;-
750 if (d->handler->supportsOption(QImageIOHandler::Quality))
d->handler->su...dler::Quality)Description
TRUEnever evaluated
FALSEnever evaluated
0
751 d->handler->setOption(QImageIOHandler::Quality, d->quality);
never executed: d->handler->setOption(QImageIOHandler::Quality, d->quality);
0
752 if (d->handler->supportsOption(QImageIOHandler::CompressionRatio))
d->handler->su...pressionRatio)Description
TRUEnever evaluated
FALSEnever evaluated
0
753 d->handler->setOption(QImageIOHandler::CompressionRatio, d->compression);
never executed: d->handler->setOption(QImageIOHandler::CompressionRatio, d->compression);
0
754 if (d->handler->supportsOption(QImageIOHandler::Gamma))
d->handler->su...andler::Gamma)Description
TRUEnever evaluated
FALSEnever evaluated
0
755 d->handler->setOption(QImageIOHandler::Gamma, d->gamma);
never executed: d->handler->setOption(QImageIOHandler::Gamma, d->gamma);
0
756 if (!d->description.isEmpty() && d->handler->supportsOption(QImageIOHandler::Description))
!d->description.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
d->handler->su...::Description)Description
TRUEnever evaluated
FALSEnever evaluated
0
757 d->handler->setOption(QImageIOHandler::Description, d->description);
never executed: d->handler->setOption(QImageIOHandler::Description, d->description);
0
758 if (!d->subType.isEmpty() && d->handler->supportsOption(QImageIOHandler::SubType))
!d->subType.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
d->handler->su...dler::SubType)Description
TRUEnever evaluated
FALSEnever evaluated
0
759 d->handler->setOption(QImageIOHandler::SubType, d->subType);
never executed: d->handler->setOption(QImageIOHandler::SubType, d->subType);
0
760 if (d->handler->supportsOption(QImageIOHandler::OptimizedWrite))
d->handler->su...ptimizedWrite)Description
TRUEnever evaluated
FALSEnever evaluated
0
761 d->handler->setOption(QImageIOHandler::OptimizedWrite, d->optimizedWrite);
never executed: d->handler->setOption(QImageIOHandler::OptimizedWrite, d->optimizedWrite);
0
762 if (d->handler->supportsOption(QImageIOHandler::ProgressiveScanWrite))
d->handler->su...siveScanWrite)Description
TRUEnever evaluated
FALSEnever evaluated
0
763 d->handler->setOption(QImageIOHandler::ProgressiveScanWrite, d->progressiveScanWrite);
never executed: d->handler->setOption(QImageIOHandler::ProgressiveScanWrite, d->progressiveScanWrite);
0
764 if (d->handler->supportsOption(QImageIOHandler::ImageTransformation))
d->handler->su...ransformation)Description
TRUEnever evaluated
FALSEnever evaluated
0
765 d->handler->setOption(QImageIOHandler::ImageTransformation, int(d->transformation));
never executed: d->handler->setOption(QImageIOHandler::ImageTransformation, int(d->transformation));
0
766 else-
767 qt_imageTransform(img, d->transformation);
never executed: qt_imageTransform(img, d->transformation);
0
768-
769 if (!d->handler->write(img))
!d->handler->write(img)Description
TRUEnever evaluated
FALSEnever evaluated
0
770 return false;
never executed: return false;
0
771 if (QFile *file = qobject_cast<QFile *>(d->device))
QFile *file = ... *>(d->device)Description
TRUEnever evaluated
FALSEnever evaluated
0
772 file->flush();
never executed: file->flush();
0
773 return true;
never executed: return true;
0
774}-
775-
776/*!-
777 Returns the type of error that last occurred.-
778-
779 \sa ImageWriterError, errorString()-
780*/-
781QImageWriter::ImageWriterError QImageWriter::error() const-
782{-
783 return d->imageWriterError;
never executed: return d->imageWriterError;
0
784}-
785-
786/*!-
787 Returns a human readable description of the last error that occurred.-
788-
789 \sa error()-
790*/-
791QString QImageWriter::errorString() const-
792{-
793 return d->errorString;
never executed: return d->errorString;
0
794}-
795-
796/*!-
797 \since 4.2-
798-
799 Returns \c true if the writer supports \a option; otherwise returns-
800 false.-
801-
802 Different image formats support different options. Call this function to-
803 determine whether a certain option is supported by the current format. For-
804 example, the PNG format allows you to embed text into the image's metadata-
805 (see text()).-
806-
807 \snippet code/src_gui_image_qimagewriter.cpp 2-
808-
809 Options can be tested after the writer has been associated with a format.-
810-
811 \sa QImageReader::supportsOption(), setFormat()-
812*/-
813bool QImageWriter::supportsOption(QImageIOHandler::ImageOption option) const-
814{-
815 if (!d->handler && (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0) {
!d->handlerDescription
TRUEnever evaluated
FALSEnever evaluated
(d->handler = ...>format)) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
816 d->imageWriterError = QImageWriter::UnsupportedFormatError;-
817 d->errorString = QImageWriter::tr("Unsupported image format");-
818 return false;
never executed: return false;
0
819 }-
820-
821 return d->handler->supportsOption(option);
never executed: return d->handler->supportsOption(option);
0
822}-
823-
824-
825#ifndef QT_NO_IMAGEFORMATPLUGIN-
826void supportedImageHandlerFormats(QFactoryLoader *loader,-
827 QImageIOPlugin::Capability cap,-
828 QList<QByteArray> *result)-
829{-
830 typedef QMultiMap<int, QString> PluginKeyMap;-
831 typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator;-
832-
833 const PluginKeyMap keyMap = loader->keyMap();-
834 const PluginKeyMapConstIterator cend = keyMap.constEnd();-
835 int i = -1;-
836 QImageIOPlugin *plugin = 0;-
837 result->reserve(result->size() + keyMap.size());-
838 for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it) {
it != cendDescription
TRUEnever evaluated
FALSEnever evaluated
0
839 if (it.key() != i) {
it.key() != iDescription
TRUEnever evaluated
FALSEnever evaluated
0
840 i = it.key();-
841 plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i));-
842 }
never executed: end of block
0
843 const QByteArray key = it.value().toLatin1();-
844 if (plugin && (plugin->capabilities(0, key) & cap) != 0)
pluginDescription
TRUEnever evaluated
FALSEnever evaluated
(plugin->capab...y) & cap) != 0Description
TRUEnever evaluated
FALSEnever evaluated
0
845 result->append(key);
never executed: result->append(key);
0
846 }
never executed: end of block
0
847}
never executed: end of block
0
848-
849void supportedImageHandlerMimeTypes(QFactoryLoader *loader,-
850 QImageIOPlugin::Capability cap,-
851 QList<QByteArray> *result)-
852{-
853 QList<QJsonObject> metaDataList = loader->metaData();-
854-
855 const int pluginCount = metaDataList.size();-
856 for (int i = 0; i < pluginCount; ++i) {
i < pluginCountDescription
TRUEnever evaluated
FALSEnever evaluated
0
857 const QJsonObject metaData = metaDataList.at(i).value(QLatin1String("MetaData")).toObject();-
858 const QJsonArray keys = metaData.value(QLatin1String("Keys")).toArray();-
859 const QJsonArray mimeTypes = metaData.value(QLatin1String("MimeTypes")).toArray();-
860 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i));-
861 const int keyCount = keys.size();-
862 for (int k = 0; k < keyCount; ++k) {
k < keyCountDescription
TRUEnever evaluated
FALSEnever evaluated
0
863 if (plugin && (plugin->capabilities(0, keys.at(k).toString().toLatin1()) & cap) != 0)
pluginDescription
TRUEnever evaluated
FALSEnever evaluated
(plugin->capab...)) & cap) != 0Description
TRUEnever evaluated
FALSEnever evaluated
0
864 result->append(mimeTypes.at(k).toString().toLatin1());
never executed: result->append(mimeTypes.at(k).toString().toLatin1());
0
865 }
never executed: end of block
0
866 }
never executed: end of block
0
867}
never executed: end of block
0
868#endif // QT_NO_IMAGEFORMATPLUGIN-
869-
870/*!-
871 Returns the list of image formats supported by QImageWriter.-
872-
873 By default, Qt can write the following formats:-
874-
875 \table-
876 \header \li Format \li MIME type \li Description-
877 \row \li BMP \li image/bmp \li Windows Bitmap-
878 \row \li JPG \li image/jpeg \li Joint Photographic Experts Group-
879 \row \li PNG \li image/png \li Portable Network Graphics-
880 \row \li PBM \li image/x-portable-bitmap \li Portable Bitmap-
881 \row \li PGM \li image/x-portable-graymap \li Portable Graymap-
882 \row \li PPM \li image/x-portable-pixmap \li Portable Pixmap-
883 \row \li XBM \li image/x-xbitmap \li X11 Bitmap-
884 \row \li XPM \li image/x-xpixmap \li X11 Pixmap-
885 \endtable-
886-
887 Reading and writing SVG files is supported through the \l{Qt SVG} module.-
888 The \l{Qt Image Formats} module provides support for additional image formats.-
889-
890 Note that the QApplication instance must be created before this function is-
891 called.-
892-
893 \sa setFormat(), QImageReader::supportedImageFormats(), QImageIOPlugin-
894*/-
895QList<QByteArray> QImageWriter::supportedImageFormats()-
896{-
897 QList<QByteArray> formats;-
898#ifndef QT_NO_IMAGEFORMAT_BMP-
899 formats << "bmp";-
900#endif-
901#ifndef QT_NO_IMAGEFORMAT_PPM-
902 formats << "pbm" << "pgm" << "ppm";-
903#endif-
904#ifndef QT_NO_IMAGEFORMAT_XBM-
905 formats << "xbm";-
906#endif-
907#ifndef QT_NO_IMAGEFORMAT_XPM-
908 formats << "xpm";-
909#endif-
910#ifndef QT_NO_IMAGEFORMAT_PNG-
911 formats << "png";-
912#endif-
913#ifndef QT_NO_IMAGEFORMAT_JPEG-
914 formats << "jpg" << "jpeg";-
915#endif-
916-
917#ifndef QT_NO_IMAGEFORMATPLUGIN-
918 supportedImageHandlerFormats(loader(), QImageIOPlugin::CanWrite, &formats);-
919#endif // QT_NO_IMAGEFORMATPLUGIN-
920-
921 std::sort(formats.begin(), formats.end());-
922 formats.erase(std::unique(formats.begin(), formats.end()), formats.end());-
923 return formats;
never executed: return formats;
0
924}-
925-
926/*!-
927 Returns the list of MIME types supported by QImageWriter.-
928-
929 Note that the QApplication instance must be created before this function is-
930 called.-
931-
932 \sa supportedImageFormats(), QImageReader::supportedMimeTypes()-
933*/-
934QList<QByteArray> QImageWriter::supportedMimeTypes()-
935{-
936 QList<QByteArray> mimeTypes;-
937#ifndef QT_NO_IMAGEFORMAT_BMP-
938 mimeTypes << "image/bmp";-
939#endif-
940#ifndef QT_NO_IMAGEFORMAT_PPM-
941 mimeTypes << "image/x-portable-bitmap";-
942 mimeTypes << "image/x-portable-graymap";-
943 mimeTypes << "image/x-portable-pixmap";-
944#endif-
945#ifndef QT_NO_IMAGEFORMAT_XBM-
946 mimeTypes << "image/x-xbitmap";-
947#endif-
948#ifndef QT_NO_IMAGEFORMAT_XPM-
949 mimeTypes << "image/x-xpixmap";-
950#endif-
951#ifndef QT_NO_IMAGEFORMAT_PNG-
952 mimeTypes << "image/png";-
953#endif-
954#ifndef QT_NO_IMAGEFORMAT_JPEG-
955 mimeTypes << "image/jpeg";-
956#endif-
957-
958#ifndef QT_NO_IMAGEFORMATPLUGIN-
959 supportedImageHandlerMimeTypes(loader(), QImageIOPlugin::CanWrite, &mimeTypes);-
960#endif // QT_NO_IMAGEFORMATPLUGIN-
961-
962 std::sort(mimeTypes.begin(), mimeTypes.end());-
963 mimeTypes.erase(std::unique(mimeTypes.begin(), mimeTypes.end()), mimeTypes.end());-
964 return mimeTypes;
never executed: return mimeTypes;
0
965}-
966-
967QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9