pre-calc the sum of a picture added to an RrImage rather than calculating it every...
[mikachu/openbox.git] / render / imagecache.c
index ec2ff4d..9c605f9 100644 (file)
 
 #include "render.h"
 #include "imagecache.h"
+#include "image.h"
 
 static gboolean RrImagePicEqual(const RrImagePic *p1,
                                 const RrImagePic *p2);
 
-RrImageCache* RrImageCacheNew()
+RrImageCache* RrImageCacheNew(gint max_resized_saved)
 {
     RrImageCache *self;
 
+    g_assert(max_resized_saved >= 0);
+
     self = g_new(RrImageCache, 1);
     self->ref = 1;
+    self->max_resized_saved = max_resized_saved;
     self->table = g_hash_table_new((GHashFunc)RrImagePicHash,
                                    (GEqualFunc)RrImagePicEqual);
     return self;
@@ -53,15 +57,11 @@ RrImage* RrImageCacheFind(RrImageCache *self,
                           RrPixel32 *data, gint w, gint h)
 {
     RrImagePic pic;
-    pic.width = w;
-    pic.height = h;
-    pic.data = data;
+
+    RrImagePicInit(&pic, w, h, data);
     return g_hash_table_lookup(self->table, &pic);
 }
 
-/* This is a fast, reversable hash function called "lookup3", found here:
-   http://burtleburtle.net/bob/c/lookup3.c
-*/
 #define hashsize(n) ((RrPixel32)1<<(n))
 #define hashmask(n) (hashsize(n)-1)
 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
@@ -87,6 +87,13 @@ RrImage* RrImageCacheFind(RrImageCache *self,
   c ^= b; c -= rot(b,24); \
 }
 
+/* This is a fast, reversable hash function called "lookup3", found here:
+   http://burtleburtle.net/bob/c/lookup3.c, by Bob Jenkins
+
+   This hashing algorithm is "reversible", that is, not cryptographically
+   secure at all.  But we don't care about that, we just want something to
+   tell when images are the same or different relatively quickly.
+*/
 guint32 hashword(const guint32 *key, gint length, guint32 initval)
 {
     guint32 a,b,c;
@@ -119,6 +126,9 @@ guint32 hashword(const guint32 *key, gint length, guint32 initval)
     return c;
 }
 
+/*! This is some arbitrary initial value for the hashing function.  It's
+  constant so that you get the same result from the same data each time.
+*/
 #define HASH_INITVAL 0xf00d
 
 guint RrImagePicHash(const RrImagePic *p)
@@ -129,21 +139,6 @@ guint RrImagePicHash(const RrImagePic *p)
 static gboolean RrImagePicEqual(const RrImagePic *p1,
                                 const RrImagePic *p2)
 {
-    guint s1, s2;
-    RrPixel32 *data1, *data2;
-    gint i;
-
-    if (p1->width != p2->width || p1->height != p2->height) return FALSE;
-
-    /* strcmp() would probably suck on 4k of data.. sum all their values and
-       see if they get the same thing.  they already matched on their hashes
-       at this point. */
-    s1 = s2 = 0;
-    data1 = p1->data;
-    data2 = p2->data;
-    for (i = 0; i < p1->width * p1->height; ++i, ++data1)
-        s1 += *data1;
-    for (i = 0; i < p2->width * p2->height; ++i, ++data2)
-        s2 += *data2;
-    return s1 == s2;
+    return p1->width == p2->width && p1->height == p2->height &&
+        p1->sum == p2->sum;
 }