1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | /*! |
5 | \class QImageWriter |
6 | \brief The QImageWriter class provides a format independent interface |
7 | for writing images to files or other devices. |
8 | |
9 | \inmodule QtGui |
10 | \reentrant |
11 | \ingroup painting |
12 | |
13 | QImageWriter supports setting format specific options, such as |
14 | compression level and quality, prior to storing the |
15 | image. If you do not need such options, you can use QImage::save() |
16 | or QPixmap::save() instead. |
17 | |
18 | To store an image, you start by constructing a QImageWriter |
19 | object. Pass either a file name or a device pointer, and the |
20 | image format to QImageWriter's constructor. You can then set |
21 | several options, such as quality (by calling setQuality()). |
22 | canWrite() returns \c true if QImageWriter can write the image |
23 | (i.e., the image format is supported and the device is open for |
24 | writing). Call write() to write the image to the device. |
25 | |
26 | If any error occurs when writing the image, write() will return |
27 | false. You can then call error() to find the type of error that |
28 | occurred, or errorString() to get a human readable description of |
29 | what went wrong. |
30 | |
31 | Call supportedImageFormats() for a list of formats that |
32 | QImageWriter can write. QImageWriter supports all built-in image |
33 | formats, in addition to any image format plugins that support |
34 | writing. |
35 | |
36 | \note QImageWriter assumes exclusive control over the file or |
37 | device that is assigned. Any attempts to modify the assigned file |
38 | or device during the lifetime of the QImageWriter object will |
39 | yield undefined results. If immediate access to a resource is |
40 | desired, the use of a scope is the recommended method. |
41 | |
42 | For example: |
43 | |
44 | \snippet qimagewriter/main.cpp 0 |
45 | |
46 | \sa QImageReader, QImageIOHandler, QImageIOPlugin, QColorSpace |
47 | */ |
48 | |
49 | /*! |
50 | \enum QImageWriter::ImageWriterError |
51 | |
52 | This enum describes errors that can occur when writing images with |
53 | QImageWriter. |
54 | |
55 | \value DeviceError QImageWriter encountered a device error when |
56 | writing the image data. Consult your device for more details on |
57 | what went wrong. |
58 | |
59 | \value UnsupportedFormatError Qt does not support the requested |
60 | image format. |
61 | |
62 | \value InvalidImageError An attempt was made to write an invalid QImage. An |
63 | example of an invalid image would be a null QImage. |
64 | |
65 | \value UnknownError An unknown error occurred. If you get this |
66 | value after calling write(), it is most likely caused by a bug in |
67 | QImageWriter. |
68 | */ |
69 | |
70 | #include "qimagewriter.h" |
71 | |
72 | #include <qbytearray.h> |
73 | #include <qfile.h> |
74 | #include <qfileinfo.h> |
75 | #include <qimage.h> |
76 | #include <qimageiohandler.h> |
77 | #include <qset.h> |
78 | #include <qvariant.h> |
79 | |
80 | // factory loader |
81 | #include <qcoreapplication.h> |
82 | #include <private/qfactoryloader_p.h> |
83 | |
84 | // image handlers |
85 | #include <private/qbmphandler_p.h> |
86 | #include <private/qppmhandler_p.h> |
87 | #include <private/qxbmhandler_p.h> |
88 | #include <private/qxpmhandler_p.h> |
89 | #ifndef QT_NO_IMAGEFORMAT_PNG |
90 | #include <private/qpnghandler_p.h> |
91 | #endif |
92 | |
93 | #include <private/qimagereaderwriterhelpers_p.h> |
94 | |
95 | #include <algorithm> |
96 | |
97 | QT_BEGIN_NAMESPACE |
98 | |
99 | using namespace Qt::StringLiterals; |
100 | |
101 | static QImageIOHandler *createWriteHandlerHelper(QIODevice *device, |
102 | const QByteArray &format) |
103 | { |
104 | QByteArray form = format.toLower(); |
105 | QByteArray suffix; |
106 | QImageIOHandler *handler = nullptr; |
107 | |
108 | #ifndef QT_NO_IMAGEFORMATPLUGIN |
109 | typedef QMultiMap<int, QString> PluginKeyMap; |
110 | |
111 | // check if any plugins can write the image |
112 | auto l = QImageReaderWriterHelpers::pluginLoader(); |
113 | const PluginKeyMap keyMap = l->keyMap(); |
114 | int suffixPluginIndex = -1; |
115 | #endif |
116 | |
117 | if (device && format.isEmpty()) { |
118 | // if there's no format, see if \a device is a file, and if so, find |
119 | // the file suffix and find support for that format among our plugins. |
120 | // this allows plugins to override our built-in handlers. |
121 | if (QFileDevice *file = qobject_cast<QFileDevice *>(object: device)) { |
122 | if (!(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()) { |
123 | #ifndef QT_NO_IMAGEFORMATPLUGIN |
124 | const int index = keyMap.key(value: QString::fromLatin1(ba: suffix), defaultKey: -1); |
125 | if (index != -1) |
126 | suffixPluginIndex = index; |
127 | #endif |
128 | } |
129 | } |
130 | } |
131 | |
132 | QByteArray testFormat = !form.isEmpty() ? form : suffix; |
133 | |
134 | #ifndef QT_NO_IMAGEFORMATPLUGIN |
135 | if (suffixPluginIndex != -1) { |
136 | // when format is missing, check if we can find a plugin for the |
137 | // suffix. |
138 | const int index = keyMap.key(value: QString::fromLatin1(ba: suffix), defaultKey: -1); |
139 | if (index != -1) { |
140 | QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(object: l->instance(index)); |
141 | if (plugin && (plugin->capabilities(device, format: suffix) & QImageIOPlugin::CanWrite)) |
142 | handler = plugin->create(device, format: suffix); |
143 | } |
144 | } |
145 | #endif // QT_NO_IMAGEFORMATPLUGIN |
146 | |
147 | // check if any built-in handlers can write the image |
148 | if (!handler && !testFormat.isEmpty()) { |
149 | if (false) { |
150 | #ifndef QT_NO_IMAGEFORMAT_PNG |
151 | } else if (testFormat == "png" ) { |
152 | handler = new QPngHandler; |
153 | #endif |
154 | #ifndef QT_NO_IMAGEFORMAT_BMP |
155 | } else if (testFormat == "bmp" ) { |
156 | handler = new QBmpHandler; |
157 | } else if (testFormat == "dib" ) { |
158 | handler = new QBmpHandler(QBmpHandler::DibFormat); |
159 | #endif |
160 | #ifndef QT_NO_IMAGEFORMAT_XPM |
161 | } else if (testFormat == "xpm" ) { |
162 | handler = new QXpmHandler; |
163 | #endif |
164 | #ifndef QT_NO_IMAGEFORMAT_XBM |
165 | } else if (testFormat == "xbm" ) { |
166 | handler = new QXbmHandler; |
167 | handler->setOption(option: QImageIOHandler::SubType, value: testFormat); |
168 | #endif |
169 | #ifndef QT_NO_IMAGEFORMAT_PPM |
170 | } else if (testFormat == "pbm" || testFormat == "pbmraw" || testFormat == "pgm" |
171 | || testFormat == "pgmraw" || testFormat == "ppm" || testFormat == "ppmraw" ) { |
172 | handler = new QPpmHandler; |
173 | handler->setOption(option: QImageIOHandler::SubType, value: testFormat); |
174 | #endif |
175 | } |
176 | } |
177 | |
178 | #ifndef QT_NO_IMAGEFORMATPLUGIN |
179 | if (!testFormat.isEmpty()) { |
180 | const int keyCount = keyMap.size(); |
181 | for (int i = 0; i < keyCount; ++i) { |
182 | QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(object: l->instance(index: i)); |
183 | if (plugin && (plugin->capabilities(device, format: testFormat) & QImageIOPlugin::CanWrite)) { |
184 | delete handler; |
185 | handler = plugin->create(device, format: testFormat); |
186 | break; |
187 | } |
188 | } |
189 | } |
190 | #endif // QT_NO_IMAGEFORMATPLUGIN |
191 | |
192 | if (!handler) |
193 | return nullptr; |
194 | |
195 | handler->setDevice(device); |
196 | if (!testFormat.isEmpty()) |
197 | handler->setFormat(testFormat); |
198 | return handler; |
199 | } |
200 | |
201 | class QImageWriterPrivate |
202 | { |
203 | public: |
204 | QImageWriterPrivate(QImageWriter *qq); |
205 | |
206 | bool canWriteHelper(); |
207 | |
208 | // device |
209 | QByteArray format; |
210 | QIODevice *device; |
211 | bool deleteDevice; |
212 | QImageIOHandler *handler; |
213 | |
214 | // image options |
215 | int quality; |
216 | int compression; |
217 | float gamma; |
218 | QString description; |
219 | QString text; |
220 | QByteArray subType; |
221 | bool optimizedWrite; |
222 | bool progressiveScanWrite; |
223 | QImageIOHandler::Transformations transformation; |
224 | |
225 | // error |
226 | QImageWriter::ImageWriterError imageWriterError; |
227 | QString errorString; |
228 | |
229 | QImageWriter *q; |
230 | }; |
231 | |
232 | /*! |
233 | \internal |
234 | */ |
235 | QImageWriterPrivate::QImageWriterPrivate(QImageWriter *qq) |
236 | { |
237 | device = nullptr; |
238 | deleteDevice = false; |
239 | handler = nullptr; |
240 | quality = -1; |
241 | compression = -1; |
242 | gamma = 0.0; |
243 | optimizedWrite = false; |
244 | progressiveScanWrite = false; |
245 | imageWriterError = QImageWriter::UnknownError; |
246 | errorString = QImageWriter::tr(sourceText: "Unknown error" ); |
247 | transformation = QImageIOHandler::TransformationNone; |
248 | |
249 | q = qq; |
250 | } |
251 | |
252 | bool QImageWriterPrivate::canWriteHelper() |
253 | { |
254 | if (!device) { |
255 | imageWriterError = QImageWriter::DeviceError; |
256 | errorString = QImageWriter::tr(sourceText: "Device is not set" ); |
257 | return false; |
258 | } |
259 | if (!device->isOpen()) { |
260 | if (!device->open(mode: QIODevice::WriteOnly)) { |
261 | imageWriterError = QImageWriter::DeviceError; |
262 | errorString = QImageWriter::tr(sourceText: "Cannot open device for writing: %1" ).arg(a: device->errorString()); |
263 | return false; |
264 | } |
265 | } |
266 | if (!device->isWritable()) { |
267 | imageWriterError = QImageWriter::DeviceError; |
268 | errorString = QImageWriter::tr(sourceText: "Device not writable" ); |
269 | return false; |
270 | } |
271 | if (!handler && (handler = createWriteHandlerHelper(device, format)) == nullptr) { |
272 | imageWriterError = QImageWriter::UnsupportedFormatError; |
273 | errorString = QImageWriter::tr(sourceText: "Unsupported image format" ); |
274 | return false; |
275 | } |
276 | return true; |
277 | } |
278 | |
279 | /*! |
280 | Constructs an empty QImageWriter object. Before writing, you must |
281 | call setFormat() to set an image format, then setDevice() or |
282 | setFileName(). |
283 | */ |
284 | QImageWriter::QImageWriter() |
285 | : d(new QImageWriterPrivate(this)) |
286 | { |
287 | } |
288 | |
289 | /*! |
290 | Constructs a QImageWriter object using the device \a device and |
291 | image format \a format. |
292 | */ |
293 | QImageWriter::QImageWriter(QIODevice *device, const QByteArray &format) |
294 | : d(new QImageWriterPrivate(this)) |
295 | { |
296 | d->device = device; |
297 | d->format = format; |
298 | } |
299 | |
300 | /*! |
301 | Constructs a QImageWriter objects that will write to a file with |
302 | the name \a fileName, using the image format \a format. If \a |
303 | format is not provided, QImageWriter will detect the image format |
304 | by inspecting the extension of \a fileName. |
305 | */ |
306 | QImageWriter::QImageWriter(const QString &fileName, const QByteArray &format) |
307 | : QImageWriter(new QFile(fileName), format) |
308 | { |
309 | d->deleteDevice = true; |
310 | } |
311 | |
312 | /*! |
313 | Destructs the QImageWriter object. |
314 | */ |
315 | QImageWriter::~QImageWriter() |
316 | { |
317 | delete d->handler; |
318 | if (d->deleteDevice) |
319 | delete d->device; |
320 | delete d; |
321 | } |
322 | |
323 | /*! |
324 | Sets the format QImageWriter will use when writing images, to \a |
325 | format. \a format is a case insensitive text string. Example: |
326 | |
327 | \snippet code/src_gui_image_qimagewriter.cpp 0 |
328 | |
329 | You can call supportedImageFormats() for the full list of formats |
330 | QImageWriter supports. |
331 | |
332 | \sa format() |
333 | */ |
334 | void QImageWriter::setFormat(const QByteArray &format) |
335 | { |
336 | d->format = format; |
337 | } |
338 | |
339 | /*! |
340 | Returns the format QImageWriter uses for writing images. |
341 | |
342 | \sa setFormat() |
343 | */ |
344 | QByteArray QImageWriter::format() const |
345 | { |
346 | return d->format; |
347 | } |
348 | |
349 | /*! |
350 | Sets QImageWriter's device to \a device. If a device has already |
351 | been set, the old device is removed from QImageWriter and is |
352 | otherwise left unchanged. |
353 | |
354 | If the device is not already open, QImageWriter will attempt to |
355 | open the device in \l QIODevice::WriteOnly mode by calling |
356 | open(). Note that this does not work for certain devices, such as |
357 | QProcess, QTcpSocket and QUdpSocket, where more logic is required |
358 | to open the device. |
359 | |
360 | \sa device(), setFileName() |
361 | */ |
362 | void QImageWriter::setDevice(QIODevice *device) |
363 | { |
364 | delete d->handler; |
365 | d->handler = nullptr; |
366 | if (d->device && d->deleteDevice) |
367 | delete d->device; |
368 | |
369 | d->device = device; |
370 | d->deleteDevice = false; |
371 | } |
372 | |
373 | /*! |
374 | Returns the device currently assigned to QImageWriter, or \nullptr |
375 | if no device has been assigned. |
376 | */ |
377 | QIODevice *QImageWriter::device() const |
378 | { |
379 | return d->device; |
380 | } |
381 | |
382 | /*! |
383 | Sets the file name of QImageWriter to \a fileName. Internally, |
384 | QImageWriter will create a QFile and open it in \l |
385 | QIODevice::WriteOnly mode, and use this file when writing images. |
386 | |
387 | \sa fileName(), setDevice() |
388 | */ |
389 | void QImageWriter::setFileName(const QString &fileName) |
390 | { |
391 | setDevice(new QFile(fileName)); |
392 | d->deleteDevice = true; |
393 | } |
394 | |
395 | /*! |
396 | If the currently assigned device is a file, or if setFileName() |
397 | has been called, this function returns the name of the file |
398 | QImageWriter writes to. Otherwise (i.e., if no device has been |
399 | assigned or the device is not a file), an empty QString is |
400 | returned. |
401 | |
402 | \sa setFileName(), setDevice() |
403 | */ |
404 | QString QImageWriter::fileName() const |
405 | { |
406 | QFileDevice *file = qobject_cast<QFileDevice *>(object: d->device); |
407 | return file ? file->fileName() : QString(); |
408 | } |
409 | |
410 | /*! |
411 | Sets the quality setting of the image format to \a quality. |
412 | |
413 | Some image formats, in particular lossy ones, entail a tradeoff between a) |
414 | visual quality of the resulting image, and b) encoding execution time and |
415 | compression level. This function sets the level of that tradeoff for image |
416 | formats that support it. For other formats, this value is ignored. |
417 | |
418 | The value range of \a quality depends on the image format. For example, |
419 | the "jpeg" format supports a quality range from 0 (low visual quality, high |
420 | compression) to 100 (high visual quality, low compression). |
421 | |
422 | \sa quality() |
423 | */ |
424 | void QImageWriter::setQuality(int quality) |
425 | { |
426 | d->quality = quality; |
427 | } |
428 | |
429 | /*! |
430 | Returns the quality setting of the image format. |
431 | |
432 | \sa setQuality() |
433 | */ |
434 | int QImageWriter::quality() const |
435 | { |
436 | return d->quality; |
437 | } |
438 | |
439 | /*! |
440 | This is an image format specific function that set the compression |
441 | of an image. For image formats that do not support setting the |
442 | compression, this value is ignored. |
443 | |
444 | The value range of \a compression depends on the image format. For |
445 | example, the "tiff" format supports two values, 0(no compression) and |
446 | 1(LZW-compression). |
447 | |
448 | \sa compression() |
449 | */ |
450 | void QImageWriter::setCompression(int compression) |
451 | { |
452 | d->compression = compression; |
453 | } |
454 | |
455 | /*! |
456 | Returns the compression of the image. |
457 | |
458 | \sa setCompression() |
459 | */ |
460 | int QImageWriter::compression() const |
461 | { |
462 | return d->compression; |
463 | } |
464 | |
465 | /*! |
466 | \since 5.4 |
467 | |
468 | This is an image format specific function that sets the |
469 | subtype of the image to \a type. Subtype can be used by |
470 | a handler to determine which format it should use while |
471 | saving the image. |
472 | |
473 | For example, saving an image in DDS format with A8R8G8R8 subtype: |
474 | |
475 | \snippet code/src_gui_image_qimagewriter.cpp 3 |
476 | */ |
477 | void QImageWriter::setSubType(const QByteArray &type) |
478 | { |
479 | d->subType = type; |
480 | } |
481 | |
482 | /*! |
483 | \since 5.4 |
484 | |
485 | Returns the subtype of the image. |
486 | |
487 | \sa setSubType() |
488 | */ |
489 | QByteArray QImageWriter::subType() const |
490 | { |
491 | return d->subType; |
492 | } |
493 | |
494 | /*! |
495 | \since 5.4 |
496 | |
497 | Returns the list of subtypes supported by an image. |
498 | */ |
499 | QList<QByteArray> QImageWriter::supportedSubTypes() const |
500 | { |
501 | if (!supportsOption(option: QImageIOHandler::SupportedSubTypes)) |
502 | return QList<QByteArray>(); |
503 | return qvariant_cast<QList<QByteArray> >(v: d->handler->option(option: QImageIOHandler::SupportedSubTypes)); |
504 | } |
505 | |
506 | /*! |
507 | \since 5.5 |
508 | |
509 | This is an image format-specific function which sets the \a optimize flags when |
510 | writing images. For image formats that do not support setting an \a optimize flag, |
511 | this value is ignored. |
512 | |
513 | The default is false. |
514 | |
515 | \sa optimizedWrite() |
516 | */ |
517 | void QImageWriter::setOptimizedWrite(bool optimize) |
518 | { |
519 | d->optimizedWrite = optimize; |
520 | } |
521 | |
522 | /*! |
523 | \since 5.5 |
524 | |
525 | Returns whether optimization has been turned on for writing the image. |
526 | |
527 | \sa setOptimizedWrite() |
528 | */ |
529 | bool QImageWriter::optimizedWrite() const |
530 | { |
531 | return d->optimizedWrite; |
532 | } |
533 | |
534 | /*! |
535 | \since 5.5 |
536 | |
537 | This is an image format-specific function which turns on \a progressive scanning |
538 | when writing images. For image formats that do not support setting a \a progressive |
539 | scan flag, this value is ignored. |
540 | |
541 | The default is false. |
542 | |
543 | \sa progressiveScanWrite() |
544 | */ |
545 | |
546 | void QImageWriter::setProgressiveScanWrite(bool progressive) |
547 | { |
548 | d->progressiveScanWrite = progressive; |
549 | } |
550 | |
551 | /*! |
552 | \since 5.5 |
553 | |
554 | Returns whether the image should be written as a progressive image. |
555 | |
556 | \sa setProgressiveScanWrite() |
557 | */ |
558 | bool QImageWriter::progressiveScanWrite() const |
559 | { |
560 | return d->progressiveScanWrite; |
561 | } |
562 | |
563 | /*! |
564 | \since 5.5 |
565 | |
566 | Sets the image transformations metadata including orientation to \a transform. |
567 | |
568 | If transformation metadata is not supported by the image format, |
569 | the transform is applied before writing. |
570 | |
571 | \sa transformation(), write() |
572 | */ |
573 | void QImageWriter::setTransformation(QImageIOHandler::Transformations transform) |
574 | { |
575 | d->transformation = transform; |
576 | } |
577 | |
578 | /*! |
579 | \since 5.5 |
580 | |
581 | Returns the transformation and orientation the image has been set to written with. |
582 | |
583 | \sa setTransformation() |
584 | */ |
585 | QImageIOHandler::Transformations QImageWriter::transformation() const |
586 | { |
587 | return d->transformation; |
588 | } |
589 | |
590 | /*! |
591 | \since 4.1 |
592 | |
593 | Sets the image text associated with the key \a key to |
594 | \a text. This is useful for storing copyright information |
595 | or other information about the image. Example: |
596 | |
597 | \snippet code/src_gui_image_qimagewriter.cpp 1 |
598 | |
599 | If you want to store a single block of data |
600 | (e.g., a comment), you can pass an empty key, or use |
601 | a generic key like "Description". |
602 | |
603 | The key and text will be embedded into the |
604 | image data after calling write(). |
605 | |
606 | Support for this option is implemented through |
607 | QImageIOHandler::Description. |
608 | |
609 | \sa QImage::setText(), QImageReader::text() |
610 | */ |
611 | void QImageWriter::setText(const QString &key, const QString &text) |
612 | { |
613 | if (!d->description.isEmpty()) |
614 | d->description += "\n\n"_L1 ; |
615 | d->description += key.simplified() + ": "_L1 + text.simplified(); |
616 | } |
617 | |
618 | /*! |
619 | Returns \c true if QImageWriter can write the image; i.e., the image |
620 | format is supported and the assigned device is open for reading. |
621 | |
622 | \sa write(), setDevice(), setFormat() |
623 | */ |
624 | bool QImageWriter::canWrite() const |
625 | { |
626 | if (QFile *file = qobject_cast<QFile *>(object: d->device)) { |
627 | const bool remove = !file->isOpen() && !file->exists(); |
628 | const bool result = d->canWriteHelper(); |
629 | |
630 | // This looks strange (why remove if it doesn't exist?) but the issue |
631 | // here is that canWriteHelper will create the file in the process of |
632 | // checking if the write can succeed. If it subsequently fails, we |
633 | // should remove that empty file. |
634 | if (!result && remove) |
635 | file->remove(); |
636 | return result; |
637 | } |
638 | |
639 | return d->canWriteHelper(); |
640 | } |
641 | |
642 | extern void qt_imageTransform(QImage &src, QImageIOHandler::Transformations orient); |
643 | |
644 | /*! |
645 | Writes the image \a image to the assigned device or file |
646 | name. Returns \c true on success; otherwise returns \c false. If the |
647 | operation fails, you can call error() to find the type of error |
648 | that occurred, or errorString() to get a human readable |
649 | description of the error. |
650 | |
651 | \sa canWrite(), error(), errorString() |
652 | */ |
653 | bool QImageWriter::write(const QImage &image) |
654 | { |
655 | // Do this before canWrite, so it doesn't create a file if this fails. |
656 | if (Q_UNLIKELY(image.isNull())) { |
657 | d->imageWriterError = QImageWriter::InvalidImageError; |
658 | d->errorString = QImageWriter::tr(sourceText: "Image is empty" ); |
659 | return false; |
660 | } |
661 | |
662 | if (!canWrite()) |
663 | return false; |
664 | |
665 | QImage img = image; |
666 | if (d->handler->supportsOption(option: QImageIOHandler::Quality)) |
667 | d->handler->setOption(option: QImageIOHandler::Quality, value: d->quality); |
668 | if (d->handler->supportsOption(option: QImageIOHandler::CompressionRatio)) |
669 | d->handler->setOption(option: QImageIOHandler::CompressionRatio, value: d->compression); |
670 | if (d->handler->supportsOption(option: QImageIOHandler::Gamma)) |
671 | d->handler->setOption(option: QImageIOHandler::Gamma, value: d->gamma); |
672 | if (!d->description.isEmpty() && d->handler->supportsOption(option: QImageIOHandler::Description)) |
673 | d->handler->setOption(option: QImageIOHandler::Description, value: d->description); |
674 | if (!d->subType.isEmpty() && d->handler->supportsOption(option: QImageIOHandler::SubType)) |
675 | d->handler->setOption(option: QImageIOHandler::SubType, value: d->subType); |
676 | if (d->handler->supportsOption(option: QImageIOHandler::OptimizedWrite)) |
677 | d->handler->setOption(option: QImageIOHandler::OptimizedWrite, value: d->optimizedWrite); |
678 | if (d->handler->supportsOption(option: QImageIOHandler::ProgressiveScanWrite)) |
679 | d->handler->setOption(option: QImageIOHandler::ProgressiveScanWrite, value: d->progressiveScanWrite); |
680 | if (d->handler->supportsOption(option: QImageIOHandler::ImageTransformation)) |
681 | d->handler->setOption(option: QImageIOHandler::ImageTransformation, value: int(d->transformation)); |
682 | else |
683 | qt_imageTransform(src&: img, orient: d->transformation); |
684 | |
685 | if (!d->handler->write(image: img)) |
686 | return false; |
687 | if (QFileDevice *file = qobject_cast<QFileDevice *>(object: d->device)) |
688 | file->flush(); |
689 | return true; |
690 | } |
691 | |
692 | /*! |
693 | Returns the type of error that last occurred. |
694 | |
695 | \sa ImageWriterError, errorString() |
696 | */ |
697 | QImageWriter::ImageWriterError QImageWriter::error() const |
698 | { |
699 | return d->imageWriterError; |
700 | } |
701 | |
702 | /*! |
703 | Returns a human readable description of the last error that occurred. |
704 | |
705 | \sa error() |
706 | */ |
707 | QString QImageWriter::errorString() const |
708 | { |
709 | return d->errorString; |
710 | } |
711 | |
712 | /*! |
713 | \since 4.2 |
714 | |
715 | Returns \c true if the writer supports \a option; otherwise returns |
716 | false. |
717 | |
718 | Different image formats support different options. Call this function to |
719 | determine whether a certain option is supported by the current format. For |
720 | example, the PNG format allows you to embed text into the image's metadata |
721 | (see text()). |
722 | |
723 | \snippet code/src_gui_image_qimagewriter.cpp 2 |
724 | |
725 | Options can be tested after the writer has been associated with a format. |
726 | |
727 | \sa QImageReader::supportsOption(), setFormat() |
728 | */ |
729 | bool QImageWriter::supportsOption(QImageIOHandler::ImageOption option) const |
730 | { |
731 | if (!d->handler && (d->handler = createWriteHandlerHelper(device: d->device, format: d->format)) == nullptr) { |
732 | d->imageWriterError = QImageWriter::UnsupportedFormatError; |
733 | d->errorString = QImageWriter::tr(sourceText: "Unsupported image format" ); |
734 | return false; |
735 | } |
736 | |
737 | return d->handler->supportsOption(option); |
738 | } |
739 | |
740 | /*! |
741 | Returns the list of image formats supported by QImageWriter. |
742 | |
743 | By default, Qt can write the following formats: |
744 | |
745 | \table |
746 | \header \li Format \li MIME type \li Description |
747 | \row \li BMP \li image/bmp \li Windows Bitmap |
748 | \row \li JPG \li image/jpeg \li Joint Photographic Experts Group |
749 | \row \li PNG \li image/png \li Portable Network Graphics |
750 | \row \li PBM \li image/x-portable-bitmap \li Portable Bitmap |
751 | \row \li PGM \li image/x-portable-graymap \li Portable Graymap |
752 | \row \li PPM \li image/x-portable-pixmap \li Portable Pixmap |
753 | \row \li XBM \li image/x-xbitmap \li X11 Bitmap |
754 | \row \li XPM \li image/x-xpixmap \li X11 Pixmap |
755 | \endtable |
756 | |
757 | Reading and writing SVG files is supported through the \l{Qt SVG} module. |
758 | The \l{Qt Image Formats} module provides support for additional image formats. |
759 | |
760 | Note that the QApplication instance must be created before this function is |
761 | called. |
762 | |
763 | \sa setFormat(), QImageReader::supportedImageFormats(), QImageIOPlugin |
764 | */ |
765 | QList<QByteArray> QImageWriter::supportedImageFormats() |
766 | { |
767 | return QImageReaderWriterHelpers::supportedImageFormats(cap: QImageReaderWriterHelpers::CanWrite); |
768 | } |
769 | |
770 | /*! |
771 | Returns the list of MIME types supported by QImageWriter. |
772 | |
773 | Note that the QApplication instance must be created before this function is |
774 | called. |
775 | |
776 | \sa supportedImageFormats(), QImageReader::supportedMimeTypes() |
777 | */ |
778 | QList<QByteArray> QImageWriter::supportedMimeTypes() |
779 | { |
780 | return QImageReaderWriterHelpers::supportedMimeTypes(cap: QImageReaderWriterHelpers::CanWrite); |
781 | } |
782 | |
783 | /*! |
784 | \since 5.12 |
785 | |
786 | Returns the list of image formats corresponding to \a mimeType. |
787 | |
788 | Note that the QGuiApplication instance must be created before this function is |
789 | called. |
790 | |
791 | \sa supportedImageFormats(), supportedMimeTypes() |
792 | */ |
793 | |
794 | QList<QByteArray> QImageWriter::imageFormatsForMimeType(const QByteArray &mimeType) |
795 | { |
796 | return QImageReaderWriterHelpers::imageFormatsForMimeType(mimeType, |
797 | cap: QImageReaderWriterHelpers::CanWrite); |
798 | } |
799 | |
800 | QT_END_NAMESPACE |
801 | |