110 QImageIOHandler *handler =
nullptr;
112 qCDebug(lcImageWriter) <<
"Finding write handler for" << device <<
"and format" << format;
114#ifndef QT_NO_IMAGEFORMATPLUGIN
115 typedef QMultiMap<
int, QString> PluginKeyMap;
118 auto l = QImageReaderWriterHelpers::pluginLoader();
119 const PluginKeyMap keyMap = l->keyMap();
120 int suffixPluginIndex = -1;
122 qCDebug(lcImageWriter) << keyMap.uniqueKeys().size() <<
"plugins available:" << keyMap.values();
125 if (device && format.isEmpty()) {
129 if (QFileDevice *file = qobject_cast<QFileDevice *>(device)) {
130 if (!(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()) {
131 qCDebug(lcImageWriter) <<
"Resolved format" << suffix <<
"from file name suffix";
132#ifndef QT_NO_IMAGEFORMATPLUGIN
133 const int index = keyMap.key(QString::fromLatin1(suffix), -1);
135 suffixPluginIndex = index;
141 QByteArray testFormat = !form.isEmpty() ? form : suffix;
143#ifndef QT_NO_IMAGEFORMATPLUGIN
144 if (suffixPluginIndex != -1) {
147 qCDebug(lcImageWriter) <<
"Checking if any plugins have explicitly declared support"
148 <<
"for the format" << testFormat;
149 const int index = keyMap.key(QString::fromLatin1(suffix), -1);
151 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(index));
152 if (plugin && (plugin->capabilities(device, suffix) & QImageIOPlugin::CanWrite)) {
153 handler = plugin->create(device, suffix);
154 qCDebug(lcImageWriter) << plugin <<
"can write the format" << testFormat;
161 if (!handler && !testFormat.isEmpty()) {
162 qCDebug(lcImageWriter) <<
"Checking if any built in handlers recognize the format"
165#ifndef QT_NO_IMAGEFORMAT_PNG
166 }
else if (testFormat ==
"png") {
167 handler =
new QPngHandler;
169#ifndef QT_NO_IMAGEFORMAT_BMP
170 }
else if (testFormat ==
"bmp") {
171 handler =
new QBmpHandler;
172 }
else if (testFormat ==
"dib") {
173 handler =
new QBmpHandler(QBmpHandler::DibFormat);
175#ifndef QT_NO_IMAGEFORMAT_XPM
176 }
else if (testFormat ==
"xpm") {
177 handler =
new QXpmHandler;
179#ifndef QT_NO_IMAGEFORMAT_XBM
180 }
else if (testFormat ==
"xbm") {
181 handler =
new QXbmHandler;
182 handler->setOption(QImageIOHandler::SubType, testFormat);
184#ifndef QT_NO_IMAGEFORMAT_PPM
185 }
else if (testFormat ==
"pbm" || testFormat ==
"pbmraw" || testFormat ==
"pgm"
186 || testFormat ==
"pgmraw" || testFormat ==
"ppm" || testFormat ==
"ppmraw") {
187 handler =
new QPpmHandler;
188 handler->setOption(QImageIOHandler::SubType, testFormat);
193 qCDebug(lcImageWriter) <<
"Using the built-in handler for format" << testFormat;
196#ifndef QT_NO_IMAGEFORMATPLUGIN
197 if (!handler && !testFormat.isEmpty()) {
198 qCDebug(lcImageWriter) <<
"Checking if any plugins recognize the format" << testFormat;
199 const int keyCount = keyMap.size();
200 for (
int i = 0; i < keyCount; ++i) {
201 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(i));
202 if (plugin && (plugin->capabilities(device, testFormat) & QImageIOPlugin::CanWrite)) {
203 handler = plugin->create(device, testFormat);
204 qCDebug(lcImageWriter) << plugin <<
"can write the format" << testFormat;
212 qCDebug(lcImageWriter,
"No handlers found. Giving up.");
216 handler->setDevice(device);
217 if (!testFormat.isEmpty())
218 handler->setFormat(testFormat);
276 imageWriterError = QImageWriter::DeviceError;
277 errorString = QImageWriter::tr(
"Device is not set");
281 if (!
device->open(QIODevice::WriteOnly)) {
282 imageWriterError = QImageWriter::DeviceError;
283 errorString = QImageWriter::tr(
"Cannot open device for writing: %1").arg(device->errorString());
287 if (!
device->isWritable()) {
288 imageWriterError = QImageWriter::DeviceError;
289 errorString = QImageWriter::tr(
"Device not writable");
292 if (!handler && (handler = createWriteHandlerHelper(device, format)) ==
nullptr) {
293 imageWriterError = QImageWriter::UnsupportedFormatError;
294 errorString = QImageWriter::tr(
"Unsupported image format");
694bool QImageWriter::write(
const QImage &image)
697 if (Q_UNLIKELY(image.isNull())) {
698 d->imageWriterError = QImageWriter::InvalidImageError;
699 d->errorString = QImageWriter::tr(
"Image is empty");
707 if (d->handler->supportsOption(QImageIOHandler::Quality))
708 d->handler->setOption(QImageIOHandler::Quality, d->quality);
709 if (d->handler->supportsOption(QImageIOHandler::CompressionRatio))
710 d->handler->setOption(QImageIOHandler::CompressionRatio, d->compression);
711 if (d->handler->supportsOption(QImageIOHandler::Gamma))
712 d->handler->setOption(QImageIOHandler::Gamma, d->gamma);
713 if (!d->description.isEmpty() && d->handler->supportsOption(QImageIOHandler::Description))
714 d->handler->setOption(QImageIOHandler::Description, d->description);
715 if (!d->subType.isEmpty() && d->handler->supportsOption(QImageIOHandler::SubType))
716 d->handler->setOption(QImageIOHandler::SubType, d->subType);
717 if (d->handler->supportsOption(QImageIOHandler::OptimizedWrite))
718 d->handler->setOption(QImageIOHandler::OptimizedWrite, d->optimizedWrite);
719 if (d->handler->supportsOption(QImageIOHandler::ProgressiveScanWrite))
720 d->handler->setOption(QImageIOHandler::ProgressiveScanWrite, d->progressiveScanWrite);
721 if (d->handler->supportsOption(QImageIOHandler::ImageTransformation))
722 d->handler->setOption(QImageIOHandler::ImageTransformation,
int(d->transformation));
724 qt_imageTransform(img, d->transformation);
726 if (!d->handler->write(img))
728 if (QFileDevice *file = qobject_cast<QFileDevice *>(d->device))
768bool QImageWriter::supportsOption(QImageIOHandler::ImageOption option)
const
770 if (!d->handler && (d->handler = createWriteHandlerHelper(d->device, d->format)) ==
nullptr) {
771 d->imageWriterError = QImageWriter::UnsupportedFormatError;
772 d->errorString = QImageWriter::tr(
"Unsupported image format");
776 return d->handler->supportsOption(option);