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