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