File indexing completed on 2024-04-21 15:12:17

0001 /************************************************************************
0002  *                                  *
0003  *  This file is part of Kooka, a scanning/OCR application using    *
0004  *  Qt <http://www.qt.io> and KDE Frameworks <http://www.kde.org>.  *
0005  *                                  *
0006  *  Copyright (C) 2009-2018 Jonathan Marten <jjm@keelhaul.me.uk>    *
0007  *                                  *
0008  *  Kooka is free software; you can redistribute it and/or modify it    *
0009  *  under the terms of the GNU Library General Public License as    *
0010  *  published by the Free Software Foundation and appearing in the  *
0011  *  file COPYING included in the packaging of this file;  either    *
0012  *  version 2 of the License, or (at your option) any later version.    *
0013  *                                  *
0014  *  As a special exception, permission is given to link this program    *
0015  *  with any version of the KADMOS OCR/ICR engine (a product of     *
0016  *  reRecognition GmbH, Kreuzlingen), and distribute the resulting  *
0017  *  executable without including the source code for KADMOS in the  *
0018  *  source distribution.                        *
0019  *                                  *
0020  *  This program is distributed in the hope that it will be useful, *
0021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of  *
0022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *
0023  *  GNU General Public License for more details.            *
0024  *                                  *
0025  *  You should have received a copy of the GNU General Public       *
0026  *  License along with this program;  see the file COPYING.  If     *
0027  *  not, see <http://www.gnu.org/licenses/>.                *
0028  *                                  *
0029  ************************************************************************/
0030 
0031 #include <stdlib.h>
0032 #include <stdio.h>
0033 
0034 #include <qhash.h>
0035 #include <qstring.h>
0036 #include <qtextstream.h>
0037 #include <qcoreapplication.h>
0038 #include <qcommandlineparser.h>
0039 #include <qimagereader.h>
0040 #include <qimagewriter.h>
0041 #include <qmimetype.h>
0042 #include <qmimedatabase.h>
0043 
0044 
0045 #define READ        0x01
0046 #define WRITE       0x02
0047 
0048 #define FMT     "%1  %2 %3    %4   %5    %6"
0049 #define FIELD1      (-8)
0050 #define FIELD23     (-5)
0051 #define FIELD4      (-30)
0052 #define FIELD5      (-9)
0053 #define FIELD6      (-5)
0054 
0055 #define YESNO(b)    ((b) ? " Y" : " -")
0056 
0057 #if (QT_VERSION>=QT_VERSION_CHECK(5, 12, 0))
0058 #define HAVE_REVERSE_LOOKUP
0059 #endif
0060 
0061 
0062 int main(int argc, char **argv)
0063 {
0064     QCoreApplication app(argc, argv);
0065     app.setApplicationName("qimageioinfo");
0066     app.setApplicationVersion("1.0.0");
0067 
0068     QCommandLineParser parser;
0069     parser.setApplicationDescription("Show QImageIO supported formats and information");
0070     parser.addHelpOption();
0071     parser.addVersionOption();
0072     parser.process(app);
0073 
0074     QHash<QByteArray,int> combinedMap;
0075 
0076     QList<QByteArray> readTypes = QImageReader::supportedImageFormats();
0077     QList<QByteArray> writeTypes = QImageWriter::supportedImageFormats();
0078 
0079     for (const QByteArray &it : qAsConst(readTypes))
0080     {
0081         combinedMap[it.toUpper().trimmed()] |= READ;
0082     }
0083 
0084     for (const QByteArray &it : qAsConst(writeTypes))
0085     {
0086         combinedMap[it.toUpper().trimmed()] |= WRITE;
0087     }
0088 
0089     QList<QByteArray> formats(combinedMap.keys());
0090     std::sort(formats.begin(), formats.end());
0091 
0092     QTextStream ts(stdout);
0093 
0094     ts << Qt::endl << QString("Found %1 supported image formats").arg(formats.count()) << Qt::endl;
0095 
0096     ts << Qt::endl;
0097     ts << QString(FMT)
0098         .arg("Format",FIELD1)
0099         .arg("Read",FIELD23)
0100         .arg("Write",FIELD23)
0101         .arg("MIME type",FIELD4)
0102 #ifdef HAVE_REVERSE_LOOKUP
0103         .arg("Reverse",FIELD5)
0104 #else
0105         .arg("",FIELD5)
0106 #endif
0107         .arg("Extension",FIELD6)
0108        << Qt::endl;
0109     ts << QString(FMT)
0110         .arg("------",FIELD1)
0111         .arg("----",FIELD23)
0112         .arg("-----",FIELD23)
0113         .arg("---------",FIELD4)
0114 #ifdef HAVE_REVERSE_LOOKUP
0115         .arg("-------",FIELD5)
0116 #else
0117         .arg("",FIELD5)
0118 #endif
0119         .arg("---------",FIELD6)
0120        << Qt::endl;
0121 
0122     QMimeDatabase db;
0123 
0124     for (const QByteArray &format : qAsConst(formats))
0125     {
0126         int sup = combinedMap[format];
0127 
0128         const QMimeType mime = db.mimeTypeForFile(QString("/tmp/x.")+format.toLower(), QMimeDatabase::MatchExtension);
0129         bool isDefault = (mime.name()=="application/octet-stream");
0130         QString type = (isDefault ? "(none)" : mime.name());
0131         QString ext = (isDefault ? "" : mime.suffixes().value(0));
0132         QString icon = mime.iconName();
0133 
0134 #ifdef HAVE_REVERSE_LOOKUP
0135         QString backfmt = "(none)";
0136         QList<QByteArray> formats = QImageReader::imageFormatsForMimeType(mime.name().toLocal8Bit());
0137         int fcount = formats.count();           // get format from file name
0138         if (fcount>0)
0139         {
0140             backfmt = formats.first().trimmed().toUpper();
0141             if (fcount>1) backfmt += QString(" (+%1)").arg(fcount-1);
0142         }
0143 #else
0144         QString backfmt = "";
0145 #endif
0146 
0147         ts << QString(FMT)
0148             .arg(format.constData(),FIELD1)
0149             .arg(YESNO(sup & READ),FIELD23)
0150             .arg(YESNO(sup & WRITE),FIELD23)
0151             .arg(type,FIELD4)
0152             .arg(backfmt,FIELD5)
0153             .arg(ext,FIELD6)
0154            << Qt::endl;
0155     }
0156 
0157     ts << Qt::endl;
0158     return (EXIT_SUCCESS);
0159 }