File indexing completed on 2023-09-24 08:04:29
0001 /* 0002 SPDX-FileCopyrightText: 2013 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 <QCommandLineOption> 0010 #include <QCommandLineParser> 0011 #include <QCoreApplication> 0012 #include <QDebug> 0013 #include <QImageReader> 0014 #include <QImageWriter> 0015 #include <QTextStream> 0016 0017 int main(int argc, char **argv) 0018 { 0019 QCoreApplication app(argc, argv); 0020 QCoreApplication::addLibraryPath(QStringLiteral(PLUGIN_DIR)); 0021 QCoreApplication::setApplicationName(QStringLiteral("imageconverter")); 0022 QCoreApplication::setApplicationVersion(QStringLiteral("1.01.01.0")); 0023 0024 QCommandLineParser parser; 0025 parser.setApplicationDescription(QStringLiteral("Converts images from one format to another")); 0026 parser.addHelpOption(); 0027 parser.addVersionOption(); 0028 parser.addPositionalArgument(QStringLiteral("in"), QStringLiteral("input image file")); 0029 parser.addPositionalArgument(QStringLiteral("out"), QStringLiteral("output image file")); 0030 QCommandLineOption informat(QStringList() << QStringLiteral("i") << QStringLiteral("informat"), 0031 QStringLiteral("Image format for input file"), 0032 QStringLiteral("format")); 0033 parser.addOption(informat); 0034 QCommandLineOption outformat(QStringList() << QStringLiteral("o") << QStringLiteral("outformat"), 0035 QStringLiteral("Image format for output file"), 0036 QStringLiteral("format")); 0037 parser.addOption(outformat); 0038 QCommandLineOption listformats(QStringList() << QStringLiteral("l") << QStringLiteral("list"), QStringLiteral("List supported image formats")); 0039 parser.addOption(listformats); 0040 0041 QCommandLineOption listmimes(QStringList() << QStringLiteral("m") << QStringLiteral("listmime"), QStringLiteral("List supported image mime formats")); 0042 parser.addOption(listmimes); 0043 0044 parser.process(app); 0045 0046 const QStringList files = parser.positionalArguments(); 0047 0048 if (parser.isSet(listformats)) { 0049 QTextStream out(stdout); 0050 out << "Input formats:\n"; 0051 const auto lstReaderSupportedFormats = QImageReader::supportedImageFormats(); 0052 for (const QByteArray &fmt : lstReaderSupportedFormats) { 0053 out << " " << fmt << '\n'; 0054 } 0055 out << "Output formats:\n"; 0056 const auto lstWriterSupportedFormats = QImageWriter::supportedImageFormats(); 0057 for (const QByteArray &fmt : lstWriterSupportedFormats) { 0058 out << " " << fmt << '\n'; 0059 } 0060 return 0; 0061 } 0062 0063 if (parser.isSet(listmimes)) { 0064 QTextStream out(stdout); 0065 out << "Input mime formats:\n"; 0066 const auto lstReaderSupportedMimes = QImageReader::supportedMimeTypes(); 0067 for (const QByteArray &fmt : lstReaderSupportedMimes) { 0068 out << " " << fmt << '\n'; 0069 } 0070 out << "Output mime formats:\n"; 0071 const auto lstWriterSupportedMimes = QImageWriter::supportedMimeTypes(); 0072 for (const QByteArray &fmt : lstWriterSupportedMimes) { 0073 out << " " << fmt << '\n'; 0074 } 0075 return 0; 0076 } 0077 0078 if (files.count() != 2) { 0079 QTextStream(stdout) << "Must provide exactly two files\n"; 0080 parser.showHelp(1); 0081 } 0082 QImageReader reader(files.at(0), parser.value(informat).toLatin1()); 0083 QImage img = reader.read(); 0084 if (img.isNull()) { 0085 QTextStream(stdout) << "Could not read image: " << reader.errorString() << '\n'; 0086 return 2; 0087 } 0088 0089 QImageWriter writer(files.at(1), parser.value(outformat).toLatin1()); 0090 if (!writer.write(img)) { 0091 QTextStream(stdout) << "Could not write image: " << writer.errorString() << '\n'; 0092 return 3; 0093 } 0094 0095 return 0; 0096 }