tests/auto/qimagereader/tst_qimagereader.cpp
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
   121     void setBackgroundColor();
   121     void setBackgroundColor();
   122 
   122 
   123     void supportsAnimation_data();
   123     void supportsAnimation_data();
   124     void supportsAnimation();
   124     void supportsAnimation();
   125 
   125 
   126     void description_data();
       
   127     void description();
       
   128 
       
   129     void readFromResources_data();
   126     void readFromResources_data();
   130     void readFromResources();
   127     void readFromResources();
   131 
   128 
   132     void dotsPerMeter_data();
   129     void dotsPerMeter_data();
   133     void dotsPerMeter();
   130     void dotsPerMeter();
   135     void physicalDpi_data();
   132     void physicalDpi_data();
   136     void physicalDpi();
   133     void physicalDpi();
   137 
   134 
   138     void sizeBeforeRead_data();
   135     void sizeBeforeRead_data();
   139     void sizeBeforeRead();
   136     void sizeBeforeRead();
       
   137 
       
   138     void sizeBeforeFormat_data();
       
   139     void sizeBeforeFormat();
   140 
   140 
   141     void imageFormatBeforeRead_data();
   141     void imageFormatBeforeRead_data();
   142     void imageFormatBeforeRead();
   142     void imageFormatBeforeRead();
   143 
   143 
   144 #if defined QTEST_HAVE_GIF
   144 #if defined QTEST_HAVE_GIF
   310 void tst_QImageReader::jpegRgbCmyk()
   310 void tst_QImageReader::jpegRgbCmyk()
   311 {
   311 {
   312     QImage image1(prefix + QLatin1String("YCbCr_cmyk.jpg"));
   312     QImage image1(prefix + QLatin1String("YCbCr_cmyk.jpg"));
   313     QImage image2(prefix + QLatin1String("YCbCr_cmyk.png"));
   313     QImage image2(prefix + QLatin1String("YCbCr_cmyk.png"));
   314 
   314 
   315     QCOMPARE(image1, image2);
   315     // first, do some obvious tests
       
   316     QCOMPARE(image1.height(), image2.height());
       
   317     QCOMPARE(image1.width(), image2.width());
       
   318     QCOMPARE(image1.format(), image2.format());
       
   319     QCOMPARE(image1.format(), QImage::Format_RGB32);
       
   320 
       
   321     // compare all the pixels with a slack of 3. This ignores rounding errors in libjpeg/libpng
       
   322     for (int h = 0; h < image1.height(); ++h) {
       
   323         const uchar *s1 = image1.constScanLine(h);
       
   324         const uchar *s2 = image2.constScanLine(h);
       
   325         for (int w = 0; w < image1.width() * 4; ++w) {
       
   326             if (*s1 != *s2) {
       
   327                 QVERIFY2(qAbs(*s1 - *s2) <= 3, qPrintable(QString("images differ in line %1, col %2 (image1: %3, image2: %4)").arg(h).arg(w).arg(*s1, 0, 16).arg(*s2, 0, 16)));
       
   328             }
       
   329             s1++;
       
   330             s2++;
       
   331         }
       
   332     }
   316 }
   333 }
   317 
   334 
   318 void tst_QImageReader::setScaledSize_data()
   335 void tst_QImageReader::setScaledSize_data()
   319 {
   336 {
   320     QTest::addColumn<QString>("fileName");
   337     QTest::addColumn<QString>("fileName");
   704     QImage image = reader.read();
   721     QImage image = reader.read();
   705     QVERIFY(!image.isNull());
   722     QVERIFY(!image.isNull());
   706     QCOMPARE(size, image.size());
   723     QCOMPARE(size, image.size());
   707 }
   724 }
   708 
   725 
       
   726 void tst_QImageReader::sizeBeforeFormat_data()
       
   727 {
       
   728     imageFormat_data();
       
   729 }
       
   730 
       
   731 void tst_QImageReader::sizeBeforeFormat()
       
   732 {
       
   733     QFETCH(QString, fileName);
       
   734 
       
   735     QByteArray formatA, formatB;
       
   736 
       
   737     {
       
   738         QImageReader reader(prefix + fileName);
       
   739         formatA = reader.format();
       
   740     }
       
   741 
       
   742     {
       
   743         QImageReader reader(prefix + fileName);
       
   744         QSize size = reader.size();
       
   745         formatB = reader.format();
       
   746     }
       
   747 
       
   748     QCOMPARE(formatA, formatB);
       
   749 }
       
   750 
   709 void tst_QImageReader::imageFormatBeforeRead_data()
   751 void tst_QImageReader::imageFormatBeforeRead_data()
   710 {
   752 {
   711     imageFormat_data();
   753     imageFormat_data();
   712 }
   754 }
   713 
   755 
  1205     QCOMPARE(expected, reader.read());
  1247     QCOMPARE(expected, reader.read());
  1206     if (format != "ppm" && format != "gif")  // Known not to work
  1248     if (format != "ppm" && format != "gif")  // Known not to work
  1207         QCOMPARE(buf.pos(), qint64(preLen+imageDataSize));
  1249         QCOMPARE(buf.pos(), qint64(preLen+imageDataSize));
  1208 }
  1250 }
  1209 
  1251 
  1210 
       
  1211 void tst_QImageReader::description_data()
       
  1212 {
       
  1213     QTest::addColumn<QString>("fileName");
       
  1214     QTest::addColumn<QStringMap>("description");
       
  1215 
       
  1216     QMap<QString, QString> willem;
       
  1217     willem["Title"] = "PngSuite";
       
  1218     willem["Author"] = "Willem A.J. van Schaik (gwillem@ntuvax.ntu.ac.sg)";
       
  1219     willem["Copyright"] = "Copyright Willem van Schaik, Singapore 1995";
       
  1220     willem["Description"] = "A compilation of a set of images created to test the "
       
  1221                             "various color-types of the PNG format. Included are "
       
  1222                             "black&white, color, paletted, with alpha channel, with "
       
  1223                             "transparency formats. All bit-depths allowed according "
       
  1224                             "to the spec are present.";
       
  1225     willem["Software"] = "Created on a NeXTstation color using \"pnmtopng\".";
       
  1226     willem["Disclaimer"] = "Freeware.";
       
  1227 
       
  1228     QTest::newRow("PNG") << QString("pngwithtext.png") << willem;
       
  1229     QTest::newRow("PNG Compressed") << QString("pngwithcompressedtext.png") << willem;
       
  1230 }
       
  1231 
       
  1232 void tst_QImageReader::description()
       
  1233 {
       
  1234     QFETCH(QString, fileName);
       
  1235     QFETCH(QStringMap, description);
       
  1236 
       
  1237     // Sanity check
       
  1238     QVERIFY(!QImage(prefix + fileName).isNull());
       
  1239 
       
  1240     QImageReader reader(prefix + fileName);
       
  1241 
       
  1242     foreach (QString key, description.keys())
       
  1243         QCOMPARE(reader.text(key), description.value(key));
       
  1244     QCOMPARE(reader.textKeys(), QStringList(description.keys()));
       
  1245 
       
  1246     QImage image = reader.read();
       
  1247     QVERIFY(!image.isNull());
       
  1248 
       
  1249     foreach (QString key, description.keys())
       
  1250         QCOMPARE(image.text(key), description.value(key));
       
  1251     QCOMPARE(image.textKeys(), QStringList(description.keys()));
       
  1252 
       
  1253     foreach (QString key, description.keys())
       
  1254         QCOMPARE(reader.text(key), description.value(key));
       
  1255     QCOMPARE(reader.textKeys(), QStringList(description.keys()));
       
  1256 }
       
  1257 
  1252 
  1258 void tst_QImageReader::readFromResources_data()
  1253 void tst_QImageReader::readFromResources_data()
  1259 {
  1254 {
  1260     QTest::addColumn<QString>("fileName");
  1255     QTest::addColumn<QString>("fileName");
  1261     QTest::addColumn<QByteArray>("format");
  1256     QTest::addColumn<QByteArray>("format");
  1358                                      << QByteArray("png") << QSize(16, 16)
  1353                                      << QByteArray("png") << QSize(16, 16)
  1359                                      << QString("");
  1354                                      << QString("");
  1360     QTest::newRow("image.png") << QString("image.png")
  1355     QTest::newRow("image.png") << QString("image.png")
  1361                                       << QByteArray("png") << QSize(22, 22)
  1356                                       << QByteArray("png") << QSize(22, 22)
  1362                                       << QString("");
  1357                                       << QString("");
  1363     QTest::newRow("pngwithcompressedtext.png") << QString("pngwithcompressedtext.png")
       
  1364                                                       << QByteArray("png") << QSize(32, 32)
       
  1365                                                       << QString("");
       
  1366     QTest::newRow("pngwithtext.png") << QString("pngwithtext.png")
       
  1367                                             << QByteArray("png") << QSize(32, 32)
       
  1368                                             << QString("");
       
  1369     QTest::newRow("kollada.png") << QString("kollada.png")
  1358     QTest::newRow("kollada.png") << QString("kollada.png")
  1370                                         << QByteArray("png") << QSize(436, 160)
  1359                                         << QByteArray("png") << QSize(436, 160)
  1371                                         << QString("");
  1360                                         << QString("");
  1372     QTest::newRow("black.png") << QString("black.png")
  1361     QTest::newRow("black.png") << QString("black.png")
  1373                                       << QByteArray("png") << QSize(48, 48)
  1362                                       << QByteArray("png") << QSize(48, 48)
  1882     QFETCH(QString, name);
  1871     QFETCH(QString, name);
  1883     QFETCH(QString, extension);
  1872     QFETCH(QString, extension);
  1884     QFETCH(QString, expected);
  1873     QFETCH(QString, expected);
  1885 
  1874 
  1886     QList<QByteArray> formats = QImageReader::supportedImageFormats();
  1875     QList<QByteArray> formats = QImageReader::supportedImageFormats();
  1887     QString fileNameBase = "images/" + name + ".";
  1876     QString fileNameBase = prefix + name + ".";
  1888 
  1877 
  1889     foreach (const QByteArray &f, formats) {
  1878     foreach (const QByteArray &f, formats) {
  1890         if (f == extension)
  1879         if (f == extension)
  1891             continue;
  1880             continue;
  1892         QFile tmp(QDir::tempPath() + "/" + name + "_" + expected + "." + f);
  1881         QFile tmp(QDir::tempPath() + "/" + name + "_" + expected + "." + f);
  1893 
  1882 
  1894         QFile::copy(fileNameBase + extension, QFileInfo(tmp).absoluteFilePath());
  1883         QVERIFY(QFile::copy(fileNameBase + extension, QFileInfo(tmp).absoluteFilePath()));
  1895 
  1884 
  1896         QString format;
  1885         QString format;
  1897         QImage image;
  1886         QImage image;
  1898         {
  1887         {
  1899             // image reader needs to be scoped for the remove() to work..
  1888             // image reader needs to be scoped for the remove() to work..