src/lib/image.c
changeset 18 f92a24ab046e
parent 17 baf3fe7c6354
child 19 ebcc49de97d0
equal deleted inserted replaced
17:baf3fe7c6354 18:f92a24ab046e
     1 #include "image.h"
     1 #include "image.h"
     2 #include "cache.h"
     2 #include "cache.h"
       
     3 #include "tile.h"
     3 #include "error.h"
     4 #include "error.h"
     4 #include "shared/util.h"
     5 #include "shared/util.h"
     5 
     6 
     6 #include <stdlib.h>
     7 #include <stdlib.h>
     7 #include <errno.h>
     8 #include <errno.h>
    99 
   100 
   100 /**
   101 /**
   101  * Update the image_info field from the given png object.
   102  * Update the image_info field from the given png object.
   102  *
   103  *
   103  * Must be called under libpng-error-trap!
   104  * Must be called under libpng-error-trap!
       
   105  *
       
   106  * XXX: currently this info is not used, pulled from the cache instead
   104  */
   107  */
   105 static int pt_image_update_info (struct pt_image *image, png_structp png, png_infop info)
   108 static int pt_image_update_info (struct pt_image *image, png_structp png, png_infop info)
   106 {
   109 {
   107     // query png_get_*
   110     // query png_get_*
   108     image->info.width = png_get_image_width(png, info); 
   111     image->info.width = png_get_image_width(png, info); 
   219 int pt_image_update (struct pt_image *image)
   222 int pt_image_update (struct pt_image *image)
   220 {
   223 {
   221     return pt_image_update_cache(image);
   224     return pt_image_update_cache(image);
   222 }
   225 }
   223 
   226 
   224 int pt_image_tile (struct pt_image *image, const struct pt_tile_info *tile_info, FILE *out)
   227 int pt_image_tile_file (struct pt_image *image, const struct pt_tile_info *info, FILE *out)
   225 {
   228 {
   226     png_structp png = NULL;
   229     struct pt_tile tile;
   227     png_infop info = NULL;
   230     int err;
   228     int err = 0;
   231 
   229         
   232     // init
   230     // open PNG writer
   233     if ((err = pt_tile_init_file(&tile, info, out)))
   231     if ((png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL)
   234         return err;
   232         JUMP_SET_ERROR(err, PT_ERR_PNG_CREATE);
   235 
   233     
   236     // render
   234     if ((info = png_create_info_struct(png)) == NULL)
   237     if ((err = pt_tile_render(&tile, image->cache)))
   235         JUMP_SET_ERROR(err, PT_ERR_PNG_CREATE);
   238         JUMP_ERROR(err);
   236 
   239 
   237     // libpng error trap
   240     // ok
   238     if (setjmp(png_jmpbuf(png)))
   241     return 0;
   239         JUMP_SET_ERROR(err, PT_ERR_PNG);
   242 
   240     
   243 error:
   241     // setup IO
   244     pt_tile_abort(&tile);
   242     png_init_io(png, out);
   245 
   243     
   246     return err;
   244     // render tile
   247 }
   245     if ((err = pt_cache_tile_png(image->cache, png, info, tile_info)))
   248 
   246         JUMP_ERROR(err);
   249 int pt_image_tile_mem (struct pt_image *image, const struct pt_tile_info *info, char **buf_ptr, size_t *len_ptr)
   247 
   250 {
   248     // done
   251     struct pt_tile tile;
   249     png_write_end(png, info);
   252     int err;
   250 
   253 
   251 error:
   254     // init
   252     // cleanup
   255     if ((err = pt_tile_init_mem(&tile, info)))
   253     png_destroy_write_struct(&png, &info);
   256         return err;
       
   257 
       
   258     // render
       
   259     if ((err = pt_tile_render(&tile, image->cache)))
       
   260         JUMP_ERROR(err);
       
   261 
       
   262     // ok
       
   263     *buf_ptr = tile.out.mem.base;
       
   264     *len_ptr = tile.out.mem.len;
       
   265 
       
   266     return 0;
       
   267 
       
   268 error:
       
   269     pt_tile_abort(&tile);
   254 
   270 
   255     return err;
   271     return err;
   256 }
   272 }
   257 
   273 
   258 void pt_image_destroy (struct pt_image *image)
   274 void pt_image_destroy (struct pt_image *image)