author | Tero Marttila <terom@fixme.fi> |
Sun, 24 Jan 2010 23:04:55 +0200 | |
changeset 53 | 8a3165c604f8 |
parent 52 | 148a120ea7d5 |
child 54 | 4a25113cb2a4 |
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 |
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 | 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 | 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 | 16 |
int pt_cache_new (struct pt_cache **cache_ptr, const char *path, int mode) |
0 | 17 |
{ |
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 | 20 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
21 |
// alloc |
0 | 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 | 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 | 45 |
int pt_cache_status (struct pt_cache *cache, const char *img_path) |
0 | 46 |
{ |
2 | 47 |
struct stat st_img, st_cache; |
48 |
||
49 |
// test original file |
|
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 | 52 |
|
53 |
// test cache file |
|
54 |
if (stat(cache->path, &st_cache) < 0) { |
|
55 |
// always stale if it doesn't exist yet |
|
56 |
if (errno == ENOENT) |
|
8 | 57 |
return PT_CACHE_NONE; |
2 | 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 | 60 |
} |
61 |
||
62 |
// compare mtime |
|
8 | 63 |
if (st_img.st_mtime > st_cache.st_mtime) |
64 |
return PT_CACHE_STALE; |
|
65 |
||
66 |
else |
|
67 |
return PT_CACHE_FRESH; |
|
0 | 68 |
} |
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 | 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 | 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 | 157 |
if (!readonly) { |
11 | 158 |
assert(cache->mode & PT_OPEN_UPDATE); |
9 | 159 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
160 |
prot |= PROT_WRITE; |
9 | 161 |
} |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
162 |
|
53
8a3165c604f8
fix cache->size calculation to not include the header
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
163 |
// mmap() the full file including header |
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 | 174 |
* Read in the cache header from the open file |
175 |
*/ |
|
176 |
static int pt_cache_read_header (struct pt_cache *cache, struct pt_cache_header *header) |
|
177 |
{ |
|
178 |
size_t len = sizeof(*header); |
|
179 |
char *buf = (char *) header; |
|
180 |
||
181 |
// seek to start |
|
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 | 184 |
|
185 |
// write out full header |
|
186 |
while (len) { |
|
187 |
ssize_t ret; |
|
188 |
||
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 | 192 |
|
193 |
// update offset |
|
194 |
buf += ret; |
|
195 |
len -= ret; |
|
196 |
} |
|
197 |
||
198 |
// done |
|
199 |
return 0; |
|
200 |
} |
|
201 |
||
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 | 233 |
*/ |
11 | 234 |
static int pt_cache_create (struct pt_cache *cache, struct pt_cache_header *header) |
0 | 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 | 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 | 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 |
53
8a3165c604f8
fix cache->size calculation to not include the header
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
248 |
cache->size = header->height * header->row_bytes; |
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 |
// 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 | 274 |
} |
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 | 295 |
int pt_cache_open (struct pt_cache *cache) |
296 |
{ |
|
297 |
struct pt_cache_header header; |
|
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 | 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 | 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 | 308 |
|
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 | 312 |
|
313 |
// calculate data size |
|
53
8a3165c604f8
fix cache->size calculation to not include the header
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
314 |
cache->size = header.height * header.row_bytes; |
9 | 315 |
|
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 | 319 |
|
320 |
cache->header = base; |
|
321 |
cache->data = base + PT_CACHE_HEADER_SIZE; |
|
322 |
||
323 |
// done |
|
324 |
return 0; |
|
325 |
||
326 |
error: |
|
327 |
// cleanup |
|
328 |
pt_cache_abort(cache); |
|
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 | 331 |
} |
332 |
||
52
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
333 |
#define min(a, b) (((a) < (b)) ? (a) : (b)) |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
334 |
|
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
335 |
/** |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
336 |
* Decode the PNG data directly to mmap() - not good for sparse backgrounds |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
337 |
*/ |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
338 |
static int decode_png_raw (struct pt_cache *cache, png_structp png, png_infop info) |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
339 |
{ |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
340 |
// write out raw image data a row at a time |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
341 |
for (size_t row = 0; row < cache->header->height; row++) { |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
342 |
// read row data, non-interlaced |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
343 |
png_read_row(png, cache->data + row * cache->header->row_bytes, NULL); |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
344 |
} |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
345 |
|
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
346 |
return 0; |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
347 |
} |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
348 |
|
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
349 |
static int decode_png_sparse (struct pt_cache *cache, png_structp png, png_infop info) |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
350 |
{ |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
351 |
// one row of pixel data |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
352 |
uint8_t *row_buf; |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
353 |
|
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
354 |
// alloc |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
355 |
if ((row_buf = malloc(cache->header->row_bytes)) == NULL) |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
356 |
RETURN_ERROR(PT_ERR_MEM); |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
357 |
|
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
358 |
// decode each row at a time |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
359 |
for (size_t row = 0; row < cache->header->height; row++) { |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
360 |
// read row data, non-interlaced |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
361 |
png_read_row(png, row_buf, NULL); |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
362 |
|
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
363 |
// skip background-colored regions to keep the cache file sparse |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
364 |
// ...in blocks of PT_CACHE_BLOCK_SIZE bytes |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
365 |
for (size_t col_base = 0; col_base < cache->header->width; ){ |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
366 |
// size of this block in bytes |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
367 |
size_t block_size = min(PT_CACHE_BLOCK_SIZE * cache->header->col_bytes, cache->header->row_bytes - col_base); |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
368 |
|
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
369 |
// ...each pixel |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
370 |
for ( |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
371 |
size_t col = col_base; |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
372 |
|
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
373 |
// BLOCK_SIZE * col_bytes wide, don't go over the edge |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
374 |
col < col_base + block_size; |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
375 |
|
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
376 |
col += cache->header->col_bytes |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
377 |
) { |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
378 |
// test this pixel |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
379 |
if (bcmp(row_buf + col, cache->header->params.background_color, cache->header->col_bytes)) { |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
380 |
// differs |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
381 |
memcpy( |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
382 |
cache->data + row * cache->header->row_bytes + col_base, |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
383 |
row_buf + col_base, |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
384 |
block_size |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
385 |
); |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
386 |
|
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
387 |
// skip to next block |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
388 |
break; |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
389 |
} |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
390 |
} |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
391 |
|
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
392 |
// skip this block |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
393 |
continue; |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
394 |
} |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
395 |
} |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
396 |
|
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
397 |
return 0; |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
398 |
} |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
399 |
|
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
400 |
int pt_cache_update_png (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_image_params *params) |
0 | 401 |
{ |
402 |
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
|
403 |
int err; |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
404 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
405 |
// XXX: check cache_mode |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
406 |
// XXX: check image doesn't use any options we don't handle |
9 | 407 |
// XXX: close any already-opened cache file |
0 | 408 |
|
409 |
memset(&header, 0, sizeof(header)); |
|
410 |
||
411 |
// fill in basic info |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
412 |
header.width = png_get_image_width(png, info); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
413 |
header.height = png_get_image_height(png, info); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
414 |
header.bit_depth = png_get_bit_depth(png, info); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
415 |
header.color_type = png_get_color_type(png, info); |
0 | 416 |
|
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
417 |
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
|
418 |
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
|
419 |
); |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
420 |
|
11 | 421 |
// only pack 1 pixel per byte, changes rowbytes |
9 | 422 |
if (header.bit_depth < 8) |
423 |
png_set_packing(png); |
|
424 |
||
0 | 425 |
// fill in other info |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
426 |
header.row_bytes = png_get_rowbytes(png, info); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
427 |
|
9 | 428 |
// calculate bpp as num_channels * bpc |
429 |
// XXX: this assumes the packed bit depth will be either 8 or 16 |
|
430 |
header.col_bytes = png_get_channels(png, info) * (header.bit_depth == 16 ? 2 : 1); |
|
431 |
||
432 |
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
|
433 |
|
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
434 |
// palette etc. |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
435 |
if (header.color_type == PNG_COLOR_TYPE_PALETTE) { |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
436 |
int num_palette; |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
437 |
png_colorp palette; |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
438 |
|
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
439 |
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
|
440 |
// 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
|
441 |
RETURN_ERROR(PT_ERR_PNG); |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
442 |
|
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
443 |
// should only be 256 of them at most |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
444 |
assert(num_palette <= PNG_MAX_PALETTE_LENGTH); |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
445 |
|
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
446 |
// copy |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
447 |
header.num_palette = num_palette; |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
448 |
memcpy(&header.palette, palette, num_palette * sizeof(*palette)); |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
449 |
|
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
450 |
log_debug("num_palette=%u", num_palette); |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
451 |
} |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
452 |
|
52
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
453 |
// any params |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
454 |
if (params) |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
455 |
header.params = *params; |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
456 |
|
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
457 |
// 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
|
458 |
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
|
459 |
return err; |
52
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
460 |
|
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
461 |
// decode |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
462 |
if ((err = decode_png_sparse(cache, png, info))) |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
463 |
return err; |
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
464 |
|
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
465 |
// 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
|
466 |
if ((err = pt_cache_create_done(cache))) |
18 | 467 |
// 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
|
468 |
return err; |
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
469 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
470 |
// done! |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
471 |
return 0; |
0 | 472 |
} |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
473 |
|
12
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
474 |
/** |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
475 |
* 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
|
476 |
*/ |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
477 |
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
|
478 |
{ |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
479 |
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
|
480 |
} |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
481 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
482 |
/** |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
483 |
* 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
|
484 |
*/ |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
485 |
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
|
486 |
{ |
15 | 487 |
// XXX: use a configureable background color, or full transparency? |
488 |
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
|
489 |
} |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
490 |
|
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 |
* 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
|
493 |
*/ |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
494 |
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
|
495 |
{ |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
496 |
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
|
497 |
// 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
|
498 |
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
|
499 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
500 |
return 0; |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
501 |
} |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
502 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
503 |
/** |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
504 |
* 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
|
505 |
*/ |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
506 |
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
|
507 |
{ |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
508 |
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
|
509 |
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
|
510 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
511 |
// 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
|
512 |
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
|
513 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
514 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
515 |
// 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
|
516 |
// 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
|
517 |
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
|
518 |
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
|
519 |
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
|
520 |
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
|
521 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
522 |
// 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
|
523 |
// 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
|
524 |
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
|
525 |
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
|
526 |
else |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
527 |
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
|
528 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
529 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
530 |
// 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
|
531 |
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
|
532 |
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
|
533 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
534 |
// 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
|
535 |
// 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
|
536 |
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
|
537 |
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
|
538 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
539 |
// 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
|
540 |
// 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
|
541 |
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
|
542 |
// 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
|
543 |
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
|
544 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
545 |
// 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
|
546 |
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
|
547 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
548 |
// write |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
549 |
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
|
550 |
} |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
551 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
552 |
// 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
|
553 |
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
|
554 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
555 |
// 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
|
556 |
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
|
557 |
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
|
558 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
559 |
// ok |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
560 |
return 0; |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
561 |
} |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
562 |
|
34 | 563 |
static size_t scale_by_zoom_factor (size_t value, int z) |
564 |
{ |
|
565 |
if (z > 0) |
|
566 |
return value << z; |
|
567 |
||
568 |
else if (z < 0) |
|
569 |
return value >> -z; |
|
570 |
||
571 |
else |
|
572 |
return value; |
|
573 |
} |
|
574 |
||
575 |
#define ADD_AVG(l, r) (l) = ((l) + (r)) / 2 |
|
576 |
||
577 |
static int png_pixel_data (png_color *out, struct pt_cache *cache, size_t row, size_t col) |
|
578 |
{ |
|
579 |
if (cache->header->color_type == PNG_COLOR_TYPE_PALETTE) { |
|
580 |
// palette entry number |
|
581 |
int p; |
|
582 |
||
583 |
if (cache->header->bit_depth == 8) |
|
584 |
p = *((uint8_t *) tile_row_col(cache, row, col)); |
|
585 |
else |
|
586 |
return -1; |
|
587 |
||
588 |
if (p >= cache->header->num_palette) |
|
589 |
return -1; |
|
590 |
||
591 |
// reference data from palette |
|
592 |
*out = cache->header->palette[p]; |
|
593 |
||
594 |
return 0; |
|
595 |
||
596 |
} else { |
|
597 |
return -1; |
|
598 |
} |
|
599 |
} |
|
600 |
||
601 |
/** |
|
602 |
* Write unscaled tile data |
|
603 |
*/ |
|
604 |
static int write_png_data_unzoomed (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti) |
|
9 | 605 |
{ |
12
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
606 |
int err; |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
607 |
|
9 | 608 |
// set basic info |
609 |
png_set_IHDR(png, info, ti->width, ti->height, cache->header->bit_depth, cache->header->color_type, |
|
610 |
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT |
|
611 |
); |
|
612 |
||
613 |
// set palette? |
|
614 |
if (cache->header->color_type == PNG_COLOR_TYPE_PALETTE) |
|
615 |
png_set_PLTE(png, info, cache->header->palette, cache->header->num_palette); |
|
616 |
||
617 |
// write meta-info |
|
618 |
png_write_info(png, info); |
|
619 |
||
12
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
620 |
// our pixel data is packed into 1 pixel per byte (8bpp or 16bpp) |
9 | 621 |
png_set_packing(png); |
622 |
||
12
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
623 |
// 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
|
624 |
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
|
625 |
// 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
|
626 |
err = write_png_data_direct(cache, png, info, ti); |
9 | 627 |
|
12
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
628 |
else |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
629 |
// 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
|
630 |
err = write_png_data_clipped(cache, png, info, ti); |
34 | 631 |
|
632 |
return err; |
|
633 |
} |
|
634 |
||
635 |
/** |
|
636 |
* Write scaled tile data |
|
637 |
*/ |
|
638 |
static int write_png_data_zoomed (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti) |
|
639 |
{ |
|
640 |
// size of the image data in px |
|
641 |
size_t data_width = scale_by_zoom_factor(ti->width, -ti->zoom); |
|
642 |
size_t data_height = scale_by_zoom_factor(ti->height, -ti->zoom); |
|
643 |
||
644 |
// input pixels per output pixel |
|
645 |
size_t pixel_size = scale_by_zoom_factor(1, -ti->zoom); |
|
646 |
||
647 |
// bytes per output pixel |
|
648 |
size_t pixel_bytes = 3; |
|
649 |
||
650 |
// size of the output tile in px |
|
651 |
size_t row_width = ti->width; |
|
652 |
||
653 |
// size of an output row in bytes (RGB) |
|
654 |
size_t row_bytes = row_width * 3; |
|
655 |
||
656 |
// buffer to hold output rows |
|
657 |
uint8_t *row_buf; |
|
658 |
||
659 |
// XXX: only supports zooming out... |
|
660 |
if (ti->zoom >= 0) |
|
661 |
RETURN_ERROR(PT_ERR_ZOOM); |
|
662 |
||
663 |
if ((row_buf = malloc(row_bytes)) == NULL) |
|
664 |
RETURN_ERROR(PT_ERR_MEM); |
|
665 |
||
666 |
||
667 |
// define pixel format: 8bpp RGB |
|
668 |
png_set_IHDR(png, info, ti->width, ti->height, 8, PNG_COLOR_TYPE_RGB, |
|
669 |
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT |
|
670 |
); |
|
671 |
||
672 |
// write meta-info |
|
673 |
png_write_info(png, info); |
|
674 |
||
675 |
// ...each output row |
|
676 |
for (size_t out_row = 0; out_row < ti->height; out_row++) { |
|
677 |
memset(row_buf, 0, row_bytes); |
|
678 |
||
679 |
// ...includes pixels starting from this row. |
|
680 |
size_t in_row_offset = ti->y + scale_by_zoom_factor(out_row, -ti->zoom); |
|
681 |
||
682 |
// ...each out row includes pixel_size in rows |
|
683 |
for (size_t in_row = in_row_offset; in_row < in_row_offset + pixel_size && in_row < cache->header->height; in_row++) { |
|
684 |
// and includes each input pixel |
|
685 |
for (size_t in_col = ti->x; in_col < ti->x + data_width && in_col < cache->header->width; in_col++) { |
|
686 |
png_color c; |
|
687 |
||
688 |
// ...for this output pixel |
|
689 |
size_t out_col = scale_by_zoom_factor(in_col - ti->x, ti->zoom); |
|
690 |
||
691 |
// get pixel RGB data |
|
692 |
if (png_pixel_data(&c, cache, in_row, in_col)) |
|
693 |
return -1; |
|
694 |
||
695 |
// average the RGB data |
|
696 |
ADD_AVG(row_buf[out_col * pixel_bytes + 0], c.red); |
|
697 |
ADD_AVG(row_buf[out_col * pixel_bytes + 1], c.green); |
|
698 |
ADD_AVG(row_buf[out_col * pixel_bytes + 2], c.blue); |
|
699 |
} |
|
700 |
} |
|
701 |
||
702 |
// output |
|
703 |
png_write_row(png, row_buf); |
|
704 |
} |
|
705 |
||
706 |
// done |
|
707 |
return 0; |
|
708 |
} |
|
709 |
||
710 |
int pt_cache_tile_png (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti) |
|
711 |
{ |
|
712 |
int err; |
|
713 |
||
714 |
// ensure open |
|
715 |
if ((err = pt_cache_open(cache))) |
|
716 |
return err; |
|
717 |
||
718 |
// check within bounds |
|
719 |
if (ti->x >= cache->header->width || ti->y >= cache->header->height) |
|
720 |
// completely outside |
|
721 |
RETURN_ERROR(PT_ERR_TILE_CLIP); |
|
722 |
||
723 |
// unscaled or scaled? |
|
724 |
if (ti->zoom) |
|
725 |
err = write_png_data_zoomed(cache, png, info, ti); |
|
726 |
||
727 |
else |
|
728 |
err = write_png_data_unzoomed(cache, png, info, ti); |
|
12
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
729 |
|
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
730 |
if (err) |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
15
diff
changeset
|
731 |
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
|
732 |
|
9 | 733 |
// done, flush remaining output |
734 |
png_write_flush(png); |
|
735 |
||
736 |
// ok |
|
737 |
return 0; |
|
738 |
} |
|
739 |
||
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
740 |
void pt_cache_destroy (struct pt_cache *cache) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
741 |
{ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
742 |
free(cache->path); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
743 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
744 |
pt_cache_abort(cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
745 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
746 |
free(cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
747 |
} |
11 | 748 |