| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the autotests in the Qt ImageFormats module. |
| 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_qheif: public QObject |
| 33 | { |
| 34 | Q_OBJECT |
| 35 | |
| 36 | private slots: |
| 37 | void initTestCase(); |
| 38 | void readImage_data(); |
| 39 | void readImage(); |
| 40 | void readProperties_data(); |
| 41 | void readProperties(); |
| 42 | void writeImage(); |
| 43 | }; |
| 44 | |
| 45 | void tst_qheif::initTestCase() |
| 46 | { |
| 47 | if (!QImageReader::supportedImageFormats().contains(t: "heic" )) |
| 48 | QSKIP("The image format handler is not installed." ); |
| 49 | } |
| 50 | |
| 51 | void tst_qheif::readImage_data() |
| 52 | { |
| 53 | QTest::addColumn<QString>(name: "fileName" ); |
| 54 | QTest::addColumn<QSize>(name: "size" ); |
| 55 | QTest::addColumn<int>(name: "transform" ); |
| 56 | |
| 57 | QTest::newRow(dataTag: "col" ) << QString("col320x480.heic" ) << QSize(320, 480) << int(QImageIOHandler::TransformationNone); |
| 58 | QTest::newRow(dataTag: "rot" ) << QString("newlogoCCW.heic" ) << QSize(110, 78) << int(QImageIOHandler::TransformationRotate90); |
| 59 | } |
| 60 | |
| 61 | void tst_qheif::readImage() |
| 62 | { |
| 63 | QFETCH(QString, fileName); |
| 64 | QFETCH(QSize, size); |
| 65 | |
| 66 | QString path = QStringLiteral(":/heif/" ) + fileName; |
| 67 | QImageReader reader(path); |
| 68 | QVERIFY(reader.canRead()); |
| 69 | QImage image = reader.read(); |
| 70 | QVERIFY(!image.isNull()); |
| 71 | QCOMPARE(image.size(), size); |
| 72 | } |
| 73 | |
| 74 | void tst_qheif::readProperties_data() |
| 75 | { |
| 76 | readImage_data(); |
| 77 | } |
| 78 | |
| 79 | void tst_qheif::readProperties() |
| 80 | { |
| 81 | QFETCH(QString, fileName); |
| 82 | QFETCH(QSize, size); |
| 83 | QFETCH(int, transform); |
| 84 | |
| 85 | QSize rawSize = (transform & QImageIOHandler::TransformationRotate90) ? size.transposed() : size; |
| 86 | |
| 87 | QString path = QStringLiteral(":/heif/" ) + fileName; |
| 88 | QImageReader reader(path); |
| 89 | QCOMPARE(reader.size(), rawSize); |
| 90 | QCOMPARE(int(reader.transformation()), transform); |
| 91 | |
| 92 | QImage image = reader.read(); |
| 93 | QCOMPARE(image.size(), size); |
| 94 | |
| 95 | QCOMPARE(reader.size(), rawSize); |
| 96 | QCOMPARE(int(reader.transformation()), transform); |
| 97 | } |
| 98 | |
| 99 | void tst_qheif::writeImage() |
| 100 | { |
| 101 | QImage img(20, 10, QImage::Format_ARGB32_Premultiplied); |
| 102 | img.fill(color: Qt::green); |
| 103 | |
| 104 | QBuffer buf1, buf2; |
| 105 | QImage rimg1; |
| 106 | |
| 107 | { |
| 108 | buf1.open(openMode: QIODevice::WriteOnly); |
| 109 | QImageWriter writer(&buf1, "heic" ); |
| 110 | QVERIFY(writer.write(img)); |
| 111 | buf1.close(); |
| 112 | QVERIFY(buf1.size() > 0); |
| 113 | |
| 114 | buf1.open(openMode: QIODevice::ReadOnly); |
| 115 | QImageReader reader(&buf1); |
| 116 | QVERIFY(reader.read(&rimg1)); |
| 117 | buf1.close(); |
| 118 | QVERIFY(rimg1.size() == img.size()); |
| 119 | } |
| 120 | |
| 121 | { |
| 122 | buf2.open(openMode: QIODevice::WriteOnly); |
| 123 | QImageWriter writer(&buf2, "heic" ); |
| 124 | writer.setQuality(20); |
| 125 | QVERIFY(writer.write(img)); |
| 126 | buf2.close(); |
| 127 | QVERIFY(buf2.size() > 0); |
| 128 | QVERIFY(buf2.size() < buf1.size()); |
| 129 | } |
| 130 | |
| 131 | { |
| 132 | buf2.open(openMode: QIODevice::WriteOnly); |
| 133 | QImageWriter writer(&buf2, "heic" ); |
| 134 | writer.setTransformation(QImageIOHandler::TransformationRotate270); |
| 135 | QVERIFY(writer.write(img)); |
| 136 | buf2.close(); |
| 137 | |
| 138 | QImage rimg2; |
| 139 | buf2.open(openMode: QIODevice::ReadOnly); |
| 140 | QImageReader reader(&buf2); |
| 141 | QVERIFY(reader.read(&rimg2)); |
| 142 | buf2.close(); |
| 143 | QVERIFY(rimg2.size() == img.size().transposed()); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | QTEST_MAIN(tst_qheif) |
| 148 | #include "tst_qheif.moc" |
| 149 | |