src/lib/cache.c
changeset 6 766df7c9b90d
parent 4 49362b34116c
child 7 997906f5fd2d
--- a/src/lib/cache.c	Mon Dec 28 20:43:33 2009 +0200
+++ b/src/lib/cache.c	Mon Dec 28 22:31:33 2009 +0200
@@ -1,5 +1,6 @@
 #include "cache.h"
 #include "shared/util.h"
+#include "shared/log.h" // only LOG_DEBUG
 
 #include <stdlib.h>
 #include <unistd.h>
@@ -8,6 +9,7 @@
 #include <fcntl.h>
 #include <sys/mman.h>
 #include <errno.h>
+#include <assert.h>
 
 
 
@@ -97,7 +99,7 @@
  *
  * XXX: needs locking
  */
-static int pt_cache_open_read (struct pt_cache *cache, int *fd_ptr)
+static int pt_cache_open_read_fd (struct pt_cache *cache, int *fd_ptr)
 {
     int fd;
     
@@ -114,7 +116,7 @@
 /**
  * Open the .tmp cache file as an fd for writing
  */
-static int pt_cache_open_tmp (struct pt_cache *cache, int *fd_ptr)
+static int pt_cache_open_tmp_fd (struct pt_cache *cache, int *fd_ptr)
 {
     int fd;
     char tmp_path[1024];
@@ -150,8 +152,7 @@
     void *addr;
 
     // determine prot
-    if (cache->mode & PT_IMG_READ)
-        prot |= PROT_READ;
+    prot |= PROT_READ;
 
     if (cache->mode & PT_IMG_WRITE)
         prot |= PROT_WRITE;
@@ -207,7 +208,7 @@
     }
 
     // open as .tmp
-    if (pt_cache_open_tmp(cache, &cache->fd))
+    if (pt_cache_open_tmp_fd(cache, &cache->fd))
         return -1;
 
     // calculate data size
@@ -269,15 +270,37 @@
     header.bit_depth = png_get_bit_depth(png, info);
     header.color_type = png_get_color_type(png, info);
 
+    log_debug("width=%u, height=%u, bit_depth=%u, color_type=%u", 
+            header.width, header.height, header.bit_depth, header.color_type
+    );
+
     // fill in other info
     header.row_bytes = png_get_rowbytes(png, info);
 
+    log_debug("row_bytes=%u", header.row_bytes);
+    
+    // palette etc.
+    if (header.color_type == PNG_COLOR_TYPE_PALETTE) {
+        int num_palette;
+        png_colorp palette;
+
+        if (png_get_PLTE(png, info, &palette, &num_palette) == 0)
+            // XXX: PLTE chunk not read?
+            return -1;
+        
+        assert(num_palette <= PNG_MAX_PALETTE_LENGTH);
+    
+        // copy
+        header.num_palette = num_palette;
+        memcpy(&header.palette, palette, num_palette * sizeof(*palette));
+        
+        log_debug("num_palette=%u", num_palette);
+    }
+
     // create .tmp and write out header
     if (pt_cache_open_create(cache, &header))
         return -1;
 
-    // XXX: pallette etc.
-
     // write out raw image data a row at a time
     for (size_t row = 0; row < header.height; row++) {
         // read row data, non-interlaced