src/lib/image.c
changeset 17 baf3fe7c6354
parent 11 eb2a1472f084
child 18 f92a24ab046e
--- a/src/lib/image.c	Tue Dec 29 15:38:31 2009 +0200
+++ b/src/lib/image.c	Tue Dec 29 16:44:48 2009 +0200
@@ -1,5 +1,6 @@
 #include "image.h"
 #include "cache.h"
+#include "error.h"
 #include "shared/util.h"
 
 #include <stdlib.h>
@@ -7,41 +8,42 @@
 
 #include <png.h>
 
-static int pt_image_new (struct pt_image **img_ptr, struct pt_ctx *ctx, const char *path)
+static int pt_image_new (struct pt_image **image_ptr, struct pt_ctx *ctx, const char *path)
 {
-    struct pt_image *img;
+    struct pt_image *image;
+    int err = 0;
 
     // alloc
-    if ((img = calloc(1, sizeof(*img))) == NULL)
-        return -1;
+    if ((image = calloc(1, sizeof(*image))) == NULL)
+        JUMP_SET_ERROR(err, PT_ERR_MEM);
 
-    if ((img->path = strdup(path)) == NULL)
-        goto error;
+    if ((image->path = strdup(path)) == NULL)
+        JUMP_SET_ERROR(err, PT_ERR_MEM);
 
     // init
-    img->ctx = ctx;
+    image->ctx = ctx;
     
     // ok
-    *img_ptr = img;
+    *image_ptr = image;
 
     return 0;
 
 error:
-    pt_image_destroy(img);
-
-    return -1;
+    pt_image_destroy(image);
+    
+    return err;
 }
 
 /**
  * Open the image's FILE
  */
-static int pt_image_open_file (struct pt_image *img, FILE **file_ptr)
+static int pt_image_open_file (struct pt_image *image, FILE **file_ptr)
 {
     FILE *fp;
     
     // open
-    if ((fp = fopen(img->path, "rb")) < 0)
-        return -1;
+    if ((fp = fopen(image->path, "rb")) == NULL)
+        RETURN_ERROR(PT_ERR_IMG_FOPEN);
 
     // ok
     *file_ptr = fp;
@@ -52,33 +54,34 @@
 /**
  * Open the PNG image, setting up the I/O and returning the png_structp and png_infop
  */
-static int pt_image_open_png (struct pt_image *img, png_structp *png_ptr, png_infop *info_ptr)
+static int pt_image_open_png (struct pt_image *image, png_structp *png_ptr, png_infop *info_ptr)
 {
     FILE *fp = NULL;
     png_structp png = NULL;
     png_infop info = NULL;
+    int err;
     
     // open I/O
-    if (pt_image_open_file(img, &fp))
-        goto error;
+    if ((err = pt_image_open_file(image, &fp)))
+        JUMP_ERROR(err);
 
     // create the struct
     if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL)
-        goto error;
+        JUMP_SET_ERROR(err, PT_ERR_PNG_CREATE);
 
     // create the info
     if ((info = png_create_info_struct(png)) == NULL)
-        goto error;
+        JUMP_SET_ERROR(err, PT_ERR_PNG_CREATE);
 
     // setup error trap for the I/O
     if (setjmp(png_jmpbuf(png)))
-        goto error;
+        JUMP_SET_ERROR(err, PT_ERR_PNG);
     
     // setup I/O to FILE
     png_init_io(png, fp);
 
     // ok
-    // XXX: what to do with fp?
+    // XXX: what to do with fp? Should fclose() when done?
     *png_ptr = png;
     *info_ptr = info;
 
@@ -90,8 +93,8 @@
 
     // cleanup PNG state
     png_destroy_read_struct(&png, &info, NULL);
-
-    return -1;
+    
+    return err;
 }
 
 /**
@@ -115,45 +118,40 @@
 {
     png_structp png;
     png_infop info;
+    int err = 0;
 
     // pre-check enabled
-    if (!(image->cache->mode & PT_OPEN_UPDATE)) {
-        errno = EPERM;
-        return -1;
-    }
+    if (!(image->cache->mode & PT_OPEN_UPDATE))
+        RETURN_ERROR_ERRNO(PT_ERR_OPEN_MODE, EACCES);
 
     // open .png
-    if (pt_image_open_png(image, &png, &info))
-        return -1;
+    if ((err = pt_image_open_png(image, &png, &info)))
+        return err;
     
     // setup error trap
     if (setjmp(png_jmpbuf(png)))
-        goto error;
+        JUMP_SET_ERROR(err, PT_ERR_PNG);
 
     // read meta-info
     png_read_info(png, info);
 
     // update our meta-info
-    if (pt_image_update_info(image, png, info))
-        goto error;
+    if ((err = pt_image_update_info(image, png, info)))
+        JUMP_ERROR(err);
 
     // pass to cache object
-    if (pt_cache_update_png(image->cache, png, info))
-        goto error;
+    if ((err = pt_cache_update_png(image->cache, png, info)))
+        JUMP_ERROR(err);
 
     // finish off, ignore trailing data
     png_read_end(png, NULL);
 
+error:
     // clean up
+    // XXX: we need to close the fopen'd .png
     png_destroy_read_struct(&png, &info, NULL);
 
-    return 0;
-
-error:
-    // clean up
-    png_destroy_read_struct(&png, &info, NULL);
-
-    return -1;
+    return err;
 }
 
 /**
@@ -162,27 +160,31 @@
  */
 static int pt_image_cache_path (struct pt_image *image, char *buf, size_t len)
 {
-    return path_with_fext(image->path, buf, len, ".cache"); 
+    if (path_with_fext(image->path, buf, len, ".cache"))
+        RETURN_ERROR(PT_ERR_PATH);
+
+    return 0;
 }
 
 int pt_image_open (struct pt_image **image_ptr, struct pt_ctx *ctx, const char *path, int cache_mode)
 {
     struct pt_image *image;
     char cache_path[1024];
+    int err;
 
     // XXX: verify that the path exists and looks like a PNG file
 
     // alloc
-    if (pt_image_new(&image, ctx, path))
-        return -1;
+    if ((err = pt_image_new(&image, ctx, path)))
+        return err;
     
     // compute cache file path
-    if (pt_image_cache_path(image, cache_path, sizeof(cache_path)))
-        goto error;
+    if ((err = pt_image_cache_path(image, cache_path, sizeof(cache_path))))
+        JUMP_ERROR(err);
 
     // create the cache object for this image (doesn't yet open it)
-    if (pt_cache_new(&image->cache, cache_path, cache_mode))
-        goto error;
+    if ((err = pt_cache_new(&image->cache, cache_path, cache_mode)))
+        JUMP_ERROR(err);
     
     // ok, ready for access
     *image_ptr = image;
@@ -192,14 +194,16 @@
 error:
     pt_image_destroy(image);
 
-    return -1;
+    return err;
 }
 
 int pt_image_info (struct pt_image *image, const struct pt_image_info **info_ptr)
 {
+    int err;
+
     // update info
-    if (pt_cache_info(image->cache, &image->info))
-        return -1;
+    if ((err = pt_cache_info(image->cache, &image->info)))
+        return err;
     
     // return pointer
     *info_ptr = &image->info;
@@ -221,38 +225,34 @@
 {
     png_structp png = NULL;
     png_infop info = NULL;
+    int err = 0;
         
     // open PNG writer
     if ((png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL)
-        goto error;
+        JUMP_SET_ERROR(err, PT_ERR_PNG_CREATE);
     
     if ((info = png_create_info_struct(png)) == NULL)
-        goto error;
+        JUMP_SET_ERROR(err, PT_ERR_PNG_CREATE);
 
     // libpng error trap
     if (setjmp(png_jmpbuf(png)))
-        goto error;
+        JUMP_SET_ERROR(err, PT_ERR_PNG);
     
     // setup IO
     png_init_io(png, out);
     
     // render tile
-    if (pt_cache_tile_png(image->cache, png, info, tile_info))
-        goto error;
+    if ((err = pt_cache_tile_png(image->cache, png, info, tile_info)))
+        JUMP_ERROR(err);
 
     // done
     png_write_end(png, info);
 
-    // cleanup
-    png_destroy_write_struct(&png, &info);
-
-    return 0;
-
 error:
     // cleanup
     png_destroy_write_struct(&png, &info);
 
-    return -1;
+    return err;
 }
 
 void pt_image_destroy (struct pt_image *image)