author | Tero Marttila <terom@fixme.fi> |
Sun, 24 Jan 2010 23:20:39 +0200 | |
changeset 55 | a3542e78ecd8 |
parent 54 | 4a25113cb2a4 |
child 56 | d5e3089906da |
permissions | -rw-r--r-- |
0 | 1 |
#include "image.h" |
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
2 |
#include "ctx.h" |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
3 |
#include "cache.h" |
18 | 4 |
#include "tile.h" |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
5 |
#include "error.h" |
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
6 |
#include "shared/util.h" |
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
7 |
#include "shared/log.h" |
0 | 8 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
9 |
#include <stdlib.h> |
54
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
10 |
#include <sys/stat.h> |
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
11 |
#include <unistd.h> |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
12 |
#include <errno.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 |
#include <png.h> |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
15 |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
16 |
static int pt_image_new (struct pt_image **image_ptr, struct pt_ctx *ctx, const char *path) |
0 | 17 |
{ |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
18 |
struct pt_image *image; |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
19 |
int err = 0; |
0 | 20 |
|
21 |
// alloc |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
22 |
if ((image = calloc(1, sizeof(*image))) == NULL) |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
23 |
JUMP_SET_ERROR(err, PT_ERR_MEM); |
0 | 24 |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
25 |
if ((image->path = strdup(path)) == NULL) |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
26 |
JUMP_SET_ERROR(err, PT_ERR_MEM); |
0 | 27 |
|
28 |
// init |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
29 |
image->ctx = ctx; |
0 | 30 |
|
31 |
// ok |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
32 |
*image_ptr = image; |
0 | 33 |
|
34 |
return 0; |
|
35 |
||
36 |
error: |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
37 |
pt_image_destroy(image); |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
38 |
|
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
39 |
return err; |
0 | 40 |
} |
41 |
||
42 |
/** |
|
43 |
* Open the image's FILE |
|
44 |
*/ |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
45 |
static int pt_image_open_file (struct pt_image *image, FILE **file_ptr) |
0 | 46 |
{ |
47 |
FILE *fp; |
|
48 |
||
49 |
// open |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
50 |
if ((fp = fopen(image->path, "rb")) == NULL) |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
51 |
RETURN_ERROR(PT_ERR_IMG_FOPEN); |
0 | 52 |
|
53 |
// ok |
|
54 |
*file_ptr = fp; |
|
55 |
||
56 |
return 0; |
|
57 |
} |
|
58 |
||
59 |
/** |
|
60 |
* Open the PNG image, setting up the I/O and returning the png_structp and png_infop |
|
61 |
*/ |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
62 |
static int pt_image_open_png (struct pt_image *image, png_structp *png_ptr, png_infop *info_ptr) |
0 | 63 |
{ |
64 |
FILE *fp = NULL; |
|
65 |
png_structp png = NULL; |
|
66 |
png_infop info = NULL; |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
67 |
int err; |
0 | 68 |
|
69 |
// open I/O |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
70 |
if ((err = pt_image_open_file(image, &fp))) |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
71 |
JUMP_ERROR(err); |
0 | 72 |
|
73 |
// create the struct |
|
74 |
if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL) |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
75 |
JUMP_SET_ERROR(err, PT_ERR_PNG_CREATE); |
0 | 76 |
|
77 |
// create the info |
|
78 |
if ((info = png_create_info_struct(png)) == NULL) |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
79 |
JUMP_SET_ERROR(err, PT_ERR_PNG_CREATE); |
0 | 80 |
|
81 |
// setup error trap for the I/O |
|
82 |
if (setjmp(png_jmpbuf(png))) |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
83 |
JUMP_SET_ERROR(err, PT_ERR_PNG); |
0 | 84 |
|
85 |
// setup I/O to FILE |
|
86 |
png_init_io(png, fp); |
|
87 |
||
88 |
// ok |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
89 |
// XXX: what to do with fp? Should fclose() when done? |
0 | 90 |
*png_ptr = png; |
91 |
*info_ptr = info; |
|
92 |
||
93 |
return 0; |
|
94 |
||
95 |
error: |
|
96 |
// cleanup file |
|
97 |
if (fp) fclose(fp); |
|
98 |
||
99 |
// cleanup PNG state |
|
100 |
png_destroy_read_struct(&png, &info, NULL); |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
101 |
|
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
102 |
return err; |
0 | 103 |
} |
104 |
||
105 |
/** |
|
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
106 |
* Update the image_info field from the given png object. |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
107 |
* |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
108 |
* Must be called under libpng-error-trap! |
18 | 109 |
* |
110 |
* XXX: currently this info is not used, pulled from the cache instead |
|
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
111 |
*/ |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
112 |
static int pt_image_update_info (struct pt_image *image, png_structp png, png_infop info) |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
113 |
{ |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
114 |
// query png_get_* |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
115 |
image->info.width = png_get_image_width(png, info); |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
116 |
image->info.height = png_get_image_height(png, info); |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
117 |
|
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
118 |
return 0; |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
119 |
} |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
120 |
|
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
121 |
/** |
0 | 122 |
* Open the PNG image, and write out to the cache |
123 |
*/ |
|
52
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
124 |
static int pt_image_update_cache (struct pt_image *image, const struct pt_image_params *params) |
0 | 125 |
{ |
126 |
png_structp png; |
|
127 |
png_infop info; |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
128 |
int err = 0; |
0 | 129 |
|
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
130 |
// pre-check enabled |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
131 |
if (!(image->cache->mode & PT_OPEN_UPDATE)) |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
132 |
RETURN_ERROR_ERRNO(PT_ERR_OPEN_MODE, EACCES); |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
133 |
|
0 | 134 |
// open .png |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
135 |
if ((err = pt_image_open_png(image, &png, &info))) |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
136 |
return err; |
0 | 137 |
|
138 |
// setup error trap |
|
139 |
if (setjmp(png_jmpbuf(png))) |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
140 |
JUMP_SET_ERROR(err, PT_ERR_PNG); |
0 | 141 |
|
142 |
// read meta-info |
|
143 |
png_read_info(png, info); |
|
144 |
||
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
145 |
// update our meta-info |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
146 |
if ((err = pt_image_update_info(image, png, info))) |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
147 |
JUMP_ERROR(err); |
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
148 |
|
0 | 149 |
// pass to cache object |
52
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
150 |
if ((err = pt_cache_update_png(image->cache, png, info, params))) |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
151 |
JUMP_ERROR(err); |
0 | 152 |
|
153 |
// finish off, ignore trailing data |
|
154 |
png_read_end(png, NULL); |
|
155 |
||
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
156 |
error: |
0 | 157 |
// clean up |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
158 |
// XXX: we need to close the fopen'd .png |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
159 |
png_destroy_read_struct(&png, &info, NULL); |
0 | 160 |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
161 |
return err; |
0 | 162 |
} |
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 |
* Build a filesystem path representing the appropriate path for this image's cache entry, and store it in the given |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
166 |
* buffer. |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
167 |
*/ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
168 |
static int pt_image_cache_path (struct pt_image *image, char *buf, size_t len) |
0 | 169 |
{ |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
170 |
if (path_with_fext(image->path, buf, len, ".cache")) |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
171 |
RETURN_ERROR(PT_ERR_PATH); |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
172 |
|
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
173 |
return 0; |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
174 |
} |
0 | 175 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
176 |
int pt_image_open (struct pt_image **image_ptr, struct pt_ctx *ctx, const char *path, int cache_mode) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
177 |
{ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
178 |
struct pt_image *image; |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
179 |
char cache_path[1024]; |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
180 |
int err; |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
181 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
182 |
// XXX: verify that the path exists and looks like a PNG file |
0 | 183 |
|
184 |
// alloc |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
185 |
if ((err = pt_image_new(&image, ctx, path))) |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
186 |
return err; |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
187 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
188 |
// compute cache file path |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
189 |
if ((err = pt_image_cache_path(image, cache_path, sizeof(cache_path)))) |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
190 |
JUMP_ERROR(err); |
0 | 191 |
|
9 | 192 |
// create the cache object for this image (doesn't yet open it) |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
193 |
if ((err = pt_cache_new(&image->cache, cache_path, cache_mode))) |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
194 |
JUMP_ERROR(err); |
0 | 195 |
|
196 |
// ok, ready for access |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
197 |
*image_ptr = image; |
0 | 198 |
|
199 |
return 0; |
|
200 |
||
201 |
error: |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
202 |
pt_image_destroy(image); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
203 |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
204 |
return err; |
0 | 205 |
} |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
206 |
|
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
207 |
int pt_image_info (struct pt_image *image, const struct pt_image_info **info_ptr) |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
208 |
{ |
54
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
209 |
struct stat st; |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
210 |
int err; |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
211 |
|
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
|
212 |
// update info |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
213 |
if ((err = pt_cache_info(image->cache, &image->info))) |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
214 |
return err; |
54
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
215 |
|
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
216 |
// stat our info |
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
217 |
if (stat(image->path, &st) < 0) { |
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
218 |
// unknown |
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
219 |
image->info.image_mtime = 0; |
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
220 |
image->info.image_bytes = 0; |
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
221 |
|
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
222 |
} else { |
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
223 |
// store |
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
224 |
image->info.image_mtime = st.st_mtime; |
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
225 |
image->info.image_bytes = st.st_size; |
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
226 |
} |
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
|
227 |
|
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
|
228 |
// return pointer |
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
229 |
*info_ptr = &image->info; |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
230 |
|
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
231 |
return 0; |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
232 |
} |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
233 |
|
8 | 234 |
int pt_image_status (struct pt_image *image) |
5
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
235 |
{ |
8 | 236 |
return pt_cache_status(image->cache, image->path); |
5
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
237 |
} |
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
238 |
|
52
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
239 |
int pt_image_update (struct pt_image *image, const struct pt_image_params *params) |
5
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
240 |
{ |
52
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
25
diff
changeset
|
241 |
return pt_image_update_cache(image, params); |
5
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
242 |
} |
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
243 |
|
25 | 244 |
int pt_image_load (struct pt_image *image) |
245 |
{ |
|
246 |
return pt_cache_open(image->cache); |
|
247 |
} |
|
248 |
||
18 | 249 |
int pt_image_tile_file (struct pt_image *image, const struct pt_tile_info *info, FILE *out) |
9 | 250 |
{ |
18 | 251 |
struct pt_tile tile; |
252 |
int err; |
|
9 | 253 |
|
18 | 254 |
// init |
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
255 |
if ((err = pt_tile_init_file(&tile, image->cache, info, out))) |
18 | 256 |
return err; |
257 |
||
258 |
// render |
|
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
259 |
if ((err = pt_tile_render(&tile))) |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
260 |
JUMP_ERROR(err); |
9 | 261 |
|
18 | 262 |
// ok |
263 |
return 0; |
|
9 | 264 |
|
265 |
error: |
|
18 | 266 |
pt_tile_abort(&tile); |
267 |
||
268 |
return err; |
|
269 |
} |
|
270 |
||
271 |
int pt_image_tile_mem (struct pt_image *image, const struct pt_tile_info *info, char **buf_ptr, size_t *len_ptr) |
|
272 |
{ |
|
273 |
struct pt_tile tile; |
|
274 |
int err; |
|
275 |
||
276 |
// init |
|
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
277 |
if ((err = pt_tile_init_mem(&tile, image->cache, info))) |
18 | 278 |
return err; |
279 |
||
280 |
// render |
|
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
281 |
if ((err = pt_tile_render(&tile))) |
18 | 282 |
JUMP_ERROR(err); |
283 |
||
284 |
// ok |
|
285 |
*buf_ptr = tile.out.mem.base; |
|
286 |
*len_ptr = tile.out.mem.len; |
|
287 |
||
288 |
return 0; |
|
289 |
||
290 |
error: |
|
291 |
pt_tile_abort(&tile); |
|
9 | 292 |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
293 |
return err; |
9 | 294 |
} |
295 |
||
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
296 |
static void _pt_image_tile_async (void *arg) |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
297 |
{ |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
298 |
struct pt_tile *tile = arg; |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
299 |
int err; |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
300 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
301 |
// do render op |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
302 |
if ((err = pt_tile_render(tile))) |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
303 |
log_warn_errno("pt_tile_render: %s", pt_strerror(err)); |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
304 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
305 |
// signal done |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
306 |
if (fclose(tile->out.file)) |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
307 |
log_warn_errno("fclose"); |
22
811c88041c06
cleanup pt_tile in _pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
308 |
|
811c88041c06
cleanup pt_tile in _pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
309 |
// cleanup |
811c88041c06
cleanup pt_tile in _pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
19
diff
changeset
|
310 |
pt_tile_destroy(tile); |
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
311 |
} |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
312 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
313 |
int pt_image_tile_async (struct pt_image *image, const struct pt_tile_info *info, FILE *out) |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
314 |
{ |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
315 |
struct pt_tile *tile; |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
316 |
int err; |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
317 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
318 |
// alloc |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
319 |
if ((err = pt_tile_new(&tile))) |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
320 |
return err; |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
321 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
322 |
// init |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
323 |
if ((err = pt_tile_init_file(tile, image->cache, info, out))) |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
324 |
JUMP_ERROR(err); |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
325 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
326 |
// enqueue work |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
327 |
if ((err = pt_ctx_work(image->ctx, _pt_image_tile_async, tile))) |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
328 |
JUMP_ERROR(err); |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
329 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
330 |
// ok, running |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
331 |
return 0; |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
332 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
333 |
error: |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
334 |
pt_tile_destroy(tile); |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
335 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
336 |
return err; |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
337 |
} |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
338 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
339 |
void pt_image_destroy (struct pt_image *image) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
340 |
{ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
341 |
free(image->path); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
342 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
343 |
if (image->cache) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
344 |
pt_cache_destroy(image->cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
345 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
346 |
free(image); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
347 |
} |
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
348 |