File indexing completed on 2024-04-21 03:54:35

0001 /*
0002     SPDX-FileCopyrightText: 2014 Alex Merry <alex.merry@kdemail.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include <stdio.h>
0008 
0009 #include <QBuffer>
0010 #include <QFile>
0011 #include <QFileInfo>
0012 #include <QImage>
0013 #include <QImageReader>
0014 #include <QImageWriter>
0015 #include <QTest>
0016 #include <QUuid>
0017 
0018 Q_DECLARE_METATYPE(QImage::Format)
0019 
0020 class PicTests : public QObject
0021 {
0022     Q_OBJECT
0023 
0024 private:
0025     void common_data()
0026     {
0027         QTest::addColumn<QString>("picfile");
0028         QTest::addColumn<QString>("pngfile");
0029         QTest::addColumn<QString>("comment");
0030         // whether the pic file has/should have an alpha channel
0031         QTest::addColumn<bool>("alpha");
0032         // the format to convert the png file to before writing
0033         // or comparing to the read image; this can be used to
0034         // induce loss of data (eg: make the image monochrome)
0035         QTest::addColumn<QImage::Format>("pngformat");
0036         QTest::addColumn<bool>("compress");
0037 
0038         QTest::newRow("4x4 no alpha RLE") << QFINDTESTDATA("pic/4x4-simple-color.pic") << QFINDTESTDATA("pic/4x4-simple-color.png") << QString() << false
0039                                           << QImage::Format_RGB32 << true;
0040 
0041         QTest::newRow("4x4 no alpha raw") << QFINDTESTDATA("pic/4x4-simple-color-uncompressed.pic") << QFINDTESTDATA("pic/4x4-simple-color.png") << QString()
0042                                           << false << QImage::Format_RGB32 << false;
0043 
0044         QTest::newRow("Short comment") << QFINDTESTDATA("pic/short-comment.pic") << QFINDTESTDATA("pic/4x4-simple-color.png")
0045                                        << QStringLiteral("Test comment value") << false << QImage::Format_RGB32 << true;
0046 
0047         QTest::newRow("Long comment") << QFINDTESTDATA("pic/long-comment.pic") << QFINDTESTDATA("pic/4x4-simple-color.png")
0048                                       << QStringLiteral("Test comment value that goes right up to the end of the comment field and has no") << false
0049                                       << QImage::Format_RGB32 << true;
0050 
0051         QTest::newRow("Long run-lengths") << QFINDTESTDATA("pic/long-runs.pic") << QFINDTESTDATA("pic/long-runs.png") << QString() << false
0052                                           << QImage::Format_RGB32 << true;
0053 
0054         QTest::newRow("4x4 with alpha RLE") << QFINDTESTDATA("pic/4x4-alpha.pic") << QFINDTESTDATA("pic/4x4-alpha.png") << QString() << true
0055                                             << QImage::Format_ARGB32 << true;
0056 
0057         QTest::newRow("4x4 with alpha raw") << QFINDTESTDATA("pic/4x4-alpha-uncompressed.pic") << QFINDTESTDATA("pic/4x4-alpha.png") << QString() << true
0058                                             << QImage::Format_ARGB32 << false;
0059     }
0060 
0061 private Q_SLOTS:
0062     void initTestCase()
0063     {
0064         QCoreApplication::addLibraryPath(QStringLiteral(PLUGIN_DIR));
0065     }
0066 
0067     void testWrite_data()
0068     {
0069         common_data();
0070 
0071         // NB: 4x4-simple-color only uses solid red, blue, green and white,
0072         //     so there is no actual data loss in converting to RGB16.
0073         //     This just tests that the pic plugin can deal with different
0074         //     input formats.
0075         QTest::newRow("altered format") << QFINDTESTDATA("pic/4x4-simple-color.pic") << QFINDTESTDATA("pic/4x4-simple-color.png") << QString() << false
0076                                         << QImage::Format_RGB16 << true;
0077     }
0078 
0079     void testRead_data()
0080     {
0081         common_data();
0082 
0083         // TODO: test reading files with unusual channel setups
0084         //       (eg: one channel for each component)
0085     }
0086 
0087     void testWrite()
0088     {
0089         QFETCH(QString, picfile);
0090         QFETCH(QString, pngfile);
0091         QFETCH(QString, comment);
0092         QFETCH(QImage::Format, pngformat);
0093         QFETCH(bool, compress);
0094 
0095         QImageReader pngReader(pngfile, "png");
0096         QImage pngImage;
0097         QVERIFY2(pngReader.read(&pngImage), qPrintable(pngReader.errorString()));
0098         pngImage = pngImage.convertToFormat(pngformat);
0099 
0100         QFile expFile(picfile);
0101         QVERIFY2(expFile.open(QIODevice::ReadOnly), qPrintable(expFile.errorString()));
0102         QByteArray expData = expFile.readAll();
0103 
0104         QByteArray picData;
0105         QBuffer buffer(&picData);
0106         QImageWriter imgWriter(&buffer, "pic");
0107         imgWriter.setText(QStringLiteral("Description"), comment);
0108         imgWriter.setCompression(compress);
0109         imgWriter.write(pngImage);
0110 
0111         if (expData != picData) {
0112             QString fileNameBase = QUuid::createUuid().toString().remove(QLatin1Char('{')).remove(QLatin1Char('}'));
0113             QFile dumpFile(fileNameBase + QStringLiteral(".pic"));
0114             QVERIFY2(dumpFile.open(QIODevice::WriteOnly), qPrintable(dumpFile.errorString()));
0115             dumpFile.write(picData);
0116             QString msg =
0117                 QStringLiteral("Written data (") + dumpFile.fileName() + QStringLiteral(") differed from expected data (") + picfile + QLatin1Char(')');
0118             QFAIL(qPrintable(msg));
0119         }
0120     }
0121 
0122     void testRead()
0123     {
0124         QFETCH(QString, picfile);
0125         QFETCH(QString, pngfile);
0126         QFETCH(bool, alpha);
0127         QFETCH(QImage::Format, pngformat);
0128 
0129         QImageReader inputReader(picfile, "pic");
0130         QImageReader expReader(pngfile, "png");
0131 
0132         QImage inputImage;
0133         QImage expImage;
0134 
0135         QVERIFY2(expReader.read(&expImage), qPrintable(expReader.errorString()));
0136         QVERIFY2(inputReader.read(&inputImage), qPrintable(inputReader.errorString()));
0137 
0138         QCOMPARE(inputImage.width(), expImage.width());
0139         QCOMPARE(inputImage.height(), expImage.height());
0140         QCOMPARE(inputImage.hasAlphaChannel(), alpha);
0141         QCOMPARE(inputImage.format(), alpha ? QImage::Format_ARGB32 : QImage::Format_RGB32);
0142 
0143         expImage = expImage.convertToFormat(pngformat);
0144         expImage = expImage.convertToFormat(alpha ? QImage::Format_ARGB32 : QImage::Format_RGB32);
0145         if (inputImage != expImage) {
0146             QString fileNameBase = QUuid::createUuid().toString().remove(QLatin1Char('{')).remove(QLatin1Char('}'));
0147             QFile picDumpFile(fileNameBase + QStringLiteral("-expected.data"));
0148             QVERIFY2(picDumpFile.open(QIODevice::WriteOnly), qPrintable(picDumpFile.errorString()));
0149             picDumpFile.write(reinterpret_cast<const char *>(inputImage.bits()), inputImage.sizeInBytes());
0150             QFile pngDumpFile(fileNameBase + QStringLiteral("-actual.data"));
0151             QVERIFY2(pngDumpFile.open(QIODevice::WriteOnly), qPrintable(pngDumpFile.errorString()));
0152             pngDumpFile.write(reinterpret_cast<const char *>(expImage.bits()), expImage.sizeInBytes());
0153             QString msg = QStringLiteral("Read image (") + picDumpFile.fileName() + QStringLiteral(") differed from expected image (") + pngDumpFile.fileName()
0154                 + QLatin1Char(')');
0155             QFAIL(qPrintable(msg));
0156         }
0157     }
0158 
0159     void testPreReadComment_data()
0160     {
0161         testRead_data();
0162     }
0163 
0164     void testPreReadComment()
0165     {
0166         QFETCH(QString, picfile);
0167         QFETCH(QString, comment);
0168 
0169         QImageReader inputReader(picfile, "pic");
0170 
0171         QCOMPARE(inputReader.text(QStringLiteral("Description")), comment);
0172     }
0173 
0174     void testPreReadSize_data()
0175     {
0176         testRead_data();
0177     }
0178 
0179     void testPreReadSize()
0180     {
0181         QFETCH(QString, picfile);
0182         QFETCH(QString, pngfile);
0183 
0184         QImageReader inputReader(picfile, "pic");
0185         QImageReader expReader(pngfile, "png");
0186 
0187         QCOMPARE(inputReader.size(), expReader.size());
0188     }
0189 
0190     void testPreReadImageFormat_data()
0191     {
0192         testRead_data();
0193     }
0194 
0195     void testPreReadImageFormat()
0196     {
0197         QFETCH(QString, picfile);
0198         QFETCH(bool, alpha);
0199 
0200         QImageReader inputReader(picfile, "pic");
0201 
0202         QCOMPARE(inputReader.imageFormat(), alpha ? QImage::Format_ARGB32 : QImage::Format_RGB32);
0203     }
0204 };
0205 
0206 QTEST_MAIN(PicTests)
0207 
0208 #include "pictest.moc"