src/lib/cache.c
changeset 56 d5e3089906da
parent 55 a3542e78ecd8
child 59 80135bdfd343
equal deleted inserted replaced
55:a3542e78ecd8 56:d5e3089906da
    40         pt_cache_destroy(cache);
    40         pt_cache_destroy(cache);
    41 
    41 
    42     return err;
    42     return err;
    43 }
    43 }
    44 
    44 
       
    45 /**
       
    46  * Open the cache file as an fd for reading
       
    47  *
       
    48  * XXX: use some kind of locking?
       
    49  */
       
    50 static int pt_cache_open_read_fd (struct pt_cache *cache, int *fd_ptr)
       
    51 {
       
    52     int fd;
       
    53     
       
    54     // actual open()
       
    55     if ((fd = open(cache->path, O_RDONLY)) < 0)
       
    56         RETURN_ERROR_ERRNO(PT_ERR_OPEN_MODE, EACCES);
       
    57 
       
    58     // ok
       
    59     *fd_ptr = fd;
       
    60 
       
    61     return 0;
       
    62 }
       
    63 
       
    64 /**
       
    65  * Read in the cache header from the open file
       
    66  */
       
    67 static int pt_cache_read_header (int fd, struct pt_cache_header *header)
       
    68 {
       
    69     size_t len = sizeof(*header);
       
    70     char *buf = (char *) header;
       
    71 
       
    72     // seek to start
       
    73     if (lseek(fd, 0, SEEK_SET) != 0)
       
    74         RETURN_ERROR(PT_ERR_CACHE_SEEK);
       
    75 
       
    76     // write out full header
       
    77     while (len) {
       
    78         ssize_t ret;
       
    79         
       
    80         // try and write out the header
       
    81         if ((ret = read(fd, buf, len)) <= 0)
       
    82             RETURN_ERROR(PT_ERR_CACHE_READ);
       
    83 
       
    84         // update offset
       
    85         buf += ret;
       
    86         len -= ret;
       
    87     }
       
    88 
       
    89     // done
       
    90     return 0;
       
    91 }
       
    92 
       
    93 /**
       
    94  * Read and return the version number from the cache file, temporarily opening it if needed
       
    95  */
       
    96 static int pt_cache_version (struct pt_cache *cache)
       
    97 {
       
    98     int fd;
       
    99     struct pt_cache_header header;
       
   100     int ret;
       
   101 
       
   102     // already open?
       
   103     if (cache->file)
       
   104         return cache->file->header.version;
       
   105 
       
   106     // temp. open
       
   107     if ((ret = pt_cache_open_read_fd(cache, &fd)))
       
   108         return ret;
       
   109 
       
   110     // read header
       
   111     if ((ret = pt_cache_read_header(fd, &header)))
       
   112         JUMP_ERROR(ret);
       
   113 
       
   114     // ok
       
   115     ret = header.version;
       
   116     
       
   117 error:
       
   118     // close
       
   119     close(fd);
       
   120     
       
   121     return ret;
       
   122 }
       
   123 
    45 int pt_cache_status (struct pt_cache *cache, const char *img_path)
   124 int pt_cache_status (struct pt_cache *cache, const char *img_path)
    46 {
   125 {
    47     struct stat st_img, st_cache;
   126     struct stat st_img, st_cache;
       
   127     int ver;
    48     
   128     
    49     // test original file
   129     // test original file
    50     if (stat(img_path, &st_img) < 0)
   130     if (stat(img_path, &st_img) < 0)
    51         RETURN_ERROR(PT_ERR_IMG_STAT);
   131         RETURN_ERROR(PT_ERR_IMG_STAT);
    52     
   132     
    60     }
   140     }
    61 
   141 
    62     // compare mtime
   142     // compare mtime
    63     if (st_img.st_mtime > st_cache.st_mtime)
   143     if (st_img.st_mtime > st_cache.st_mtime)
    64         return PT_CACHE_STALE;
   144         return PT_CACHE_STALE;
    65 
   145     
    66     else
   146     // read version
    67         return PT_CACHE_FRESH;
   147     if ((ver = pt_cache_version(cache)) < 0)
    68 }
   148         // fail
    69 
   149         return ver;
    70 int pt_cache_info (struct pt_cache *cache, struct pt_image_info *info)
   150 
       
   151     // compare version
       
   152     if (ver != PT_CACHE_VERSION)
       
   153         return PT_CACHE_INCOMPAT;
       
   154     
       
   155     // ok, should be in order
       
   156     return PT_CACHE_FRESH;
       
   157 }
       
   158 
       
   159 void pt_cache_info (struct pt_cache *cache, struct pt_image_info *info)
    71 {
   160 {
    72     struct stat st;
   161     struct stat st;
    73     int err;
   162 
    74 
   163     if (cache->file)
    75     // ensure open
   164         // img info
    76     if ((err = pt_cache_open(cache)))
   165         pt_png_info(&cache->file->header.png, info);
    77         return err;
       
    78 
       
    79     info->width = cache->header->width;
       
    80     info->height = cache->header->height;
       
    81 
   166 
    82     // stat
   167     // stat
    83     if (stat(cache->path, &st) < 0) {
   168     if (stat(cache->path, &st) < 0) {
    84         // unknown
   169         // unknown
    85         info->cache_mtime = 0;
   170         info->cache_mtime = 0;
    86         info->cache_bytes = 0;
   171         info->cache_bytes = 0;
    87         info->cache_blocks = 0;
   172         info->cache_blocks = 0;
    88 
   173 
    89     } else {
   174     } else {
    90         // store
   175         // store
       
   176         info->cache_version = pt_cache_version(cache);
    91         info->cache_mtime = st.st_mtime;
   177         info->cache_mtime = st.st_mtime;
    92         info->cache_bytes = st.st_size;
   178         info->cache_bytes = st.st_size;
    93         info->cache_blocks = st.st_blocks;
   179         info->cache_blocks = st.st_blocks;
    94     }
   180     }
    95 
       
    96     return 0;
       
    97 }
   181 }
    98 
   182 
    99 /**
   183 /**
   100  * Abort any incomplete open operation, cleaning up
   184  * Abort any incomplete open operation, cleaning up
   101  */
   185  */
   102 static void pt_cache_abort (struct pt_cache *cache)
   186 static void pt_cache_abort (struct pt_cache *cache)
   103 {
   187 {
   104     if (cache->header != NULL) {
   188     if (cache->file != NULL) {
   105         munmap(cache->header, PT_CACHE_HEADER_SIZE + cache->size);
   189         munmap(cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size);
   106 
   190 
   107         cache->header = NULL;
   191         cache->file = NULL;
   108         cache->data = NULL;
       
   109     }
   192     }
   110 
   193 
   111     if (cache->fd >= 0) {
   194     if (cache->fd >= 0) {
   112         close(cache->fd);
   195         close(cache->fd);
   113 
   196 
   114         cache->fd = -1;
   197         cache->fd = -1;
   115     }
   198     }
   116 }
       
   117 
       
   118 /**
       
   119  * Open the cache file as an fd for reading
       
   120  *
       
   121  * XXX: needs locking
       
   122  */
       
   123 static int pt_cache_open_read_fd (struct pt_cache *cache, int *fd_ptr)
       
   124 {
       
   125     int fd;
       
   126     
       
   127     // actual open()
       
   128     if ((fd = open(cache->path, O_RDONLY)) < 0)
       
   129         RETURN_ERROR_ERRNO(PT_ERR_OPEN_MODE, EACCES);
       
   130 
       
   131     // ok
       
   132     *fd_ptr = fd;
       
   133 
       
   134     return 0;
       
   135 }
   199 }
   136 
   200 
   137 /**
   201 /**
   138  * Open the .tmp cache file as an fd for writing
   202  * Open the .tmp cache file as an fd for writing
   139  */
   203  */
   145     // get .tmp path
   209     // get .tmp path
   146     if (path_with_fext(cache->path, tmp_path, sizeof(tmp_path), ".tmp"))
   210     if (path_with_fext(cache->path, tmp_path, sizeof(tmp_path), ".tmp"))
   147         RETURN_ERROR(PT_ERR_PATH);
   211         RETURN_ERROR(PT_ERR_PATH);
   148 
   212 
   149     // open for write, create
   213     // open for write, create
   150     // XXX: locking?
   214     // XXX: locking? At least O_EXCL...
   151     if ((fd = open(tmp_path, O_RDWR | O_CREAT, 0644)) < 0)
   215     if ((fd = open(tmp_path, O_RDWR | O_CREAT, 0644)) < 0)
   152         RETURN_ERROR(PT_ERR_CACHE_OPEN_TMP);
   216         RETURN_ERROR(PT_ERR_CACHE_OPEN_TMP);
   153 
   217 
   154     // ok
   218     // ok
   155     *fd_ptr = fd;
   219     *fd_ptr = fd;
   157     return 0;
   221     return 0;
   158 }
   222 }
   159 
   223 
   160 
   224 
   161 /**
   225 /**
   162  * Mmap the opened cache file using PT_CACHE_HEADER_SIZE plus the calculated size stored in cache->size
   226  * Mmap the pt_cache_file using sizeof(struct pt_cache_file) + data_size
   163  */
   227  */
   164 static int pt_cache_open_mmap (struct pt_cache *cache, void **addr_ptr, bool readonly)
   228 static int pt_cache_open_mmap (struct pt_cache *cache, void **addr_ptr, size_t data_size, bool readonly)
   165 {
   229 {
   166     int prot = 0;
   230     int prot = 0;
   167     void *addr;
   231     void *addr;
   168 
   232 
   169     // determine prot
   233     // determine prot
   174 
   238 
   175         prot |= PROT_WRITE;
   239         prot |= PROT_WRITE;
   176     }
   240     }
   177 
   241 
   178     // mmap() the full file including header
   242     // mmap() the full file including header
   179     if ((addr = mmap(NULL, PT_CACHE_HEADER_SIZE + cache->size, prot, MAP_SHARED, cache->fd, 0)) == MAP_FAILED)
   243     if ((addr = mmap(NULL, sizeof(struct pt_cache_file) + data_size, prot, MAP_SHARED, cache->fd, 0)) == MAP_FAILED)
   180         RETURN_ERROR(PT_ERR_CACHE_MMAP);
   244         RETURN_ERROR(PT_ERR_CACHE_MMAP);
   181 
   245 
   182     // ok
   246     // ok
   183     *addr_ptr = addr;
   247     *addr_ptr = addr;
   184 
   248 
   185     return 0;
   249     return 0;
   186 }
   250 }
   187 
   251 
   188 /**
   252 int pt_cache_open (struct pt_cache *cache)
   189  * Read in the cache header from the open file
   253 {
   190  */
   254     struct pt_cache_header header;
   191 static int pt_cache_read_header (struct pt_cache *cache, struct pt_cache_header *header)
   255     int err;
   192 {
   256 
   193     size_t len = sizeof(*header);
   257     // ignore if already open
   194     char *buf = (char *) header;
   258     if (cache->file)
   195 
   259         return 0;
   196     // seek to start
   260 
   197     if (lseek(cache->fd, 0, SEEK_SET) != 0)
   261     // open the .cache in readonly mode
   198         RETURN_ERROR(PT_ERR_CACHE_SEEK);
   262     if ((err = pt_cache_open_read_fd(cache, &cache->fd)))
   199 
   263         return err;
   200     // write out full header
   264 
   201     while (len) {
   265     // read in header
   202         ssize_t ret;
   266     if ((err = pt_cache_read_header(cache->fd, &header)))
   203         
   267         JUMP_ERROR(err);
   204         // try and write out the header
   268 
   205         if ((ret = read(cache->fd, buf, len)) <= 0)
   269     // check version
   206             RETURN_ERROR(PT_ERR_CACHE_READ);
   270     if (header.version != PT_CACHE_VERSION)
   207 
   271         JUMP_SET_ERROR(err, PT_ERR_CACHE_VERSION);
   208         // update offset
   272 
   209         buf += ret;
   273     // mmap the header + data
   210         len -= ret;
   274     if ((err = pt_cache_open_mmap(cache, (void **) &cache->file, header.data_size, true)))
   211     }
   275         JUMP_ERROR(err);
   212 
   276 
   213     // done
   277     // done
   214     return 0;
   278     return 0;
       
   279 
       
   280 error:
       
   281     // cleanup
       
   282     pt_cache_abort(cache);
       
   283 
       
   284     return err;
   215 }
   285 }
   216 
   286 
   217 /**
   287 /**
   218  * Write out the cache header into the opened file
   288  * Write out the cache header into the opened file
   219  */
   289  */
   246 /**
   316 /**
   247  * Create a new .tmp cache file, open it, and write out the header.
   317  * Create a new .tmp cache file, open it, and write out the header.
   248  */
   318  */
   249 static int pt_cache_create (struct pt_cache *cache, struct pt_cache_header *header)
   319 static int pt_cache_create (struct pt_cache *cache, struct pt_cache_header *header)
   250 {
   320 {
   251     void *base;
       
   252     int err;
   321     int err;
   253 
   322 
   254     // no access
   323     // no access
   255     if (!(cache->mode & PT_OPEN_UPDATE))
   324     if (!(cache->mode & PT_OPEN_UPDATE))
   256         RETURN_ERROR(PT_ERR_OPEN_MODE);
   325         RETURN_ERROR(PT_ERR_OPEN_MODE);
   257 
   326 
   258     // open as .tmp
   327     // open as .tmp
   259     if ((err = pt_cache_open_tmp_fd(cache, &cache->fd)))
   328     if ((err = pt_cache_open_tmp_fd(cache, &cache->fd)))
   260         return err;
   329         return err;
   261 
   330 
   262     // calculate data size
   331     // write header
   263     cache->size = header->height * header->row_bytes;
   332     if ((err = pt_cache_write_header(cache, header)))
       
   333         JUMP_ERROR(err);
   264 
   334 
   265     // grow file
   335     // grow file
   266     if (ftruncate(cache->fd, PT_CACHE_HEADER_SIZE + cache->size) < 0)
   336     if (ftruncate(cache->fd, sizeof(struct pt_cache_file) + header->data_size) < 0)
   267         JUMP_SET_ERROR(err, PT_ERR_CACHE_TRUNC);
   337         JUMP_SET_ERROR(err, PT_ERR_CACHE_TRUNC);
   268 
   338 
   269     // mmap header and data
   339     // mmap header and data
   270     if ((err = pt_cache_open_mmap(cache, &base, false)))
   340     if ((err = pt_cache_open_mmap(cache, (void **) &cache->file, header->data_size, false)))
   271         JUMP_ERROR(err);
       
   272 
       
   273     cache->header = base;
       
   274     cache->data = base + PT_CACHE_HEADER_SIZE;
       
   275 
       
   276     // write header
       
   277     // XXX: should just mmap...
       
   278     if ((err = pt_cache_write_header(cache, header)))
       
   279         JUMP_ERROR(err);
   341         JUMP_ERROR(err);
   280 
   342 
   281     // done
   343     // done
   282     return 0;
   344     return 0;
   283 
   345 
   305 
   367 
   306     // ok
   368     // ok
   307     return 0;
   369     return 0;
   308 }
   370 }
   309 
   371 
   310 int pt_cache_open (struct pt_cache *cache)
   372 int pt_cache_update (struct pt_cache *cache, struct pt_png_img *img, const struct pt_image_params *params)
   311 {
       
   312     struct pt_cache_header header;
       
   313     void *base;
       
   314     int err;
       
   315 
       
   316     // ignore if already open
       
   317     if (cache->header && cache->data)
       
   318         return 0;
       
   319 
       
   320     // open the .cache
       
   321     if ((err = pt_cache_open_read_fd(cache, &cache->fd)))
       
   322         return err;
       
   323 
       
   324     // read in header
       
   325     if ((err = pt_cache_read_header(cache, &header)))
       
   326         JUMP_ERROR(err);
       
   327 
       
   328     // calculate data size
       
   329     cache->size = header.height * header.row_bytes;
       
   330 
       
   331     // mmap header and data
       
   332     if ((err = pt_cache_open_mmap(cache, &base, true)))
       
   333         JUMP_ERROR(err);
       
   334 
       
   335     cache->header = base;
       
   336     cache->data = base + PT_CACHE_HEADER_SIZE;
       
   337 
       
   338     // done
       
   339     return 0;
       
   340 
       
   341 error:
       
   342     // cleanup
       
   343     pt_cache_abort(cache);
       
   344 
       
   345     return err;
       
   346 }
       
   347 
       
   348 #define min(a, b) (((a) < (b)) ? (a) : (b))
       
   349 
       
   350 /**
       
   351  * Decode the PNG data directly to mmap() - not good for sparse backgrounds
       
   352  */
       
   353 static int decode_png_raw (struct pt_cache *cache, png_structp png, png_infop info)
       
   354 {
       
   355     // write out raw image data a row at a time
       
   356     for (size_t row = 0; row < cache->header->height; row++) {
       
   357         // read row data, non-interlaced
       
   358         png_read_row(png, cache->data + row * cache->header->row_bytes, NULL);
       
   359     }
       
   360 
       
   361     return 0;
       
   362 }
       
   363 
       
   364 static int decode_png_sparse (struct pt_cache *cache, png_structp png, png_infop info)
       
   365 {
       
   366     // one row of pixel data
       
   367     uint8_t *row_buf;
       
   368 
       
   369     // alloc
       
   370     if ((row_buf = malloc(cache->header->row_bytes)) == NULL)
       
   371         RETURN_ERROR(PT_ERR_MEM);
       
   372 
       
   373     // decode each row at a time
       
   374     for (size_t row = 0; row < cache->header->height; row++) {
       
   375         // read row data, non-interlaced
       
   376         png_read_row(png, row_buf, NULL);
       
   377         
       
   378         // skip background-colored regions to keep the cache file sparse
       
   379         // ...in blocks of PT_CACHE_BLOCK_SIZE bytes
       
   380         for (size_t col_base = 0; col_base < cache->header->width; col_base += PT_CACHE_BLOCK_SIZE) {
       
   381             // size of this block in bytes
       
   382             size_t block_size = min(PT_CACHE_BLOCK_SIZE * cache->header->col_bytes, cache->header->row_bytes - col_base);
       
   383 
       
   384             // ...each pixel
       
   385             for (
       
   386                     size_t col = col_base;
       
   387 
       
   388                     // BLOCK_SIZE * col_bytes wide, don't go over the edge
       
   389                     col < col_base + block_size; 
       
   390 
       
   391                     col += cache->header->col_bytes
       
   392             ) {
       
   393                 // test this pixel
       
   394                 if (bcmp(row_buf + col, cache->header->params.background_color, cache->header->col_bytes)) {
       
   395                     // differs
       
   396                     memcpy(
       
   397                             cache->data + row * cache->header->row_bytes + col_base, 
       
   398                             row_buf + col_base,
       
   399                             block_size
       
   400                     );
       
   401                     
       
   402                     // skip to next block
       
   403                     break;
       
   404                 }
       
   405             }
       
   406 
       
   407             // skip this block
       
   408             continue;
       
   409         }
       
   410     }
       
   411 
       
   412     return 0;
       
   413 }
       
   414 
       
   415 int pt_cache_update_png (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_image_params *params)
       
   416 {
   373 {
   417     struct pt_cache_header header;
   374     struct pt_cache_header header;
   418     int err;
   375     int err;
   419     
   376     
   420     // XXX: check cache_mode
   377     // XXX: check cache_mode
   421     // XXX: check image doesn't use any options we don't handle
       
   422     // XXX: close any already-opened cache file
   378     // XXX: close any already-opened cache file
   423 
   379     
   424     memset(&header, 0, sizeof(header));
   380     // prep header
   425 
   381     header.version = PT_CACHE_VERSION;
   426     // fill in basic info
   382     header.format = PT_IMG_PNG;
   427     header.width = png_get_image_width(png, info);
   383 
   428     header.height = png_get_image_height(png, info);
   384     // read img header
   429     header.bit_depth = png_get_bit_depth(png, info);
   385     if ((err = pt_png_read_header(img, &header.png, &header.data_size)))
   430     header.color_type = png_get_color_type(png, info);
   386         return err;
   431 
   387 
   432     log_debug("width=%u, height=%u, bit_depth=%u, color_type=%u", 
   388     // save any params
   433             header.width, header.height, header.bit_depth, header.color_type
       
   434     );
       
   435 
       
   436     // only pack 1 pixel per byte, changes rowbytes
       
   437     if (header.bit_depth < 8)
       
   438         png_set_packing(png);
       
   439 
       
   440     // fill in other info
       
   441     header.row_bytes = png_get_rowbytes(png, info);
       
   442 
       
   443     // calculate bpp as num_channels * bpc
       
   444     // XXX: this assumes the packed bit depth will be either 8 or 16
       
   445     header.col_bytes = png_get_channels(png, info) * (header.bit_depth == 16 ? 2 : 1);
       
   446 
       
   447     log_debug("row_bytes=%u, col_bytes=%u", header.row_bytes, header.col_bytes);
       
   448     
       
   449     // palette etc.
       
   450     if (header.color_type == PNG_COLOR_TYPE_PALETTE) {
       
   451         int num_palette;
       
   452         png_colorp palette;
       
   453 
       
   454         if (png_get_PLTE(png, info, &palette, &num_palette) == 0)
       
   455             // XXX: PLTE chunk not read?
       
   456             RETURN_ERROR(PT_ERR_PNG);
       
   457         
       
   458         // should only be 256 of them at most
       
   459         assert(num_palette <= PNG_MAX_PALETTE_LENGTH);
       
   460     
       
   461         // copy
       
   462         header.num_palette = num_palette;
       
   463         memcpy(&header.palette, palette, num_palette * sizeof(*palette));
       
   464         
       
   465         log_debug("num_palette=%u", num_palette);
       
   466     }
       
   467 
       
   468     // any params
       
   469     if (params)
   389     if (params)
   470         header.params = *params;
   390         header.params = *params;
   471 
   391 
   472     // create .tmp and write out header
   392     // create/open .tmp and write out header
   473     if ((err = pt_cache_create(cache, &header)))
   393     if ((err = pt_cache_create(cache, &header)))
   474         return err;
   394         return err;
   475     
   395 
   476     // decode
   396     // decode to disk
   477     if ((err = decode_png_sparse(cache, png, info)))
   397     if ((err = pt_png_decode(img, &cache->file->header.png, &cache->file->header.params, cache->file->data)))
   478         return err;
   398         return err;
   479 
   399 
   480     // move from .tmp to .cache
   400     // done, commit .tmp
   481     if ((err = pt_cache_create_done(cache)))
   401     if ((err = pt_cache_create_done(cache)))
   482         // XXX: pt_cache_abort?
   402         // XXX: pt_cache_abort?
   483         return err;
   403         return err;
   484 
   404 
   485     // done!
   405     return 0;
   486     return 0;
   406 }
   487 }
   407 
   488 
   408 int pt_cache_tile (struct pt_cache *cache, struct pt_tile *tile)
   489 /**
       
   490  * Return a pointer to the pixel data on \a row, starting at \a col.
       
   491  */
       
   492 static inline void* tile_row_col (struct pt_cache *cache, size_t row, size_t col)
       
   493 {
       
   494     return cache->data + (row * cache->header->row_bytes) + (col * cache->header->col_bytes);
       
   495 }
       
   496 
       
   497 /**
       
   498  * Fill in a clipped region of \a width_px pixels at the given row segment
       
   499  */
       
   500 static inline void tile_row_fill_clip (struct pt_cache *cache, png_byte *row, size_t width_px)
       
   501 {
       
   502     // XXX: use a configureable background color, or full transparency?
       
   503     memset(row, /* 0xd7 */ 0x00, width_px * cache->header->col_bytes);
       
   504 }
       
   505 
       
   506 /**
       
   507  * Write raw tile image data, directly from the cache
       
   508  */
       
   509 static int write_png_data_direct (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti)
       
   510 {
       
   511     for (size_t row = ti->y; row < ti->y + ti->height; row++)
       
   512         // write data directly
       
   513         png_write_row(png, tile_row_col(cache, row, ti->x));
       
   514 
       
   515     return 0;
       
   516 }
       
   517 
       
   518 /**
       
   519  * 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
       
   520  */
       
   521 static int write_png_data_clipped (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti)
       
   522 {
       
   523     png_byte *rowbuf;
       
   524     size_t row;
       
   525     
       
   526     // image data goes from (ti->x ... clip_x, ti->y ... clip_y), remaining region is filled
       
   527     size_t clip_x, clip_y;
       
   528 
       
   529 
       
   530     // figure out if the tile clips over the right edge
       
   531     // XXX: use min()
       
   532     if (ti->x + ti->width > cache->header->width)
       
   533         clip_x = cache->header->width;
       
   534     else
       
   535         clip_x = ti->x + ti->width;
       
   536     
       
   537     // figure out if the tile clips over the bottom edge
       
   538     // XXX: use min()
       
   539     if (ti->y + ti->height > cache->header->height)
       
   540         clip_y = cache->header->height;
       
   541     else
       
   542         clip_y = ti->y + ti->height;
       
   543 
       
   544 
       
   545     // allocate buffer for a single row of image data
       
   546     if ((rowbuf = malloc(ti->width * cache->header->col_bytes)) == NULL)
       
   547         RETURN_ERROR(PT_ERR_MEM);
       
   548 
       
   549     // how much data we actually have for each row, in px and bytes
       
   550     // from [(tile x)---](clip x)
       
   551     size_t row_px = clip_x - ti->x;
       
   552     size_t row_bytes = row_px * cache->header->col_bytes;
       
   553 
       
   554     // write the rows that we have
       
   555     // from [(tile y]---](clip y)
       
   556     for (row = ti->y; row < clip_y; row++) {
       
   557         // copy in the actual tile data...
       
   558         memcpy(rowbuf, tile_row_col(cache, row, ti->x), row_bytes);
       
   559 
       
   560         // generate the data for the remaining, clipped, columns
       
   561         tile_row_fill_clip(cache, rowbuf + row_bytes, (ti->width - row_px));
       
   562 
       
   563         // write
       
   564         png_write_row(png, rowbuf);
       
   565     }
       
   566 
       
   567     // generate the data for the remaining, clipped, rows
       
   568     tile_row_fill_clip(cache, rowbuf, ti->width);
       
   569     
       
   570     // write out the remaining rows as clipped data
       
   571     for (; row < ti->y + ti->height; row++)
       
   572         png_write_row(png, rowbuf);
       
   573 
       
   574     // ok
       
   575     return 0;
       
   576 }
       
   577 
       
   578 static size_t scale_by_zoom_factor (size_t value, int z)
       
   579 {
       
   580     if (z > 0)
       
   581         return value << z;
       
   582 
       
   583     else if (z < 0)
       
   584         return value >> -z;
       
   585 
       
   586     else
       
   587         return value;
       
   588 }
       
   589 
       
   590 #define ADD_AVG(l, r) (l) = ((l) + (r)) / 2
       
   591 
       
   592 static int png_pixel_data (png_color *out, struct pt_cache *cache, size_t row, size_t col)
       
   593 {
       
   594     if (cache->header->color_type == PNG_COLOR_TYPE_PALETTE) {
       
   595         // palette entry number
       
   596         int p;
       
   597 
       
   598         if (cache->header->bit_depth == 8)
       
   599             p = *((uint8_t *) tile_row_col(cache, row, col));
       
   600         else
       
   601             return -1;
       
   602 
       
   603         if (p >= cache->header->num_palette)
       
   604             return -1;
       
   605         
       
   606         // reference data from palette
       
   607         *out = cache->header->palette[p];
       
   608 
       
   609         return 0;
       
   610 
       
   611     } else {
       
   612         return -1;
       
   613     }
       
   614 }
       
   615 
       
   616 /**
       
   617  * Write unscaled tile data
       
   618  */
       
   619 static int write_png_data_unzoomed (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti)
       
   620 {
       
   621     int err;
       
   622 
       
   623     // set basic info
       
   624     png_set_IHDR(png, info, ti->width, ti->height, cache->header->bit_depth, cache->header->color_type,
       
   625             PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT
       
   626     );
       
   627 
       
   628     // set palette?
       
   629     if (cache->header->color_type == PNG_COLOR_TYPE_PALETTE)
       
   630         png_set_PLTE(png, info, cache->header->palette, cache->header->num_palette);
       
   631 
       
   632     // write meta-info
       
   633     png_write_info(png, info);
       
   634 
       
   635     // our pixel data is packed into 1 pixel per byte (8bpp or 16bpp)
       
   636     png_set_packing(png);
       
   637     
       
   638     // figure out if the tile clips
       
   639     if (ti->x + ti->width <= cache->header->width && ti->y + ti->height <= cache->header->height)
       
   640         // doesn't clip, just use the raw data
       
   641         err = write_png_data_direct(cache, png, info, ti);
       
   642 
       
   643     else
       
   644         // fill in clipped regions
       
   645         err = write_png_data_clipped(cache, png, info, ti);
       
   646     
       
   647     return err;
       
   648 }
       
   649 
       
   650 /**
       
   651  * Write scaled tile data
       
   652  */
       
   653 static int write_png_data_zoomed (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti)
       
   654 {
       
   655     // size of the image data in px
       
   656     size_t data_width = scale_by_zoom_factor(ti->width, -ti->zoom);
       
   657     size_t data_height = scale_by_zoom_factor(ti->height, -ti->zoom);
       
   658 
       
   659     // input pixels per output pixel
       
   660     size_t pixel_size = scale_by_zoom_factor(1, -ti->zoom);
       
   661 
       
   662     // bytes per output pixel
       
   663     size_t pixel_bytes = 3;
       
   664 
       
   665     // size of the output tile in px
       
   666     size_t row_width = ti->width;
       
   667 
       
   668     // size of an output row in bytes (RGB)
       
   669     size_t row_bytes = row_width * 3;
       
   670 
       
   671     // buffer to hold output rows
       
   672     uint8_t *row_buf;
       
   673     
       
   674     // XXX: only supports zooming out...
       
   675     if (ti->zoom >= 0)
       
   676         RETURN_ERROR(PT_ERR_ZOOM);
       
   677 
       
   678     if ((row_buf = malloc(row_bytes)) == NULL)
       
   679         RETURN_ERROR(PT_ERR_MEM);
       
   680 
       
   681 
       
   682     // define pixel format: 8bpp RGB
       
   683     png_set_IHDR(png, info, ti->width, ti->height, 8, PNG_COLOR_TYPE_RGB,
       
   684             PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT
       
   685     );
       
   686     
       
   687     // write meta-info
       
   688     png_write_info(png, info);
       
   689 
       
   690     // ...each output row
       
   691     for (size_t out_row = 0; out_row < ti->height; out_row++) {
       
   692         memset(row_buf, 0, row_bytes);
       
   693 
       
   694         // ...includes pixels starting from this row.
       
   695         size_t in_row_offset = ti->y + scale_by_zoom_factor(out_row, -ti->zoom);
       
   696         
       
   697         // ...each out row includes pixel_size in rows
       
   698         for (size_t in_row = in_row_offset; in_row < in_row_offset + pixel_size && in_row < cache->header->height; in_row++) {
       
   699             // and includes each input pixel
       
   700             for (size_t in_col = ti->x; in_col < ti->x + data_width && in_col < cache->header->width; in_col++) {
       
   701                 png_color c;
       
   702 
       
   703                 // ...for this output pixel
       
   704                 size_t out_col = scale_by_zoom_factor(in_col - ti->x, ti->zoom);
       
   705                 
       
   706                 // get pixel RGB data
       
   707                 if (png_pixel_data(&c, cache, in_row, in_col))
       
   708                     return -1;
       
   709                 
       
   710                 // average the RGB data        
       
   711                 ADD_AVG(row_buf[out_col * pixel_bytes + 0], c.red);
       
   712                 ADD_AVG(row_buf[out_col * pixel_bytes + 1], c.green);
       
   713                 ADD_AVG(row_buf[out_col * pixel_bytes + 2], c.blue);
       
   714             }
       
   715         }
       
   716 
       
   717         // output
       
   718         png_write_row(png, row_buf);
       
   719     }
       
   720 
       
   721     // done
       
   722     return 0;
       
   723 }
       
   724 
       
   725 int pt_cache_tile_png (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti)
       
   726 {
   409 {
   727     int err;
   410     int err;
   728 
   411 
   729     // ensure open
   412     // ensure open
   730     if ((err = pt_cache_open(cache)))
   413     if ((err = pt_cache_open(cache)))
   731         return err;
   414         return err;
   732 
   415 
   733     // check within bounds
   416     // render
   734     if (ti->x >= cache->header->width || ti->y >= cache->header->height)
   417     if ((err = pt_png_tile(&cache->file->header.png, cache->file->data, tile)))
   735         // completely outside
   418         return err;
   736         RETURN_ERROR(PT_ERR_TILE_CLIP);
   419 
   737    
       
   738     // unscaled or scaled?
       
   739     if (ti->zoom)
       
   740         err = write_png_data_zoomed(cache, png, info, ti);
       
   741 
       
   742     else
       
   743         err = write_png_data_unzoomed(cache, png, info, ti);
       
   744 
       
   745     if (err)
       
   746         return err;
       
   747     
       
   748     // done, flush remaining output
       
   749     png_write_flush(png);
       
   750 
       
   751     // ok
       
   752     return 0;
   420     return 0;
   753 }
   421 }
   754 
   422 
   755 void pt_cache_destroy (struct pt_cache *cache)
   423 void pt_cache_destroy (struct pt_cache *cache)
   756 {
   424 {