terom@0: #ifndef PNGTILE_CACHE_H terom@0: #define PNGTILE_CACHE_H terom@0: terom@0: /** terom@0: * @file terom@0: * terom@0: * Internal image cache implementation terom@0: */ terom@0: #include "image.h" terom@56: #include "png.h" terom@0: terom@1: #include terom@1: #include terom@1: terom@56: /** terom@56: * Cache format version terom@56: */ terom@56: #define PT_CACHE_VERSION 2 terom@1: terom@0: /** terom@56: * Size used to store the cache header terom@56: */ terom@56: #define PT_CACHE_HEADER_SIZE 4096 terom@56: terom@56: /** terom@56: * On-disk header terom@56: */ terom@56: struct pt_cache_header { terom@56: /** Set to PT_CACHE_VERSION */ terom@56: uint16_t version; terom@56: terom@56: /** Image format */ terom@56: enum pt_img_format { terom@56: PT_IMG_PNG, ///< @see pt_png terom@56: } format; terom@56: terom@56: /** Data header by format */ terom@56: union { terom@56: struct pt_png_header png; terom@56: }; terom@56: terom@56: /** Parameters used */ terom@56: struct pt_image_params params; terom@56: terom@56: /** Size of the data segment */ terom@56: size_t data_size; terom@56: }; terom@56: terom@56: /** terom@72: * On-disk data format. This struct is always exactly PT_CACHE_HEADER_SIZE long terom@56: */ terom@56: struct pt_cache_file { terom@56: /** Header */ terom@56: struct pt_cache_header header; terom@56: terom@56: /** Padding for data */ terom@56: uint8_t padding[PT_CACHE_HEADER_SIZE - sizeof(struct pt_cache_header)]; terom@56: terom@56: /** Data follows, header.data_size bytes */ terom@56: uint8_t data[]; terom@56: }; terom@56: terom@56: /** terom@56: * Cache state terom@0: */ terom@0: struct pt_cache { terom@1: /** Filesystem path to cache file */ terom@1: char *path; terom@0: terom@1: /** The mode we are operating in, bitmask of PT_IMG_* */ terom@1: int mode; terom@1: terom@1: /** Opened file */ terom@1: int fd; terom@1: terom@7: /** Size of the data segment in bytes, starting at PT_CACHE_HEADER_SIZE */ terom@56: size_t data_size; terom@1: terom@56: /** The mmap'd file */ terom@56: struct pt_cache_file *file; terom@0: }; terom@0: terom@0: /** terom@0: * Construct the image cache info object associated with the given image. terom@0: */ terom@9: int pt_cache_new (struct pt_cache **cache_ptr, const char *path, int mode); terom@0: terom@0: /** terom@8: * Verify if the cached data eixsts, or has become stale compared to the given original file. terom@8: * terom@8: * @return one of pt_cache_status; <0 on error, 0 if fresh, >0 otherwise terom@0: */ terom@8: int pt_cache_status (struct pt_cache *cache, const char *img_path); terom@0: terom@0: /** terom@56: * Get info for the cached image. terom@56: * terom@56: * Does not open it if not yet opened. terom@10: */ terom@56: void pt_cache_info (struct pt_cache *cache, struct pt_image_info *info); terom@10: terom@10: /** terom@56: * Update the cache data from the given image data terom@0: */ terom@56: int pt_cache_update (struct pt_cache *cache, struct pt_png_img *img, const struct pt_image_params *params); terom@0: terom@1: /** terom@10: * Open the existing .cache for use. If already opened, does nothing. terom@9: */ terom@9: int pt_cache_open (struct pt_cache *cache); terom@9: terom@9: /** terom@56: * Render out the given tile terom@9: * terom@9: * If the cache is not yet open, this will open it terom@9: */ terom@56: int pt_cache_tile (struct pt_cache *cache, struct pt_tile *tile); terom@9: terom@9: /** terom@59: * Close the cache, if opened terom@59: */ terom@59: int pt_cache_close (struct pt_cache *cache); terom@59: terom@59: /** terom@1: * Release all resources associated with the given cache object without any cleanup. terom@1: */ terom@1: void pt_cache_destroy (struct pt_cache *cache); terom@1: terom@0: #endif