| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtAddOn.ImageFormats module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include <QtTest/QtTest> |
| 30 | #include <QtGui/QtGui> |
| 31 | |
| 32 | class tst_qwebp : public QObject |
| 33 | { |
| 34 | Q_OBJECT |
| 35 | |
| 36 | private slots: |
| 37 | void initTestCase(); |
| 38 | void readImage_data(); |
| 39 | void readImage(); |
| 40 | void readAnimation_data(); |
| 41 | void readAnimation(); |
| 42 | void writeImage_data(); |
| 43 | void writeImage(); |
| 44 | }; |
| 45 | |
| 46 | void tst_qwebp::initTestCase() |
| 47 | { |
| 48 | if (!QImageReader::supportedImageFormats().contains(t: "webp" )) |
| 49 | QSKIP("The image format handler is not installed." ); |
| 50 | } |
| 51 | |
| 52 | void tst_qwebp::readImage_data() |
| 53 | { |
| 54 | QTest::addColumn<QString>(name: "fileName" ); |
| 55 | QTest::addColumn<QSize>(name: "size" ); |
| 56 | QTest::addColumn<bool>(name: "alpha" ); |
| 57 | |
| 58 | QTest::newRow(dataTag: "kollada" ) << QString("kollada" ) << QSize(436, 160) << true; |
| 59 | QTest::newRow(dataTag: "kollada_lossless" ) << QString("kollada_lossless" ) << QSize(436, 160) << true; |
| 60 | QTest::newRow(dataTag: "kollada_noalpha" ) << QString("kollada_noalpha" ) << QSize(436, 160) << false; |
| 61 | } |
| 62 | |
| 63 | void tst_qwebp::readImage() |
| 64 | { |
| 65 | QFETCH(QString, fileName); |
| 66 | QFETCH(QSize, size); |
| 67 | QFETCH(bool, alpha); |
| 68 | |
| 69 | const QString path = QStringLiteral(":/images/" ) + fileName + QStringLiteral(".webp" ); |
| 70 | QImageReader reader(path); |
| 71 | QVERIFY(reader.canRead()); |
| 72 | QImage image = reader.read(); |
| 73 | QVERIFY2(!image.isNull(), qPrintable(reader.errorString())); |
| 74 | QCOMPARE(image.size(), size); |
| 75 | QCOMPARE(image.hasAlphaChannel(), alpha); |
| 76 | } |
| 77 | |
| 78 | void tst_qwebp::readAnimation_data() |
| 79 | { |
| 80 | QTest::addColumn<QString>(name: "fileName" ); |
| 81 | QTest::addColumn<QSize>(name: "size" ); |
| 82 | QTest::addColumn<int>(name: "imageCount" ); |
| 83 | QTest::addColumn<int>(name: "loopCount" ); |
| 84 | QTest::addColumn<QColor>(name: "bgColor" ); |
| 85 | QTest::addColumn<QList<QRect> >(name: "imageRects" ); |
| 86 | QTest::addColumn<QList<int> >(name: "imageDelays" ); |
| 87 | |
| 88 | QTest::newRow(dataTag: "kollada" ) |
| 89 | << QString("kollada" ) |
| 90 | << QSize(436, 160) |
| 91 | << 1 |
| 92 | << 0 |
| 93 | << QColor() |
| 94 | << (QList<QRect>() << QRect(0, 0, 436, 160)) |
| 95 | << (QList<int>() << 0); |
| 96 | QTest::newRow(dataTag: "kollada_animation" ) |
| 97 | << QString("kollada_animation" ) |
| 98 | << QSize(536, 260) |
| 99 | << 2 |
| 100 | << 2 |
| 101 | << QColor(128, 128, 128, 128) |
| 102 | << (QList<QRect>() << QRect(0, 0, 436, 160) << QRect(100, 100, 436, 160)) |
| 103 | << (QList<int>() << 1000 << 1200); |
| 104 | } |
| 105 | |
| 106 | void tst_qwebp::readAnimation() |
| 107 | { |
| 108 | QFETCH(QString, fileName); |
| 109 | QFETCH(QSize, size); |
| 110 | QFETCH(int, imageCount); |
| 111 | QFETCH(int, loopCount); |
| 112 | QFETCH(QColor, bgColor); |
| 113 | QFETCH(QList<QRect>, imageRects); |
| 114 | QFETCH(QList<int>, imageDelays); |
| 115 | |
| 116 | const QString path = QStringLiteral(":/images/" ) + fileName + QStringLiteral(".webp" ); |
| 117 | QImageReader reader(path); |
| 118 | QVERIFY(reader.canRead()); |
| 119 | QCOMPARE(reader.size(), size); |
| 120 | QCOMPARE(reader.imageCount(), imageCount); |
| 121 | QCOMPARE(reader.loopCount(), loopCount); |
| 122 | QCOMPARE(reader.backgroundColor(), bgColor); |
| 123 | |
| 124 | for (int i = 0; i < reader.imageCount(); ++i) { |
| 125 | QImage image = reader.read(); |
| 126 | QVERIFY2(!image.isNull(), qPrintable(reader.errorString())); |
| 127 | QCOMPARE(image.size(), size); |
| 128 | QCOMPARE(reader.currentImageNumber(), i); |
| 129 | QCOMPARE(reader.currentImageRect(), imageRects[i]); |
| 130 | QCOMPARE(reader.nextImageDelay(), imageDelays[i]); |
| 131 | } |
| 132 | |
| 133 | QVERIFY(reader.read().isNull()); |
| 134 | QCOMPARE(reader.canRead(), !reader.supportsAnimation()); |
| 135 | } |
| 136 | |
| 137 | void tst_qwebp::writeImage_data() |
| 138 | { |
| 139 | QTest::addColumn<QString>(name: "fileName" ); |
| 140 | QTest::addColumn<QString>(name: "postfix" ); |
| 141 | QTest::addColumn<int>(name: "quality" ); |
| 142 | QTest::addColumn<QSize>(name: "size" ); |
| 143 | QTest::addColumn<bool>(name: "alpha" ); |
| 144 | QTest::addColumn<bool>(name: "needcheck" ); |
| 145 | |
| 146 | QTest::newRow(dataTag: "kollada-75" ) << QString("kollada" ) << QString(".png" ) << 75 << QSize(436, 160) << true << false; |
| 147 | QTest::newRow(dataTag: "kollada-100" ) << QString("kollada" ) << QString(".png" ) << 100 << QSize(436, 160) << true << true; |
| 148 | QTest::newRow(dataTag: "kollada_noalpha-75" ) << QString("kollada_noalpha" ) << QString(".webp" ) << 75 << QSize(436, 160) << false << false; |
| 149 | QTest::newRow(dataTag: "kollada_noalpha-100" ) << QString("kollada_noalpha" ) << QString(".webp" ) << 100 << QSize(436, 160) << false << true; |
| 150 | } |
| 151 | |
| 152 | void tst_qwebp::writeImage() |
| 153 | { |
| 154 | QFETCH(QString, fileName); |
| 155 | QFETCH(QString, postfix); |
| 156 | QFETCH(int, quality); |
| 157 | QFETCH(QSize, size); |
| 158 | QFETCH(bool, alpha); |
| 159 | QFETCH(bool, needcheck); |
| 160 | |
| 161 | const QString path = QString("%1-%2.webp" ).arg(a: fileName).arg(a: quality); |
| 162 | const QString sourcePath = QStringLiteral(":/images/" ) + fileName + postfix; |
| 163 | |
| 164 | QImage image(sourcePath); |
| 165 | QVERIFY(!image.isNull()); |
| 166 | QVERIFY(image.size() == size); |
| 167 | |
| 168 | QImageWriter writer(path, QByteArrayLiteral("webp" )); |
| 169 | QVERIFY2(writer.canWrite(), qPrintable(writer.errorString())); |
| 170 | writer.setQuality(quality); |
| 171 | QVERIFY2(writer.write(image), qPrintable(writer.errorString())); |
| 172 | |
| 173 | QImage reread(path); |
| 174 | QVERIFY(!reread.isNull()); |
| 175 | QVERIFY(reread.size() == size); |
| 176 | QVERIFY(reread.hasAlphaChannel() == alpha); |
| 177 | |
| 178 | if (needcheck) |
| 179 | QVERIFY(image == reread); |
| 180 | } |
| 181 | |
| 182 | QTEST_MAIN(tst_qwebp) |
| 183 | #include "tst_qwebp.moc" |
| 184 | |