1 /*
2     SPDX-FileCopyrightText: 2014 Alex Merry <alex.merry@kdemail.net>
3 
4     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 
7 #include <stdio.h>
8 
9 #include <QBuffer>
10 #include <QCommandLineParser>
11 #include <QCoreApplication>
12 #include <QDir>
13 #include <QFileInfo>
14 #include <QImage>
15 #include <QImageReader>
16 #include <QImageWriter>
17 #include <QTextStream>
18 
main(int argc,char ** argv)19 int main(int argc, char **argv)
20 {
21     QCoreApplication app(argc, argv);
22     QCoreApplication::removeLibraryPath(QStringLiteral(PLUGIN_DIR));
23     QCoreApplication::addLibraryPath(QStringLiteral(PLUGIN_DIR));
24     QCoreApplication::setApplicationName(QStringLiteral("readtest"));
25     QCoreApplication::setApplicationVersion(QStringLiteral("1.0.0"));
26 
27     QCommandLineParser parser;
28     parser.setApplicationDescription(QStringLiteral("Performs basic image conversion checking."));
29     parser.addHelpOption();
30     parser.addVersionOption();
31     parser.addPositionalArgument(QStringLiteral("format"), QStringLiteral("format to test."));
32     QCommandLineOption lossless(QStringList() << QStringLiteral("l") << QStringLiteral("lossless"),
33                                 QStringLiteral("Check that reading back the data gives the same image."));
34     parser.addOption(lossless);
35 
36     parser.process(app);
37 
38     const QStringList args = parser.positionalArguments();
39     if (args.count() < 1) {
40         QTextStream(stderr) << "Must provide a format\n";
41         parser.showHelp(1);
42     } else if (args.count() > 1) {
43         QTextStream(stderr) << "Too many arguments\n";
44         parser.showHelp(1);
45     }
46 
47     QString suffix = args.at(0);
48     QByteArray format = suffix.toLatin1();
49 
50     QDir imgdir(QStringLiteral(IMAGEDIR));
51     imgdir.setNameFilters(QStringList(QLatin1String("*.") + suffix));
52     imgdir.setFilter(QDir::Files);
53 
54     int passed = 0;
55     int failed = 0;
56 
57     QTextStream(stdout) << "********* "
58                         << "Starting basic write tests for " << suffix << " images *********\n";
59     const QFileInfoList lstImgDir = imgdir.entryInfoList();
60     for (const QFileInfo &fi : lstImgDir) {
61         int suffixPos = fi.filePath().count() - suffix.count();
62         QString pngfile = fi.filePath().replace(suffixPos, suffix.count(), QStringLiteral("png"));
63         QString pngfilename = QFileInfo(pngfile).fileName();
64 
65         QImageReader pngReader(pngfile, "png");
66         QImage pngImage;
67         if (!pngReader.read(&pngImage)) {
68             QTextStream(stdout) << "ERROR: " << fi.fileName() << ": could not load " << pngfilename << ": " << pngReader.errorString() << "\n";
69             ++failed;
70             continue;
71         }
72 
73         QFile expFile(fi.filePath());
74         if (!expFile.open(QIODevice::ReadOnly)) {
75             QTextStream(stdout) << "ERROR: " << fi.fileName() << ": could not open " << fi.fileName() << ": " << expFile.errorString() << "\n";
76             ++failed;
77             continue;
78         }
79         QByteArray expData = expFile.readAll();
80         if (expData.isEmpty()) {
81             // check if there was actually anything to read
82             expFile.reset();
83             char buf[1];
84             qint64 result = expFile.read(buf, 1);
85             if (result < 0) {
86                 QTextStream(stdout) << "ERROR: " << fi.fileName() << ": could not load " << fi.fileName() << ": " << expFile.errorString() << "\n";
87                 ++failed;
88                 continue;
89             }
90         }
91 
92         QByteArray writtenData;
93         {
94             QBuffer buffer(&writtenData);
95             QImageWriter imgWriter(&buffer, format.constData());
96             if (!imgWriter.write(pngImage)) {
97                 QTextStream(stdout) << "FAIL : " << fi.fileName() << ": failed to write image data\n";
98                 ++failed;
99                 continue;
100             }
101         }
102 
103         if (expData != writtenData) {
104             QTextStream(stdout) << "FAIL : " << fi.fileName() << ": written data differs from " << fi.fileName() << "\n";
105             ++failed;
106             continue;
107         }
108 
109         QImage reReadImage;
110         {
111             QBuffer buffer(&writtenData);
112             QImageReader imgReader(&buffer, format.constData());
113             if (!imgReader.read(&reReadImage)) {
114                 QTextStream(stdout) << "FAIL : " << fi.fileName() << ": could not read back the written data\n";
115                 ++failed;
116                 continue;
117             }
118             reReadImage = reReadImage.convertToFormat(pngImage.format());
119         }
120 
121         if (parser.isSet(lossless)) {
122             if (pngImage != reReadImage) {
123                 QTextStream(stdout) << "FAIL : " << fi.fileName() << ": re-reading the data resulted in a different image\n";
124                 ++failed;
125                 continue;
126             }
127         }
128 
129         QTextStream(stdout) << "PASS : " << fi.fileName() << "\n";
130         ++passed;
131     }
132 
133     QTextStream(stdout) << "Totals: " << passed << " passed, " << failed << " failed\n";
134     QTextStream(stdout) << "********* "
135                         << "Finished basic write tests for " << suffix << " images *********\n";
136 
137     return failed == 0 ? 0 : 1;
138 }
139