如何减少使用 QImage 保存 png 所需的时间?
使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
QImage::save(const QString & fileName, const char) 的第三个参数* format = 0, intquality = -1 ) 可能会帮助你。该文档说明如下:
如果幸运的话,通过更改质量值,您可以更改 zlib 尝试压缩图像数据所花费的时间。我会使用各种质量值调用 QImage::save() 并查看执行时间是否改变。
尽管 Qt 文档说质量
必须在 0 到 100 范围内
,并且指定 0 以获得小型压缩文件,100 以获得大型未压缩文件
zlib 手册 显示不同的范围:尝试基于两个范围的值。
The third argument of QImage::save(const QString & fileName, const char * format = 0, int quality = -1 ) might help you. The documentation says the following:
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
andspecify 0 to obtain small compressed files, 100 for large uncompressed files
the zlib manual shows different range:Try values based on both ranges.