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