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(); }
never executed: delete x;
executed: return thisGlobalStatic.pointer.load();
Execution Count:528
partially evaluated: !thisGlobalStatic.pointer.testAndSetOrdered(0, x)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:12
evaluated: !thisGlobalStatic.pointer.load()
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:516
partially evaluated: !thisGlobalStatic.destroyed
TRUEFALSE
yes
Evaluation Count:12
no
Evaluation Count:0
0-528
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()) {
partially evaluated: device
TRUEFALSE
yes
Evaluation Count:511
no
Evaluation Count:0
evaluated: format.isEmpty()
TRUEFALSE
yes
Evaluation Count:18
yes
Evaluation Count:493
0-511
31 -
32 -
33 -
34 if (QFile *file = qobject_cast<QFile *>(device)) {
partially evaluated: QFile *file = qobject_cast<QFile *>(device)
TRUEFALSE
yes
Evaluation Count:18
no
Evaluation Count:0
0-18
35 if (!(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()) {
partially evaluated: !(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()
TRUEFALSE
yes
Evaluation Count:18
no
Evaluation Count:0
0-18
36 -
37 const int index = keyMap.key(QString::fromLatin1(suffix), -1); -
38 if (index != -1)
partially evaluated: index != -1
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:18
0-18
39 suffixPluginIndex = index;
never executed: suffixPluginIndex = index;
0
40 -
41 }
executed: }
Execution Count:18
18
42 }
executed: }
Execution Count:18
18
43 }
executed: }
Execution Count:18
18
44 -
45 QByteArray testFormat = !form.isEmpty() ? form : suffix;
evaluated: !form.isEmpty()
TRUEFALSE
yes
Evaluation Count:493
yes
Evaluation Count:18
18-493
46 -
47 -
48 if (suffixPluginIndex != -1) {
partially evaluated: suffixPluginIndex != -1
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:511
0-511
49 -
50 -
51 const int index = keyMap.key(QString::fromLatin1(suffix), -1); -
52 if (index != -1) {
never evaluated: index != -1
0
53 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(index)); -
54 if (plugin && (plugin->capabilities(device, suffix) & QImageIOPlugin::CanWrite))
never evaluated: (plugin->capabilities(device, suffix) & QImageIOPlugin::CanWrite)
never evaluated: plugin
0
55 handler = plugin->create(device, suffix);
never executed: handler = plugin->create(device, suffix);
0
56 }
never executed: }
0
57 }
never executed: }
0
58 -
59 -
60 -
61 if (!handler && !testFormat.isEmpty()) {
partially evaluated: !handler
TRUEFALSE
yes
Evaluation Count:511
no
Evaluation Count:0
partially evaluated: !testFormat.isEmpty()
TRUEFALSE
yes
Evaluation Count:511
no
Evaluation Count:0
0-511
62 if (false) {
partially evaluated: false
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:511
0-511
63 -
64 } else if (testFormat == "png") {
evaluated: testFormat == "png"
TRUEFALSE
yes
Evaluation Count:129
yes
Evaluation Count:382
never executed: }
0-382
65 handler = new QPngHandler; -
66 } else if (testFormat == "bmp") {
executed: }
Execution Count:129
evaluated: testFormat == "bmp"
TRUEFALSE
yes
Evaluation Count:34
yes
Evaluation Count:348
34-348
67 handler = new QBmpHandler; -
68 } else if (testFormat == "dib") {
executed: }
Execution Count:34
partially evaluated: testFormat == "dib"
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:348
0-348
69 handler = new QBmpHandler(QBmpHandler::DibFormat); -
70 -
71 -
72 } else if (testFormat == "xpm") {
never executed: }
evaluated: testFormat == "xpm"
TRUEFALSE
yes
Evaluation Count:291
yes
Evaluation Count:57
0-291
73 handler = new QXpmHandler; -
74 -
75 -
76 } else if (testFormat == "xbm") {
executed: }
Execution Count:291
evaluated: testFormat == "xbm"
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:51
6-291
77 handler = new QXbmHandler; -
78 handler->setOption(QImageIOHandler::SubType, testFormat); -
79 -
80 -
81 } else if (testFormat == "pbm" || testFormat == "pbmraw" || testFormat == "pgm"
evaluated: testFormat == "pbm"
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:47
partially evaluated: testFormat == "pbmraw"
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:47
evaluated: testFormat == "pgm"
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:46
executed: }
Execution Count:6
0-47
82 || testFormat == "pgmraw" || testFormat == "ppm" || testFormat == "ppmraw") {
partially evaluated: testFormat == "pgmraw"
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:46
evaluated: testFormat == "ppm"
TRUEFALSE
yes
Evaluation Count:22
yes
Evaluation Count:24
partially evaluated: testFormat == "ppmraw"
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:24
0-46
83 handler = new QPpmHandler; -
84 handler->setOption(QImageIOHandler::SubType, testFormat); -
85 -
86 }
executed: }
Execution Count:27
27
87 } -
88 -
89 -
90 if (!testFormat.isEmpty()) {
partially evaluated: !testFormat.isEmpty()
TRUEFALSE
yes
Evaluation Count:511
no
Evaluation Count:0
0-511
91 const int keyCount = keyMap.keys().size(); -
92 for (int i = 0; i < keyCount; ++i) {
evaluated: i < keyCount
TRUEFALSE
yes
Evaluation Count:2021
yes
Evaluation Count:488
488-2021
93 QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(i)); -
94 if (plugin && (plugin->capabilities(device, testFormat) & QImageIOPlugin::CanWrite)) {
evaluated: plugin
TRUEFALSE
yes
Evaluation Count:1533
yes
Evaluation Count:488
evaluated: (plugin->capabilities(device, testFormat) & QImageIOPlugin::CanWrite)
TRUEFALSE
yes
Evaluation Count:23
yes
Evaluation Count:1510
23-1533
95 delete handler; -
96 handler = plugin->create(device, testFormat); -
97 break;
executed: break;
Execution Count:23
23
98 } -
99 }
executed: }
Execution Count:1998
1998
100 }
executed: }
Execution Count:511
511
101 -
102 -
103 if (!handler)
evaluated: !handler
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:510
1-510
104 return 0;
executed: return 0;
Execution Count:1
1
105 -
106 handler->setDevice(device); -
107 if (!testFormat.isEmpty())
partially evaluated: !testFormat.isEmpty()
TRUEFALSE
yes
Evaluation Count:510
no
Evaluation Count:0
0-510
108 handler->setFormat(testFormat);
executed: handler->setFormat(testFormat);
Execution Count:510
510
109 return handler;
executed: return handler;
Execution Count:510
510
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}
executed: }
Execution Count:512
512
153 -
154 -
155 -
156 -
157 -
158 -
159QImageWriter::QImageWriter() -
160 : d(new QImageWriterPrivate(this)) -
161{ -
162}
executed: }
Execution Count:1
1
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}
executed: }
Execution Count:145
145
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}
executed: }
Execution Count:366
366
189 -
190 -
191 -
192 -
193QImageWriter::~QImageWriter() -
194{ -
195 if (d->deleteDevice)
evaluated: d->deleteDevice
TRUEFALSE
yes
Evaluation Count:366
yes
Evaluation Count:146
146-366
196 delete d->device;
executed: delete d->device;
Execution Count:366
366
197 delete d->handler; -
198 delete d; -
199}
executed: }
Execution Count:512
512
200void QImageWriter::setFormat(const QByteArray &format) -
201{ -
202 d->format = format; -
203}
executed: }
Execution Count:2
2
204 -
205 -
206 -
207 -
208 -
209 -
210QByteArray QImageWriter::format() const -
211{ -
212 return d->format;
never executed: return d->format;
0
213} -
214void QImageWriter::setDevice(QIODevice *device) -
215{ -
216 if (d->device && d->deleteDevice)
evaluated: d->device
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:1
partially evaluated: d->deleteDevice
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-1
217 delete d->device;
never executed: delete d->device;
0
218 -
219 d->device = device; -
220 d->deleteDevice = false; -
221 delete d->handler; -
222 d->handler = 0; -
223}
executed: }
Execution Count:2
2
224 -
225 -
226 -
227 -
228 -
229QIODevice *QImageWriter::device() const -
230{ -
231 return d->device;
executed: return d->device;
Execution Count:2
2
232} -
233void QImageWriter::setFileName(const QString &fileName) -
234{ -
235 setDevice(new QFile(fileName)); -
236 d->deleteDevice = true; -
237}
never executed: }
0
238QString QImageWriter::fileName() const -
239{ -
240 QFile *file = qobject_cast<QFile *>(d->device); -
241 return file ? file->fileName() : QString();
executed: return file ? file->fileName() : QString();
Execution Count:2
2
242} -
243void QImageWriter::setQuality(int quality) -
244{ -
245 d->quality = quality; -
246}
executed: }
Execution Count:3
3
247 -
248 -
249 -
250 -
251 -
252 -
253int QImageWriter::quality() const -
254{ -
255 return d->quality;
executed: return d->quality;
Execution Count:3
3
256} -
257void QImageWriter::setCompression(int compression) -
258{ -
259 d->compression = compression; -
260}
executed: }
Execution Count:3
3
261 -
262 -
263 -
264 -
265 -
266 -
267int QImageWriter::compression() const -
268{ -
269 return d->compression;
executed: return d->compression;
Execution Count:3
3
270} -
271void QImageWriter::setGamma(float gamma) -
272{ -
273 d->gamma = gamma; -
274}
executed: }
Execution Count:2
2
275 -
276 -
277 -
278 -
279 -
280 -
281float QImageWriter::gamma() const -
282{ -
283 return d->gamma;
executed: return d->gamma;
Execution Count:2
2
284} -
285void QImageWriter::setDescription(const QString &description) -
286{ -
287 d->description = description; -
288}
never executed: }
0
289QString QImageWriter::description() const -
290{ -
291 return d->description;
never executed: return d->description;
0
292} -
293void QImageWriter::setText(const QString &key, const QString &text) -
294{ -
295 if (!d->description.isEmpty())
evaluated: !d->description.isEmpty()
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:12
12
296 d->description += QLatin1String("\n\n");
executed: d->description += QLatin1String("\n\n");
Execution Count:12
12
297 d->description += key.simplified() + QLatin1String(": ") + text.simplified(); -
298}
executed: }
Execution Count:24
24
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) {
partially evaluated: d->device
TRUEFALSE
yes
Evaluation Count:511
no
Evaluation Count:0
evaluated: !d->handler
TRUEFALSE
yes
Evaluation Count:510
yes
Evaluation Count:1
evaluated: (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:509
0-511
309 d->imageWriterError = QImageWriter::UnsupportedFormatError; -
310 d->errorString = QLatin1String("Unsupported image format"); -
311 -
312 return false;
executed: return false;
Execution Count:1
1
313 } -
314 if (d->device && !d->device->isOpen())
partially evaluated: d->device
TRUEFALSE
yes
Evaluation Count:510
no
Evaluation Count:0
evaluated: !d->device->isOpen()
TRUEFALSE
yes
Evaluation Count:376
yes
Evaluation Count:134
0-510
315 d->device->open(QIODevice::WriteOnly);
executed: d->device->open(QIODevice::WriteOnly);
Execution Count:376
376
316 if (!d->device || !d->device->isWritable()) {
partially evaluated: !d->device
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:510
evaluated: !d->device->isWritable()
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:498
0-510
317 d->imageWriterError = QImageWriter::DeviceError; -
318 d->errorString = QLatin1String("Device not writable"); -
319 -
320 return false;
executed: return false;
Execution Count:12
12
321 } -
322 return true;
executed: return true;
Execution Count:498
498
323} -
324bool QImageWriter::write(const QImage &image) -
325{ -
326 if (!canWrite())
evaluated: !canWrite()
TRUEFALSE
yes
Evaluation Count:11
yes
Evaluation Count:497
11-497
327 return false;
executed: return false;
Execution Count:11
11
328 -
329 if (d->handler->supportsOption(QImageIOHandler::Quality))
evaluated: d->handler->supportsOption(QImageIOHandler::Quality)
TRUEFALSE
yes
Evaluation Count:145
yes
Evaluation Count:352
145-352
330 d->handler->setOption(QImageIOHandler::Quality, d->quality);
executed: d->handler->setOption(QImageIOHandler::Quality, d->quality);
Execution Count:145
145
331 if (d->handler->supportsOption(QImageIOHandler::CompressionRatio))
partially evaluated: d->handler->supportsOption(QImageIOHandler::CompressionRatio)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:497
0-497
332 d->handler->setOption(QImageIOHandler::CompressionRatio, d->compression);
never executed: d->handler->setOption(QImageIOHandler::CompressionRatio, d->compression);
0
333 if (d->handler->supportsOption(QImageIOHandler::Gamma))
evaluated: d->handler->supportsOption(QImageIOHandler::Gamma)
TRUEFALSE
yes
Evaluation Count:123
yes
Evaluation Count:374
123-374
334 d->handler->setOption(QImageIOHandler::Gamma, d->gamma);
executed: d->handler->setOption(QImageIOHandler::Gamma, d->gamma);
Execution Count:123
123
335 if (!d->description.isEmpty() && d->handler->supportsOption(QImageIOHandler::Description))
evaluated: !d->description.isEmpty()
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:485
partially evaluated: d->handler->supportsOption(QImageIOHandler::Description)
TRUEFALSE
yes
Evaluation Count:12
no
Evaluation Count:0
0-485
336 d->handler->setOption(QImageIOHandler::Description, d->description);
executed: d->handler->setOption(QImageIOHandler::Description, d->description);
Execution Count:12
12
337 -
338 if (!d->handler->write(image))
partially evaluated: !d->handler->write(image)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:497
0-497
339 return false;
never executed: return false;
0
340 if (QFile *file = qobject_cast<QFile *>(d->device))
evaluated: QFile *file = qobject_cast<QFile *>(d->device)
TRUEFALSE
yes
Evaluation Count:408
yes
Evaluation Count:89
89-408
341 file->flush();
executed: file->flush();
Execution Count:408
408
342 return true;
executed: return true;
Execution Count:497
497
343} -
344 -
345 -
346 -
347 -
348 -
349 -
350QImageWriter::ImageWriterError QImageWriter::error() const -
351{ -
352 return d->imageWriterError;
executed: return d->imageWriterError;
Execution Count:5
5
353} -
354 -
355 -
356 -
357 -
358 -
359 -
360QString QImageWriter::errorString() const -
361{ -
362 return d->errorString;
executed: return d->errorString;
Execution Count:6
6
363} -
364bool QImageWriter::supportsOption(QImageIOHandler::ImageOption option) const -
365{ -
366 if (!d->handler && (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0) {
evaluated: !d->handler
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:13
partially evaluated: (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-13
367 d->imageWriterError = QImageWriter::UnsupportedFormatError; -
368 d->errorString = QLatin1String("Unsupported image format"); -
369 -
370 return false;
never executed: return false;
0
371 } -
372 -
373 return d->handler->supportsOption(option);
executed: return d->handler->supportsOption(option);
Execution Count:14
14
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) {
evaluated: it != cend
TRUEFALSE
yes
Evaluation Count:2740
yes
Evaluation Count:685
685-2740
390 if (it.key() != i) {
evaluated: it.key() != i
TRUEFALSE
yes
Evaluation Count:2055
yes
Evaluation Count:685
685-2055
391 i = it.key(); -
392 plugin = qobject_cast<QImageIOPlugin *>(loader->instance(i)); -
393 }
executed: }
Execution Count:2055
2055
394 const QByteArray key = it.value().toLatin1(); -
395 if (plugin && (plugin->capabilities(0, key) & cap) != 0)
partially evaluated: plugin
TRUEFALSE
yes
Evaluation Count:2740
no
Evaluation Count:0
evaluated: (plugin->capabilities(0, key) & cap) != 0
TRUEFALSE
yes
Evaluation Count:2723
yes
Evaluation Count:17
0-2740
396 result->insert(key);
executed: result->insert(key);
Execution Count:2723
2723
397 }
executed: }
Execution Count:2740
2740
398}
executed: }
Execution Count:685
685
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