如何减少使用QImage保存png所需的时间? [英] How can I decrease the amount of time it takes to save a png using QImage?

查看:796
本文介绍了如何减少使用QImage保存png所需的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Qt 4.8rc1,我有一个要转换为png文件的QImage.似乎将其转换为png格式所花费的时间比应花费的时间长:对于800x800空png来说,它需要大约70ms.有什么办法可以提高效率,还是我本身就受到png/zlib的限制?

Using Qt 4.8rc1, I have a QImage that I want to convert to a png file. It seems like it is taking longer than it should to convert to png format: ~70ms for an 800x800 empty png. Is there a way I can make this more efficient, or am I just inherently limited by png/zlib?

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

Here is the benchmark I am running:

#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 )";
}

推荐答案

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

The third argument of QImage::save(const QString & fileName, const char * format = 0, int quality = -1 ) might help you. The documentation says the following:

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

The quality factor must be in the range 0 to 100 or -1. Specify 0 to obtain small compressed files, 100 for large uncompressed files, and -1 (the default) to use the default settings.

如果幸运的话,可以通过更改quality的值来更改zlib尝试压缩图像数据所花费的时间.我会用各种质量值调用QImage :: save(),看看执行时间是否改变.

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.

尽管Qt文档说质量must be in the range 0 to 100specify 0 to obtain small compressed files, 100 for large uncompressed files zlib手册显示了不同的范围:

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.

这篇关于如何减少使用QImage保存png所需的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆