File indexing completed on 2024-05-12 04:19:59

0001 #include <QBuffer>
0002 #include <QCoreApplication>
0003 #include <QDebug>
0004 #include <QElapsedTimer>
0005 #include <QFile>
0006 #include <QImage>
0007 #include <QImageReader>
0008 
0009 const int ITERATIONS = 2;
0010 const QSize SCALED_SIZE(1280, 800);
0011 
0012 static void bench(QIODevice *device, const QString &outputName)
0013 {
0014     QElapsedTimer chrono;
0015     chrono.start();
0016     for (int iteration = 0; iteration < ITERATIONS; ++iteration) {
0017         qDebug() << "Iteration:" << iteration;
0018 
0019         device->open(QIODevice::ReadOnly);
0020         QImageReader reader(device);
0021         QSize size = reader.size();
0022         size.scale(SCALED_SIZE, Qt::KeepAspectRatio);
0023         reader.setScaledSize(size);
0024         QImage img = reader.read();
0025         device->close();
0026 
0027         if (iteration == ITERATIONS - 1) {
0028             qDebug() << "time:" << chrono.elapsed();
0029             img.save(outputName, "png");
0030         }
0031     }
0032 }
0033 
0034 int main(int argc, char **argv)
0035 {
0036     QCoreApplication app(argc, argv);
0037     if (argc != 2) {
0038         qDebug() << "Usage: imageloadbench <file.jpg>";
0039         return 1;
0040     }
0041 
0042     const QString fileName = QString::fromUtf8(argv[1]);
0043 
0044     QFile file(fileName);
0045     if (!file.open(QIODevice::ReadOnly)) {
0046         qDebug() << QStringLiteral("Could not open '%1'").arg(fileName);
0047         return 2;
0048     }
0049     QByteArray data = file.readAll();
0050     QBuffer buffer(&data);
0051 
0052     qDebug() << "Using Qt loader";
0053     bench(&buffer, "qt.png");
0054 
0055     return 0;
0056 }