File indexing completed on 2024-09-08 12:18:07
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 <QCommandLineParser> 0011 #include <QCoreApplication> 0012 #include <QDir> 0013 #include <QFileInfo> 0014 #include <QImage> 0015 #include <QImageReader> 0016 #include <QImageWriter> 0017 #include <QTextStream> 0018 0019 #include "fuzzyeq.cpp" 0020 0021 int main(int argc, char **argv) 0022 { 0023 QCoreApplication app(argc, argv); 0024 QCoreApplication::removeLibraryPath(QStringLiteral(PLUGIN_DIR)); 0025 QCoreApplication::addLibraryPath(QStringLiteral(PLUGIN_DIR)); 0026 QCoreApplication::setApplicationName(QStringLiteral("readtest")); 0027 QCoreApplication::setApplicationVersion(QStringLiteral("1.0.0")); 0028 0029 QCommandLineParser parser; 0030 parser.setApplicationDescription(QStringLiteral("Performs basic image conversion checking.")); 0031 parser.addHelpOption(); 0032 parser.addVersionOption(); 0033 parser.addPositionalArgument(QStringLiteral("format"), QStringLiteral("format to test.")); 0034 QCommandLineOption lossless(QStringList() << QStringLiteral("l") << QStringLiteral("lossless"), 0035 QStringLiteral("Check that reading back the data gives the same image.")); 0036 QCommandLineOption ignoreDataCheck({QStringLiteral("no-data-check")}, QStringLiteral("Don't check that write data is exactly the same.")); 0037 QCommandLineOption fuzz(QStringList() << QStringLiteral("f") << QStringLiteral("fuzz"), 0038 QStringLiteral("Allow for some deviation in ARGB data."), 0039 QStringLiteral("max")); 0040 parser.addOption(lossless); 0041 parser.addOption(ignoreDataCheck); 0042 parser.addOption(fuzz); 0043 0044 parser.process(app); 0045 0046 const QStringList args = parser.positionalArguments(); 0047 if (args.count() < 1) { 0048 QTextStream(stderr) << "Must provide a format\n"; 0049 parser.showHelp(1); 0050 } else if (args.count() > 1) { 0051 QTextStream(stderr) << "Too many arguments\n"; 0052 parser.showHelp(1); 0053 } 0054 0055 uchar fuzziness = 0; 0056 if (parser.isSet(fuzz)) { 0057 bool ok; 0058 uint fuzzarg = parser.value(fuzz).toUInt(&ok); 0059 if (!ok || fuzzarg > 255) { 0060 QTextStream(stderr) << "Error: max fuzz argument must be a number between 0 and 255\n"; 0061 parser.showHelp(1); 0062 } 0063 fuzziness = uchar(fuzzarg); 0064 } 0065 0066 QString suffix = args.at(0); 0067 QByteArray format = suffix.toLatin1(); 0068 0069 QDir imgdir(QStringLiteral(IMAGEDIR)); 0070 if (parser.isSet(ignoreDataCheck)) { 0071 imgdir.setNameFilters({QLatin1String("*.png")}); 0072 } else { 0073 imgdir.setNameFilters(QStringList(QLatin1String("*.") + suffix)); 0074 } 0075 imgdir.setFilter(QDir::Files); 0076 0077 int passed = 0; 0078 int failed = 0; 0079 0080 QTextStream(stdout) << "********* " 0081 << "Starting basic write tests for " << suffix << " images *********\n"; 0082 const QFileInfoList lstImgDir = imgdir.entryInfoList(); 0083 for (const QFileInfo &fi : lstImgDir) { 0084 QString pngfile; 0085 if (parser.isSet(ignoreDataCheck)) { 0086 pngfile = fi.filePath(); 0087 } else { 0088 int suffixPos = fi.filePath().count() - suffix.count(); 0089 pngfile = fi.filePath().replace(suffixPos, suffix.count(), QStringLiteral("png")); 0090 } 0091 QString pngfilename = QFileInfo(pngfile).fileName(); 0092 0093 QImageReader pngReader(pngfile, "png"); 0094 QImage pngImage; 0095 if (!pngReader.read(&pngImage)) { 0096 QTextStream(stdout) << "ERROR: " << fi.fileName() << ": could not load " << pngfilename << ": " << pngReader.errorString() << "\n"; 0097 ++failed; 0098 continue; 0099 } 0100 0101 QByteArray writtenData; 0102 { 0103 QBuffer buffer(&writtenData); 0104 QImageWriter imgWriter(&buffer, format.constData()); 0105 if (parser.isSet(lossless)) { 0106 imgWriter.setQuality(100); 0107 } 0108 if (!imgWriter.write(pngImage)) { 0109 QTextStream(stdout) << "FAIL : " << fi.fileName() << ": failed to write image data\n"; 0110 ++failed; 0111 continue; 0112 } 0113 } 0114 0115 if (!parser.isSet(ignoreDataCheck)) { 0116 QFile expFile(fi.filePath()); 0117 if (!expFile.open(QIODevice::ReadOnly)) { 0118 QTextStream(stdout) << "ERROR: " << fi.fileName() << ": could not open " << fi.fileName() << ": " << expFile.errorString() << "\n"; 0119 ++failed; 0120 continue; 0121 } 0122 QByteArray expData = expFile.readAll(); 0123 if (expData.isEmpty()) { 0124 // check if there was actually anything to read 0125 expFile.reset(); 0126 char buf[1]; 0127 qint64 result = expFile.read(buf, 1); 0128 if (result < 0) { 0129 QTextStream(stdout) << "ERROR: " << fi.fileName() << ": could not load " << fi.fileName() << ": " << expFile.errorString() << "\n"; 0130 ++failed; 0131 continue; 0132 } 0133 } 0134 0135 if (expData != writtenData) { 0136 QTextStream(stdout) << "FAIL : " << fi.fileName() << ": written data differs from " << fi.fileName() << "\n"; 0137 ++failed; 0138 continue; 0139 } 0140 } 0141 0142 QImage reReadImage; 0143 { 0144 QBuffer buffer(&writtenData); 0145 QImageReader imgReader(&buffer, format.constData()); 0146 if (!imgReader.read(&reReadImage)) { 0147 QTextStream(stdout) << "FAIL : " << fi.fileName() << ": could not read back the written data\n"; 0148 ++failed; 0149 continue; 0150 } 0151 reReadImage = reReadImage.convertToFormat(pngImage.format()); 0152 } 0153 0154 if (parser.isSet(lossless)) { 0155 if (!fuzzyeq(pngImage, reReadImage, fuzziness)) { 0156 QTextStream(stdout) << "FAIL : " << fi.fileName() << ": re-reading the data resulted in a different image\n"; 0157 if (pngImage.size() == reReadImage.size()) { 0158 for (int i = 0; i < pngImage.width(); ++i) { 0159 for (int j = 0; j < pngImage.height(); ++j) { 0160 if (pngImage.pixel(i, j) != reReadImage.pixel(i, j)) { 0161 QTextStream(stdout) << "Pixel is different " << i << ',' << j << ' ' << pngImage.pixel(i, j) << ' ' << reReadImage.pixel(i, j) 0162 << '\n'; 0163 } 0164 } 0165 } 0166 } 0167 ++failed; 0168 continue; 0169 } 0170 } 0171 0172 QTextStream(stdout) << "PASS : " << fi.fileName() << "\n"; 0173 ++passed; 0174 } 0175 0176 QTextStream(stdout) << "Totals: " << passed << " passed, " << failed << " failed\n"; 0177 QTextStream(stdout) << "********* " 0178 << "Finished basic write tests for " << suffix << " images *********\n"; 0179 0180 return failed == 0 ? 0 : 1; 0181 }