make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
authorTero Marttila <terom@fixme.fi>
Tue, 29 Dec 2009 00:22:57 +0200
changeset 10 6806a90d934f
parent 9 a31048ff76a2
child 11 eb2a1472f084
make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
src/lib/cache.c
src/lib/cache.h
src/lib/image.c
--- a/src/lib/cache.c	Tue Dec 29 00:18:17 2009 +0200
+++ b/src/lib/cache.c	Tue Dec 29 00:22:57 2009 +0200
@@ -65,6 +65,18 @@
         return PT_CACHE_FRESH;
 }
 
+int pt_cache_info (struct pt_cache *cache, struct pt_image_info *info)
+{
+    // ensure open
+    if (pt_cache_open(cache))
+        return -1;
+
+    info->width = cache->header->width;
+    info->height = cache->header->height;
+
+    return 0;
+}
+
 /**
  * Abort any incomplete open operation, cleaning up
  */
@@ -288,6 +300,10 @@
     struct pt_cache_header header;
     void *base;
 
+    // ignore if already open
+    if (cache->header && cache->data)
+        return 0;
+
     // open the .cache
     if (pt_cache_open_read_fd(cache, &cache->fd))
         return -1;
@@ -390,11 +406,9 @@
 
 int pt_cache_tile_png (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti)
 {
-    if (!cache->data) {
-        // not yet open
-        if (pt_cache_open(cache))
-            return -1;
-    }
+    // ensure open
+    if (pt_cache_open(cache))
+        return -1;
 
     // set basic info
     png_set_IHDR(png, info, ti->width, ti->height, cache->header->bit_depth, cache->header->color_type,
--- a/src/lib/cache.h	Tue Dec 29 00:18:17 2009 +0200
+++ b/src/lib/cache.h	Tue Dec 29 00:22:57 2009 +0200
@@ -77,12 +77,17 @@
 int pt_cache_status (struct pt_cache *cache, const char *img_path);
 
 /**
+ * Get info for the cached image, open it if not already open.
+ */
+int pt_cache_info (struct pt_cache *cache, struct pt_image_info *info);
+
+/**
  * Update the cache data from the given PNG image.
  */
 int pt_cache_update_png (struct pt_cache *cache, png_structp png, png_infop info);
 
 /**
- * Actually open the existing .cache for use
+ * Open the existing .cache for use. If already opened, does nothing.
  */
 int pt_cache_open (struct pt_cache *cache);
 
--- a/src/lib/image.c	Tue Dec 29 00:18:17 2009 +0200
+++ b/src/lib/image.c	Tue Dec 29 00:22:57 2009 +0200
@@ -197,8 +197,11 @@
 
 int pt_image_info (struct pt_image *image, const struct pt_image_info **info_ptr)
 {
-    // XXX: ensure that this was read?
-    // XXX: get this from image->cache?
+    // update info
+    if (pt_cache_info(image->cache, &image->info))
+        return -1;
+    
+    // return pointer
     *info_ptr = &image->info;
 
     return 0;