109 QImageIOHandler *handler =
nullptr;
111 qCDebug(lcImageWriter) <<
"Finding write handler for" << device <<
"and format" << format;
113#ifndef QT_NO_IMAGEFORMATPLUGIN
114 typedef QMultiMap<
int, QString> PluginKeyMap;
117 auto l = QImageReaderWriterHelpers::pluginLoader();
118 const PluginKeyMap keyMap = l->keyMap();
119 int suffixPluginIndex = -1;
121 qCDebug(lcImageWriter) << keyMap.uniqueKeys().size() <<
"plugins available:" << keyMap.values();
124 if (device && format.isEmpty()) {
128 if (QFileDevice *file = qobject_cast<QFileDevice *>(device)) {
129 if (!(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()) {
130 qCDebug(lcImageWriter) <<
"Resolved format" << suffix <<
"from file name suffix";
131#ifndef QT_NO_IMAGEFORMATPLUGIN
132 const int index = keyMap.key(QString::fromLatin1(suffix), -1);
134 suffixPluginIndex = index;
140 QByteArray testFormat = !form.isEmpty() ? form : suffix;
142#ifndef QT_NO_IMAGEFORMATPLUGIN
143 if (suffixPluginIndex != -1) {
146 qCDebug(lcImageWriter) <<
"Checking if any plugins have explicitly declared support"
147 <<
"for the format" << testFormat;
148 const int index = keyMap.key(QString::fromLatin1(suffix), -1);
150 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(index));
151 if (plugin && (plugin->capabilities(device, suffix) & QImageIOPlugin::CanWrite)) {
152 handler = plugin->create(device, suffix);
153 qCDebug(lcImageWriter) << plugin <<
"can write the format" << testFormat;
160 if (!handler && !testFormat.isEmpty()) {
161 qCDebug(lcImageWriter) <<
"Checking if any built in handlers recognize the format"
164#ifndef QT_NO_IMAGEFORMAT_PNG
165 }
else if (testFormat ==
"png") {
166 handler =
new QPngHandler;
168#ifndef QT_NO_IMAGEFORMAT_BMP
169 }
else if (testFormat ==
"bmp") {
170 handler =
new QBmpHandler;
171 }
else if (testFormat ==
"dib") {
172 handler =
new QBmpHandler(QBmpHandler::DibFormat);
174#ifndef QT_NO_IMAGEFORMAT_XPM
175 }
else if (testFormat ==
"xpm") {
176 handler =
new QXpmHandler;
178#ifndef QT_NO_IMAGEFORMAT_XBM
179 }
else if (testFormat ==
"xbm") {
180 handler =
new QXbmHandler;
181 handler->setOption(QImageIOHandler::SubType, testFormat);
183#ifndef QT_NO_IMAGEFORMAT_PPM
184 }
else if (testFormat ==
"pbm" || testFormat ==
"pbmraw" || testFormat ==
"pgm"
185 || testFormat ==
"pgmraw" || testFormat ==
"ppm" || testFormat ==
"ppmraw") {
186 handler =
new QPpmHandler;
187 handler->setOption(QImageIOHandler::SubType, testFormat);
192 qCDebug(lcImageWriter) <<
"Using the built-in handler for format" << testFormat;
195#ifndef QT_NO_IMAGEFORMATPLUGIN
196 if (!handler && !testFormat.isEmpty()) {
197 qCDebug(lcImageWriter) <<
"Checking if any plugins recognize the format" << testFormat;
198 const int keyCount = keyMap.size();
199 for (
int i = 0; i < keyCount; ++i) {
200 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(i));
201 if (plugin && (plugin->capabilities(device, testFormat) & QImageIOPlugin::CanWrite)) {
202 handler = plugin->create(device, testFormat);
203 qCDebug(lcImageWriter) << plugin <<
"can write the format" << testFormat;
211 qCDebug(lcImageWriter,
"No handlers found. Giving up.");
215 handler->setDevice(device);
216 if (!testFormat.isEmpty())
217 handler->setFormat(testFormat);
275 imageWriterError = QImageWriter::DeviceError;
276 errorString = QImageWriter::tr(
"Device is not set");
280 if (!device->open(QIODevice::WriteOnly)) {
281 imageWriterError = QImageWriter::DeviceError;
282 errorString = QImageWriter::tr(
"Cannot open device for writing: %1").arg(device->errorString());
286 if (!
device->isWritable()) {
287 imageWriterError = QImageWriter::DeviceError;
288 errorString = QImageWriter::tr(
"Device not writable");
291 if (!handler && (handler = createWriteHandlerHelper(device, format)) ==
nullptr) {
292 imageWriterError = QImageWriter::UnsupportedFormatError;
293 errorString = QImageWriter::tr(
"Unsupported image format");
671bool QImageWriter::write(
const QImage &image)
674 if (Q_UNLIKELY(image.isNull())) {
675 d->imageWriterError = QImageWriter::InvalidImageError;
676 d->errorString = QImageWriter::tr(
"Image is empty");
684 if (d->handler->supportsOption(QImageIOHandler::Quality))
685 d->handler->setOption(QImageIOHandler::Quality, d->quality);
686 if (d->handler->supportsOption(QImageIOHandler::CompressionRatio))
687 d->handler->setOption(QImageIOHandler::CompressionRatio, d->compression);
688 if (d->handler->supportsOption(QImageIOHandler::Gamma))
689 d->handler->setOption(QImageIOHandler::Gamma, d->gamma);
690 if (!d->description.isEmpty() && d->handler->supportsOption(QImageIOHandler::Description))
691 d->handler->setOption(QImageIOHandler::Description, d->description);
692 if (!d->subType.isEmpty() && d->handler->supportsOption(QImageIOHandler::SubType))
693 d->handler->setOption(QImageIOHandler::SubType, d->subType);
694 if (d->handler->supportsOption(QImageIOHandler::OptimizedWrite))
695 d->handler->setOption(QImageIOHandler::OptimizedWrite, d->optimizedWrite);
696 if (d->handler->supportsOption(QImageIOHandler::ProgressiveScanWrite))
697 d->handler->setOption(QImageIOHandler::ProgressiveScanWrite, d->progressiveScanWrite);
698 if (d->handler->supportsOption(QImageIOHandler::ImageTransformation))
699 d->handler->setOption(QImageIOHandler::ImageTransformation,
int(d->transformation));
701 qt_imageTransform(img, d->transformation);
703 if (!d->handler->write(img))
705 if (QFileDevice *file = qobject_cast<QFileDevice *>(d->device))
745bool QImageWriter::supportsOption(QImageIOHandler::ImageOption option)
const
747 if (!d->handler && (d->handler = createWriteHandlerHelper(d->device, d->format)) ==
nullptr) {
748 d->imageWriterError = QImageWriter::UnsupportedFormatError;
749 d->errorString = QImageWriter::tr(
"Unsupported image format");
753 return d->handler->supportsOption(option);