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

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 <QFile>
0014 #include <QImageReader>
0015 #include <QMetaEnum>
0016 #include <QMetaObject>
0017 #include <QTextStream>
0018 
0019 #include "format-enum.h"
0020 
0021 int main(int argc, char **argv)
0022 {
0023     QCoreApplication app(argc, argv);
0024     QCoreApplication::addLibraryPath(QStringLiteral(PLUGIN_DIR));
0025     QCoreApplication::setApplicationName(QStringLiteral("imagedump"));
0026     QCoreApplication::setApplicationVersion(QStringLiteral("1.0.0.0"));
0027 
0028     QCommandLineParser parser;
0029     parser.setApplicationDescription(QStringLiteral("Dumps the content of QImage::bits()"));
0030     parser.addHelpOption();
0031     parser.addVersionOption();
0032     parser.addPositionalArgument(QStringLiteral("image"), QStringLiteral("image file"));
0033     parser.addPositionalArgument(QStringLiteral("datafile"), QStringLiteral("file QImage data should be written to"));
0034     QCommandLineOption informat(QStringList() << QStringLiteral("f") << QStringLiteral("file-format"),
0035                                 QStringLiteral("Image file format"),
0036                                 QStringLiteral("format"));
0037     parser.addOption(informat);
0038     QCommandLineOption qimgformat(QStringList() << QStringLiteral("q") << QStringLiteral("qimage-format"),
0039                                   QStringLiteral("QImage data format"),
0040                                   QStringLiteral("format"));
0041     parser.addOption(qimgformat);
0042     QCommandLineOption listformats(QStringList() << QStringLiteral("l") << QStringLiteral("list-file-formats"),
0043                                    QStringLiteral("List supported image file formats"));
0044     parser.addOption(listformats);
0045     QCommandLineOption listmimetypes(QStringList() << QStringLiteral("m") << QStringLiteral("list-mime-types"),
0046                                      QStringLiteral("List supported image mime types"));
0047     parser.addOption(listmimetypes);
0048     QCommandLineOption listqformats(QStringList() << QStringLiteral("p") << QStringLiteral("list-qimage-formats"),
0049                                     QStringLiteral("List supported QImage data formats"));
0050     parser.addOption(listqformats);
0051 
0052     parser.process(app);
0053 
0054     const QStringList files = parser.positionalArguments();
0055 
0056     if (parser.isSet(listformats)) {
0057         QTextStream out(stdout);
0058         out << "File formats:\n";
0059         const auto lstSupportedFormats = QImageReader::supportedImageFormats();
0060         for (const auto &fmt : lstSupportedFormats) {
0061             out << "  " << fmt << '\n';
0062         }
0063         return 0;
0064     }
0065     if (parser.isSet(listmimetypes)) {
0066         QTextStream out(stdout);
0067         out << "MIME types:\n";
0068         const auto lstSupportedMimeTypes = QImageReader::supportedMimeTypes();
0069         for (const auto &fmt : lstSupportedMimeTypes) {
0070             out << "  " << fmt << '\n';
0071         }
0072         return 0;
0073     }
0074     if (parser.isSet(listqformats)) {
0075         QTextStream out(stdout);
0076         out << "QImage formats:\n";
0077         // skip QImage::Format_Invalid
0078         for (int i = 1; i < QImage::NImageFormats; ++i) {
0079             out << "  " << formatToString(static_cast<QImage::Format>(i)) << '\n';
0080         }
0081         return 0;
0082     }
0083 
0084     if (files.count() != 2) {
0085         QTextStream(stderr) << "Must provide exactly two files\n";
0086         parser.showHelp(1);
0087     }
0088     QImageReader reader(files.at(0), parser.value(informat).toLatin1());
0089     QImage img = reader.read();
0090     if (img.isNull()) {
0091         QTextStream(stderr) << "Could not read image: " << reader.errorString() << '\n';
0092         return 2;
0093     }
0094 
0095     QFile output(files.at(1));
0096     if (!output.open(QIODevice::WriteOnly)) {
0097         QTextStream(stderr) << "Could not open " << files.at(1) << " for writing: " << output.errorString() << '\n';
0098         return 3;
0099     }
0100     if (parser.isSet(qimgformat)) {
0101         QImage::Format qformat = formatFromString(parser.value(qimgformat));
0102         if (qformat == QImage::Format_Invalid) {
0103             QTextStream(stderr) << "Unknown QImage data format " << parser.value(qimgformat) << '\n';
0104             return 4;
0105         }
0106         img = img.convertToFormat(qformat);
0107     }
0108     qint64 written = output.write(reinterpret_cast<const char *>(img.bits()), img.sizeInBytes());
0109     if (written != img.sizeInBytes()) {
0110         QTextStream(stderr) << "Could not write image data to " << files.at(1) << ":" << output.errorString() << "\n";
0111         return 5;
0112     }
0113     QTextStream(stdout) << "Created " << files.at(1) << " with data format " << formatToString(img.format()) << "\n";
0114 
0115     return 0;
0116 }