src/lib/cache.c
author Tero Marttila <terom@fixme.fi>
Tue, 29 Dec 2009 02:11:27 +0200
changeset 12 3576e00388fd
parent 11 eb2a1472f084
child 13 294201975f8c
permissions -rw-r--r--
draw clipped tiles by filling in a zeros for the background pixels
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include "cache.h"
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
     2
#include "shared/util.h"
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
     3
#include "shared/log.h" // only LOG_DEBUG
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
     5
#include <stdlib.h>
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
     6
#include <unistd.h>
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
     7
#include <sys/types.h>
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
     8
#include <sys/stat.h>
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
     9
#include <fcntl.h>
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    10
#include <sys/mman.h>
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    11
#include <errno.h>
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
    12
#include <assert.h>
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    13
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    14
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    15
int pt_cache_new (struct pt_cache **cache_ptr, const char *path, int mode)
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
{
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
    struct pt_cache *cache;
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    19
    // alloc
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
    if ((cache = calloc(1, sizeof(*cache))) == NULL)
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
        return -1;
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    23
    if ((cache->path = strdup(path)) == NULL)
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    24
        goto error;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    25
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    26
    // init
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    27
    cache->fd = -1;
3
da7c6dcafb43 store cache->mode, error handling for pt_image_update_cache
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
    28
    cache->mode = mode;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    29
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    30
    // ok
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    31
    *cache_ptr = cache;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    32
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    33
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    34
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    35
error:
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    36
    // cleanup
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    37
    if (cache)
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    38
        pt_cache_destroy(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    39
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    40
    return -1;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    41
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    42
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    43
int pt_cache_status (struct pt_cache *cache, const char *img_path)
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
{
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    45
    struct stat st_img, st_cache;
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    46
    
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    47
    // test original file
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    48
    if (stat(img_path, &st_img) < 0)
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    49
        return -1;
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    50
    
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    51
    // test cache file
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    52
    if (stat(cache->path, &st_cache) < 0) {
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    53
        // always stale if it doesn't exist yet
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    54
        if (errno == ENOENT)
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    55
            return PT_CACHE_NONE;
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    56
        else
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    57
            return -1;
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    58
    }
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    59
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    60
    // compare mtime
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    61
    if (st_img.st_mtime > st_cache.st_mtime)
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    62
        return PT_CACHE_STALE;
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    63
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    64
    else
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    65
        return PT_CACHE_FRESH;
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
}
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
10
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
    68
int pt_cache_info (struct pt_cache *cache, struct pt_image_info *info)
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
    69
{
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
    70
    // ensure open
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
    71
    if (pt_cache_open(cache))
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
    72
        return -1;
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
    73
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
    74
    info->width = cache->header->width;
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
    75
    info->height = cache->header->height;
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
    76
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
    77
    return 0;
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
    78
}
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
    79
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
/**
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    81
 * Abort any incomplete open operation, cleaning up
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    82
 */
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    83
static void pt_cache_abort (struct pt_cache *cache)
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    84
{
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    85
    if (cache->header != NULL) {
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    86
        munmap(cache->header, PT_CACHE_HEADER_SIZE + cache->size);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    87
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    88
        cache->header = NULL;
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    89
        cache->data = NULL;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    90
    }
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    91
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    92
    if (cache->fd >= 0) {
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    93
        close(cache->fd);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    94
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    95
        cache->fd = -1;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    96
    }
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    97
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    98
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    99
/**
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   100
 * Open the cache file as an fd for reading
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   101
 *
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   102
 * XXX: needs locking
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   103
 */
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   104
static int pt_cache_open_read_fd (struct pt_cache *cache, int *fd_ptr)
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   105
{
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   106
    int fd;
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   107
    
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   108
    // actual open()
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   109
    if ((fd = open(cache->path, O_RDONLY)) < 0)
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   110
        return -1;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   111
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   112
    // ok
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   113
    *fd_ptr = fd;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   114
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   115
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   116
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   117
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   118
/**
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   119
 * Open the .tmp cache file as an fd for writing
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   120
 */
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   121
static int pt_cache_open_tmp_fd (struct pt_cache *cache, int *fd_ptr)
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   122
{
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   123
    int fd;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   124
    char tmp_path[1024];
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   125
    
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   126
    // get .tmp path
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   127
    if (path_with_fext(cache->path, tmp_path, sizeof(tmp_path), ".tmp"))
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   128
        return -1;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   129
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   130
    // open for write, create
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   131
    // XXX: locking?
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   132
    if ((fd = open(tmp_path, O_RDWR | O_CREAT, 0644)) < 0)
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   133
        return -1;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   134
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   135
    // ok
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   136
    *fd_ptr = fd;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   137
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   138
    return 0;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   139
}
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   140
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   141
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   142
/**
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   143
 * Mmap the opened cache file using PT_CACHE_HEADER_SIZE plus the calculated size stored in cache->size
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   144
 */
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   145
static int pt_cache_open_mmap (struct pt_cache *cache, void **addr_ptr, bool readonly)
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   146
{
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   147
    int prot = 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   148
    void *addr;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   149
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   150
    // determine prot
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   151
    prot |= PROT_READ;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   152
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   153
    if (!readonly) {
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   154
        assert(cache->mode & PT_OPEN_UPDATE);
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   155
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   156
        prot |= PROT_WRITE;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   157
    }
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   158
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   159
    // perform mmap() from second page on
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   160
    if ((addr = mmap(NULL, PT_CACHE_HEADER_SIZE + cache->size, prot, MAP_SHARED, cache->fd, 0)) == MAP_FAILED)
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   161
        return -1;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   162
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   163
    // ok
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   164
    *addr_ptr = addr;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   165
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   166
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   167
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   168
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   169
/**
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   170
 * Read in the cache header from the open file
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   171
 */
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   172
static int pt_cache_read_header (struct pt_cache *cache, struct pt_cache_header *header)
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   173
{
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   174
    size_t len = sizeof(*header);
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   175
    char *buf = (char *) header;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   176
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   177
    // seek to start
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   178
    if (lseek(cache->fd, 0, SEEK_SET) != 0)
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   179
        return -1;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   180
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   181
    // write out full header
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   182
    while (len) {
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   183
        ssize_t ret;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   184
        
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   185
        // try and write out the header
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   186
        if ((ret = read(cache->fd, buf, len)) < 0)
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   187
            return -1;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   188
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   189
        // update offset
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   190
        buf += ret;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   191
        len -= ret;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   192
    }
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   193
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   194
    // done
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   195
    return 0;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   196
}
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   197
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   198
/**
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   199
 * Write out the cache header into the opened file
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   200
 */
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   201
static int pt_cache_write_header (struct pt_cache *cache, const struct pt_cache_header *header)
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   202
{
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   203
    size_t len = sizeof(*header);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   204
    const char *buf = (const char *) header;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   205
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   206
    // seek to start
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   207
    if (lseek(cache->fd, 0, SEEK_SET) != 0)
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   208
        return -1;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   209
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   210
    // write out full header
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   211
    while (len) {
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   212
        ssize_t ret;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   213
        
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   214
        // try and write out the header
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   215
        if ((ret = write(cache->fd, buf, len)) < 0)
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   216
            return -1;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   217
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   218
        // update offset
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   219
        buf += ret;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   220
        len -= ret;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   221
    }
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   222
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   223
    // done
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   224
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   225
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   226
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   227
/**
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   228
 * Create a new .tmp cache file, open it, and write out the header.
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   229
 */
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   230
static int pt_cache_create (struct pt_cache *cache, struct pt_cache_header *header)
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   231
{
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   232
    void *base;
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   233
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   234
    // no access
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   235
    if (!(cache->mode & PT_OPEN_UPDATE)) {
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   236
        errno = EPERM;
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   237
        return -1;
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   238
    }
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   239
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   240
    // open as .tmp
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   241
    if (pt_cache_open_tmp_fd(cache, &cache->fd))
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   242
        return -1;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   243
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   244
    // calculate data size
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   245
    cache->size = sizeof(*header) + header->height * header->row_bytes;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   246
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   247
    // grow file
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   248
    if (ftruncate(cache->fd, PT_CACHE_HEADER_SIZE + cache->size) < 0)
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   249
        goto error;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   250
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   251
    // mmap header and data
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   252
    if (pt_cache_open_mmap(cache, &base, false))
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   253
        goto error;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   254
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   255
    cache->header = base;
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   256
    cache->data = base + PT_CACHE_HEADER_SIZE;
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   257
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   258
    // write header
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   259
    // XXX: should just mmap...
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   260
    if (pt_cache_write_header(cache, header))
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   261
        goto error;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   262
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   263
    // done
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   264
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   265
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   266
error:
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   267
    // cleanup
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   268
    pt_cache_abort(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   269
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   270
    return -1;
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   271
}
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   272
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   273
/**
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   274
 * Rename the opened .tmp to .cache
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   275
 */
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   276
static int pt_cache_create_done (struct pt_cache *cache)
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   277
{
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   278
    char tmp_path[1024];
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   279
    
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   280
    // get .tmp path
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   281
    if (path_with_fext(cache->path, tmp_path, sizeof(tmp_path), ".tmp"))
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   282
        return -1;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   283
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   284
    // rename
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   285
    if (rename(tmp_path, cache->path) < 0)
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   286
        return -1;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   287
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   288
    // ok
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   289
    return 0;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   290
}
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   291
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   292
int pt_cache_open (struct pt_cache *cache)
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   293
{
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   294
    struct pt_cache_header header;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   295
    void *base;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   296
10
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   297
    // ignore if already open
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   298
    if (cache->header && cache->data)
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   299
        return 0;
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   300
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   301
    // open the .cache
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   302
    if (pt_cache_open_read_fd(cache, &cache->fd))
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   303
        return -1;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   304
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   305
    // read in header
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   306
    if (pt_cache_read_header(cache, &header))
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   307
        return -1;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   308
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   309
    // calculate data size
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   310
    cache->size = sizeof(header) + header.height * header.row_bytes;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   311
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   312
    // mmap header and data
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   313
    if (pt_cache_open_mmap(cache, &base, true))
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   314
        goto error;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   315
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   316
    cache->header = base;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   317
    cache->data = base + PT_CACHE_HEADER_SIZE;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   318
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   319
    // done
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   320
    return 0;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   321
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   322
error:
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   323
    // cleanup
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   324
    pt_cache_abort(cache);
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   325
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   326
    return -1;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   327
}
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   328
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   329
int pt_cache_update_png (struct pt_cache *cache, png_structp png, png_infop info)
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   330
{
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   331
    struct pt_cache_header header;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   332
    
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   333
    // XXX: check cache_mode
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   334
    // XXX: check image doesn't use any options we don't handle
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   335
    // XXX: close any already-opened cache file
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   336
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   337
    memset(&header, 0, sizeof(header));
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   338
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   339
    // fill in basic info
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   340
    header.width = png_get_image_width(png, info);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   341
    header.height = png_get_image_height(png, info);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   342
    header.bit_depth = png_get_bit_depth(png, info);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   343
    header.color_type = png_get_color_type(png, info);
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   344
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   345
    log_debug("width=%u, height=%u, bit_depth=%u, color_type=%u", 
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   346
            header.width, header.height, header.bit_depth, header.color_type
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   347
    );
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   348
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   349
    // only pack 1 pixel per byte, changes rowbytes
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   350
    if (header.bit_depth < 8)
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   351
        png_set_packing(png);
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   352
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   353
    // fill in other info
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   354
    header.row_bytes = png_get_rowbytes(png, info);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   355
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   356
    // calculate bpp as num_channels * bpc
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   357
    // XXX: this assumes the packed bit depth will be either 8 or 16
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   358
    header.col_bytes = png_get_channels(png, info) * (header.bit_depth == 16 ? 2 : 1);
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   359
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   360
    log_debug("row_bytes=%u, col_bytes=%u", header.row_bytes, header.col_bytes);
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   361
    
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   362
    // palette etc.
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   363
    if (header.color_type == PNG_COLOR_TYPE_PALETTE) {
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   364
        int num_palette;
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   365
        png_colorp palette;
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   366
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   367
        if (png_get_PLTE(png, info, &palette, &num_palette) == 0)
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   368
            // XXX: PLTE chunk not read?
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   369
            return -1;
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   370
        
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   371
        // should only be 256 of them at most
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   372
        assert(num_palette <= PNG_MAX_PALETTE_LENGTH);
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   373
    
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   374
        // copy
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   375
        header.num_palette = num_palette;
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   376
        memcpy(&header.palette, palette, num_palette * sizeof(*palette));
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   377
        
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   378
        log_debug("num_palette=%u", num_palette);
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   379
    }
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   380
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   381
    // create .tmp and write out header
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   382
    if (pt_cache_create(cache, &header))
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   383
        return -1;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   384
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   385
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   386
    // write out raw image data a row at a time
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   387
    for (size_t row = 0; row < header.height; row++) {
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   388
        // read row data, non-interlaced
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   389
        png_read_row(png, cache->data + row * header.row_bytes, NULL);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   390
    }
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   391
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   392
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   393
    // move from .tmp to .cache
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   394
    if (pt_cache_create_done(cache))
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   395
        return -1;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   396
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   397
    // done!
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   398
    return 0;
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   399
}
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   400
12
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   401
/**
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   402
 * Return a pointer to the pixel data on \a row, starting at \a col.
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   403
 */
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   404
static inline void* tile_row_col (struct pt_cache *cache, size_t row, size_t col)
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   405
{
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   406
    return cache->data + (row * cache->header->row_bytes) + (col * cache->header->col_bytes);
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   407
}
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   408
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   409
/**
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   410
 * Fill in a clipped region of \a width_px pixels at the given row segment
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   411
 */
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   412
static inline void tile_row_fill_clip (struct pt_cache *cache, png_byte *row, size_t width_px)
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   413
{
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   414
    // XXX: use a defined background color, or full transparency?
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   415
    memset(row, 0, width_px * cache->header->col_bytes);
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   416
}
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   417
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   418
/**
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   419
 * Write raw tile image data, directly from the cache
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   420
 */
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   421
static int write_png_data_direct (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti)
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   422
{
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   423
    for (size_t row = ti->y; row < ti->y + ti->height; row++)
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   424
        // write data directly
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   425
        png_write_row(png, tile_row_col(cache, row, ti->x));
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   426
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   427
    return 0;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   428
}
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   429
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   430
/**
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   431
 * 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
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   432
 */
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   433
static int write_png_data_clipped (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti)
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   434
{
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   435
    png_byte *rowbuf;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   436
    size_t row;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   437
    
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   438
    // image data goes from (ti->x ... clip_x, ti->y ... clip_y), remaining region is filled
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   439
    size_t clip_x, clip_y;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   440
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   441
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   442
    // figure out if the tile clips over the right edge
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   443
    // XXX: use min()
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   444
    if (ti->x + ti->width > cache->header->width)
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   445
        clip_x = cache->header->width;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   446
    else
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   447
        clip_y = ti->x + ti->width;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   448
    
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   449
    // figure out if the tile clips over the bottom edge
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   450
    // XXX: use min()
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   451
    if (ti->y + ti->height > cache->header->height)
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   452
        clip_y = cache->header->height;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   453
    else
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   454
        clip_y = ti->y + ti->height;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   455
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   456
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   457
    // allocate buffer for a single row of image data
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   458
    if ((rowbuf = malloc(ti->width * cache->header->col_bytes)) == NULL)
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   459
        return -1;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   460
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   461
    // how much data we actually have for each row, in px and bytes
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   462
    // from [(tile x)---](clip x)
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   463
    size_t row_px = clip_x - ti->x;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   464
    size_t row_bytes = row_px * cache->header->col_bytes;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   465
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   466
    // write the rows that we have
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   467
    // from [(tile y]---](clip y)
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   468
    for (row = ti->y; row < clip_y; row++) {
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   469
        // copy in the actual tile data...
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   470
        memcpy(rowbuf, tile_row_col(cache, row, ti->x), row_bytes);
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   471
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   472
        // generate the data for the remaining, clipped, columns
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   473
        tile_row_fill_clip(cache, rowbuf + row_bytes, (ti->width - row_px));
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   474
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   475
        // write
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   476
        png_write_row(png, rowbuf);
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   477
    }
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   478
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   479
    // generate the data for the remaining, clipped, rows
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   480
    tile_row_fill_clip(cache, rowbuf, ti->width);
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   481
    
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   482
    // write out the remaining rows as clipped data
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   483
    for (; row < ti->y + ti->height; row++)
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   484
        png_write_row(png, rowbuf);
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   485
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   486
    // ok
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   487
    return 0;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   488
}
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   489
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   490
int pt_cache_tile_png (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti)
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   491
{
12
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   492
    int err;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   493
10
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   494
    // ensure open
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   495
    if (pt_cache_open(cache))
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   496
        return -1;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   497
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   498
    // set basic info
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   499
    png_set_IHDR(png, info, ti->width, ti->height, cache->header->bit_depth, cache->header->color_type,
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   500
            PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   501
    );
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   502
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   503
    // set palette?
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   504
    if (cache->header->color_type == PNG_COLOR_TYPE_PALETTE)
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   505
        png_set_PLTE(png, info, cache->header->palette, cache->header->num_palette);
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   506
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   507
    // write meta-info
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   508
    png_write_info(png, info);
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   509
12
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   510
    // our pixel data is packed into 1 pixel per byte (8bpp or 16bpp)
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   511
    png_set_packing(png);
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   512
    
12
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   513
    // figure out if the tile clips
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   514
    if (ti->x + ti->width <= cache->header->width && ti->y + ti->height <= cache->header->height)
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   515
        // doesn't clip, just use the raw data
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   516
        err = write_png_data_direct(cache, png, info, ti);
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   517
12
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   518
    else
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   519
        // fill in clipped regions
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   520
        err = write_png_data_clipped(cache, png, info, ti);
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   521
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   522
    if (err)
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   523
        return -1;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   524
    
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   525
    // done, flush remaining output
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   526
    png_write_flush(png);
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   527
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   528
    // ok
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   529
    return 0;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   530
}
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   531
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   532
void pt_cache_destroy (struct pt_cache *cache)
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   533
{
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   534
    free(cache->path);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   535
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   536
    pt_cache_abort(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   537
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   538
    free(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   539
}
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   540