terom@0: #include "cache.h" terom@4: #include "shared/util.h" terom@6: #include "shared/log.h" // only LOG_DEBUG terom@0: terom@1: #include terom@1: #include terom@1: #include terom@1: #include terom@1: #include terom@1: #include terom@2: #include terom@6: #include terom@1: terom@1: terom@9: int pt_cache_new (struct pt_cache **cache_ptr, const char *path, int mode) terom@0: { terom@0: struct pt_cache *cache; terom@0: terom@1: // alloc terom@0: if ((cache = calloc(1, sizeof(*cache))) == NULL) terom@0: return -1; terom@0: terom@1: if ((cache->path = strdup(path)) == NULL) terom@1: goto error; terom@1: terom@1: // init terom@1: cache->fd = -1; terom@3: cache->mode = mode; terom@1: terom@1: // ok terom@1: *cache_ptr = cache; terom@1: terom@1: return 0; terom@1: terom@1: error: terom@1: // cleanup terom@1: if (cache) terom@1: pt_cache_destroy(cache); terom@1: terom@1: return -1; terom@1: } terom@1: terom@8: int pt_cache_status (struct pt_cache *cache, const char *img_path) terom@0: { terom@2: struct stat st_img, st_cache; terom@2: terom@2: // test original file terom@2: if (stat(img_path, &st_img) < 0) terom@2: return -1; terom@2: terom@2: // test cache file terom@2: if (stat(cache->path, &st_cache) < 0) { terom@2: // always stale if it doesn't exist yet terom@2: if (errno == ENOENT) terom@8: return PT_CACHE_NONE; terom@2: else terom@2: return -1; terom@2: } terom@2: terom@2: // compare mtime terom@8: if (st_img.st_mtime > st_cache.st_mtime) terom@8: return PT_CACHE_STALE; terom@8: terom@8: else terom@8: return PT_CACHE_FRESH; terom@0: } terom@0: terom@10: int pt_cache_info (struct pt_cache *cache, struct pt_image_info *info) terom@10: { terom@10: // ensure open terom@10: if (pt_cache_open(cache)) terom@10: return -1; terom@10: terom@10: info->width = cache->header->width; terom@10: info->height = cache->header->height; terom@10: terom@10: return 0; terom@10: } terom@10: terom@0: /** terom@1: * Abort any incomplete open operation, cleaning up terom@1: */ terom@1: static void pt_cache_abort (struct pt_cache *cache) terom@1: { terom@7: if (cache->header != NULL) { terom@7: munmap(cache->header, PT_CACHE_HEADER_SIZE + cache->size); terom@1: terom@7: cache->header = NULL; terom@7: cache->data = NULL; terom@1: } terom@1: terom@1: if (cache->fd >= 0) { terom@1: close(cache->fd); terom@1: terom@1: cache->fd = -1; terom@1: } terom@1: } terom@1: terom@1: /** terom@4: * Open the cache file as an fd for reading terom@1: * terom@1: * XXX: needs locking terom@1: */ terom@6: static int pt_cache_open_read_fd (struct pt_cache *cache, int *fd_ptr) terom@1: { terom@1: int fd; terom@4: terom@1: // actual open() terom@4: if ((fd = open(cache->path, O_RDONLY)) < 0) terom@1: return -1; terom@1: terom@1: // ok terom@1: *fd_ptr = fd; terom@1: terom@1: return 0; terom@1: } terom@1: terom@1: /** terom@4: * Open the .tmp cache file as an fd for writing terom@4: */ terom@6: static int pt_cache_open_tmp_fd (struct pt_cache *cache, int *fd_ptr) terom@4: { terom@4: int fd; terom@4: char tmp_path[1024]; terom@4: terom@4: // get .tmp path terom@4: if (path_with_fext(cache->path, tmp_path, sizeof(tmp_path), ".tmp")) terom@4: return -1; terom@4: terom@4: // open for write, create terom@4: // XXX: locking? terom@4: if ((fd = open(tmp_path, O_RDWR | O_CREAT, 0644)) < 0) terom@4: return -1; terom@4: terom@4: // ok terom@4: *fd_ptr = fd; terom@4: terom@4: return 0; terom@4: } terom@4: terom@4: terom@4: /** terom@7: * Mmap the opened cache file using PT_CACHE_HEADER_SIZE plus the calculated size stored in cache->size terom@1: */ terom@9: static int pt_cache_open_mmap (struct pt_cache *cache, void **addr_ptr, bool readonly) terom@1: { terom@1: int prot = 0; terom@1: void *addr; terom@1: terom@1: // determine prot terom@6: prot |= PROT_READ; terom@1: terom@9: if (!readonly) { terom@11: assert(cache->mode & PT_OPEN_UPDATE); terom@9: terom@1: prot |= PROT_WRITE; terom@9: } terom@1: terom@1: // perform mmap() from second page on terom@7: if ((addr = mmap(NULL, PT_CACHE_HEADER_SIZE + cache->size, prot, MAP_SHARED, cache->fd, 0)) == MAP_FAILED) terom@1: return -1; terom@1: terom@1: // ok terom@1: *addr_ptr = addr; terom@1: terom@1: return 0; terom@1: } terom@1: terom@1: /** terom@9: * Read in the cache header from the open file terom@9: */ terom@9: static int pt_cache_read_header (struct pt_cache *cache, struct pt_cache_header *header) terom@9: { terom@9: size_t len = sizeof(*header); terom@9: char *buf = (char *) header; terom@9: terom@9: // seek to start terom@9: if (lseek(cache->fd, 0, SEEK_SET) != 0) terom@9: return -1; terom@9: terom@9: // write out full header terom@9: while (len) { terom@9: ssize_t ret; terom@9: terom@9: // try and write out the header terom@9: if ((ret = read(cache->fd, buf, len)) < 0) terom@9: return -1; terom@9: terom@9: // update offset terom@9: buf += ret; terom@9: len -= ret; terom@9: } terom@9: terom@9: // done terom@9: return 0; terom@9: } terom@9: terom@9: /** terom@1: * Write out the cache header into the opened file terom@1: */ terom@1: static int pt_cache_write_header (struct pt_cache *cache, const struct pt_cache_header *header) terom@1: { terom@1: size_t len = sizeof(*header); terom@1: const char *buf = (const char *) header; terom@1: terom@1: // seek to start terom@1: if (lseek(cache->fd, 0, SEEK_SET) != 0) terom@1: return -1; terom@1: terom@1: // write out full header terom@1: while (len) { terom@1: ssize_t ret; terom@1: terom@1: // try and write out the header terom@1: if ((ret = write(cache->fd, buf, len)) < 0) terom@1: return -1; terom@1: terom@1: // update offset terom@1: buf += ret; terom@1: len -= ret; terom@1: } terom@1: terom@1: // done terom@1: return 0; terom@1: } terom@1: terom@1: /** terom@4: * Create a new .tmp cache file, open it, and write out the header. terom@0: */ terom@11: static int pt_cache_create (struct pt_cache *cache, struct pt_cache_header *header) terom@0: { terom@7: void *base; terom@7: terom@2: // no access terom@11: if (!(cache->mode & PT_OPEN_UPDATE)) { terom@2: errno = EPERM; terom@2: return -1; terom@2: } terom@2: terom@4: // open as .tmp terom@6: if (pt_cache_open_tmp_fd(cache, &cache->fd)) terom@1: return -1; terom@1: terom@1: // calculate data size terom@1: cache->size = sizeof(*header) + header->height * header->row_bytes; terom@1: terom@1: // grow file terom@7: if (ftruncate(cache->fd, PT_CACHE_HEADER_SIZE + cache->size) < 0) terom@1: goto error; terom@1: terom@7: // mmap header and data terom@9: if (pt_cache_open_mmap(cache, &base, false)) terom@1: goto error; terom@1: terom@7: cache->header = base; terom@7: cache->data = base + PT_CACHE_HEADER_SIZE; terom@7: terom@1: // write header terom@7: // XXX: should just mmap... terom@1: if (pt_cache_write_header(cache, header)) terom@1: goto error; terom@1: terom@1: // done terom@1: return 0; terom@1: terom@1: error: terom@1: // cleanup terom@1: pt_cache_abort(cache); terom@1: terom@1: return -1; terom@0: } terom@0: terom@4: /** terom@4: * Rename the opened .tmp to .cache terom@4: */ terom@4: static int pt_cache_create_done (struct pt_cache *cache) terom@4: { terom@4: char tmp_path[1024]; terom@4: terom@4: // get .tmp path terom@4: if (path_with_fext(cache->path, tmp_path, sizeof(tmp_path), ".tmp")) terom@4: return -1; terom@4: terom@4: // rename terom@4: if (rename(tmp_path, cache->path) < 0) terom@4: return -1; terom@4: terom@4: // ok terom@4: return 0; terom@4: } terom@4: terom@9: int pt_cache_open (struct pt_cache *cache) terom@9: { terom@9: struct pt_cache_header header; terom@9: void *base; terom@9: terom@10: // ignore if already open terom@10: if (cache->header && cache->data) terom@10: return 0; terom@10: terom@9: // open the .cache terom@9: if (pt_cache_open_read_fd(cache, &cache->fd)) terom@9: return -1; terom@9: terom@9: // read in header terom@9: if (pt_cache_read_header(cache, &header)) terom@9: return -1; terom@9: terom@9: // calculate data size terom@9: cache->size = sizeof(header) + header.height * header.row_bytes; terom@9: terom@9: // mmap header and data terom@9: if (pt_cache_open_mmap(cache, &base, true)) terom@9: goto error; terom@9: terom@9: cache->header = base; terom@9: cache->data = base + PT_CACHE_HEADER_SIZE; terom@9: terom@9: // done terom@9: return 0; terom@9: terom@9: error: terom@9: // cleanup terom@9: pt_cache_abort(cache); terom@9: terom@9: return -1; terom@9: } terom@9: terom@0: int pt_cache_update_png (struct pt_cache *cache, png_structp png, png_infop info) terom@0: { terom@0: struct pt_cache_header header; terom@1: terom@1: // XXX: check cache_mode terom@1: // XXX: check image doesn't use any options we don't handle terom@9: // XXX: close any already-opened cache file terom@0: terom@0: memset(&header, 0, sizeof(header)); terom@0: terom@0: // fill in basic info terom@1: header.width = png_get_image_width(png, info); terom@1: header.height = png_get_image_height(png, info); terom@1: header.bit_depth = png_get_bit_depth(png, info); terom@1: header.color_type = png_get_color_type(png, info); terom@0: terom@6: log_debug("width=%u, height=%u, bit_depth=%u, color_type=%u", terom@6: header.width, header.height, header.bit_depth, header.color_type terom@6: ); terom@6: terom@11: // only pack 1 pixel per byte, changes rowbytes terom@9: if (header.bit_depth < 8) terom@9: png_set_packing(png); terom@9: terom@0: // fill in other info terom@1: header.row_bytes = png_get_rowbytes(png, info); terom@1: terom@9: // calculate bpp as num_channels * bpc terom@9: // XXX: this assumes the packed bit depth will be either 8 or 16 terom@9: header.col_bytes = png_get_channels(png, info) * (header.bit_depth == 16 ? 2 : 1); terom@9: terom@9: log_debug("row_bytes=%u, col_bytes=%u", header.row_bytes, header.col_bytes); terom@6: terom@6: // palette etc. terom@6: if (header.color_type == PNG_COLOR_TYPE_PALETTE) { terom@6: int num_palette; terom@6: png_colorp palette; terom@6: terom@6: if (png_get_PLTE(png, info, &palette, &num_palette) == 0) terom@6: // XXX: PLTE chunk not read? terom@6: return -1; terom@6: terom@7: // should only be 256 of them at most terom@6: assert(num_palette <= PNG_MAX_PALETTE_LENGTH); terom@6: terom@6: // copy terom@6: header.num_palette = num_palette; terom@6: memcpy(&header.palette, palette, num_palette * sizeof(*palette)); terom@6: terom@6: log_debug("num_palette=%u", num_palette); terom@6: } terom@6: terom@4: // create .tmp and write out header terom@11: if (pt_cache_create(cache, &header)) terom@1: return -1; terom@1: terom@7: terom@1: // write out raw image data a row at a time terom@1: for (size_t row = 0; row < header.height; row++) { terom@1: // read row data, non-interlaced terom@7: png_read_row(png, cache->data + row * header.row_bytes, NULL); terom@1: } terom@1: terom@7: terom@4: // move from .tmp to .cache terom@4: if (pt_cache_create_done(cache)) terom@4: return -1; terom@4: terom@1: // done! terom@1: return 0; terom@0: } terom@1: terom@12: /** terom@12: * Return a pointer to the pixel data on \a row, starting at \a col. terom@12: */ terom@12: static inline void* tile_row_col (struct pt_cache *cache, size_t row, size_t col) terom@12: { terom@12: return cache->data + (row * cache->header->row_bytes) + (col * cache->header->col_bytes); terom@12: } terom@12: terom@12: /** terom@12: * Fill in a clipped region of \a width_px pixels at the given row segment terom@12: */ terom@12: static inline void tile_row_fill_clip (struct pt_cache *cache, png_byte *row, size_t width_px) terom@12: { terom@12: // XXX: use a defined background color, or full transparency? terom@12: memset(row, 0, width_px * cache->header->col_bytes); terom@12: } terom@12: terom@12: /** terom@12: * Write raw tile image data, directly from the cache terom@12: */ terom@12: static int write_png_data_direct (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti) terom@12: { terom@12: for (size_t row = ti->y; row < ti->y + ti->height; row++) terom@12: // write data directly terom@12: png_write_row(png, tile_row_col(cache, row, ti->x)); terom@12: terom@12: return 0; terom@12: } terom@12: terom@12: /** terom@12: * Write clipped tile image data (a tile that goes over the edge of the actual image) by aligning the data from the cache as needed terom@12: */ terom@12: static int write_png_data_clipped (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti) terom@12: { terom@12: png_byte *rowbuf; terom@12: size_t row; terom@12: terom@12: // image data goes from (ti->x ... clip_x, ti->y ... clip_y), remaining region is filled terom@12: size_t clip_x, clip_y; terom@12: terom@12: terom@12: // figure out if the tile clips over the right edge terom@12: // XXX: use min() terom@12: if (ti->x + ti->width > cache->header->width) terom@12: clip_x = cache->header->width; terom@12: else terom@12: clip_y = ti->x + ti->width; terom@12: terom@12: // figure out if the tile clips over the bottom edge terom@12: // XXX: use min() terom@12: if (ti->y + ti->height > cache->header->height) terom@12: clip_y = cache->header->height; terom@12: else terom@12: clip_y = ti->y + ti->height; terom@12: terom@12: terom@12: // allocate buffer for a single row of image data terom@12: if ((rowbuf = malloc(ti->width * cache->header->col_bytes)) == NULL) terom@12: return -1; terom@12: terom@12: // how much data we actually have for each row, in px and bytes terom@12: // from [(tile x)---](clip x) terom@12: size_t row_px = clip_x - ti->x; terom@12: size_t row_bytes = row_px * cache->header->col_bytes; terom@12: terom@12: // write the rows that we have terom@12: // from [(tile y]---](clip y) terom@12: for (row = ti->y; row < clip_y; row++) { terom@12: // copy in the actual tile data... terom@12: memcpy(rowbuf, tile_row_col(cache, row, ti->x), row_bytes); terom@12: terom@12: // generate the data for the remaining, clipped, columns terom@12: tile_row_fill_clip(cache, rowbuf + row_bytes, (ti->width - row_px)); terom@12: terom@12: // write terom@12: png_write_row(png, rowbuf); terom@12: } terom@12: terom@12: // generate the data for the remaining, clipped, rows terom@12: tile_row_fill_clip(cache, rowbuf, ti->width); terom@12: terom@12: // write out the remaining rows as clipped data terom@12: for (; row < ti->y + ti->height; row++) terom@12: png_write_row(png, rowbuf); terom@12: terom@12: // ok terom@12: return 0; terom@12: } terom@12: terom@9: int pt_cache_tile_png (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti) terom@9: { terom@12: int err; terom@12: terom@10: // ensure open terom@10: if (pt_cache_open(cache)) terom@10: return -1; terom@9: terom@9: // set basic info terom@9: png_set_IHDR(png, info, ti->width, ti->height, cache->header->bit_depth, cache->header->color_type, terom@9: PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT terom@9: ); terom@9: terom@9: // set palette? terom@9: if (cache->header->color_type == PNG_COLOR_TYPE_PALETTE) terom@9: png_set_PLTE(png, info, cache->header->palette, cache->header->num_palette); terom@9: terom@9: // write meta-info terom@9: png_write_info(png, info); terom@9: terom@12: // our pixel data is packed into 1 pixel per byte (8bpp or 16bpp) terom@9: png_set_packing(png); terom@9: terom@12: // figure out if the tile clips terom@12: if (ti->x + ti->width <= cache->header->width && ti->y + ti->height <= cache->header->height) terom@12: // doesn't clip, just use the raw data terom@12: err = write_png_data_direct(cache, png, info, ti); terom@9: terom@12: else terom@12: // fill in clipped regions terom@12: err = write_png_data_clipped(cache, png, info, ti); terom@12: terom@12: if (err) terom@12: return -1; terom@12: terom@9: // done, flush remaining output terom@9: png_write_flush(png); terom@9: terom@9: // ok terom@9: return 0; terom@9: } terom@9: terom@1: void pt_cache_destroy (struct pt_cache *cache) terom@1: { terom@1: free(cache->path); terom@1: terom@1: pt_cache_abort(cache); terom@1: terom@1: free(cache); terom@1: } terom@11: