如何减少使用 QImage 保存 png 所需的时间?

发布于 2024-12-12 22:33:33 字数 736 浏览 7 评论 0原文

使用 Qt 4.8rc1,我有一个 QImage,我想将其转换为 png 文件。转换为 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 )";
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水染的天色ゝ 2024-12-19 22:33:33

QImage::save(const QString & fileName, const char) 的第三个参数* format = 0, intquality = -1 ) 可能会帮助你。该文档说明如下:

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

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

尽管 Qt 文档说质量必须在 0 到 100 范围内,并且指定 0 以获得小型压缩文件,100 以获得大型未压缩文件 zlib 手册 显示不同的范围:

// Compression levels.
#define Z_NO_COMPRESSION         0
#define Z_BEST_SPEED             1
#define Z_BEST_COMPRESSION       9
#define Z_DEFAULT_COMPRESSION  (-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:

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文