image/qimagewriter.cpp

Switch to Source codePreprocessed file
LineSource CodeCoverage
1 -
2 -
3 -
4 -
5 -
6 -
7 -
8 -
9 -
10static QFactoryLoader *loader() { static QGlobalStatic<QFactoryLoader > thisGlobalStatic = { { (0) }, false }; if (!thisGlobalStatic.pointer.load() && !thisGlobalStatic.destroyed) { QFactoryLoader *x = new QFactoryLoader ("org.qt-project.Qt.QImageIOHandlerFactoryInterface", QLatin1String("/imageformats")); if (!thisGlobalStatic.pointer.testAndSetOrdered(0, x)) delete x; else static QGlobalStaticDeleter<QFactoryLoader > cleanup(thisGlobalStatic); } return thisGlobalStatic.pointer.load(); }
-
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) {
-
63 -
64 } 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.keys().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 -
118 QByteArray format; -
119 QIODevice *device; -
120 bool deleteDevice; -
121 QImageIOHandler *handler; -
122 -
123 -
124 int quality; -
125 int compression; -
126 float gamma; -
127 QString description; -
128 QString text; -
129 -
130 -
131 QImageWriter::ImageWriterError imageWriterError; -
132 QString errorString; -
133 -
134 QImageWriter *q; -
135}; -
136 -
137 -
138 -
139 -
140QImageWriterPrivate::QImageWriterPrivate(QImageWriter *qq) -
141{ -
142 device = 0; -
143 deleteDevice = false; -
144 handler = 0; -
145 quality = -1; -
146 compression = 0; -
147 gamma = 0.0; -
148 imageWriterError = QImageWriter::UnknownError; -
149 errorString = QLatin1String("Unknown error"); -
150 -
151 q = qq; -
152}
-
153 -
154 -
155 -
156 -
157 -
158 -
159QImageWriter::QImageWriter() -
160 : d(new QImageWriterPrivate(this)) -
161{ -
162}
-
163 -
164 -
165 -
166 -
167 -
168QImageWriter::QImageWriter(QIODevice *device, const QByteArray &format) -
169 : d(new QImageWriterPrivate(this)) -
170{ -
171 d->device = device; -
172 d->format = format; -
173}
-
174 -
175 -
176 -
177 -
178 -
179 -
180 -
181QImageWriter::QImageWriter(const QString &fileName, const QByteArray &format) -
182 : d(new QImageWriterPrivate(this)) -
183{ -
184 QFile *file = new QFile(fileName); -
185 d->device = file; -
186 d->deleteDevice = true; -
187 d->format = format; -
188}
-
189 -
190 -
191 -
192 -
193QImageWriter::~QImageWriter() -
194{ -
195 if (d->deleteDevice)
-
196 delete d->device;
-
197 delete d->handler; -
198 delete d; -
199}
-
200void QImageWriter::setFormat(const QByteArray &format) -
201{ -
202 d->format = format; -
203}
-
204 -
205 -
206 -
207 -
208 -
209 -
210QByteArray QImageWriter::format() const -
211{ -
212 return d->format;
-
213} -
214void QImageWriter::setDevice(QIODevice *device) -
215{ -
216 if (d->device && d->deleteDevice)
-
217 delete d->device;
-
218 -
219 d->device = device; -
220 d->deleteDevice = false; -
221 delete d->handler; -
222 d->handler = 0; -
223}
-
224 -
225 -
226 -
227 -
228 -
229QIODevice *QImageWriter::device() const -
230{ -
231 return d->device;
-
232} -
233void QImageWriter::setFileName(const QString &fileName) -
234{ -
235 setDevice(new QFile(fileName)); -
236 d->deleteDevice = true; -
237}
-
238QString QImageWriter::fileName() const -
239{ -
240 QFile *file = qobject_cast<QFile *>(d->device); -
241 return file ? file->fileName() : QString();
-
242} -
243void QImageWriter::setQuality(int quality) -
244{ -
245 d->quality = quality; -
246}
-
247 -
248 -
249 -
250 -
251 -
252 -
253int QImageWriter::quality() const -
254{ -
255 return d->quality;
-
256} -
257void QImageWriter::setCompression(int compression) -
258{ -
259 d->compression = compression; -
260}
-
261 -
262 -
263 -
264 -
265 -
266 -
267int QImageWriter::compression() const -
268{ -
269 return d->compression;
-
270} -
271void QImageWriter::setGamma(float gamma) -
272{ -
273 d->gamma = gamma; -
274}
-
275 -
276 -
277 -
278 -
279 -
280 -
281float QImageWriter::gamma() const -
282{ -
283 return d->gamma;
-
284} -
285void QImageWriter::setDescription(const QString &description) -
286{ -
287 d->description = description; -
288}
-
289QString QImageWriter::description() const -
290{ -
291 return d->description;
-
292} -
293void QImageWriter::setText(const QString &key, const QString &text) -
294{ -
295 if (!d->description.isEmpty())
-
296 d->description += QLatin1String("\n\n");
-
297 d->description += key.simplified() + QLatin1String(": ") + text.simplified(); -
298}
-
299 -
300 -
301 -
302 -
303 -
304 -
305 -
306bool QImageWriter::canWrite() const -
307{ -
308 if (d->device && !d->handler && (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0) {
-
309 d->imageWriterError = QImageWriter::UnsupportedFormatError; -
310 d->errorString = QLatin1String("Unsupported image format"); -
311 -
312 return false;
-
313 } -
314 if (d->device && !d->device->isOpen())
-
315 d->device->open(QIODevice::WriteOnly);
-
316 if (!d->device || !d->device->isWritable()) {
-
317 d->imageWriterError = QImageWriter::DeviceError; -
318 d->errorString = QLatin1String("Device not writable"); -
319 -
320 return false;
-
321 } -
322 return true;
-
323} -
324bool QImageWriter::write(const QImage &image) -
325{ -
326 if (!canWrite())
-
327 return false;
-
328 -
329 if (d->handler->supportsOption(QImageIOHandler::Quality))
-
330 d->handler->setOption(QImageIOHandler::Quality, d->quality);
-
331 if (d->handler->supportsOption(QImageIOHandler::CompressionRatio))
-
332 d->handler->setOption(QImageIOHandler::CompressionRatio, d->compression);
-
333 if (d->handler->supportsOption(QImageIOHandler::Gamma))
-
334 d->handler->setOption(QImageIOHandler::Gamma, d->gamma);
-
335 if (!d->description.isEmpty() && d->handler->supportsOption(QImageIOHandler::Description))
-
336 d->handler->setOption(QImageIOHandler::Description, d->description);
-
337 -
338 if (!d->handler->write(image))
-
339 return false;
-
340 if (QFile *file = qobject_cast<QFile *>(d->device))
-
341 file->flush();
-
342 return true;
-
343} -
344 -
345 -
346 -
347 -
348 -
349 -
350QImageWriter::ImageWriterError QImageWriter::error() const -
351{ -
352 return d->imageWriterError;
-
353} -
354 -
355 -
356 -
357 -
358 -
359 -
360QString QImageWriter::errorString() const -
361{ -
362 return d->errorString;
-
363} -
364bool QImageWriter::supportsOption(QImageIOHandler::ImageOption option) const -
365{ -
366 if (!d->handler && (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0) {
-
367 d->imageWriterError = QImageWriter::UnsupportedFormatError; -
368 d->errorString = QLatin1String("Unsupported image format"); -
369 -
370 return false;
-
371 } -
372 -
373 return d->handler->supportsOption(option);
-
374} -
375 -
376 -
377 -
378void supportedImageHandlerFormats(QFactoryLoader *loader, -
379 QImageIOPlugin::Capability cap, -
380 QSet<QByteArray> *result) -
381{ -
382 typedef QMultiMap<int, QString> PluginKeyMap; -
383 typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator; -
384 -
385 const PluginKeyMap keyMap = loader->keyMap(); -
386 const PluginKeyMapConstIterator cend = keyMap.constEnd(); -
387 int i = -1; -
388 QImageIOPlugin *plugin = 0; -
389 for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it) {
-
390 if (it.key() != i) {
-
391 i = it.key(); -
392 plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i)); -
393 }
-
394 const QByteArray key = it.value().toLatin1(); -
395 if (plugin && (plugin->capabilities(0, key) & cap) != 0)
-
396 result->insert(key);
-
397 }
-
398}
-
399QList<QByteArray> QImageWriter::supportedImageFormats() -
400{ -
401 QSet<QByteArray> formats; -
402 -
403 formats << "bmp"; -
404 -
405 -
406 formats << "pbm" << "pgm" << "ppm"; -
407 -
408 -
409 formats << "xbm"; -
410 -
411 -
412 formats << "xpm"; -
413 -
414 -
415 formats << "png"; -
416 supportedImageHandlerFormats(loader(), QImageIOPlugin::CanWrite, &formats); -
417 -
418 -
419 QList<QByteArray> sortedFormats; -
420 for (QSet<QByteArray>::ConstIterator it = formats.constBegin(); it != formats.constEnd(); ++it)
evaluated: it != formats.constEnd()
TRUEFALSE
yes
Evaluation Count:170
yes
Evaluation Count:17
17-170
421 sortedFormats << *it;
executed: sortedFormats << *it;
Execution Count:170
170
422 -
423 qSort(sortedFormats); -
424 return sortedFormats;
executed: return sortedFormats;
Execution Count:17
17
425} -
426 -
427 -
428 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial