author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 3 | 41300fa6a67c |
child 33 | 3e2da88830cd |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
||
45 |
||
46 |
#include <QDebug> |
|
47 |
#include <QFile> |
|
48 |
#include <QImage> |
|
49 |
#include <QImageReader> |
|
50 |
#include <QImageWriter> |
|
51 |
#include <QLabel> |
|
52 |
#include <QPainter> |
|
53 |
#include <QSet> |
|
54 |
||
55 |
#if defined(Q_OS_SYMBIAN) |
|
56 |
# define SRCDIR "" |
|
57 |
#endif |
|
58 |
typedef QMap<QString, QString> QStringMap; |
|
59 |
typedef QList<int> QIntList; |
|
60 |
Q_DECLARE_METATYPE(QImage) |
|
61 |
Q_DECLARE_METATYPE(QStringMap) |
|
62 |
Q_DECLARE_METATYPE(QIntList) |
|
63 |
Q_DECLARE_METATYPE(QImageWriter::ImageWriterError) |
|
64 |
Q_DECLARE_METATYPE(QIODevice *) |
|
65 |
Q_DECLARE_METATYPE(QImage::Format) |
|
66 |
||
67 |
//TESTED_FILES= |
|
68 |
||
69 |
class tst_QImageWriter : public QObject |
|
70 |
{ |
|
71 |
Q_OBJECT |
|
72 |
||
73 |
public: |
|
74 |
tst_QImageWriter(); |
|
75 |
virtual ~tst_QImageWriter(); |
|
76 |
||
77 |
public slots: |
|
78 |
void init(); |
|
79 |
void cleanup(); |
|
80 |
||
81 |
private slots: |
|
82 |
void getSetCheck(); |
|
83 |
void writeImage_data(); |
|
84 |
void writeImage(); |
|
85 |
void writeImage2_data(); |
|
86 |
void writeImage2(); |
|
87 |
void supportedFormats(); |
|
88 |
||
89 |
void readWriteNonDestructive_data(); |
|
90 |
void readWriteNonDestructive(); |
|
91 |
||
92 |
#if defined QTEST_HAVE_TIFF |
|
93 |
void largeTiff(); |
|
94 |
#endif |
|
95 |
||
96 |
void setDescription_data(); |
|
97 |
void setDescription(); |
|
98 |
||
99 |
void writeToInvalidDevice(); |
|
100 |
||
101 |
void supportsOption_data(); |
|
102 |
void supportsOption(); |
|
103 |
||
104 |
void saveWithNoFormat_data(); |
|
105 |
void saveWithNoFormat(); |
|
106 |
||
107 |
void resolution_data(); |
|
108 |
void resolution(); |
|
109 |
||
110 |
void saveToTemporaryFile(); |
|
111 |
}; |
|
112 |
#ifdef Q_OS_SYMBIAN |
|
113 |
static const QLatin1String prefix(SRCDIR "images/"); |
|
114 |
#else |
|
115 |
static const QLatin1String prefix(SRCDIR "/images/"); |
|
116 |
#endif |
|
117 |
static void initializePadding(QImage *image) |
|
118 |
{ |
|
119 |
int effectiveBytesPerLine = (image->width() * image->depth() + 7) / 8; |
|
120 |
int paddingBytes = image->bytesPerLine() - effectiveBytesPerLine; |
|
121 |
if (paddingBytes == 0) |
|
122 |
return; |
|
123 |
for (int y = 0; y < image->height(); ++y) { |
|
124 |
qMemSet(image->scanLine(y) + effectiveBytesPerLine, 0, paddingBytes); |
|
125 |
} |
|
126 |
} |
|
127 |
||
128 |
// Testing get/set functions |
|
129 |
void tst_QImageWriter::getSetCheck() |
|
130 |
{ |
|
131 |
QImageWriter obj1; |
|
132 |
// QIODevice * QImageWriter::device() |
|
133 |
// void QImageWriter::setDevice(QIODevice *) |
|
134 |
QFile *var1 = new QFile; |
|
135 |
obj1.setDevice(var1); |
|
136 |
||
137 |
QCOMPARE((QIODevice *) var1, obj1.device()); |
|
138 |
// The class should possibly handle a 0-pointer as a device, since |
|
139 |
// there is a default contructor, so it's "handling" a 0 device by default. |
|
140 |
// For example: QMovie::setDevice(0) works just fine |
|
141 |
obj1.setDevice((QIODevice *)0); |
|
142 |
QCOMPARE((QIODevice *) 0, obj1.device()); |
|
143 |
delete var1; |
|
144 |
||
145 |
// int QImageWriter::quality() |
|
146 |
// void QImageWriter::setQuality(int) |
|
147 |
obj1.setQuality(0); |
|
148 |
QCOMPARE(0, obj1.quality()); |
|
149 |
obj1.setQuality(INT_MIN); |
|
150 |
QCOMPARE(INT_MIN, obj1.quality()); |
|
151 |
obj1.setQuality(INT_MAX); |
|
152 |
QCOMPARE(INT_MAX, obj1.quality()); |
|
153 |
||
154 |
// int QImageWriter::compression() |
|
155 |
// void QImageWriter::setCompression(int) |
|
156 |
obj1.setCompression(0); |
|
157 |
QCOMPARE(0, obj1.compression()); |
|
158 |
obj1.setCompression(INT_MIN); |
|
159 |
QCOMPARE(INT_MIN, obj1.compression()); |
|
160 |
obj1.setCompression(INT_MAX); |
|
161 |
QCOMPARE(INT_MAX, obj1.compression()); |
|
162 |
||
163 |
// float QImageWriter::gamma() |
|
164 |
// void QImageWriter::setGamma(float) |
|
165 |
obj1.setGamma(0.0f); |
|
166 |
QCOMPARE(0.0f, obj1.gamma()); |
|
167 |
obj1.setGamma(1.1f); |
|
168 |
QCOMPARE(1.1f, obj1.gamma()); |
|
169 |
} |
|
170 |
||
171 |
tst_QImageWriter::tst_QImageWriter() |
|
172 |
{ |
|
173 |
} |
|
174 |
||
175 |
tst_QImageWriter::~tst_QImageWriter() |
|
176 |
{ |
|
177 |
QDir dir(prefix); |
|
178 |
QStringList filesToDelete = dir.entryList(QStringList() << "gen-*" , QDir::NoDotAndDotDot | QDir::Files); |
|
179 |
foreach( QString file, filesToDelete) { |
|
180 |
QFile::remove(dir.absoluteFilePath(file)); |
|
181 |
} |
|
182 |
||
183 |
} |
|
184 |
||
185 |
void tst_QImageWriter::init() |
|
186 |
{ |
|
187 |
} |
|
188 |
||
189 |
void tst_QImageWriter::cleanup() |
|
190 |
{ |
|
191 |
} |
|
192 |
||
193 |
void tst_QImageWriter::writeImage_data() |
|
194 |
{ |
|
195 |
QTest::addColumn<QString>("fileName"); |
|
196 |
QTest::addColumn<bool>("lossy"); |
|
197 |
QTest::addColumn<QByteArray>("format"); |
|
198 |
||
199 |
QTest::newRow("BMP: colorful") << QString("colorful.bmp") << false << QByteArray("bmp"); |
|
200 |
QTest::newRow("BMP: font") << QString("font.bmp") << false << QByteArray("bmp"); |
|
201 |
QTest::newRow("XPM: marble") << QString("marble.xpm") << false << QByteArray("xpm"); |
|
202 |
QTest::newRow("PNG: kollada") << QString("kollada.png") << false << QByteArray("png"); |
|
203 |
QTest::newRow("PPM: teapot") << QString("teapot.ppm") << false << QByteArray("ppm"); |
|
204 |
QTest::newRow("PBM: ship63") << QString("ship63.pbm") << true << QByteArray("pbm"); |
|
205 |
QTest::newRow("XBM: gnus") << QString("gnus.xbm") << false << QByteArray("xbm"); |
|
206 |
QTest::newRow("JPEG: beavis") << QString("beavis.jpg") << true << QByteArray("jpeg"); |
|
207 |
#if defined QTEST_HAVE_TIFF |
|
208 |
QTest::newRow("TIFF: teapot") << QString("teapot.tiff") << false << QByteArray("tiff"); |
|
209 |
#endif |
|
210 |
} |
|
211 |
||
212 |
void tst_QImageWriter::writeImage() |
|
213 |
{ |
|
214 |
QFETCH(QString, fileName); |
|
215 |
QFETCH(bool, lossy); |
|
216 |
QFETCH(QByteArray, format); |
|
217 |
||
218 |
QImage image; |
|
219 |
{ |
|
220 |
QImageReader reader(prefix + fileName); |
|
221 |
image = reader.read(); |
|
222 |
QVERIFY2(!image.isNull(), qPrintable(reader.errorString())); |
|
223 |
} |
|
224 |
{ |
|
225 |
QImageWriter writer(prefix + "gen-" + fileName, format); |
|
226 |
QVERIFY(writer.write(image)); |
|
227 |
} |
|
228 |
||
229 |
{ |
|
230 |
// Shouldn't be able to write to read-only file |
|
231 |
QFile sourceFile(prefix + "gen-" + fileName); |
|
232 |
QFile::Permissions permissions = sourceFile.permissions(); |
|
233 |
QVERIFY(sourceFile.setPermissions(QFile::ReadOwner | QFile::ReadUser | QFile::ReadGroup | QFile::ReadOther)); |
|
234 |
||
235 |
QImageWriter writer(prefix + "gen-" + fileName, format); |
|
236 |
QVERIFY(!writer.write(image)); |
|
237 |
||
238 |
QVERIFY(sourceFile.setPermissions(permissions)); |
|
239 |
} |
|
240 |
||
241 |
QImage image2; |
|
242 |
{ |
|
243 |
QImageReader reader(prefix + "gen-" + fileName); |
|
244 |
image2 = reader.read(); |
|
245 |
QVERIFY(!image2.isNull()); |
|
246 |
} |
|
247 |
if (!lossy) { |
|
248 |
QCOMPARE(image, image2); |
|
249 |
} else { |
|
250 |
QCOMPARE(image.format(), image2.format()); |
|
251 |
QCOMPARE(image.depth(), image2.depth()); |
|
252 |
} |
|
253 |
} |
|
254 |
||
255 |
void tst_QImageWriter::writeImage2_data() |
|
256 |
{ |
|
257 |
QTest::addColumn<QString>("fileName"); |
|
258 |
QTest::addColumn<QByteArray>("format"); |
|
259 |
QTest::addColumn<QImage>("image"); |
|
260 |
||
261 |
const QStringList formats = QStringList() << "bmp" << "xpm" << "png" |
|
262 |
<< "ppm"; //<< "jpeg"; |
|
263 |
QImage image0(70, 70, QImage::Format_ARGB32); |
|
264 |
image0.fill(QColor(Qt::red).rgb()); |
|
265 |
||
266 |
QImage::Format imgFormat = QImage::Format_Mono; |
|
267 |
while (imgFormat != QImage::NImageFormats) { |
|
268 |
QImage image = image0.convertToFormat(imgFormat); |
|
269 |
initializePadding(&image); |
|
270 |
foreach (const QString format, formats) { |
|
271 |
const QString fileName = QString("solidcolor_%1.%2").arg(imgFormat) |
|
272 |
.arg(format); |
|
273 |
QTest::newRow(fileName.toLatin1()) << fileName |
|
274 |
<< format.toLatin1() |
|
275 |
<< image; |
|
276 |
} |
|
277 |
imgFormat = QImage::Format(int(imgFormat) + 1); |
|
278 |
} |
|
279 |
} |
|
280 |
||
281 |
#if defined QTEST_HAVE_TIFF |
|
282 |
void tst_QImageWriter::largeTiff() |
|
283 |
{ |
|
284 |
#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) |
|
285 |
QImage img(4096, 2048, QImage::Format_ARGB32); |
|
286 |
||
287 |
QPainter p(&img); |
|
288 |
img.fill(0x0); |
|
289 |
p.fillRect(0, 0, 4096, 2048, QBrush(Qt::CrossPattern)); |
|
290 |
p.end(); |
|
291 |
||
292 |
QByteArray array; |
|
293 |
QBuffer writeBuffer(&array); |
|
294 |
writeBuffer.open(QIODevice::WriteOnly); |
|
295 |
||
296 |
QImageWriter writer(&writeBuffer, "tiff"); |
|
297 |
QVERIFY(writer.write(img)); |
|
298 |
||
299 |
writeBuffer.close(); |
|
300 |
||
301 |
QBuffer readBuffer(&array); |
|
302 |
readBuffer.open(QIODevice::ReadOnly); |
|
303 |
||
304 |
QImageReader reader(&readBuffer, "tiff"); |
|
305 |
||
306 |
QImage img2 = reader.read(); |
|
307 |
QVERIFY(!img2.isNull()); |
|
308 |
||
309 |
QCOMPARE(img, img2); |
|
310 |
#else |
|
311 |
QWARN("not tested on Symbian/WinCE"); |
|
312 |
#endif |
|
313 |
} |
|
314 |
#endif |
|
315 |
||
316 |
/* |
|
317 |
Workaround for the equality operator for indexed formats |
|
318 |
(which fails if the colortables are different). |
|
319 |
||
320 |
Images must have the same format and size. |
|
321 |
*/ |
|
322 |
static bool equalImageContents(const QImage &image1, const QImage &image2) |
|
323 |
{ |
|
324 |
switch (image1.format()) { |
|
325 |
case QImage::Format_Mono: |
|
326 |
case QImage::Format_Indexed8: |
|
327 |
for (int y = 0; y < image1.height(); ++y) |
|
328 |
for (int x = 0; x < image1.width(); ++x) |
|
329 |
if (image1.pixel(x, y) != image2.pixel(x, y)) |
|
330 |
return false; |
|
331 |
return true; |
|
332 |
default: |
|
333 |
return (image1 == image2); |
|
334 |
} |
|
335 |
} |
|
336 |
||
337 |
void tst_QImageWriter::writeImage2() |
|
338 |
{ |
|
339 |
QFETCH(QString, fileName); |
|
340 |
QFETCH(QByteArray, format); |
|
341 |
QFETCH(QImage, image); |
|
342 |
||
343 |
//we reduce the scope of writer so that it closes the associated file |
|
344 |
// and QFile::remove can actually work |
|
345 |
{ |
|
346 |
QImageWriter writer(fileName, format); |
|
347 |
QVERIFY(writer.write(image)); |
|
348 |
} |
|
349 |
||
350 |
QImage written; |
|
351 |
||
352 |
//we reduce the scope of reader so that it closes the associated file |
|
353 |
// and QFile::remove can actually work |
|
354 |
{ |
|
355 |
QImageReader reader(fileName, format); |
|
356 |
QVERIFY(reader.read(&written)); |
|
357 |
} |
|
358 |
||
359 |
written = written.convertToFormat(image.format()); |
|
360 |
if (!equalImageContents(written, image)) { |
|
361 |
qDebug() << "image" << image.format() << image.width() |
|
362 |
<< image.height() << image.depth() |
|
363 |
<< hex << image.pixel(0, 0); |
|
364 |
qDebug() << "written" << written.format() << written.width() |
|
365 |
<< written.height() << written.depth() |
|
366 |
<< hex << written.pixel(0, 0); |
|
367 |
} |
|
368 |
QVERIFY(equalImageContents(written, image)); |
|
369 |
||
370 |
QVERIFY(QFile::remove(fileName)); |
|
371 |
} |
|
372 |
||
373 |
void tst_QImageWriter::supportedFormats() |
|
374 |
{ |
|
375 |
QList<QByteArray> formats = QImageWriter::supportedImageFormats(); |
|
376 |
QList<QByteArray> sortedFormats = formats; |
|
377 |
qSort(sortedFormats); |
|
378 |
||
379 |
// check that the list is sorted |
|
380 |
QCOMPARE(formats, sortedFormats); |
|
381 |
||
382 |
QSet<QByteArray> formatSet; |
|
383 |
foreach (QByteArray format, formats) |
|
384 |
formatSet << format; |
|
385 |
||
386 |
// check that the list does not contain duplicates |
|
387 |
QCOMPARE(formatSet.size(), formats.size()); |
|
388 |
} |
|
389 |
||
390 |
void tst_QImageWriter::readWriteNonDestructive_data() |
|
391 |
{ |
|
392 |
QTest::addColumn<QImage::Format>("format"); |
|
393 |
QTest::addColumn<QImage::Format>("expectedFormat"); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
394 |
QTest::addColumn<bool>("grayscale"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
395 |
QTest::newRow("tiff mono") << QImage::Format_Mono << QImage::Format_Mono << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
396 |
QTest::newRow("tiff indexed") << QImage::Format_Indexed8 << QImage::Format_Indexed8 << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
397 |
QTest::newRow("tiff rgb32") << QImage::Format_ARGB32 << QImage::Format_ARGB32 << false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
398 |
QTest::newRow("tiff grayscale") << QImage::Format_Indexed8 << QImage::Format_Indexed8 << true; |
0 | 399 |
} |
400 |
||
401 |
void tst_QImageWriter::readWriteNonDestructive() |
|
402 |
{ |
|
403 |
QFETCH(QImage::Format, format); |
|
404 |
QFETCH(QImage::Format, expectedFormat); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
405 |
QFETCH(bool, grayscale); |
0 | 406 |
QImage image = QImage(prefix + "colorful.bmp").convertToFormat(format); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
407 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
408 |
if (grayscale) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
409 |
QVector<QRgb> colors; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
410 |
for (int i = 0; i < 256; ++i) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
411 |
colors << qRgb(i, i, i); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
412 |
image.setColorTable(colors); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
413 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
414 |
|
0 | 415 |
QVERIFY(image.save(prefix + "gen-readWriteNonDestructive.tiff")); |
416 |
||
417 |
QImage image2 = QImage(prefix + "gen-readWriteNonDestructive.tiff"); |
|
418 |
QImage::Format readFormat = image2.format(); |
|
419 |
QCOMPARE(readFormat, expectedFormat); |
|
420 |
QCOMPARE(image, image2); |
|
421 |
} |
|
422 |
||
423 |
void tst_QImageWriter::setDescription_data() |
|
424 |
{ |
|
425 |
QTest::addColumn<QString>("fileName"); |
|
426 |
QTest::addColumn<QStringMap>("description"); |
|
427 |
||
428 |
QMap<QString, QString> willem; |
|
429 |
willem["Title"] = "PngSuite"; |
|
430 |
willem["Author"] = "Willem A.J. van Schaik (willem@schaik.com)"; |
|
431 |
willem["Copyright"] = "Copyright Willem van Schaik, Singapore 1995-96"; |
|
432 |
willem["Description"] = "A compilation of a set of images created to test the " |
|
433 |
"various color-types of the PNG format. Included are " |
|
434 |
"black&white, color, paletted, with alpha channel, with " |
|
435 |
"transparency formats. All bit-depths allowed according " |
|
436 |
"to the spec are present."; |
|
437 |
willem["Software"] = "Created on a NeXTstation color using \"pnmtopng\"."; |
|
438 |
willem["Disclaimer"] = "Freeware."; |
|
439 |
||
440 |
QTest::newRow("PNG") << prefix + QString("gen-pngwithtext.png") << willem; |
|
441 |
} |
|
442 |
||
443 |
void tst_QImageWriter::setDescription() |
|
444 |
{ |
|
445 |
QFETCH(QString, fileName); |
|
446 |
QFETCH(QStringMap, description); |
|
447 |
||
448 |
QImageWriter writer(fileName, "png"); |
|
449 |
foreach (QString key, description.keys()) |
|
450 |
writer.setText(key, description.value(key)); |
|
451 |
QVERIFY(writer.write(QImage(prefix + "kollada.png"))); |
|
452 |
||
453 |
QImageReader reader(fileName); |
|
454 |
foreach (QString key, description.keys()) |
|
455 |
QCOMPARE(reader.text(key), description.value(key)); |
|
456 |
} |
|
457 |
||
458 |
void tst_QImageWriter::writeToInvalidDevice() |
|
459 |
{ |
|
460 |
QLatin1String fileName("/these/directories/do/not/exist/001.png"); |
|
461 |
{ |
|
462 |
QImageWriter writer(fileName); |
|
463 |
QVERIFY(!writer.canWrite()); |
|
464 |
QCOMPARE(writer.error(), QImageWriter::DeviceError); |
|
465 |
} |
|
466 |
{ |
|
467 |
QImageWriter writer(fileName); |
|
468 |
writer.setFormat("png"); |
|
469 |
QVERIFY(!writer.canWrite()); |
|
470 |
QCOMPARE(writer.error(), QImageWriter::DeviceError); |
|
471 |
} |
|
472 |
{ |
|
473 |
QImageWriter writer(fileName); |
|
474 |
QImage im(10, 10, QImage::Format_ARGB32); |
|
475 |
QVERIFY(!writer.write(im)); |
|
476 |
QCOMPARE(writer.error(), QImageWriter::DeviceError); |
|
477 |
} |
|
478 |
{ |
|
479 |
QImageWriter writer(fileName); |
|
480 |
writer.setFormat("png"); |
|
481 |
QImage im(10, 10, QImage::Format_ARGB32); |
|
482 |
QVERIFY(!writer.write(im)); |
|
483 |
QCOMPARE(writer.error(), QImageWriter::DeviceError); |
|
484 |
} |
|
485 |
} |
|
486 |
||
487 |
void tst_QImageWriter::supportsOption_data() |
|
488 |
{ |
|
489 |
QTest::addColumn<QString>("fileName"); |
|
490 |
QTest::addColumn<QIntList>("options"); |
|
491 |
||
492 |
QTest::newRow("png") << QString("gen-black.png") |
|
493 |
<< (QIntList() << QImageIOHandler::Gamma |
|
494 |
<< QImageIOHandler::Description |
|
495 |
<< QImageIOHandler::Quality |
|
496 |
<< QImageIOHandler::Size); |
|
497 |
#if defined QTEST_HAVE_TIFF |
|
498 |
QTest::newRow("tiff") << QString("gen-black.tiff") |
|
499 |
<< (QIntList() << QImageIOHandler::Size |
|
500 |
<< QImageIOHandler::CompressionRatio); |
|
501 |
#endif |
|
502 |
} |
|
503 |
||
504 |
void tst_QImageWriter::supportsOption() |
|
505 |
{ |
|
506 |
QFETCH(QString, fileName); |
|
507 |
QFETCH(QIntList, options); |
|
508 |
||
509 |
QSet<QImageIOHandler::ImageOption> allOptions; |
|
510 |
allOptions << QImageIOHandler::Size |
|
511 |
<< QImageIOHandler::ClipRect |
|
512 |
<< QImageIOHandler::Description |
|
513 |
<< QImageIOHandler::ScaledClipRect |
|
514 |
<< QImageIOHandler::ScaledSize |
|
515 |
<< QImageIOHandler::CompressionRatio |
|
516 |
<< QImageIOHandler::Gamma |
|
517 |
<< QImageIOHandler::Quality |
|
518 |
<< QImageIOHandler::Name |
|
519 |
<< QImageIOHandler::SubType |
|
520 |
<< QImageIOHandler::IncrementalReading |
|
521 |
<< QImageIOHandler::Endianness |
|
522 |
<< QImageIOHandler::Animation |
|
523 |
<< QImageIOHandler::BackgroundColor; |
|
524 |
||
525 |
QImageWriter writer(prefix + fileName); |
|
526 |
for (int i = 0; i < options.size(); ++i) { |
|
527 |
QVERIFY(writer.supportsOption(QImageIOHandler::ImageOption(options.at(i)))); |
|
528 |
allOptions.remove(QImageIOHandler::ImageOption(options.at(i))); |
|
529 |
} |
|
530 |
||
531 |
foreach (QImageIOHandler::ImageOption option, allOptions) |
|
532 |
QVERIFY(!writer.supportsOption(option)); |
|
533 |
} |
|
534 |
||
535 |
void tst_QImageWriter::saveWithNoFormat_data() |
|
536 |
{ |
|
537 |
QTest::addColumn<QString>("fileName"); |
|
538 |
QTest::addColumn<QByteArray>("format"); |
|
539 |
QTest::addColumn<QImageWriter::ImageWriterError>("error"); |
|
540 |
||
541 |
QTest::newRow("garble") << prefix + QString("gen-out.garble") << QByteArray("jpeg") << QImageWriter::UnsupportedFormatError; |
|
542 |
QTest::newRow("bmp") << prefix + QString("gen-out.bmp") << QByteArray("bmp") << QImageWriter::ImageWriterError(0); |
|
543 |
QTest::newRow("xbm") << prefix + QString("gen-out.xbm") << QByteArray("xbm") << QImageWriter::ImageWriterError(0); |
|
544 |
QTest::newRow("xpm") << prefix + QString("gen-out.xpm") << QByteArray("xpm") << QImageWriter::ImageWriterError(0); |
|
545 |
QTest::newRow("png") << prefix + QString("gen-out.png") << QByteArray("png") << QImageWriter::ImageWriterError(0); |
|
546 |
QTest::newRow("ppm") << prefix + QString("gen-out.ppm") << QByteArray("ppm") << QImageWriter::ImageWriterError(0); |
|
547 |
QTest::newRow("pbm") << prefix + QString("gen-out.pbm") << QByteArray("pbm") << QImageWriter::ImageWriterError(0); |
|
548 |
#if defined QTEST_HAVE_TIFF |
|
549 |
QTest::newRow("tiff") << prefix + QString("gen-out.tiff") << QByteArray("tiff") << QImageWriter::ImageWriterError(0); |
|
550 |
#endif |
|
551 |
} |
|
552 |
||
553 |
void tst_QImageWriter::saveWithNoFormat() |
|
554 |
{ |
|
555 |
QFETCH(QString, fileName); |
|
556 |
QFETCH(QByteArray, format); |
|
557 |
QFETCH(QImageWriter::ImageWriterError, error); |
|
558 |
||
559 |
QImage niceImage(64, 64, QImage::Format_ARGB32); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
560 |
qMemSet(niceImage.bits(), 0, niceImage.byteCount()); |
0 | 561 |
|
562 |
QImageWriter writer(fileName /* , 0 - no format! */); |
|
563 |
if (error != 0) { |
|
564 |
QVERIFY(!writer.write(niceImage)); |
|
565 |
QCOMPARE(writer.error(), error); |
|
566 |
return; |
|
567 |
} |
|
568 |
||
569 |
QVERIFY2(writer.write(niceImage), qPrintable(writer.errorString())); |
|
570 |
||
571 |
QImageReader reader(fileName); |
|
572 |
QCOMPARE(reader.format(), format); |
|
573 |
||
574 |
QVERIFY(reader.canRead()); |
|
575 |
||
576 |
QImage outImage = reader.read(); |
|
577 |
QVERIFY2(!outImage.isNull(), qPrintable(reader.errorString())); |
|
578 |
} |
|
579 |
||
580 |
void tst_QImageWriter::resolution_data() |
|
581 |
{ |
|
582 |
QTest::addColumn<QString>("filename"); |
|
583 |
QTest::addColumn<int>("expectedDotsPerMeterX"); |
|
584 |
QTest::addColumn<int>("expectedDotsPerMeterY"); |
|
585 |
#if defined QTEST_HAVE_TIFF |
|
586 |
QTest::newRow("TIFF: 100 dpi") << ("image_100dpi.tif") << qRound(100 * (100 / 2.54)) << qRound(100 * (100 / 2.54)); |
|
587 |
QTest::newRow("TIFF: 50 dpi") << ("image_50dpi.tif") << qRound(50 * (100 / 2.54)) << qRound(50 * (100 / 2.54)); |
|
588 |
QTest::newRow("TIFF: 300 dot per meter") << ("image_300dpm.tif") << 300 << 300; |
|
589 |
#endif |
|
590 |
} |
|
591 |
||
592 |
void tst_QImageWriter::resolution() |
|
593 |
{ |
|
594 |
QFETCH(QString, filename); |
|
595 |
QFETCH(int, expectedDotsPerMeterX); |
|
596 |
QFETCH(int, expectedDotsPerMeterY); |
|
597 |
||
598 |
QImage image(prefix + QLatin1String("colorful.bmp")); |
|
599 |
image.setDotsPerMeterX(expectedDotsPerMeterX); |
|
600 |
image.setDotsPerMeterY(expectedDotsPerMeterY); |
|
601 |
const QString generatedFilepath = prefix + "gen-" + filename; |
|
602 |
{ |
|
603 |
QImageWriter writer(generatedFilepath); |
|
604 |
QVERIFY(writer.write(image)); |
|
605 |
} |
|
606 |
QImageReader reader(generatedFilepath); |
|
607 |
const QImage generatedImage = reader.read(); |
|
608 |
||
609 |
QCOMPARE(expectedDotsPerMeterX, generatedImage.dotsPerMeterX()); |
|
610 |
QCOMPARE(expectedDotsPerMeterY, generatedImage.dotsPerMeterY()); |
|
611 |
} |
|
612 |
||
613 |
void tst_QImageWriter::saveToTemporaryFile() |
|
614 |
{ |
|
615 |
QImage image(prefix + "kollada.png"); |
|
616 |
QVERIFY(!image.isNull()); |
|
617 |
||
618 |
{ |
|
619 |
// 1) Via QImageWriter's API, with a standard temp file name |
|
620 |
QTemporaryFile file; |
|
621 |
QVERIFY(file.open()); |
|
622 |
QImageWriter writer(&file, "PNG"); |
|
623 |
if (writer.canWrite()) |
|
624 |
QVERIFY(writer.write(image)); |
|
625 |
else |
|
626 |
qWarning() << file.errorString(); |
|
627 |
#if defined(Q_OS_WINCE) |
|
628 |
file.reset(); |
|
629 |
#endif |
|
630 |
QCOMPARE(QImage(writer.fileName()), image); |
|
631 |
} |
|
632 |
{ |
|
633 |
// 2) Via QImage's API, with a standard temp file name |
|
634 |
QTemporaryFile file; |
|
635 |
QVERIFY(file.open()); |
|
636 |
QVERIFY(image.save(&file, "PNG")); |
|
637 |
file.reset(); |
|
638 |
QImage tmp; |
|
639 |
QVERIFY(tmp.load(&file, "PNG")); |
|
640 |
QCOMPARE(tmp, image); |
|
641 |
} |
|
642 |
{ |
|
643 |
// 3) Via QImageWriter's API, with a named temp file |
|
644 |
QTemporaryFile file("tempXXXXXX"); |
|
645 |
QVERIFY(file.open()); |
|
646 |
QImageWriter writer(&file, "PNG"); |
|
647 |
QVERIFY(writer.write(image)); |
|
648 |
#if defined(Q_OS_WINCE) |
|
649 |
file.reset(); |
|
650 |
#endif |
|
651 |
QCOMPARE(QImage(writer.fileName()), image); |
|
652 |
} |
|
653 |
{ |
|
654 |
// 4) Via QImage's API, with a named temp file |
|
655 |
QTemporaryFile file("tempXXXXXX"); |
|
656 |
QVERIFY(file.open()); |
|
657 |
QVERIFY(image.save(&file, "PNG")); |
|
658 |
file.reset(); |
|
659 |
QImage tmp; |
|
660 |
QVERIFY(tmp.load(&file, "PNG")); |
|
661 |
QCOMPARE(tmp, image); |
|
662 |
} |
|
663 |
} |
|
664 |
||
665 |
QTEST_MAIN(tst_QImageWriter) |
|
666 |
#include "tst_qimagewriter.moc" |