imagedump.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. SPDX-FileCopyrightText: 2013 Alex Merry <alex.merry@kdemail.net>
  3. SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
  4. */
  5. #include <stdio.h>
  6. #include <QCommandLineOption>
  7. #include <QCommandLineParser>
  8. #include <QCoreApplication>
  9. #include <QDebug>
  10. #include <QFile>
  11. #include <QImageReader>
  12. #include <QMetaEnum>
  13. #include <QMetaObject>
  14. #include <QTextStream>
  15. #include "format-enum.h"
  16. int main(int argc, char **argv)
  17. {
  18. QCoreApplication app(argc, argv);
  19. QCoreApplication::addLibraryPath(QStringLiteral(PLUGIN_DIR));
  20. QCoreApplication::setApplicationName(QStringLiteral("imagedump"));
  21. QCoreApplication::setApplicationVersion(QStringLiteral("1.0.0.0"));
  22. QCommandLineParser parser;
  23. parser.setApplicationDescription(QStringLiteral("Dumps the content of QImage::bits()"));
  24. parser.addHelpOption();
  25. parser.addVersionOption();
  26. parser.addPositionalArgument(QStringLiteral("image"), QStringLiteral("image file"));
  27. parser.addPositionalArgument(QStringLiteral("datafile"), QStringLiteral("file QImage data should be written to"));
  28. QCommandLineOption informat(QStringList() << QStringLiteral("f") << QStringLiteral("file-format"),
  29. QStringLiteral("Image file format"),
  30. QStringLiteral("format"));
  31. parser.addOption(informat);
  32. QCommandLineOption qimgformat(QStringList() << QStringLiteral("q") << QStringLiteral("qimage-format"),
  33. QStringLiteral("QImage data format"),
  34. QStringLiteral("format"));
  35. parser.addOption(qimgformat);
  36. QCommandLineOption listformats(QStringList() << QStringLiteral("l") << QStringLiteral("list-file-formats"),
  37. QStringLiteral("List supported image file formats"));
  38. parser.addOption(listformats);
  39. QCommandLineOption listmimetypes(QStringList() << QStringLiteral("m") << QStringLiteral("list-mime-types"),
  40. QStringLiteral("List supported image mime types"));
  41. parser.addOption(listmimetypes);
  42. QCommandLineOption listqformats(QStringList() << QStringLiteral("p") << QStringLiteral("list-qimage-formats"),
  43. QStringLiteral("List supported QImage data formats"));
  44. parser.addOption(listqformats);
  45. parser.process(app);
  46. const QStringList files = parser.positionalArguments();
  47. if (parser.isSet(listformats)) {
  48. QTextStream out(stdout);
  49. out << "File formats:\n";
  50. const auto lstSupportedFormats = QImageReader::supportedImageFormats();
  51. for (const auto &fmt : lstSupportedFormats) {
  52. out << " " << fmt << '\n';
  53. }
  54. return 0;
  55. }
  56. if (parser.isSet(listmimetypes)) {
  57. QTextStream out(stdout);
  58. out << "MIME types:\n";
  59. const auto lstSupportedMimeTypes = QImageReader::supportedMimeTypes();
  60. for (const auto &fmt : lstSupportedMimeTypes) {
  61. out << " " << fmt << '\n';
  62. }
  63. return 0;
  64. }
  65. if (parser.isSet(listqformats)) {
  66. QTextStream out(stdout);
  67. out << "QImage formats:\n";
  68. // skip QImage::Format_Invalid
  69. for (int i = 1; i < QImage::NImageFormats; ++i) {
  70. out << " " << formatToString(static_cast<QImage::Format>(i)) << '\n';
  71. }
  72. return 0;
  73. }
  74. if (files.count() != 2) {
  75. QTextStream(stderr) << "Must provide exactly two files\n";
  76. parser.showHelp(1);
  77. }
  78. QImageReader reader(files.at(0), parser.value(informat).toLatin1());
  79. QImage img = reader.read();
  80. if (img.isNull()) {
  81. QTextStream(stderr) << "Could not read image: " << reader.errorString() << '\n';
  82. return 2;
  83. }
  84. QFile output(files.at(1));
  85. if (!output.open(QIODevice::WriteOnly)) {
  86. QTextStream(stderr) << "Could not open " << files.at(1) << " for writing: " << output.errorString() << '\n';
  87. return 3;
  88. }
  89. if (parser.isSet(qimgformat)) {
  90. QImage::Format qformat = formatFromString(parser.value(qimgformat));
  91. if (qformat == QImage::Format_Invalid) {
  92. QTextStream(stderr) << "Unknown QImage data format " << parser.value(qimgformat) << '\n';
  93. return 4;
  94. }
  95. img = img.convertToFormat(qformat);
  96. }
  97. qint64 written = output.write(reinterpret_cast<const char *>(img.bits()), img.sizeInBytes());
  98. if (written != img.sizeInBytes()) {
  99. QTextStream(stderr) << "Could not write image data to " << files.at(1) << ":" << output.errorString() << "\n";
  100. return 5;
  101. }
  102. QTextStream(stdout) << "Created " << files.at(1) << " with data format " << formatToString(img.format()) << "\n";
  103. return 0;
  104. }