| 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 QIODeviceBase::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 | Sets the image text associated with the key \a key to |
| 592 | \a text. This is useful for storing copyright information |
| 593 | or other information about the image. Example: |
| 594 | |
| 595 | \snippet code/src_gui_image_qimagewriter.cpp 1 |
| 596 | |
| 597 | If you want to store a single block of data |
| 598 | (e.g., a comment), you can pass an empty key, or use |
| 599 | a generic key like "Description". |
| 600 | |
| 601 | The key and text will be embedded into the |
| 602 | image data after calling write(). |
| 603 | |
| 604 | Support for this option is implemented through |
| 605 | QImageIOHandler::Description. |
| 606 | |
| 607 | \sa QImage::setText(), QImageReader::text() |
| 608 | */ |
| 609 | void QImageWriter::setText(const QString &key, const QString &text) |
| 610 | { |
| 611 | if (!d->description.isEmpty()) |
| 612 | d->description += "\n\n"_L1 ; |
| 613 | d->description += key.simplified() + ": "_L1 + text.simplified(); |
| 614 | } |
| 615 | |
| 616 | /*! |
| 617 | Returns \c true if QImageWriter can write the image; i.e., the image |
| 618 | format is supported and the assigned device is open for reading. |
| 619 | |
| 620 | \sa write(), setDevice(), setFormat() |
| 621 | */ |
| 622 | bool QImageWriter::canWrite() const |
| 623 | { |
| 624 | if (QFile *file = qobject_cast<QFile *>(object: d->device)) { |
| 625 | const bool remove = !file->isOpen() && !file->exists(); |
| 626 | const bool result = d->canWriteHelper(); |
| 627 | |
| 628 | // This looks strange (why remove if it doesn't exist?) but the issue |
| 629 | // here is that canWriteHelper will create the file in the process of |
| 630 | // checking if the write can succeed. If it subsequently fails, we |
| 631 | // should remove that empty file. |
| 632 | if (!result && remove) |
| 633 | file->remove(); |
| 634 | return result; |
| 635 | } |
| 636 | |
| 637 | return d->canWriteHelper(); |
| 638 | } |
| 639 | |
| 640 | extern void qt_imageTransform(QImage &src, QImageIOHandler::Transformations orient); |
| 641 | |
| 642 | /*! |
| 643 | Writes the image \a image to the assigned device or file |
| 644 | name. Returns \c true on success; otherwise returns \c false. If the |
| 645 | operation fails, you can call error() to find the type of error |
| 646 | that occurred, or errorString() to get a human readable |
| 647 | description of the error. |
| 648 | |
| 649 | \sa canWrite(), error(), errorString() |
| 650 | */ |
| 651 | bool QImageWriter::write(const QImage &image) |
| 652 | { |
| 653 | // Do this before canWrite, so it doesn't create a file if this fails. |
| 654 | if (Q_UNLIKELY(image.isNull())) { |
| 655 | d->imageWriterError = QImageWriter::InvalidImageError; |
| 656 | d->errorString = QImageWriter::tr(sourceText: "Image is empty" ); |
| 657 | return false; |
| 658 | } |
| 659 | |
| 660 | if (!canWrite()) |
| 661 | return false; |
| 662 | |
| 663 | QImage img = image; |
| 664 | if (d->handler->supportsOption(option: QImageIOHandler::Quality)) |
| 665 | d->handler->setOption(option: QImageIOHandler::Quality, value: d->quality); |
| 666 | if (d->handler->supportsOption(option: QImageIOHandler::CompressionRatio)) |
| 667 | d->handler->setOption(option: QImageIOHandler::CompressionRatio, value: d->compression); |
| 668 | if (d->handler->supportsOption(option: QImageIOHandler::Gamma)) |
| 669 | d->handler->setOption(option: QImageIOHandler::Gamma, value: d->gamma); |
| 670 | if (!d->description.isEmpty() && d->handler->supportsOption(option: QImageIOHandler::Description)) |
| 671 | d->handler->setOption(option: QImageIOHandler::Description, value: d->description); |
| 672 | if (!d->subType.isEmpty() && d->handler->supportsOption(option: QImageIOHandler::SubType)) |
| 673 | d->handler->setOption(option: QImageIOHandler::SubType, value: d->subType); |
| 674 | if (d->handler->supportsOption(option: QImageIOHandler::OptimizedWrite)) |
| 675 | d->handler->setOption(option: QImageIOHandler::OptimizedWrite, value: d->optimizedWrite); |
| 676 | if (d->handler->supportsOption(option: QImageIOHandler::ProgressiveScanWrite)) |
| 677 | d->handler->setOption(option: QImageIOHandler::ProgressiveScanWrite, value: d->progressiveScanWrite); |
| 678 | if (d->handler->supportsOption(option: QImageIOHandler::ImageTransformation)) |
| 679 | d->handler->setOption(option: QImageIOHandler::ImageTransformation, value: int(d->transformation)); |
| 680 | else |
| 681 | qt_imageTransform(src&: img, orient: d->transformation); |
| 682 | |
| 683 | if (!d->handler->write(image: img)) |
| 684 | return false; |
| 685 | if (QFileDevice *file = qobject_cast<QFileDevice *>(object: d->device)) |
| 686 | file->flush(); |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | /*! |
| 691 | Returns the type of error that last occurred. |
| 692 | |
| 693 | \sa ImageWriterError, errorString() |
| 694 | */ |
| 695 | QImageWriter::ImageWriterError QImageWriter::error() const |
| 696 | { |
| 697 | return d->imageWriterError; |
| 698 | } |
| 699 | |
| 700 | /*! |
| 701 | Returns a human readable description of the last error that occurred. |
| 702 | |
| 703 | \sa error() |
| 704 | */ |
| 705 | QString QImageWriter::errorString() const |
| 706 | { |
| 707 | return d->errorString; |
| 708 | } |
| 709 | |
| 710 | /*! |
| 711 | Returns \c true if the writer supports \a option; otherwise returns |
| 712 | false. |
| 713 | |
| 714 | Different image formats support different options. Call this function to |
| 715 | determine whether a certain option is supported by the current format. For |
| 716 | example, the PNG format allows you to embed text into the image's metadata |
| 717 | (see text()). |
| 718 | |
| 719 | \snippet code/src_gui_image_qimagewriter.cpp 2 |
| 720 | |
| 721 | Options can be tested after the writer has been associated with a format. |
| 722 | |
| 723 | \sa QImageReader::supportsOption(), setFormat() |
| 724 | */ |
| 725 | bool QImageWriter::supportsOption(QImageIOHandler::ImageOption option) const |
| 726 | { |
| 727 | if (!d->handler && (d->handler = createWriteHandlerHelper(device: d->device, format: d->format)) == nullptr) { |
| 728 | d->imageWriterError = QImageWriter::UnsupportedFormatError; |
| 729 | d->errorString = QImageWriter::tr(sourceText: "Unsupported image format" ); |
| 730 | return false; |
| 731 | } |
| 732 | |
| 733 | return d->handler->supportsOption(option); |
| 734 | } |
| 735 | |
| 736 | /*! |
| 737 | Returns the list of image formats supported by QImageWriter. |
| 738 | |
| 739 | By default, Qt can write the following formats: |
| 740 | |
| 741 | \table |
| 742 | \header \li Format \li MIME type \li Description |
| 743 | \row \li BMP \li image/bmp \li Windows Bitmap |
| 744 | \row \li JPG \li image/jpeg \li Joint Photographic Experts Group |
| 745 | \row \li PNG \li image/png \li Portable Network Graphics |
| 746 | \row \li PBM \li image/x-portable-bitmap \li Portable Bitmap |
| 747 | \row \li PGM \li image/x-portable-graymap \li Portable Graymap |
| 748 | \row \li PPM \li image/x-portable-pixmap \li Portable Pixmap |
| 749 | \row \li XBM \li image/x-xbitmap \li X11 Bitmap |
| 750 | \row \li XPM \li image/x-xpixmap \li X11 Pixmap |
| 751 | \endtable |
| 752 | |
| 753 | Reading and writing SVG files is supported through the \l{Qt SVG} module. |
| 754 | The \l{Qt Image Formats} module provides support for additional image formats. |
| 755 | |
| 756 | Note that the QApplication instance must be created before this function is |
| 757 | called. |
| 758 | |
| 759 | \sa setFormat(), QImageReader::supportedImageFormats(), QImageIOPlugin |
| 760 | */ |
| 761 | QList<QByteArray> QImageWriter::supportedImageFormats() |
| 762 | { |
| 763 | return QImageReaderWriterHelpers::supportedImageFormats(cap: QImageReaderWriterHelpers::CanWrite); |
| 764 | } |
| 765 | |
| 766 | /*! |
| 767 | Returns the list of MIME types supported by QImageWriter. |
| 768 | |
| 769 | Note that the QApplication instance must be created before this function is |
| 770 | called. |
| 771 | |
| 772 | \sa supportedImageFormats(), QImageReader::supportedMimeTypes() |
| 773 | */ |
| 774 | QList<QByteArray> QImageWriter::supportedMimeTypes() |
| 775 | { |
| 776 | return QImageReaderWriterHelpers::supportedMimeTypes(cap: QImageReaderWriterHelpers::CanWrite); |
| 777 | } |
| 778 | |
| 779 | /*! |
| 780 | \since 5.12 |
| 781 | |
| 782 | Returns the list of image formats corresponding to \a mimeType. |
| 783 | |
| 784 | Note that the QGuiApplication instance must be created before this function is |
| 785 | called. |
| 786 | |
| 787 | \sa supportedImageFormats(), supportedMimeTypes() |
| 788 | */ |
| 789 | |
| 790 | QList<QByteArray> QImageWriter::imageFormatsForMimeType(const QByteArray &mimeType) |
| 791 | { |
| 792 | return QImageReaderWriterHelpers::imageFormatsForMimeType(mimeType, |
| 793 | cap: QImageReaderWriterHelpers::CanWrite); |
| 794 | } |
| 795 | |
| 796 | QT_END_NAMESPACE |
| 797 | |