什么是获得QPixmap的散列的最好的方法? [英] What is the best way to get the hash of a QPixmap?

查看:155
本文介绍了什么是获得QPixmap的散列的最好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Qt 4.5开发一个图形应用程序,并把图像放入QPixmapCache,我想优化这个,如果一个用户插入一个已经在缓存中的图像,它会使用。

I am developing a graphics application using Qt 4.5 and am putting images in the QPixmapCache, I wanted to optimise this so that if a user inserts an image which is already in the cache it will use that.

现在每个图像都有一个唯一的id,它有助于优化绘画事件。但是我意识到,如果我可以计算图像的哈希,我可以查找缓存,以查看它是否已经存在并使用它(它将有助于更多的重复对象当然)。

Right now each image has a unique id which helps optimises itself on paint events. However I realise that if I could calculate a hash of the image I could lookup the cache to see if it already exists and use that (it would help more for duplicate objects of course).

我的问题是,如果一个大的QPixmap将哈希计算它慢下来或有更快的方式?

My problem is that if its a large QPixmap will a hash calculation of it slow things down or is there a quicker way?

推荐答案

关于此的几个注释:


  1. 如果你要生成一个散列/一个像素图,那么您可能想要跳过QPixmapCache和直接使用QCache 。这将消除使用QStrings作为键的一些开销(除非你还想使用文件路径来定位项目)

  1. If you're going to be generating a hash/cache key of a pixmap, then you may want to skip the QPixmapCache and use QCache directly. This would eliminate some overhead of using QStrings as keys (unless you also want to use the file path to locate the items)

从Qt4.4开始,QPixmap具有与其相关联的哈希值(请参阅 QPixmap :: cacheKey())。文档声明不同的QPixmap对象只能具有相同的缓存键,如果他们引用相同的内容。然而,由于Qt使用共享数据复制,这可能仅适用于复制的像素映射,而不适用于从同一图像加载的两个不同的像素图。

As of Qt4.4, QPixmap has a "hash" value associated with it (see QPixmap::cacheKey() ). The documentation claims "Distinct QPixmap objects can only have the same cache key if they refer to the same contents." However, since Qt uses shared-data copying, this may only apply to copied pixmaps and not to two distinct pixmaps loaded from the same image. A bit of testing would tell you if it works, and if it does, it would let you easily get a hash value.

如果你真的想做一个测试,你可以很容易地得到一个哈希值。一个好的,相当快速的缓存删除重复,你可能想看看自己的数据结构,根据大小,颜色深度,图像类型和这样的东西排序。然后你只需要找到相同类型的图像具有相同的尺寸,位深度等哈希实际的图像数据。当然,如果你的用户通常打开很多图像与这些东西相同,它不会

If you really want to do a good, fairly quick cache with removing duplications, you might want to look at your own data structure that sorts according to sizes, color depths, image types, and things such as that. Then you would only need to hash the actual image data after you find the same type of image with the same dimensions, bit-depths, etc. Of course, if your users generally open a lot of images with those things the same, it wouldn't help at all.

性能:不要忘记Qt在4.5中添加的基准测试工具,它可以让你比较你的各种哈希想法,看看哪一个运行最快。我还没有检查过,但看起来很整洁。

Performance: Don't forget about the benchmarking stuff Qt added in 4.5, which would let you compare your various hashing ideas and see which one runs the fastest. I haven't checked it out yet, but it looks pretty neat.

这篇关于什么是获得QPixmap的散列的最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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