[Xfce4-commits] <ristretto:master> Implement image-cache size calculation

Stephan Arts stephan at xfce.org
Wed Aug 12 12:18:44 CEST 2009


Updating branch refs/heads/master
         to 4b1c367e21a57eaf0f1785838268f379f22bde89 (commit)
       from e81a905eac295ffb23214cac5f239e8c0370379d (commit)

commit 4b1c367e21a57eaf0f1785838268f379f22bde89
Author: Stephan Arts <stephan at xfce.org>
Date:   Sat May 2 00:32:50 2009 +0200

    Implement image-cache size calculation

 ChangeLog         |    6 ++++++
 src/image.c       |   14 ++++++++++++++
 src/image.h       |    1 +
 src/image_cache.c |   31 +++++++++++++++++++------------
 4 files changed, 40 insertions(+), 12 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3742c2a..dcfc193 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2009-05-02  Stephan Arts <stephan at xfce.org>
 
+	* src/image,c
+	  src/image.h
+	  src/image_cache.c: Implement image-cache size calculation
+
+2009-05-02  Stephan Arts <stephan at xfce.org>
+
 	* src/main_window.c: Add jpeg filter to the open-files dialog
 
 2009-05-01  Stephan Arts <stephan at xfce.org>
diff --git a/src/image.c b/src/image.c
index 1442f64..9c53043 100644
--- a/src/image.c
+++ b/src/image.c
@@ -791,3 +791,17 @@ cb_rstto_image_update(RsttoImage *image)
     }
     return TRUE;
 }
+
+guint64
+rstto_image_get_size (RsttoImage *image)
+{
+    GdkPixbuf *pixbuf = rstto_image_get_pixbuf (image);
+    if (pixbuf)
+    {
+        gint n_channels = gdk_pixbuf_get_n_channels (pixbuf);
+        gint rowstride = gdk_pixbuf_get_rowstride (pixbuf);
+        gint height = gdk_pixbuf_get_height (pixbuf);
+        return rowstride * height *n_channels;
+    }
+    return 0;
+}
diff --git a/src/image.h b/src/image.h
index f8f4544..a7866ea 100644
--- a/src/image.h
+++ b/src/image.h
@@ -88,6 +88,7 @@ rstto_image_pop_transformation (RsttoImage *image, GError **error);
 
 /* Should only be used by image-transformation implementations */
 void rstto_image_set_pixbuf (RsttoImage *image, GdkPixbuf *pixbuf);
+guint64 rstto_image_get_size (RsttoImage *image);
 
 G_END_DECLS
 
diff --git a/src/image_cache.c b/src/image_cache.c
index 0c2aed6..55f2de3 100644
--- a/src/image_cache.c
+++ b/src/image_cache.c
@@ -90,10 +90,18 @@ void
 rstto_image_cache_push_image (RsttoImageCache *cache, RsttoImage *image, gboolean last)
 {
     RsttoSettings *settings = rstto_settings_new();
-    GValue val = {0, };
+    GValue val = {0, }, val_cache_size = {0, };
+    guint64 size = 0;
+    guint64 cache_size = 0;
+    RsttoImage *c_image;
+    GList *iter = NULL;
 
     g_value_init (&val, G_TYPE_BOOLEAN);
+    g_value_init (&val_cache_size, G_TYPE_UINT);
     g_object_get_property (G_OBJECT (settings), "enable-cache", &val);
+    g_object_get_property (G_OBJECT (settings), "cache-size", &val_cache_size);
+
+    cache_size = g_value_get_uint(&val_cache_size)*1000000;
 
     if (cache->cache_list)
     {
@@ -115,24 +123,23 @@ rstto_image_cache_push_image (RsttoImageCache *cache, RsttoImage *image, gboolea
     {
         if (g_list_length (cache->cache_list) > 1)
         {
-            RsttoImage *c_image = g_list_last (cache->cache_list)->data;
+            c_image = g_list_last (cache->cache_list)->data;
             rstto_image_unload (c_image);
             cache->cache_list = g_list_remove (cache->cache_list, c_image);
-            g_object_unref (image);
         }
     }
     else
     {
-        /**
-         * TODO:
-         * Fix the cache-size calculation
-         */
-        if (g_list_length (cache->cache_list) > 3)
+        for (iter = cache->cache_list->next; iter != NULL; iter = g_list_next (iter))
         {
-            RsttoImage *c_image = g_list_last (cache->cache_list)->data;
-            rstto_image_unload (c_image);
-            cache->cache_list = g_list_remove (cache->cache_list, c_image);
-            g_object_unref (image);
+            c_image = iter->data;
+            size += rstto_image_get_size (c_image);
+            if (size > cache_size)
+            {
+                rstto_image_unload (c_image);
+                cache->cache_list = g_list_remove (cache->cache_list, c_image);
+                iter = g_list_previous(iter);
+            } 
         }
     }
     g_value_unset (&val);



More information about the Xfce4-commits mailing list