terom@0: #include "cache.h" terom@4: #include "shared/util.h" terom@6: #include "shared/log.h" // only LOG_DEBUG terom@17: #include "error.h" 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@17: int err; terom@0: terom@1: // alloc terom@0: if ((cache = calloc(1, sizeof(*cache))) == NULL) terom@17: RETURN_ERROR(PT_ERR_MEM); terom@0: terom@1: if ((cache->path = strdup(path)) == NULL) terom@17: JUMP_SET_ERROR(err, PT_ERR_MEM); 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@17: return err; 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@17: RETURN_ERROR(PT_ERR_IMG_STAT); 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@17: RETURN_ERROR(PT_ERR_CACHE_STAT); 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@54: struct stat st; terom@17: int err; terom@17: terom@10: // ensure open terom@17: if ((err = pt_cache_open(cache))) terom@17: return err; terom@10: terom@10: info->width = cache->header->width; terom@10: info->height = cache->header->height; terom@10: terom@54: // stat terom@54: if (stat(cache->path, &st) < 0) { terom@54: // unknown terom@54: info->cache_mtime = 0; terom@54: info->cache_bytes = 0; terom@54: info->cache_blocks = 0; terom@54: terom@54: } else { terom@54: // store terom@54: info->cache_mtime = st.st_mtime; terom@54: info->cache_bytes = st.st_size; terom@54: info->cache_blocks = st.st_blocks; terom@54: } terom@54: 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@17: RETURN_ERROR_ERRNO(PT_ERR_OPEN_MODE, EACCES); 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@17: RETURN_ERROR(PT_ERR_PATH); 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@17: RETURN_ERROR(PT_ERR_CACHE_OPEN_TMP); 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@53: // mmap() the full file including header terom@7: if ((addr = mmap(NULL, PT_CACHE_HEADER_SIZE + cache->size, prot, MAP_SHARED, cache->fd, 0)) == MAP_FAILED) terom@17: RETURN_ERROR(PT_ERR_CACHE_MMAP); 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@17: RETURN_ERROR(PT_ERR_CACHE_SEEK); 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@17: if ((ret = read(cache->fd, buf, len)) <= 0) terom@17: RETURN_ERROR(PT_ERR_CACHE_READ); 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@17: RETURN_ERROR(PT_ERR_CACHE_SEEK); 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@17: if ((ret = write(cache->fd, buf, len)) <= 0) terom@17: RETURN_ERROR(PT_ERR_CACHE_WRITE); 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@17: int err; terom@7: terom@2: // no access terom@17: if (!(cache->mode & PT_OPEN_UPDATE)) terom@17: RETURN_ERROR(PT_ERR_OPEN_MODE); terom@2: terom@4: // open as .tmp terom@17: if ((err = pt_cache_open_tmp_fd(cache, &cache->fd))) terom@17: return err; terom@1: terom@1: // calculate data size terom@53: cache->size = 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@17: JUMP_SET_ERROR(err, PT_ERR_CACHE_TRUNC); terom@1: terom@7: // mmap header and data terom@17: if ((err = pt_cache_open_mmap(cache, &base, false))) terom@17: JUMP_ERROR(err); 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@17: if ((err = pt_cache_write_header(cache, header))) terom@17: JUMP_ERROR(err); 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@17: return err; 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@17: RETURN_ERROR(PT_ERR_PATH); terom@4: terom@4: // rename terom@4: if (rename(tmp_path, cache->path) < 0) terom@17: RETURN_ERROR(PT_ERR_CACHE_RENAME_TMP); 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@17: int err; 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@17: if ((err = pt_cache_open_read_fd(cache, &cache->fd))) terom@17: return err; terom@9: terom@9: // read in header terom@17: if ((err = pt_cache_read_header(cache, &header))) terom@17: JUMP_ERROR(err); terom@9: terom@9: // calculate data size terom@53: cache->size = header.height * header.row_bytes; terom@9: terom@9: // mmap header and data terom@17: if ((err = pt_cache_open_mmap(cache, &base, true))) terom@17: JUMP_ERROR(err); 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@17: return err; terom@9: } terom@9: terom@52: #define min(a, b) (((a) < (b)) ? (a) : (b)) terom@52: terom@52: /** terom@52: * Decode the PNG data directly to mmap() - not good for sparse backgrounds terom@52: */ terom@52: static int decode_png_raw (struct pt_cache *cache, png_structp png, png_infop info) terom@52: { terom@52: // write out raw image data a row at a time terom@52: for (size_t row = 0; row < cache->header->height; row++) { terom@52: // read row data, non-interlaced terom@52: png_read_row(png, cache->data + row * cache->header->row_bytes, NULL); terom@52: } terom@52: terom@52: return 0; terom@52: } terom@52: terom@52: static int decode_png_sparse (struct pt_cache *cache, png_structp png, png_infop info) terom@52: { terom@52: // one row of pixel data terom@52: uint8_t *row_buf; terom@52: terom@52: // alloc terom@52: if ((row_buf = malloc(cache->header->row_bytes)) == NULL) terom@52: RETURN_ERROR(PT_ERR_MEM); terom@52: terom@52: // decode each row at a time terom@52: for (size_t row = 0; row < cache->header->height; row++) { terom@52: // read row data, non-interlaced terom@52: png_read_row(png, row_buf, NULL); terom@52: terom@52: // skip background-colored regions to keep the cache file sparse terom@52: // ...in blocks of PT_CACHE_BLOCK_SIZE bytes terom@55: for (size_t col_base = 0; col_base < cache->header->width; col_base += PT_CACHE_BLOCK_SIZE) { terom@52: // size of this block in bytes terom@52: size_t block_size = min(PT_CACHE_BLOCK_SIZE * cache->header->col_bytes, cache->header->row_bytes - col_base); terom@52: terom@52: // ...each pixel terom@52: for ( terom@52: size_t col = col_base; terom@52: terom@52: // BLOCK_SIZE * col_bytes wide, don't go over the edge terom@52: col < col_base + block_size; terom@52: terom@52: col += cache->header->col_bytes terom@52: ) { terom@52: // test this pixel terom@52: if (bcmp(row_buf + col, cache->header->params.background_color, cache->header->col_bytes)) { terom@52: // differs terom@52: memcpy( terom@52: cache->data + row * cache->header->row_bytes + col_base, terom@52: row_buf + col_base, terom@52: block_size terom@52: ); terom@52: terom@52: // skip to next block terom@52: break; terom@52: } terom@52: } terom@52: terom@52: // skip this block terom@52: continue; terom@52: } terom@52: } terom@52: terom@52: return 0; terom@52: } terom@52: terom@52: int pt_cache_update_png (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_image_params *params) terom@0: { terom@0: struct pt_cache_header header; terom@17: int err; 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@17: RETURN_ERROR(PT_ERR_PNG); 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@52: // any params terom@52: if (params) terom@52: header.params = *params; terom@52: terom@4: // create .tmp and write out header terom@17: if ((err = pt_cache_create(cache, &header))) terom@17: return err; terom@52: terom@52: // decode terom@52: if ((err = decode_png_sparse(cache, png, info))) terom@52: return err; terom@7: terom@4: // move from .tmp to .cache terom@17: if ((err = pt_cache_create_done(cache))) terom@18: // XXX: pt_cache_abort? terom@17: return err; 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@15: // XXX: use a configureable background color, or full transparency? terom@15: memset(row, /* 0xd7 */ 0x00, 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@13: clip_x = 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@17: RETURN_ERROR(PT_ERR_MEM); 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@34: static size_t scale_by_zoom_factor (size_t value, int z) terom@34: { terom@34: if (z > 0) terom@34: return value << z; terom@34: terom@34: else if (z < 0) terom@34: return value >> -z; terom@34: terom@34: else terom@34: return value; terom@34: } terom@34: terom@34: #define ADD_AVG(l, r) (l) = ((l) + (r)) / 2 terom@34: terom@34: static int png_pixel_data (png_color *out, struct pt_cache *cache, size_t row, size_t col) terom@34: { terom@34: if (cache->header->color_type == PNG_COLOR_TYPE_PALETTE) { terom@34: // palette entry number terom@34: int p; terom@34: terom@34: if (cache->header->bit_depth == 8) terom@34: p = *((uint8_t *) tile_row_col(cache, row, col)); terom@34: else terom@34: return -1; terom@34: terom@34: if (p >= cache->header->num_palette) terom@34: return -1; terom@34: terom@34: // reference data from palette terom@34: *out = cache->header->palette[p]; terom@34: terom@34: return 0; terom@34: terom@34: } else { terom@34: return -1; terom@34: } terom@34: } terom@34: terom@34: /** terom@34: * Write unscaled tile data terom@34: */ terom@34: static int write_png_data_unzoomed (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@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@34: terom@34: return err; terom@34: } terom@34: terom@34: /** terom@34: * Write scaled tile data terom@34: */ terom@34: static int write_png_data_zoomed (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti) terom@34: { terom@34: // size of the image data in px terom@34: size_t data_width = scale_by_zoom_factor(ti->width, -ti->zoom); terom@34: size_t data_height = scale_by_zoom_factor(ti->height, -ti->zoom); terom@34: terom@34: // input pixels per output pixel terom@34: size_t pixel_size = scale_by_zoom_factor(1, -ti->zoom); terom@34: terom@34: // bytes per output pixel terom@34: size_t pixel_bytes = 3; terom@34: terom@34: // size of the output tile in px terom@34: size_t row_width = ti->width; terom@34: terom@34: // size of an output row in bytes (RGB) terom@34: size_t row_bytes = row_width * 3; terom@34: terom@34: // buffer to hold output rows terom@34: uint8_t *row_buf; terom@34: terom@34: // XXX: only supports zooming out... terom@34: if (ti->zoom >= 0) terom@34: RETURN_ERROR(PT_ERR_ZOOM); terom@34: terom@34: if ((row_buf = malloc(row_bytes)) == NULL) terom@34: RETURN_ERROR(PT_ERR_MEM); terom@34: terom@34: terom@34: // define pixel format: 8bpp RGB terom@34: png_set_IHDR(png, info, ti->width, ti->height, 8, PNG_COLOR_TYPE_RGB, terom@34: PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT terom@34: ); terom@34: terom@34: // write meta-info terom@34: png_write_info(png, info); terom@34: terom@34: // ...each output row terom@34: for (size_t out_row = 0; out_row < ti->height; out_row++) { terom@34: memset(row_buf, 0, row_bytes); terom@34: terom@34: // ...includes pixels starting from this row. terom@34: size_t in_row_offset = ti->y + scale_by_zoom_factor(out_row, -ti->zoom); terom@34: terom@34: // ...each out row includes pixel_size in rows terom@34: for (size_t in_row = in_row_offset; in_row < in_row_offset + pixel_size && in_row < cache->header->height; in_row++) { terom@34: // and includes each input pixel terom@34: for (size_t in_col = ti->x; in_col < ti->x + data_width && in_col < cache->header->width; in_col++) { terom@34: png_color c; terom@34: terom@34: // ...for this output pixel terom@34: size_t out_col = scale_by_zoom_factor(in_col - ti->x, ti->zoom); terom@34: terom@34: // get pixel RGB data terom@34: if (png_pixel_data(&c, cache, in_row, in_col)) terom@34: return -1; terom@34: terom@34: // average the RGB data terom@34: ADD_AVG(row_buf[out_col * pixel_bytes + 0], c.red); terom@34: ADD_AVG(row_buf[out_col * pixel_bytes + 1], c.green); terom@34: ADD_AVG(row_buf[out_col * pixel_bytes + 2], c.blue); terom@34: } terom@34: } terom@34: terom@34: // output terom@34: png_write_row(png, row_buf); terom@34: } terom@34: terom@34: // done terom@34: return 0; terom@34: } terom@34: terom@34: int pt_cache_tile_png (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti) terom@34: { terom@34: int err; terom@34: terom@34: // ensure open terom@34: if ((err = pt_cache_open(cache))) terom@34: return err; terom@34: terom@34: // check within bounds terom@34: if (ti->x >= cache->header->width || ti->y >= cache->header->height) terom@34: // completely outside terom@34: RETURN_ERROR(PT_ERR_TILE_CLIP); terom@34: terom@34: // unscaled or scaled? terom@34: if (ti->zoom) terom@34: err = write_png_data_zoomed(cache, png, info, ti); terom@34: terom@34: else terom@34: err = write_png_data_unzoomed(cache, png, info, ti); terom@12: terom@12: if (err) terom@17: return err; 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: