4

使用 Qt 4.8rc1,我有一个想要转换为 png 文件的 QImage。转换为 png 格式所需的时间似乎比应有的要长:对于 800x800 的空 png,大约需要 70 毫秒。有没有办法可以提高效率,或者我只是天生受到 png/zlib 的限制?

这是我正在运行的基准测试:

#include <QtGui>
#include <QTimer>


int
main(int argc, char *argv[]) {
  int times = 1000;
  QString format("png");

  QByteArray ba;
  QBuffer* buffer = new QBuffer(&ba);
  buffer->open(QIODevice::WriteOnly);

  QTime timer;
  timer.start();

  while(times--) {
    QImage image(800, 800, QImage::Format_RGB32);
    image.save(buffer, format.toAscii(), -1);
  }

  int elapsed = timer.elapsed();

  qDebug() << "Completed 1000 runs in" << elapsed << "ms. (" <<  (elapsed / 1000) << "ms / render )";
}
4

1 回答 1

3

QImage::save(const QString & fileName, const char * format = 0, int quality = -1)的第三个参数可能会对您有所帮助。该文档说明了以下内容:

品质因数必须在 0 到 100 或 -1 的范围内。指定 0 获取小压缩文件,指定 100 获取大未压缩文件,指定 -1(默认值)使用默认设置。

If you are lucky then by changing the value of quality you can change how much time zlib spends on trying to compress the image data. I would call QImage::save() with various quality values and see if execution time changes or not.

Though the Qt doc says that quality must be in the range 0 to 100 and specify 0 to obtain small compressed files, 100 for large uncompressed files the zlib manual shows different range:

// Compression levels.
#define Z_NO_COMPRESSION         0
#define Z_BEST_SPEED             1
#define Z_BEST_COMPRESSION       9
#define Z_DEFAULT_COMPRESSION  (-1)

Try values based on both ranges.

于 2011-10-31T09:23:57.330 回答