qimagewriter.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/image/qimagewriter.cpp
Switch to Source codePreprocessed file
LineSourceCount
1-
2-
3-
4-
5-
6-
7-
8-
9-
10namespace { namespace Q_QGS_loader { typedef QFactoryLoader Type; QBasicAtomicInt guard = { QtGlobalStatic::Uninitialized }; __attribute__((visibility("hidden"))) inline Type *innerFunction() { struct HolderBase { ~HolderBase() noexcept { if (guard.load() == QtGlobalStatic::Initialized) guard.store(QtGlobalStatic::Destroyed); } }; static struct Holder : public HolderBase { Type value; Holder() noexcept(noexcept(Type ("org.qt-project.Qt.QImageIOHandlerFactoryInterface", QLatin1String("/imageformats")))) : value ("org.qt-project.Qt.QImageIOHandlerFactoryInterface", QLatin1String("/imageformats")) { guard.store(QtGlobalStatic::Initialized); } } holder; return &holder.value; } } } static QGlobalStatic<QFactoryLoader, Q_QGS_loader::innerFunction, Q_QGS_loader::guard> loader;-
11-
12-
13-
14static QImageIOHandler *createWriteHandlerHelper(QIODevice *device,-
15 const QByteArray &format)-
16{-
17 QByteArray form = format.toLower();-
18 QByteArray suffix;-
19 QImageIOHandler *handler = 0;-
20-
21-
22 typedef QMultiMap<int, QString> PluginKeyMap;-
23-
24-
25 QFactoryLoader *l = loader();-
26 const PluginKeyMap keyMap = l->keyMap();-
27 int suffixPluginIndex = -1;-
28-
29-
30 if (device && format.isEmpty()) {-
31-
32-
33-
34 if (QFile *file = qobject_cast<QFile *>(device)) {-
35 if (!(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()) {-
36-
37 const int index = keyMap.key(QString::fromLatin1(suffix), -1);-
38 if (index != -1)-
39 suffixPluginIndex = index;-
40-
41 }-
42 }-
43 }-
44-
45 QByteArray testFormat = !form.isEmpty() ? form : suffix;-
46-
47-
48 if (suffixPluginIndex != -1) {-
49-
50-
51 const int index = keyMap.key(QString::fromLatin1(suffix), -1);-
52 if (index != -1) {-
53 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(index));-
54 if (plugin && (plugin->capabilities(device, suffix) & QImageIOPlugin::CanWrite))-
55 handler = plugin->create(device, suffix);-
56 }-
57 }-
58-
59-
60-
61 if (!handler && !testFormat.isEmpty()) {-
62 if (false) {
dead code: { }
-
63-
64 }
dead code: { }
else if (testFormat == "png") {
-
65 handler = new QPngHandler;-
66 } else if (testFormat == "bmp") {-
67 handler = new QBmpHandler;-
68 } else if (testFormat == "dib") {-
69 handler = new QBmpHandler(QBmpHandler::DibFormat);-
70-
71-
72 } else if (testFormat == "xpm") {-
73 handler = new QXpmHandler;-
74-
75-
76 } else if (testFormat == "xbm") {-
77 handler = new QXbmHandler;-
78 handler->setOption(QImageIOHandler::SubType, testFormat);-
79-
80-
81 } else if (testFormat == "pbm" || testFormat == "pbmraw" || testFormat == "pgm"-
82 || testFormat == "pgmraw" || testFormat == "ppm" || testFormat == "ppmraw") {-
83 handler = new QPpmHandler;-
84 handler->setOption(QImageIOHandler::SubType, testFormat);-
85-
86 }-
87 }-
88-
89-
90 if (!testFormat.isEmpty()) {-
91 const int keyCount = keyMap.size();-
92 for (int i = 0; i < keyCount; ++i) {-
93 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(i));-
94 if (plugin && (plugin->capabilities(device, testFormat) & QImageIOPlugin::CanWrite)) {-
95 delete handler;-
96 handler = plugin->create(device, testFormat);-
97 break;-
98 }-
99 }-
100 }-
101-
102-
103 if (!handler)-
104 return 0;-
105-
106 handler->setDevice(device);-
107 if (!testFormat.isEmpty())-
108 handler->setFormat(testFormat);-
109 return handler;-
110}-
111-
112class QImageWriterPrivate-
113{-
114public:-
115 QImageWriterPrivate(QImageWriter *qq);-
116-
117 bool canWriteHelper();-
118-
119-
120 QByteArray format;-
121 QIODevice *device;-
122 bool deleteDevice;-
123 QImageIOHandler *handler;-
124-
125-
126 int quality;-
127 int compression;-
128 float gamma;-
129 QString description;-
130 QString text;-
131 QByteArray subType;-
132 bool optimizedWrite;-
133 bool progressiveScanWrite;-
134 QImageIOHandler::Transformations transformation;-
135-
136-
137 QImageWriter::ImageWriterError imageWriterError;-
138 QString errorString;-
139-
140 QImageWriter *q;-
141};-
142-
143-
144-
145-
146QImageWriterPrivate::QImageWriterPrivate(QImageWriter *qq)-
147{-
148 device = 0;-
149 deleteDevice = false;-
150 handler = 0;-
151 quality = -1;-
152 compression = 0;-
153 gamma = 0.0;-
154 optimizedWrite = false;-
155 progressiveScanWrite = false;-
156 imageWriterError = QImageWriter::UnknownError;-
157 errorString = QImageWriter::tr("Unknown error");-
158 transformation = QImageIOHandler::TransformationNone;-
159-
160 q = qq;-
161}-
162-
163bool QImageWriterPrivate::canWriteHelper()-
164{-
165 if (!device) {-
166 imageWriterError = QImageWriter::DeviceError;-
167 errorString = QImageWriter::tr("Device is not set");-
168 return false;-
169 }-
170 if (!device->isOpen())-
171 device->open(QIODevice::WriteOnly);-
172 if (!device->isWritable()) {-
173 imageWriterError = QImageWriter::DeviceError;-
174 errorString = QImageWriter::tr("Device not writable");-
175 return false;-
176 }-
177 if (!handler && (handler = createWriteHandlerHelper(device, format)) == 0) {-
178 imageWriterError = QImageWriter::UnsupportedFormatError;-
179 errorString = QImageWriter::tr("Unsupported image format");-
180 return false;-
181 }-
182 return true;-
183}-
184-
185-
186-
187-
188-
189-
190QImageWriter::QImageWriter()-
191 : d(new QImageWriterPrivate(this))-
192{-
193}-
194-
195-
196-
197-
198-
199QImageWriter::QImageWriter(QIODevice *device, const QByteArray &format)-
200 : d(new QImageWriterPrivate(this))-
201{-
202 d->device = device;-
203 d->format = format;-
204}-
205-
206-
207-
208-
209-
210-
211-
212QImageWriter::QImageWriter(const QString &fileName, const QByteArray &format)-
213 : d(new QImageWriterPrivate(this))-
214{-
215 QFile *file = new QFile(fileName);-
216 d->device = file;-
217 d->deleteDevice = true;-
218 d->format = format;-
219}-
220-
221-
222-
223-
224QImageWriter::~QImageWriter()-
225{-
226 if (d->deleteDevice)-
227 delete d->device;-
228 delete d->handler;-
229 delete d;-
230}-
231void QImageWriter::setFormat(const QByteArray &format)-
232{-
233 d->format = format;-
234}-
235-
236-
237-
238-
239-
240-
241QByteArray QImageWriter::format() const-
242{-
243 return d->format;-
244}-
245void QImageWriter::setDevice(QIODevice *device)-
246{-
247 if (d->device && d->deleteDevice)-
248 delete d->device;-
249-
250 d->device = device;-
251 d->deleteDevice = false;-
252 delete d->handler;-
253 d->handler = 0;-
254}-
255-
256-
257-
258-
259-
260QIODevice *QImageWriter::device() const-
261{-
262 return d->device;-
263}-
264void QImageWriter::setFileName(const QString &fileName)-
265{-
266 setDevice(new QFile(fileName));-
267 d->deleteDevice = true;-
268}-
269QString QImageWriter::fileName() const-
270{-
271 QFile *file = qobject_cast<QFile *>(d->device);-
272 return file ? file->fileName() : QString();-
273}-
274void QImageWriter::setQuality(int quality)-
275{-
276 d->quality = quality;-
277}-
278-
279-
280-
281-
282-
283-
284int QImageWriter::quality() const-
285{-
286 return d->quality;-
287}-
288void QImageWriter::setCompression(int compression)-
289{-
290 d->compression = compression;-
291}-
292-
293-
294-
295-
296-
297-
298int QImageWriter::compression() const-
299{-
300 return d->compression;-
301}-
302void QImageWriter::setGamma(float gamma)-
303{-
304 d->gamma = gamma;-
305}-
306-
307-
308-
309-
310-
311-
312float QImageWriter::gamma() const-
313{-
314 return d->gamma;-
315}-
316void QImageWriter::setSubType(const QByteArray &type)-
317{-
318 d->subType = type;-
319}-
320QByteArray QImageWriter::subType() const-
321{-
322 return d->subType;-
323}-
324-
325-
326-
327-
328-
329-
330QList<QByteArray> QImageWriter::supportedSubTypes() const-
331{-
332 if (!supportsOption(QImageIOHandler::SupportedSubTypes))-
333 return QList<QByteArray>();-
334 return d->handler->option(QImageIOHandler::SupportedSubTypes).value< QList<QByteArray> >();-
335}-
336void QImageWriter::setOptimizedWrite(bool optimize)-
337{-
338 d->optimizedWrite = optimize;-
339}-
340bool QImageWriter::optimizedWrite() const-
341{-
342 return d->optimizedWrite;-
343}-
344void QImageWriter::setProgressiveScanWrite(bool progressive)-
345{-
346 d->progressiveScanWrite = progressive;-
347}-
348bool QImageWriter::progressiveScanWrite() const-
349{-
350 return d->progressiveScanWrite;-
351}-
352void QImageWriter::setTransformation(QImageIOHandler::Transformations transform)-
353{-
354 d->transformation = transform;-
355}-
356QImageIOHandler::Transformations QImageWriter::transformation() const-
357{-
358 return d->transformation;-
359}-
360void QImageWriter::setDescription(const QString &description)-
361{-
362 d->description = description;-
363}-
364QString QImageWriter::description() const-
365{-
366 return d->description;-
367}-
368void QImageWriter::setText(const QString &key, const QString &text)-
369{-
370 if (!d->description.isEmpty())-
371 d->description += QLatin1String("\n\n");-
372 d->description += key.simplified() + QLatin1String(": ") + text.simplified();-
373}-
374-
375-
376-
377-
378-
379-
380-
381bool QImageWriter::canWrite() const-
382{-
383 if (QFile *file = qobject_cast<QFile *>(d->device)) {-
384 const bool remove = !file->isOpen() && !file->exists();-
385 const bool result = d->canWriteHelper();-
386 if (!result && remove)-
387 file->remove();-
388 return result;-
389 }-
390-
391 return d->canWriteHelper();-
392}-
393-
394extern void qt_imageTransform(QImage &src, QImageIOHandler::Transformations orient);-
395bool QImageWriter::write(const QImage &image)-
396{-
397 if (!canWrite())-
398 return false;-
399-
400 QImage img = image;-
401 if (d->handler->supportsOption(QImageIOHandler::Quality))-
402 d->handler->setOption(QImageIOHandler::Quality, d->quality);-
403 if (d->handler->supportsOption(QImageIOHandler::CompressionRatio))-
404 d->handler->setOption(QImageIOHandler::CompressionRatio, d->compression);-
405 if (d->handler->supportsOption(QImageIOHandler::Gamma))-
406 d->handler->setOption(QImageIOHandler::Gamma, d->gamma);-
407 if (!d->description.isEmpty() && d->handler->supportsOption(QImageIOHandler::Description))-
408 d->handler->setOption(QImageIOHandler::Description, d->description);-
409 if (!d->subType.isEmpty() && d->handler->supportsOption(QImageIOHandler::SubType))-
410 d->handler->setOption(QImageIOHandler::SubType, d->subType);-
411 if (d->handler->supportsOption(QImageIOHandler::OptimizedWrite))-
412 d->handler->setOption(QImageIOHandler::OptimizedWrite, d->optimizedWrite);-
413 if (d->handler->supportsOption(QImageIOHandler::ProgressiveScanWrite))-
414 d->handler->setOption(QImageIOHandler::ProgressiveScanWrite, d->progressiveScanWrite);-
415 if (d->handler->supportsOption(QImageIOHandler::ImageTransformation))-
416 d->handler->setOption(QImageIOHandler::ImageTransformation, int(d->transformation));-
417 else-
418 qt_imageTransform(img, d->transformation);-
419-
420 if (!d->handler->write(img))-
421 return false;-
422 if (QFile *file = qobject_cast<QFile *>(d->device))-
423 file->flush();-
424 return true;-
425}-
426-
427-
428-
429-
430-
431-
432QImageWriter::ImageWriterError QImageWriter::error() const-
433{-
434 return d->imageWriterError;-
435}-
436-
437-
438-
439-
440-
441-
442QString QImageWriter::errorString() const-
443{-
444 return d->errorString;-
445}-
446bool QImageWriter::supportsOption(QImageIOHandler::ImageOption option) const-
447{-
448 if (!d->handler && (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0) {-
449 d->imageWriterError = QImageWriter::UnsupportedFormatError;-
450 d->errorString = QImageWriter::tr("Unsupported image format");-
451 return false;-
452 }-
453-
454 return d->handler->supportsOption(option);-
455}-
456-
457-
458-
459void supportedImageHandlerFormats(QFactoryLoader *loader,-
460 QImageIOPlugin::Capability cap,-
461 QList<QByteArray> *result)-
462{-
463 typedef QMultiMap<int, QString> PluginKeyMap;-
464 typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator;-
465-
466 const PluginKeyMap keyMap = loader->keyMap();-
467 const PluginKeyMapConstIterator cend = keyMap.constEnd();-
468 int i = -1;-
469 QImageIOPlugin *plugin = 0;-
470 result->reserve(result->size() + keyMap.size());-
471 for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it) {-
472 if (it.key() != i) {-
473 i = it.key();-
474 plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i));-
475 }-
476 const QByteArray key = it.value().toLatin1();-
477 if (plugin && (plugin->capabilities(0, key) & cap) != 0)-
478 result->append(key);-
479 }-
480}-
481-
482void supportedImageHandlerMimeTypes(QFactoryLoader *loader,-
483 QImageIOPlugin::Capability cap,-
484 QList<QByteArray> *result)-
485{-
486 QList<QJsonObject> metaDataList = loader->metaData();-
487-
488 const int pluginCount = metaDataList.size();-
489 for (int i = 0; i < pluginCount
i < pluginCountDescription
TRUEnever evaluated
FALSEnever evaluated
; ++i) {
0
490 const QJsonObject metaData = metaDataList.at(i).value(([]() -> QString { enum { Size = sizeof(u"" "MetaData")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeofQLatin1String(QStringData) }, u"" "MetaData"}; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }())).)).toObject();-
491 const QJsonArray keys = metaData.value(([]() -> QString { enum { Size = sizeof(u"" "Keys")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeofQLatin1String(QStringData) }, u"" "Keys"}; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }())).)).toArray();-
492 const QJsonArray mimeTypes = metaData.value(([]() -> QString { enum { Size = sizeof(u"" "MimeTypes")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeofQLatin1String(QStringData) }, u"" "MimeTypes"}; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }())).)).toArray();-
493 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i));-
494 const int keyCount = keys.size();-
495 for (int k = 0; k < keyCount
k < keyCountDescription
TRUEnever evaluated
FALSEnever evaluated
; ++k) {
0
496 if (plugin
pluginDescription
TRUEnever evaluated
FALSEnever evaluated
&& (
(plugin->capab...)) & cap) != 0Description
TRUEnever evaluated
FALSEnever evaluated
plugin->capabilities(0, keys.at(k).toString().toLatin1()) & cap) != 0
(plugin->capab...)) & cap) != 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
497 result->append(mimeTypes.at(k).toString().toLatin1());
never executed: result->append(mimeTypes.at(k).toString().toLatin1());
0
498 }
never executed: end of block
0
499 }
never executed: end of block
0
500}
never executed: end of block
0
501QList<QByteArray> QImageWriter::supportedImageFormats()-
502{-
503 QList<QByteArray> formats;-
504-
505 formats << "bmp";-
506-
507-
508 formats << "pbm" << "pgm" << "ppm";-
509-
510-
511 formats << "xbm";-
512-
513-
514 formats << "xpm";-
515-
516-
517 formats << "png";-
518-
519-
520-
521-
522-
523-
524 supportedImageHandlerFormats(loader(), QImageIOPlugin::CanWrite, &formats);-
525-
526-
527 std::sort(formats.begin(), formats.end());-
528 formats.erase(std::unique(formats.begin(), formats.end()), formats.end());-
529 return formats;-
530}-
531QList<QByteArray> QImageWriter::supportedMimeTypes()-
532{-
533 QList<QByteArray> mimeTypes;-
534-
535 mimeTypes << "image/bmp";-
536-
537-
538 mimeTypes << "image/x-portable-bitmap";-
539 mimeTypes << "image/x-portable-graymap";-
540 mimeTypes << "image/x-portable-pixmap";-
541-
542-
543 mimeTypes << "image/x-xbitmap";-
544-
545-
546 mimeTypes << "image/x-xpixmap";-
547-
548-
549 mimeTypes << "image/png";-
550-
551-
552-
553-
554-
555-
556 supportedImageHandlerMimeTypes(loader(), QImageIOPlugin::CanWrite, &mimeTypes);-
557-
558-
559 std::sort(mimeTypes.begin(), mimeTypes.end());-
560 mimeTypes.erase(std::unique(mimeTypes.begin(), mimeTypes.end()), mimeTypes.end());-
561 return mimeTypes;-
562}-
563-
564-
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9