views:

57

answers:

2

On application startup I build a cache of icons (24x24 32bbp pre-multiplied argb bitmaps). this cache contains roughly a thousand items. I don't want to store the same image multiple times in this cache, for both memory and performance reasons. I figured the best way would be to create some sort of crc from each bitmap as it goes into the cache, and to compare new bitmaps against this list of crcs.

What is a good (and fast) way to create a crc from a bitmap which is only loaded in memory?

Or am I totally on the wrong track and is there a better way to build a bitmap-cache?

+1  A: 

A CRC has the same flaw as any hashing function: an equal CRC value does not proof that the images are identical. Your program will randomly, but infrequently, display the wrong image.

You need something else. Like the filename from which you retrieved the bitmap.

Hans Passant
Hi Hans, images typically come from resources, or might even be created on the spot using drawing functions. There is no filename.
David Rutten
Well, good thing that 2.3 MB is peanuts on a modern machine.
Hans Passant
+1  A: 
Andras Zoltan
Andras, thank you so much for the extensive answer. I've implemented an MD5 hash of the bitmap bytes and initial tests are good. No false positives, no false negatives. The image cache contains about 500 entries now (more in the future), but the number of instances in my app that use these icons numbers easily into the thousands. That is the overhead I'm hoping to get rid of.
David Rutten
@David Rutten: Glad to be of help. It's an interesting challenge (and I'm sure there are other ways, but I think this is probably the best 'easy' solution). I had a similar issue with storing XML of 7k + in a DB but not wishing to store dupes. I used MD5 hashing for that and never got a collision.
Andras Zoltan