author | Tero Marttila <terom@fixme.fi> |
Tue, 29 Dec 2009 00:22:57 +0200 | |
changeset 10 | 6806a90d934f |
parent 9 | a31048ff76a2 |
child 11 | eb2a1472f084 |
permissions | -rw-r--r-- |
0 | 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 | 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 | 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 | 15 |
int pt_cache_new (struct pt_cache **cache_ptr, const char *path, int mode) |
0 | 16 |
{ |
17 |
struct pt_cache *cache; |
|
18 |
||
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
19 |
// alloc |
0 | 20 |
if ((cache = calloc(1, sizeof(*cache))) == NULL) |
21 |
return -1; |
|
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 | 43 |
int pt_cache_status (struct pt_cache *cache, const char *img_path) |
0 | 44 |
{ |
2 | 45 |
struct stat st_img, st_cache; |
46 |
||
47 |
// test original file |
|
48 |
if (stat(img_path, &st_img) < 0) |
|
49 |
return -1; |
|
50 |
||
51 |
// test cache file |
|
52 |
if (stat(cache->path, &st_cache) < 0) { |
|
53 |
// always stale if it doesn't exist yet |
|
54 |
if (errno == ENOENT) |
|
8 | 55 |
return PT_CACHE_NONE; |
2 | 56 |
else |
57 |
return -1; |
|
58 |
} |
|
59 |
||
60 |
// compare mtime |
|
8 | 61 |
if (st_img.st_mtime > st_cache.st_mtime) |
62 |
return PT_CACHE_STALE; |
|
63 |
||
64 |
else |
|
65 |
return PT_CACHE_FRESH; |
|
0 | 66 |
} |
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 | 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 |
// check mode |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
127 |
if (!(cache->mode & PT_IMG_WRITE)) { |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
128 |
errno = EPERM; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
129 |
return -1; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
130 |
} |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
131 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
132 |
// get .tmp path |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
133 |
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
|
134 |
return -1; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
135 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
136 |
// open for write, create |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
137 |
// XXX: locking? |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
138 |
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
|
139 |
return -1; |
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 |
// ok |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
142 |
*fd_ptr = fd; |
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 |
return 0; |
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 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
147 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
148 |
/** |
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
149 |
* 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
|
150 |
*/ |
9 | 151 |
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
|
152 |
{ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
153 |
int prot = 0; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
154 |
void *addr; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
155 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
156 |
// determine prot |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
157 |
prot |= PROT_READ; |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
158 |
|
9 | 159 |
if (!readonly) { |
160 |
assert(cache->mode & PT_IMG_WRITE); |
|
161 |
||
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
162 |
prot |= PROT_WRITE; |
9 | 163 |
} |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
164 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
165 |
// 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
|
166 |
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
|
167 |
return -1; |
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 |
// ok |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
170 |
*addr_ptr = addr; |
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 |
return 0; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
173 |
} |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
174 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
175 |
/** |
9 | 176 |
* Read in the cache header from the open file |
177 |
*/ |
|
178 |
static int pt_cache_read_header (struct pt_cache *cache, struct pt_cache_header *header) |
|
179 |
{ |
|
180 |
size_t len = sizeof(*header); |
|
181 |
char *buf = (char *) header; |
|
182 |
||
183 |
// seek to start |
|
184 |
if (lseek(cache->fd, 0, SEEK_SET) != 0) |
|
185 |
return -1; |
|
186 |
||
187 |
// write out full header |
|
188 |
while (len) { |
|
189 |
ssize_t ret; |
|
190 |
||
191 |
// try and write out the header |
|
192 |
if ((ret = read(cache->fd, buf, len)) < 0) |
|
193 |
return -1; |
|
194 |
||
195 |
// update offset |
|
196 |
buf += ret; |
|
197 |
len -= ret; |
|
198 |
} |
|
199 |
||
200 |
// done |
|
201 |
return 0; |
|
202 |
} |
|
203 |
||
204 |
/** |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
205 |
* Write out the cache header into the opened file |
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 |
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
|
208 |
{ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
209 |
size_t len = sizeof(*header); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
210 |
const char *buf = (const char *) header; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
211 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
212 |
// seek to start |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
213 |
if (lseek(cache->fd, 0, SEEK_SET) != 0) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
214 |
return -1; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
215 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
216 |
// write out full header |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
217 |
while (len) { |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
218 |
ssize_t ret; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
219 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
220 |
// try and write out the header |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
221 |
if ((ret = write(cache->fd, buf, len)) < 0) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
222 |
return -1; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
223 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
224 |
// update offset |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
225 |
buf += ret; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
226 |
len -= ret; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
227 |
} |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
228 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
229 |
// done |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
230 |
return 0; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
231 |
} |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
232 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
233 |
/** |
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
234 |
* Create a new .tmp cache file, open it, and write out the header. |
0 | 235 |
*/ |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
236 |
static int pt_cache_open_create (struct pt_cache *cache, struct pt_cache_header *header) |
0 | 237 |
{ |
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
238 |
void *base; |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
239 |
|
2 | 240 |
// no access |
241 |
if (!(cache->mode & PT_IMG_WRITE)) { |
|
242 |
errno = EPERM; |
|
243 |
return -1; |
|
244 |
} |
|
245 |
||
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
246 |
// open as .tmp |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
247 |
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
|
248 |
return -1; |
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 |
// calculate data size |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
251 |
cache->size = sizeof(*header) + header->height * header->row_bytes; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
252 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
253 |
// grow file |
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
254 |
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
|
255 |
goto error; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
256 |
|
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
257 |
// mmap header and data |
9 | 258 |
if (pt_cache_open_mmap(cache, &base, false)) |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
259 |
goto error; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
260 |
|
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
261 |
cache->header = base; |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
262 |
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
|
263 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
264 |
// write header |
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
265 |
// XXX: should just mmap... |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
266 |
if (pt_cache_write_header(cache, header)) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
267 |
goto error; |
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 |
// done |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
270 |
return 0; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
271 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
272 |
error: |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
273 |
// cleanup |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
274 |
pt_cache_abort(cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
275 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
276 |
return -1; |
0 | 277 |
} |
278 |
||
4
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 |
* 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
|
281 |
*/ |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
282 |
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
|
283 |
{ |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
284 |
char tmp_path[1024]; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
285 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
286 |
// get .tmp path |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
287 |
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
|
288 |
return -1; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
289 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
290 |
// rename |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
291 |
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
|
292 |
return -1; |
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 |
// ok |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
295 |
return 0; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
296 |
} |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
297 |
|
9 | 298 |
int pt_cache_open (struct pt_cache *cache) |
299 |
{ |
|
300 |
struct pt_cache_header header; |
|
301 |
void *base; |
|
302 |
||
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
|
303 |
// 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
|
304 |
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
|
305 |
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
|
306 |
|
9 | 307 |
// open the .cache |
308 |
if (pt_cache_open_read_fd(cache, &cache->fd)) |
|
309 |
return -1; |
|
310 |
||
311 |
// read in header |
|
312 |
if (pt_cache_read_header(cache, &header)) |
|
313 |
return -1; |
|
314 |
||
315 |
// calculate data size |
|
316 |
cache->size = sizeof(header) + header.height * header.row_bytes; |
|
317 |
||
318 |
// mmap header and data |
|
319 |
if (pt_cache_open_mmap(cache, &base, true)) |
|
320 |
goto error; |
|
321 |
||
322 |
cache->header = base; |
|
323 |
cache->data = base + PT_CACHE_HEADER_SIZE; |
|
324 |
||
325 |
// done |
|
326 |
return 0; |
|
327 |
||
328 |
error: |
|
329 |
// cleanup |
|
330 |
pt_cache_abort(cache); |
|
331 |
||
332 |
return -1; |
|
333 |
} |
|
334 |
||
0 | 335 |
int pt_cache_update_png (struct pt_cache *cache, png_structp png, png_infop info) |
336 |
{ |
|
337 |
struct pt_cache_header header; |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
338 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
339 |
// XXX: check cache_mode |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
340 |
// XXX: check image doesn't use any options we don't handle |
9 | 341 |
// XXX: close any already-opened cache file |
0 | 342 |
|
343 |
memset(&header, 0, sizeof(header)); |
|
344 |
||
345 |
// fill in basic info |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
346 |
header.width = png_get_image_width(png, info); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
347 |
header.height = png_get_image_height(png, info); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
348 |
header.bit_depth = png_get_bit_depth(png, info); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
349 |
header.color_type = png_get_color_type(png, info); |
0 | 350 |
|
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
351 |
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
|
352 |
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
|
353 |
); |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
354 |
|
9 | 355 |
// only pack 1 pixel per byte |
356 |
if (header.bit_depth < 8) |
|
357 |
png_set_packing(png); |
|
358 |
||
0 | 359 |
// fill in other info |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
360 |
header.row_bytes = png_get_rowbytes(png, info); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
361 |
|
9 | 362 |
// calculate bpp as num_channels * bpc |
363 |
// XXX: this assumes the packed bit depth will be either 8 or 16 |
|
364 |
header.col_bytes = png_get_channels(png, info) * (header.bit_depth == 16 ? 2 : 1); |
|
365 |
||
366 |
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
|
367 |
|
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
368 |
// palette etc. |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
369 |
if (header.color_type == PNG_COLOR_TYPE_PALETTE) { |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
370 |
int num_palette; |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
371 |
png_colorp palette; |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
372 |
|
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
373 |
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
|
374 |
// XXX: PLTE chunk not read? |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
375 |
return -1; |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
376 |
|
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
377 |
// should only be 256 of them at most |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
378 |
assert(num_palette <= PNG_MAX_PALETTE_LENGTH); |
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 |
// copy |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
381 |
header.num_palette = num_palette; |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
382 |
memcpy(&header.palette, palette, num_palette * sizeof(*palette)); |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
383 |
|
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
384 |
log_debug("num_palette=%u", num_palette); |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
385 |
} |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
386 |
|
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
387 |
// create .tmp and write out header |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
388 |
if (pt_cache_open_create(cache, &header)) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
389 |
return -1; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
390 |
|
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
391 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
392 |
// 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
|
393 |
for (size_t row = 0; row < header.height; row++) { |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
394 |
// read row data, non-interlaced |
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
395 |
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
|
396 |
} |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
397 |
|
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
398 |
|
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
399 |
// 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
|
400 |
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
|
401 |
return -1; |
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 | 405 |
} |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
406 |
|
9 | 407 |
int pt_cache_tile_png (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti) |
408 |
{ |
|
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
|
409 |
// 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
|
410 |
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
|
411 |
return -1; |
9 | 412 |
|
413 |
// set basic info |
|
414 |
png_set_IHDR(png, info, ti->width, ti->height, cache->header->bit_depth, cache->header->color_type, |
|
415 |
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT |
|
416 |
); |
|
417 |
||
418 |
// set palette? |
|
419 |
if (cache->header->color_type == PNG_COLOR_TYPE_PALETTE) |
|
420 |
png_set_PLTE(png, info, cache->header->palette, cache->header->num_palette); |
|
421 |
||
422 |
// write meta-info |
|
423 |
png_write_info(png, info); |
|
424 |
||
425 |
// pixel data is packed into 1 pixel per byte |
|
426 |
png_set_packing(png); |
|
427 |
||
428 |
// write image data |
|
429 |
for (size_t row = ti->y; row < ti->y + ti->height; row++) { |
|
430 |
size_t col = ti->x; |
|
431 |
||
432 |
// XXX: fill out-of-range regions in some background color |
|
433 |
png_write_row(png, cache->data + (row * cache->header->row_bytes) + (col * cache->header->col_bytes)); |
|
434 |
} |
|
435 |
||
436 |
// done, flush remaining output |
|
437 |
png_write_flush(png); |
|
438 |
||
439 |
// ok |
|
440 |
return 0; |
|
441 |
} |
|
442 |
||
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
443 |
void pt_cache_destroy (struct pt_cache *cache) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
444 |
{ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
445 |
free(cache->path); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
446 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
447 |
pt_cache_abort(cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
448 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
449 |
free(cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
450 |
} |