0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
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");
|
|
394 |
QTest::newRow("tiff mono") << QImage::Format_Mono << QImage::Format_Mono;
|
|
395 |
QTest::newRow("tiff indexed") << QImage::Format_Indexed8 << QImage::Format_Indexed8;
|
|
396 |
QTest::newRow("tiff rgb32") << QImage::Format_ARGB32 << QImage::Format_ARGB32;
|
|
397 |
}
|
|
398 |
|
|
399 |
void tst_QImageWriter::readWriteNonDestructive()
|
|
400 |
{
|
|
401 |
QFETCH(QImage::Format, format);
|
|
402 |
QFETCH(QImage::Format, expectedFormat);
|
|
403 |
QImage image = QImage(prefix + "colorful.bmp").convertToFormat(format);
|
|
404 |
QVERIFY(image.save(prefix + "gen-readWriteNonDestructive.tiff"));
|
|
405 |
|
|
406 |
QImage image2 = QImage(prefix + "gen-readWriteNonDestructive.tiff");
|
|
407 |
QImage::Format readFormat = image2.format();
|
|
408 |
QCOMPARE(readFormat, expectedFormat);
|
|
409 |
QCOMPARE(image, image2);
|
|
410 |
}
|
|
411 |
|
|
412 |
void tst_QImageWriter::setDescription_data()
|
|
413 |
{
|
|
414 |
QTest::addColumn<QString>("fileName");
|
|
415 |
QTest::addColumn<QStringMap>("description");
|
|
416 |
|
|
417 |
QMap<QString, QString> willem;
|
|
418 |
willem["Title"] = "PngSuite";
|
|
419 |
willem["Author"] = "Willem A.J. van Schaik (willem@schaik.com)";
|
|
420 |
willem["Copyright"] = "Copyright Willem van Schaik, Singapore 1995-96";
|
|
421 |
willem["Description"] = "A compilation of a set of images created to test the "
|
|
422 |
"various color-types of the PNG format. Included are "
|
|
423 |
"black&white, color, paletted, with alpha channel, with "
|
|
424 |
"transparency formats. All bit-depths allowed according "
|
|
425 |
"to the spec are present.";
|
|
426 |
willem["Software"] = "Created on a NeXTstation color using \"pnmtopng\".";
|
|
427 |
willem["Disclaimer"] = "Freeware.";
|
|
428 |
|
|
429 |
QTest::newRow("PNG") << prefix + QString("gen-pngwithtext.png") << willem;
|
|
430 |
}
|
|
431 |
|
|
432 |
void tst_QImageWriter::setDescription()
|
|
433 |
{
|
|
434 |
QFETCH(QString, fileName);
|
|
435 |
QFETCH(QStringMap, description);
|
|
436 |
|
|
437 |
QImageWriter writer(fileName, "png");
|
|
438 |
foreach (QString key, description.keys())
|
|
439 |
writer.setText(key, description.value(key));
|
|
440 |
QVERIFY(writer.write(QImage(prefix + "kollada.png")));
|
|
441 |
|
|
442 |
QImageReader reader(fileName);
|
|
443 |
foreach (QString key, description.keys())
|
|
444 |
QCOMPARE(reader.text(key), description.value(key));
|
|
445 |
}
|
|
446 |
|
|
447 |
void tst_QImageWriter::writeToInvalidDevice()
|
|
448 |
{
|
|
449 |
QLatin1String fileName("/these/directories/do/not/exist/001.png");
|
|
450 |
{
|
|
451 |
QImageWriter writer(fileName);
|
|
452 |
QVERIFY(!writer.canWrite());
|
|
453 |
QCOMPARE(writer.error(), QImageWriter::DeviceError);
|
|
454 |
}
|
|
455 |
{
|
|
456 |
QImageWriter writer(fileName);
|
|
457 |
writer.setFormat("png");
|
|
458 |
QVERIFY(!writer.canWrite());
|
|
459 |
QCOMPARE(writer.error(), QImageWriter::DeviceError);
|
|
460 |
}
|
|
461 |
{
|
|
462 |
QImageWriter writer(fileName);
|
|
463 |
QImage im(10, 10, QImage::Format_ARGB32);
|
|
464 |
QVERIFY(!writer.write(im));
|
|
465 |
QCOMPARE(writer.error(), QImageWriter::DeviceError);
|
|
466 |
}
|
|
467 |
{
|
|
468 |
QImageWriter writer(fileName);
|
|
469 |
writer.setFormat("png");
|
|
470 |
QImage im(10, 10, QImage::Format_ARGB32);
|
|
471 |
QVERIFY(!writer.write(im));
|
|
472 |
QCOMPARE(writer.error(), QImageWriter::DeviceError);
|
|
473 |
}
|
|
474 |
}
|
|
475 |
|
|
476 |
void tst_QImageWriter::supportsOption_data()
|
|
477 |
{
|
|
478 |
QTest::addColumn<QString>("fileName");
|
|
479 |
QTest::addColumn<QIntList>("options");
|
|
480 |
|
|
481 |
QTest::newRow("png") << QString("gen-black.png")
|
|
482 |
<< (QIntList() << QImageIOHandler::Gamma
|
|
483 |
<< QImageIOHandler::Description
|
|
484 |
<< QImageIOHandler::Quality
|
|
485 |
<< QImageIOHandler::Size);
|
|
486 |
#if defined QTEST_HAVE_TIFF
|
|
487 |
QTest::newRow("tiff") << QString("gen-black.tiff")
|
|
488 |
<< (QIntList() << QImageIOHandler::Size
|
|
489 |
<< QImageIOHandler::CompressionRatio);
|
|
490 |
#endif
|
|
491 |
}
|
|
492 |
|
|
493 |
void tst_QImageWriter::supportsOption()
|
|
494 |
{
|
|
495 |
QFETCH(QString, fileName);
|
|
496 |
QFETCH(QIntList, options);
|
|
497 |
|
|
498 |
QSet<QImageIOHandler::ImageOption> allOptions;
|
|
499 |
allOptions << QImageIOHandler::Size
|
|
500 |
<< QImageIOHandler::ClipRect
|
|
501 |
<< QImageIOHandler::Description
|
|
502 |
<< QImageIOHandler::ScaledClipRect
|
|
503 |
<< QImageIOHandler::ScaledSize
|
|
504 |
<< QImageIOHandler::CompressionRatio
|
|
505 |
<< QImageIOHandler::Gamma
|
|
506 |
<< QImageIOHandler::Quality
|
|
507 |
<< QImageIOHandler::Name
|
|
508 |
<< QImageIOHandler::SubType
|
|
509 |
<< QImageIOHandler::IncrementalReading
|
|
510 |
<< QImageIOHandler::Endianness
|
|
511 |
<< QImageIOHandler::Animation
|
|
512 |
<< QImageIOHandler::BackgroundColor;
|
|
513 |
|
|
514 |
QImageWriter writer(prefix + fileName);
|
|
515 |
for (int i = 0; i < options.size(); ++i) {
|
|
516 |
QVERIFY(writer.supportsOption(QImageIOHandler::ImageOption(options.at(i))));
|
|
517 |
allOptions.remove(QImageIOHandler::ImageOption(options.at(i)));
|
|
518 |
}
|
|
519 |
|
|
520 |
foreach (QImageIOHandler::ImageOption option, allOptions)
|
|
521 |
QVERIFY(!writer.supportsOption(option));
|
|
522 |
}
|
|
523 |
|
|
524 |
void tst_QImageWriter::saveWithNoFormat_data()
|
|
525 |
{
|
|
526 |
QTest::addColumn<QString>("fileName");
|
|
527 |
QTest::addColumn<QByteArray>("format");
|
|
528 |
QTest::addColumn<QImageWriter::ImageWriterError>("error");
|
|
529 |
|
|
530 |
QTest::newRow("garble") << prefix + QString("gen-out.garble") << QByteArray("jpeg") << QImageWriter::UnsupportedFormatError;
|
|
531 |
QTest::newRow("bmp") << prefix + QString("gen-out.bmp") << QByteArray("bmp") << QImageWriter::ImageWriterError(0);
|
|
532 |
QTest::newRow("xbm") << prefix + QString("gen-out.xbm") << QByteArray("xbm") << QImageWriter::ImageWriterError(0);
|
|
533 |
QTest::newRow("xpm") << prefix + QString("gen-out.xpm") << QByteArray("xpm") << QImageWriter::ImageWriterError(0);
|
|
534 |
QTest::newRow("png") << prefix + QString("gen-out.png") << QByteArray("png") << QImageWriter::ImageWriterError(0);
|
|
535 |
QTest::newRow("ppm") << prefix + QString("gen-out.ppm") << QByteArray("ppm") << QImageWriter::ImageWriterError(0);
|
|
536 |
QTest::newRow("pbm") << prefix + QString("gen-out.pbm") << QByteArray("pbm") << QImageWriter::ImageWriterError(0);
|
|
537 |
#if defined QTEST_HAVE_TIFF
|
|
538 |
QTest::newRow("tiff") << prefix + QString("gen-out.tiff") << QByteArray("tiff") << QImageWriter::ImageWriterError(0);
|
|
539 |
#endif
|
|
540 |
}
|
|
541 |
|
|
542 |
void tst_QImageWriter::saveWithNoFormat()
|
|
543 |
{
|
|
544 |
QFETCH(QString, fileName);
|
|
545 |
QFETCH(QByteArray, format);
|
|
546 |
QFETCH(QImageWriter::ImageWriterError, error);
|
|
547 |
|
|
548 |
QImage niceImage(64, 64, QImage::Format_ARGB32);
|
|
549 |
qMemSet(niceImage.bits(), 0, niceImage.numBytes());
|
|
550 |
|
|
551 |
QImageWriter writer(fileName /* , 0 - no format! */);
|
|
552 |
if (error != 0) {
|
|
553 |
QVERIFY(!writer.write(niceImage));
|
|
554 |
QCOMPARE(writer.error(), error);
|
|
555 |
return;
|
|
556 |
}
|
|
557 |
|
|
558 |
QVERIFY2(writer.write(niceImage), qPrintable(writer.errorString()));
|
|
559 |
|
|
560 |
QImageReader reader(fileName);
|
|
561 |
QCOMPARE(reader.format(), format);
|
|
562 |
|
|
563 |
QVERIFY(reader.canRead());
|
|
564 |
|
|
565 |
QImage outImage = reader.read();
|
|
566 |
QVERIFY2(!outImage.isNull(), qPrintable(reader.errorString()));
|
|
567 |
}
|
|
568 |
|
|
569 |
void tst_QImageWriter::resolution_data()
|
|
570 |
{
|
|
571 |
QTest::addColumn<QString>("filename");
|
|
572 |
QTest::addColumn<int>("expectedDotsPerMeterX");
|
|
573 |
QTest::addColumn<int>("expectedDotsPerMeterY");
|
|
574 |
#if defined QTEST_HAVE_TIFF
|
|
575 |
QTest::newRow("TIFF: 100 dpi") << ("image_100dpi.tif") << qRound(100 * (100 / 2.54)) << qRound(100 * (100 / 2.54));
|
|
576 |
QTest::newRow("TIFF: 50 dpi") << ("image_50dpi.tif") << qRound(50 * (100 / 2.54)) << qRound(50 * (100 / 2.54));
|
|
577 |
QTest::newRow("TIFF: 300 dot per meter") << ("image_300dpm.tif") << 300 << 300;
|
|
578 |
#endif
|
|
579 |
}
|
|
580 |
|
|
581 |
void tst_QImageWriter::resolution()
|
|
582 |
{
|
|
583 |
QFETCH(QString, filename);
|
|
584 |
QFETCH(int, expectedDotsPerMeterX);
|
|
585 |
QFETCH(int, expectedDotsPerMeterY);
|
|
586 |
|
|
587 |
QImage image(prefix + QLatin1String("colorful.bmp"));
|
|
588 |
image.setDotsPerMeterX(expectedDotsPerMeterX);
|
|
589 |
image.setDotsPerMeterY(expectedDotsPerMeterY);
|
|
590 |
const QString generatedFilepath = prefix + "gen-" + filename;
|
|
591 |
{
|
|
592 |
QImageWriter writer(generatedFilepath);
|
|
593 |
QVERIFY(writer.write(image));
|
|
594 |
}
|
|
595 |
QImageReader reader(generatedFilepath);
|
|
596 |
const QImage generatedImage = reader.read();
|
|
597 |
|
|
598 |
QCOMPARE(expectedDotsPerMeterX, generatedImage.dotsPerMeterX());
|
|
599 |
QCOMPARE(expectedDotsPerMeterY, generatedImage.dotsPerMeterY());
|
|
600 |
}
|
|
601 |
|
|
602 |
void tst_QImageWriter::saveToTemporaryFile()
|
|
603 |
{
|
|
604 |
QImage image(prefix + "kollada.png");
|
|
605 |
QVERIFY(!image.isNull());
|
|
606 |
|
|
607 |
{
|
|
608 |
// 1) Via QImageWriter's API, with a standard temp file name
|
|
609 |
QTemporaryFile file;
|
|
610 |
QVERIFY(file.open());
|
|
611 |
QImageWriter writer(&file, "PNG");
|
|
612 |
if (writer.canWrite())
|
|
613 |
QVERIFY(writer.write(image));
|
|
614 |
else
|
|
615 |
qWarning() << file.errorString();
|
|
616 |
#if defined(Q_OS_WINCE)
|
|
617 |
file.reset();
|
|
618 |
#endif
|
|
619 |
QCOMPARE(QImage(writer.fileName()), image);
|
|
620 |
}
|
|
621 |
{
|
|
622 |
// 2) Via QImage's API, with a standard temp file name
|
|
623 |
QTemporaryFile file;
|
|
624 |
QVERIFY(file.open());
|
|
625 |
QVERIFY(image.save(&file, "PNG"));
|
|
626 |
file.reset();
|
|
627 |
QImage tmp;
|
|
628 |
QVERIFY(tmp.load(&file, "PNG"));
|
|
629 |
QCOMPARE(tmp, image);
|
|
630 |
}
|
|
631 |
{
|
|
632 |
// 3) Via QImageWriter's API, with a named temp file
|
|
633 |
QTemporaryFile file("tempXXXXXX");
|
|
634 |
QVERIFY(file.open());
|
|
635 |
QImageWriter writer(&file, "PNG");
|
|
636 |
QVERIFY(writer.write(image));
|
|
637 |
#if defined(Q_OS_WINCE)
|
|
638 |
file.reset();
|
|
639 |
#endif
|
|
640 |
QCOMPARE(QImage(writer.fileName()), image);
|
|
641 |
}
|
|
642 |
{
|
|
643 |
// 4) Via QImage's API, with a named temp file
|
|
644 |
QTemporaryFile file("tempXXXXXX");
|
|
645 |
QVERIFY(file.open());
|
|
646 |
QVERIFY(image.save(&file, "PNG"));
|
|
647 |
file.reset();
|
|
648 |
QImage tmp;
|
|
649 |
QVERIFY(tmp.load(&file, "PNG"));
|
|
650 |
QCOMPARE(tmp, image);
|
|
651 |
}
|
|
652 |
}
|
|
653 |
|
|
654 |
QTEST_MAIN(tst_QImageWriter)
|
|
655 |
#include "tst_qimagewriter.moc"
|