src/lib/cache.c
author Tero Marttila <terom@fixme.fi>
Thu, 31 Dec 2009 14:01:37 +0200
changeset 18 f92a24ab046e
parent 17 baf3fe7c6354
child 34 a387bc77ad52
permissions -rw-r--r--
add missing lib/tile.*
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
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
     4
#include "error.h"
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
     6
#include <stdlib.h>
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
     7
#include <unistd.h>
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
     8
#include <sys/types.h>
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
     9
#include <sys/stat.h>
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    10
#include <fcntl.h>
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    11
#include <sys/mman.h>
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    12
#include <errno.h>
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
    13
#include <assert.h>
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    14
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    15
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    16
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
    17
{
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    struct pt_cache *cache;
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
    19
    int err;
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    21
    // alloc
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
    if ((cache = calloc(1, sizeof(*cache))) == NULL)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
    23
        RETURN_ERROR(PT_ERR_MEM);
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    25
    if ((cache->path = strdup(path)) == NULL)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
    26
        JUMP_SET_ERROR(err, PT_ERR_MEM);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    27
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    28
    // init
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    29
    cache->fd = -1;
3
da7c6dcafb43 store cache->mode, error handling for pt_image_update_cache
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
    30
    cache->mode = mode;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    31
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    32
    // ok
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    33
    *cache_ptr = cache;
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
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    36
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    37
error:
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    38
    // cleanup
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    39
    if (cache)
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    40
        pt_cache_destroy(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    41
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
    42
    return err;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    43
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    44
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    45
int pt_cache_status (struct pt_cache *cache, const char *img_path)
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
{
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    47
    struct stat st_img, st_cache;
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    48
    
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    49
    // test original file
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    50
    if (stat(img_path, &st_img) < 0)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
    51
        RETURN_ERROR(PT_ERR_IMG_STAT);
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    52
    
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    53
    // test cache file
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    54
    if (stat(cache->path, &st_cache) < 0) {
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    55
        // always stale if it doesn't exist yet
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    56
        if (errno == ENOENT)
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    57
            return PT_CACHE_NONE;
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    58
        else
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
    59
            RETURN_ERROR(PT_ERR_CACHE_STAT);
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    60
    }
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    61
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    62
    // compare mtime
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    63
    if (st_img.st_mtime > st_cache.st_mtime)
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    64
        return PT_CACHE_STALE;
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    65
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    66
    else
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
    67
        return PT_CACHE_FRESH;
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
}
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
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
    70
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
    71
{
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
    72
    int err;
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
    73
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
    74
    // ensure open
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
    75
    if ((err = pt_cache_open(cache)))
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
    76
        return err;
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
    77
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
    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
    79
    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
    80
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
    81
    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
    82
}
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
    83
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
/**
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    85
 * Abort any incomplete open operation, cleaning up
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    86
 */
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    87
static void pt_cache_abort (struct pt_cache *cache)
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    88
{
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    89
    if (cache->header != NULL) {
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    90
        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
    91
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    92
        cache->header = NULL;
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
    93
        cache->data = NULL;
1
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
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    96
    if (cache->fd >= 0) {
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    97
        close(cache->fd);
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
        cache->fd = -1;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   100
    }
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
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   103
/**
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   104
 * 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
   105
 *
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   106
 * XXX: needs locking
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   107
 */
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   108
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
   109
{
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   110
    int fd;
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   111
    
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   112
    // actual open()
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   113
    if ((fd = open(cache->path, O_RDONLY)) < 0)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   114
        RETURN_ERROR_ERRNO(PT_ERR_OPEN_MODE, EACCES);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   115
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   116
    // ok
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   117
    *fd_ptr = fd;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   118
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   119
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   120
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   121
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   122
/**
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   123
 * 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
   124
 */
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   125
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
   126
{
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   127
    int fd;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   128
    char tmp_path[1024];
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
    // get .tmp path
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   131
    if (path_with_fext(cache->path, tmp_path, sizeof(tmp_path), ".tmp"))
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   132
        RETURN_ERROR(PT_ERR_PATH);
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   133
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   134
    // open for write, create
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   135
    // XXX: locking?
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   136
    if ((fd = open(tmp_path, O_RDWR | O_CREAT, 0644)) < 0)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   137
        RETURN_ERROR(PT_ERR_CACHE_OPEN_TMP);
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   138
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   139
    // ok
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   140
    *fd_ptr = fd;
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
    return 0;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   143
}
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   144
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   145
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   146
/**
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   147
 * 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
   148
 */
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   149
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
   150
{
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   151
    int prot = 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   152
    void *addr;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   153
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   154
    // determine prot
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   155
    prot |= PROT_READ;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   156
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   157
    if (!readonly) {
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   158
        assert(cache->mode & PT_OPEN_UPDATE);
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   159
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   160
        prot |= PROT_WRITE;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   161
    }
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
    // 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
   164
    if ((addr = mmap(NULL, PT_CACHE_HEADER_SIZE + cache->size, prot, MAP_SHARED, cache->fd, 0)) == MAP_FAILED)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   165
        RETURN_ERROR(PT_ERR_CACHE_MMAP);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   166
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   167
    // ok
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   168
    *addr_ptr = addr;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   169
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   170
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   171
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   172
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   173
/**
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   174
 * 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
   175
 */
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   176
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
   177
{
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   178
    size_t len = sizeof(*header);
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   179
    char *buf = (char *) header;
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
    // seek to start
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   182
    if (lseek(cache->fd, 0, SEEK_SET) != 0)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   183
        RETURN_ERROR(PT_ERR_CACHE_SEEK);
9
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
    // write out full header
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   186
    while (len) {
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   187
        ssize_t ret;
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
        // try and write out the header
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   190
        if ((ret = read(cache->fd, buf, len)) <= 0)
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   191
            RETURN_ERROR(PT_ERR_CACHE_READ);
9
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
        // update offset
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   194
        buf += ret;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   195
        len -= ret;
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
    // done
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   199
    return 0;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   200
}
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   201
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   202
/**
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   203
 * Write out the cache header into the opened file
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   204
 */
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   205
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
   206
{
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   207
    size_t len = sizeof(*header);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   208
    const char *buf = (const char *) header;
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
    // seek to start
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   211
    if (lseek(cache->fd, 0, SEEK_SET) != 0)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   212
        RETURN_ERROR(PT_ERR_CACHE_SEEK);
1
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
    // write out full header
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   215
    while (len) {
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   216
        ssize_t ret;
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
        // try and write out the header
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   219
        if ((ret = write(cache->fd, buf, len)) <= 0)
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   220
            RETURN_ERROR(PT_ERR_CACHE_WRITE);
1
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
        // update offset
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   223
        buf += ret;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   224
        len -= ret;
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
    // done
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   228
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   229
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   230
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   231
/**
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   232
 * 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
   233
 */
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   234
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
   235
{
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   236
    void *base;
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   237
    int err;
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   238
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   239
    // no access
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   240
    if (!(cache->mode & PT_OPEN_UPDATE))
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   241
        RETURN_ERROR(PT_ERR_OPEN_MODE);
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   242
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   243
    // open as .tmp
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   244
    if ((err = pt_cache_open_tmp_fd(cache, &cache->fd)))
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   245
        return err;
1
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
    // calculate data size
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   248
    cache->size = sizeof(*header) + header->height * header->row_bytes;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   249
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   250
    // grow file
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   251
    if (ftruncate(cache->fd, PT_CACHE_HEADER_SIZE + cache->size) < 0)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   252
        JUMP_SET_ERROR(err, PT_ERR_CACHE_TRUNC);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   253
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   254
    // mmap header and data
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   255
    if ((err = pt_cache_open_mmap(cache, &base, false)))
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   256
        JUMP_ERROR(err);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   257
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   258
    cache->header = base;
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   259
    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
   260
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   261
    // write header
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   262
    // XXX: should just mmap...
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   263
    if ((err = pt_cache_write_header(cache, header)))
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   264
        JUMP_ERROR(err);
1
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
    // done
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   267
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   268
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   269
error:
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   270
    // cleanup
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   271
    pt_cache_abort(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   272
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   273
    return err;
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   274
}
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   275
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   276
/**
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   277
 * 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
   278
 */
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   279
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
   280
{
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   281
    char tmp_path[1024];
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   282
    
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   283
    // get .tmp path
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   284
    if (path_with_fext(cache->path, tmp_path, sizeof(tmp_path), ".tmp"))
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   285
        RETURN_ERROR(PT_ERR_PATH);
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   286
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   287
    // rename
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   288
    if (rename(tmp_path, cache->path) < 0)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   289
        RETURN_ERROR(PT_ERR_CACHE_RENAME_TMP);
4
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
    // ok
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   292
    return 0;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   293
}
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   294
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   295
int pt_cache_open (struct pt_cache *cache)
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   296
{
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   297
    struct pt_cache_header header;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   298
    void *base;
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   299
    int err;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   300
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
   301
    // 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
   302
    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
   303
        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
   304
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   305
    // open the .cache
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   306
    if ((err = pt_cache_open_read_fd(cache, &cache->fd)))
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   307
        return err;
9
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
    // read in header
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   310
    if ((err = pt_cache_read_header(cache, &header)))
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   311
        JUMP_ERROR(err);
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   312
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   313
    // calculate data size
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   314
    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
   315
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   316
    // mmap header and data
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   317
    if ((err = pt_cache_open_mmap(cache, &base, true)))
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   318
        JUMP_ERROR(err);
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   319
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   320
    cache->header = base;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   321
    cache->data = base + PT_CACHE_HEADER_SIZE;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   322
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   323
    // done
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   324
    return 0;
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
error:
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   327
    // cleanup
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   328
    pt_cache_abort(cache);
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   329
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   330
    return err;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   331
}
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   332
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   333
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
   334
{
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   335
    struct pt_cache_header header;
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   336
    int err;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   337
    
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   338
    // XXX: check cache_mode
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   339
    // 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
   340
    // XXX: close any already-opened cache file
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   341
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   342
    memset(&header, 0, sizeof(header));
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   343
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   344
    // fill in basic info
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   345
    header.width = png_get_image_width(png, info);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   346
    header.height = png_get_image_height(png, info);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   347
    header.bit_depth = png_get_bit_depth(png, info);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   348
    header.color_type = png_get_color_type(png, info);
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   349
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   350
    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
   351
            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
   352
    );
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   353
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   354
    // 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
   355
    if (header.bit_depth < 8)
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   356
        png_set_packing(png);
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   357
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   358
    // fill in other info
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   359
    header.row_bytes = png_get_rowbytes(png, info);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   360
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   361
    // calculate bpp as num_channels * bpc
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   362
    // 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
   363
    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
   364
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   365
    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
   366
    
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   367
    // palette etc.
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   368
    if (header.color_type == PNG_COLOR_TYPE_PALETTE) {
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   369
        int num_palette;
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   370
        png_colorp palette;
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   371
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   372
        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
   373
            // XXX: PLTE chunk not read?
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   374
            RETURN_ERROR(PT_ERR_PNG);
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   375
        
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   376
        // should only be 256 of them at most
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   377
        assert(num_palette <= PNG_MAX_PALETTE_LENGTH);
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   378
    
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   379
        // copy
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   380
        header.num_palette = num_palette;
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   381
        memcpy(&header.palette, palette, num_palette * sizeof(*palette));
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   382
        
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   383
        log_debug("num_palette=%u", num_palette);
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   384
    }
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   385
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   386
    // create .tmp and write out header
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   387
    if ((err = pt_cache_create(cache, &header)))
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   388
        return err;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   389
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   390
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   391
    // 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
   392
    for (size_t row = 0; row < header.height; row++) {
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   393
        // read row data, non-interlaced
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   394
        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
   395
    }
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   396
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   397
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   398
    // move from .tmp to .cache
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   399
    if ((err = pt_cache_create_done(cache)))
18
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   400
        // XXX: pt_cache_abort?
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   401
        return err;
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   402
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   403
    // done!
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   404
    return 0;
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   405
}
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   406
12
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
 * 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
   409
 */
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   410
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
   411
{
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   412
    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
   413
}
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   414
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   415
/**
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   416
 * 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
   417
 */
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   418
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
   419
{
15
01de253f3bbf small fixes
Tero Marttila <terom@fixme.fi>
parents: 13
diff changeset
   420
    // XXX: use a configureable background color, or full transparency?
01de253f3bbf small fixes
Tero Marttila <terom@fixme.fi>
parents: 13
diff changeset
   421
    memset(row, /* 0xd7 */ 0x00, width_px * cache->header->col_bytes);
12
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
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   424
/**
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   425
 * 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
   426
 */
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   427
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
   428
{
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   429
    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
   430
        // 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
   431
        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
   432
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   433
    return 0;
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
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   436
/**
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   437
 * 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
   438
 */
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   439
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
   440
{
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   441
    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
   442
    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
   443
    
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   444
    // 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
   445
    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
   446
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   447
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   448
    // 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
   449
    // 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
   450
    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
   451
        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
   452
    else
13
294201975f8c fix clip_x bug, and reject images that are completely out of bounds
Tero Marttila <terom@fixme.fi>
parents: 12
diff changeset
   453
        clip_x = ti->x + ti->width;
12
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   454
    
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   455
    // 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
   456
    // 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
   457
    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
   458
        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
   459
    else
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   460
        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
   461
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   462
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   463
    // 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
   464
    if ((rowbuf = malloc(ti->width * cache->header->col_bytes)) == NULL)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   465
        RETURN_ERROR(PT_ERR_MEM);
12
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   466
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   467
    // 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
   468
    // 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
   469
    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
   470
    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
   471
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   472
    // 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
   473
    // 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
   474
    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
   475
        // 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
   476
        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
   477
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   478
        // 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
   479
        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
   480
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   481
        // write
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   482
        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
   483
    }
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   484
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   485
    // 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
   486
    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
   487
    
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   488
    // 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
   489
    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
   490
        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
   491
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   492
    // ok
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   493
    return 0;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   494
}
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   495
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   496
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
   497
{
12
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   498
    int err;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   499
18
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   500
    // ensure open
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   501
    if ((err = pt_cache_open(cache)))
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   502
        return err;
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   503
13
294201975f8c fix clip_x bug, and reject images that are completely out of bounds
Tero Marttila <terom@fixme.fi>
parents: 12
diff changeset
   504
    // check within bounds
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   505
    if (ti->x >= cache->header->width || ti->y >= cache->header->height)
13
294201975f8c fix clip_x bug, and reject images that are completely out of bounds
Tero Marttila <terom@fixme.fi>
parents: 12
diff changeset
   506
        // completely outside
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   507
        RETURN_ERROR(PT_ERR_TILE_CLIP);
13
294201975f8c fix clip_x bug, and reject images that are completely out of bounds
Tero Marttila <terom@fixme.fi>
parents: 12
diff changeset
   508
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   509
    // set basic info
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   510
    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
   511
            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
   512
    );
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   513
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   514
    // set palette?
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   515
    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
   516
        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
   517
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   518
    // write meta-info
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   519
    png_write_info(png, info);
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   520
12
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   521
    // 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
   522
    png_set_packing(png);
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   523
    
12
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   524
    // 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
   525
    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
   526
        // 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
   527
        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
   528
12
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   529
    else
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   530
        // 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
   531
        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
   532
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   533
    if (err)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   534
        return err;
12
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   535
    
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   536
    // done, flush remaining output
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   537
    png_write_flush(png);
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   538
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   539
    // ok
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   540
    return 0;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   541
}
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   542
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   543
void pt_cache_destroy (struct pt_cache *cache)
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   544
{
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   545
    free(cache->path);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   546
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   547
    pt_cache_abort(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   548
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   549
    free(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   550
}
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   551