| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Copyright (C) 2016 Ivan Komissarov. |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the DDS plugin in the Qt ImageFormats module. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 22 | ** included in the packaging of this file. Please review the following |
| 23 | ** information to ensure the GNU General Public License requirements will |
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 25 | ** |
| 26 | ** $QT_END_LICENSE$ |
| 27 | ** |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | #include <QtTest/QtTest> |
| 31 | #include <QtGui/QtGui> |
| 32 | |
| 33 | class tst_qdds: public QObject |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | |
| 37 | private slots: |
| 38 | void initTestCase(); |
| 39 | void readImage_data(); |
| 40 | void readImage(); |
| 41 | void testMipmaps_data(); |
| 42 | void testMipmaps(); |
| 43 | void testWriteImage_data(); |
| 44 | void testWriteImage(); |
| 45 | }; |
| 46 | |
| 47 | void tst_qdds::initTestCase() |
| 48 | { |
| 49 | if (!QImageReader::supportedImageFormats().contains(t: "dds" )) |
| 50 | QSKIP("The image format handler is not installed." ); |
| 51 | } |
| 52 | |
| 53 | void tst_qdds::readImage_data() |
| 54 | { |
| 55 | QTest::addColumn<QString>(name: "fileName" ); |
| 56 | QTest::addColumn<QSize>(name: "size" ); |
| 57 | |
| 58 | QTest::newRow(dataTag: "1" ) << QString("A1R5G5B5" ) << QSize(64, 64); |
| 59 | QTest::newRow(dataTag: "2" ) << QString("A2B10G10R10" ) << QSize(64, 64); |
| 60 | QTest::newRow(dataTag: "3" ) << QString("A2R10G10B10" ) << QSize(64, 64); |
| 61 | QTest::newRow(dataTag: "4" ) << QString("A2W10V10U10" ) << QSize(64, 64); |
| 62 | QTest::newRow(dataTag: "5" ) << QString("A4L4" ) << QSize(64, 64); |
| 63 | QTest::newRow(dataTag: "6" ) << QString("A4R4G4B4" ) << QSize(64, 64); |
| 64 | QTest::newRow(dataTag: "7" ) << QString("A8" ) << QSize(64, 64); |
| 65 | QTest::newRow(dataTag: "8" ) << QString("A8B8G8R8" ) << QSize(64, 64); |
| 66 | QTest::newRow(dataTag: "9" ) << QString("A8L8" ) << QSize(64, 64); |
| 67 | QTest::newRow(dataTag: "10" ) << QString("A8R3G3B2" ) << QSize(64, 64); |
| 68 | QTest::newRow(dataTag: "11" ) << QString("A8R8G8B8" ) << QSize(64, 64); |
| 69 | QTest::newRow(dataTag: "12" ) << QString("A16B16G16R16" ) << QSize(64, 64); |
| 70 | QTest::newRow(dataTag: "13" ) << QString("A16B16G16R16F" ) << QSize(64, 64); |
| 71 | QTest::newRow(dataTag: "14" ) << QString("A32B32G32R32F" ) << QSize(64, 64); |
| 72 | QTest::newRow(dataTag: "15" ) << QString("CxV8U8" ) << QSize(64, 64); |
| 73 | QTest::newRow(dataTag: "16" ) << QString("DXT1" ) << QSize(50, 50); |
| 74 | QTest::newRow(dataTag: "17" ) << QString("DXT2" ) << QSize(64, 64); |
| 75 | QTest::newRow(dataTag: "18" ) << QString("DXT3" ) << QSize(64, 64); |
| 76 | QTest::newRow(dataTag: "19" ) << QString("DXT4" ) << QSize(64, 64); |
| 77 | QTest::newRow(dataTag: "20" ) << QString("DXT5" ) << QSize(64, 64); |
| 78 | QTest::newRow(dataTag: "21" ) << QString("G8R8_G8B8" ) << QSize(64, 64); |
| 79 | QTest::newRow(dataTag: "22" ) << QString("G16R16" ) << QSize(64, 64); |
| 80 | QTest::newRow(dataTag: "23" ) << QString("G16R16F" ) << QSize(64, 64); |
| 81 | QTest::newRow(dataTag: "24" ) << QString("G32R32F" ) << QSize(64, 64); |
| 82 | QTest::newRow(dataTag: "25" ) << QString("L6V5U5" ) << QSize(64, 64); |
| 83 | QTest::newRow(dataTag: "26" ) << QString("L8" ) << QSize(64, 64); |
| 84 | QTest::newRow(dataTag: "27" ) << QString("L16" ) << QSize(64, 64); |
| 85 | QTest::newRow(dataTag: "28" ) << QString("P8" ) << QSize(64, 64); |
| 86 | QTest::newRow(dataTag: "29" ) << QString("Q8W8V8U8" ) << QSize(64, 64); |
| 87 | QTest::newRow(dataTag: "30" ) << QString("Q16W16V16U16" ) << QSize(64, 64); |
| 88 | QTest::newRow(dataTag: "31" ) << QString("R3G3B2" ) << QSize(64, 64); |
| 89 | QTest::newRow(dataTag: "32" ) << QString("R5G6B5" ) << QSize(64, 64); |
| 90 | QTest::newRow(dataTag: "33" ) << QString("R8G8_B8G8" ) << QSize(64, 64); |
| 91 | QTest::newRow(dataTag: "34" ) << QString("R8G8B8" ) << QSize(64, 64); |
| 92 | QTest::newRow(dataTag: "35" ) << QString("R16F" ) << QSize(64, 64); |
| 93 | QTest::newRow(dataTag: "36" ) << QString("R32F" ) << QSize(64, 64); |
| 94 | QTest::newRow(dataTag: "37" ) << QString("UYVY" ) << QSize(64, 64); |
| 95 | QTest::newRow(dataTag: "38" ) << QString("V8U8" ) << QSize(64, 64); |
| 96 | QTest::newRow(dataTag: "39" ) << QString("V16U16" ) << QSize(64, 64); |
| 97 | QTest::newRow(dataTag: "40" ) << QString("X1R5G5B5" ) << QSize(64, 64); |
| 98 | QTest::newRow(dataTag: "41" ) << QString("X4R4G4B4" ) << QSize(64, 64); |
| 99 | QTest::newRow(dataTag: "42" ) << QString("X8B8G8R8" ) << QSize(64, 64); |
| 100 | QTest::newRow(dataTag: "43" ) << QString("X8L8V8U8" ) << QSize(64, 64); |
| 101 | QTest::newRow(dataTag: "44" ) << QString("X8R8G8B8" ) << QSize(64, 64); |
| 102 | QTest::newRow(dataTag: "45" ) << QString("YUY2" ) << QSize(64, 64); |
| 103 | QTest::newRow(dataTag: "46" ) << QString("RXGB" ) << QSize(64, 64); |
| 104 | QTest::newRow(dataTag: "47" ) << QString("ATI2" ) << QSize(64, 64); |
| 105 | QTest::newRow(dataTag: "48" ) << QString("P4" ) << QSize(64, 64); |
| 106 | QTest::newRow(dataTag: "49" ) << QString("A8R8G8B8.2" ) << QSize(64, 32); |
| 107 | } |
| 108 | |
| 109 | void tst_qdds::readImage() |
| 110 | { |
| 111 | QFETCH(QString, fileName); |
| 112 | QFETCH(QSize, size); |
| 113 | |
| 114 | const QString path = QStringLiteral(":/dds/" ) + fileName + QStringLiteral(".dds" ); |
| 115 | const QByteArray subType = fileName.left(n: fileName.lastIndexOf(c: QLatin1Char('.'))).toLatin1(); |
| 116 | QImageReader reader(path); |
| 117 | QVERIFY(reader.canRead()); |
| 118 | QVERIFY(reader.supportsOption(QImageIOHandler::SubType)); |
| 119 | QCOMPARE(reader.subType(), subType); |
| 120 | QVERIFY(reader.supportsOption(QImageIOHandler::SupportedSubTypes)); |
| 121 | QImage image = reader.read(); |
| 122 | QVERIFY2(!image.isNull(), qPrintable(reader.errorString())); |
| 123 | QCOMPARE(image.size(), size); |
| 124 | } |
| 125 | |
| 126 | void tst_qdds::testMipmaps_data() |
| 127 | { |
| 128 | QTest::addColumn<QString>(name: "fileName" ); |
| 129 | QTest::addColumn<QSize>(name: "size" ); |
| 130 | QTest::addColumn<int>(name: "imageCount" ); |
| 131 | |
| 132 | QTest::newRow(dataTag: "1" ) << QString("mipmaps" ) << QSize(64, 64) << 7; |
| 133 | } |
| 134 | |
| 135 | void tst_qdds::testMipmaps() |
| 136 | { |
| 137 | QFETCH(QString, fileName); |
| 138 | QFETCH(QSize, size); |
| 139 | QFETCH(int, imageCount); |
| 140 | |
| 141 | const QString path = QStringLiteral(":/dds/" ) + fileName + QStringLiteral(".dds" ); |
| 142 | QImageReader reader(path); |
| 143 | QVERIFY(reader.canRead()); |
| 144 | QCOMPARE(reader.imageCount(), imageCount); |
| 145 | |
| 146 | for (int i = 0; i < reader.imageCount(); ++i) { |
| 147 | reader.jumpToImage(imageNumber: i); |
| 148 | QImage image = reader.read(); |
| 149 | QVERIFY2(!image.isNull(), qPrintable(reader.errorString())); |
| 150 | QCOMPARE(image.size(), size / (1 << i)); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void tst_qdds::testWriteImage_data() |
| 155 | { |
| 156 | QTest::addColumn<QString>(name: "fileName" ); |
| 157 | QTest::addColumn<QSize>(name: "size" ); |
| 158 | |
| 159 | QTest::newRow(dataTag: "1" ) << QString("A8R8G8B8" ) << QSize(64, 64); |
| 160 | QTest::newRow(dataTag: "2" ) << QString("A8R8G8B8.2" ) << QSize(64, 32); |
| 161 | } |
| 162 | |
| 163 | void tst_qdds::testWriteImage() |
| 164 | { |
| 165 | QFETCH(QString, fileName); |
| 166 | QFETCH(QSize, size); |
| 167 | |
| 168 | const QString path = fileName + QStringLiteral(".dds" ); |
| 169 | const QString sourcePath = QStringLiteral(":/dds/" ) + fileName + QStringLiteral(".dds" ); |
| 170 | const QByteArray subType = fileName.left(n: fileName.lastIndexOf(c: QLatin1Char('.'))).toLatin1(); |
| 171 | |
| 172 | QImage image(sourcePath); |
| 173 | QVERIFY(!image.isNull()); |
| 174 | QVERIFY(image.size() == size); |
| 175 | |
| 176 | QImageWriter writer(path, QByteArrayLiteral("dds" )); |
| 177 | QVERIFY2(writer.canWrite(), qPrintable(writer.errorString())); |
| 178 | writer.setSubType(subType); |
| 179 | QVERIFY2(writer.write(image), qPrintable(writer.errorString())); |
| 180 | |
| 181 | QVERIFY(image == QImage(path)); |
| 182 | |
| 183 | QImageReader reader(path); |
| 184 | QVERIFY(reader.canRead()); |
| 185 | QCOMPARE(reader.size(), size); |
| 186 | QCOMPARE(reader.subType(), subType); |
| 187 | } |
| 188 | |
| 189 | QTEST_MAIN(tst_qdds) |
| 190 | #include "tst_qdds.moc" |
| 191 | |