author | Tero Marttila <terom@fixme.fi> |
Mon, 28 Dec 2009 23:15:18 +0200 | |
changeset 8 | 400ddf1e7aa9 |
parent 7 | 997906f5fd2d |
child 9 | a31048ff76a2 |
permissions | -rw-r--r-- |
0 | 1 |
#include "image.h" |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
2 |
#include "cache.h" |
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
3 |
#include "shared/util.h" |
0 | 4 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
5 |
#include <stdlib.h> |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
6 |
#include <errno.h> |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
7 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
8 |
#include <png.h> |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
9 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
10 |
static int pt_image_new (struct pt_image **img_ptr, struct pt_ctx *ctx, const char *path) |
0 | 11 |
{ |
12 |
struct pt_image *img; |
|
13 |
||
14 |
// alloc |
|
15 |
if ((img = calloc(1, sizeof(*img))) == NULL) |
|
16 |
return -1; |
|
17 |
||
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
18 |
if ((img->path = strdup(path)) == NULL) |
0 | 19 |
goto error; |
20 |
||
21 |
// init |
|
22 |
img->ctx = ctx; |
|
23 |
||
24 |
// ok |
|
25 |
*img_ptr = img; |
|
26 |
||
27 |
return 0; |
|
28 |
||
29 |
error: |
|
30 |
pt_image_destroy(img); |
|
31 |
||
32 |
return -1; |
|
33 |
} |
|
34 |
||
35 |
/** |
|
36 |
* Open the image's FILE |
|
37 |
*/ |
|
38 |
static int pt_image_open_file (struct pt_image *img, FILE **file_ptr) |
|
39 |
{ |
|
40 |
FILE *fp; |
|
41 |
||
42 |
// open |
|
2 | 43 |
if ((fp = fopen(img->path, "rb")) < 0) |
0 | 44 |
return -1; |
45 |
||
46 |
// ok |
|
47 |
*file_ptr = fp; |
|
48 |
||
49 |
return 0; |
|
50 |
} |
|
51 |
||
52 |
/** |
|
53 |
* Open the PNG image, setting up the I/O and returning the png_structp and png_infop |
|
54 |
*/ |
|
55 |
static int pt_image_open_png (struct pt_image *img, png_structp *png_ptr, png_infop *info_ptr) |
|
56 |
{ |
|
57 |
FILE *fp = NULL; |
|
58 |
png_structp png = NULL; |
|
59 |
png_infop info = NULL; |
|
60 |
||
61 |
// open I/O |
|
62 |
if (pt_image_open_file(img, &fp)) |
|
63 |
goto error; |
|
64 |
||
65 |
// create the struct |
|
66 |
if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL) |
|
67 |
goto error; |
|
68 |
||
69 |
// create the info |
|
70 |
if ((info = png_create_info_struct(png)) == NULL) |
|
71 |
goto error; |
|
72 |
||
73 |
// setup error trap for the I/O |
|
74 |
if (setjmp(png_jmpbuf(png))) |
|
75 |
goto error; |
|
76 |
||
77 |
// setup I/O to FILE |
|
78 |
png_init_io(png, fp); |
|
79 |
||
80 |
// ok |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
81 |
// XXX: what to do with fp? |
0 | 82 |
*png_ptr = png; |
83 |
*info_ptr = info; |
|
84 |
||
85 |
return 0; |
|
86 |
||
87 |
error: |
|
88 |
// cleanup file |
|
89 |
if (fp) fclose(fp); |
|
90 |
||
91 |
// cleanup PNG state |
|
92 |
png_destroy_read_struct(&png, &info, NULL); |
|
93 |
||
94 |
return -1; |
|
95 |
} |
|
96 |
||
97 |
/** |
|
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
98 |
* 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
|
99 |
* |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
100 |
* Must be called under libpng-error-trap! |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
101 |
*/ |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
102 |
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
|
103 |
{ |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
104 |
// query png_get_* |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
105 |
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
|
106 |
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
|
107 |
|
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
108 |
return 0; |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
109 |
} |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
110 |
|
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
111 |
/** |
0 | 112 |
* Open the PNG image, and write out to the cache |
113 |
*/ |
|
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
114 |
static int pt_image_update_cache (struct pt_image *image) |
0 | 115 |
{ |
116 |
png_structp png; |
|
117 |
png_infop info; |
|
118 |
||
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
119 |
// pre-check enabled |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
120 |
if (!(image->cache->mode & PT_IMG_WRITE)) { |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
121 |
errno = EPERM; |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
122 |
return -1; |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
123 |
} |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
124 |
|
0 | 125 |
// open .png |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
126 |
if (pt_image_open_png(image, &png, &info)) |
0 | 127 |
return -1; |
128 |
||
129 |
// setup error trap |
|
130 |
if (setjmp(png_jmpbuf(png))) |
|
131 |
goto error; |
|
132 |
||
133 |
// read meta-info |
|
134 |
png_read_info(png, info); |
|
135 |
||
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
136 |
// update our meta-info |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
137 |
if (pt_image_update_info(image, png, info)) |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
138 |
goto error; |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
139 |
|
0 | 140 |
// pass to cache object |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
141 |
if (pt_cache_update_png(image->cache, png, info)) |
0 | 142 |
goto error; |
143 |
||
144 |
// finish off, ignore trailing data |
|
145 |
png_read_end(png, NULL); |
|
146 |
||
147 |
// clean up |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
148 |
png_destroy_read_struct(&png, &info, NULL); |
0 | 149 |
|
150 |
return 0; |
|
151 |
||
152 |
error: |
|
153 |
// clean up |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
154 |
png_destroy_read_struct(&png, &info, NULL); |
0 | 155 |
|
156 |
return -1; |
|
157 |
} |
|
158 |
||
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
159 |
/** |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
160 |
* 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
|
161 |
* buffer. |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
162 |
*/ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
163 |
static int pt_image_cache_path (struct pt_image *image, char *buf, size_t len) |
0 | 164 |
{ |
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
165 |
return path_with_fext(image->path, buf, len, ".cache"); |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
166 |
} |
0 | 167 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
168 |
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
|
169 |
{ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
170 |
struct pt_image *image; |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
171 |
char cache_path[1024]; |
1
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 |
// XXX: verify that the path exists and looks like a PNG file |
0 | 174 |
|
175 |
// alloc |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
176 |
if (pt_image_new(&image, ctx, path)) |
0 | 177 |
return -1; |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
178 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
179 |
// compute cache file path |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
180 |
if (pt_image_cache_path(image, cache_path, sizeof(cache_path))) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
181 |
goto error; |
0 | 182 |
|
183 |
// open the cache object for this image |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
184 |
if (pt_cache_open(&image->cache, cache_path, cache_mode)) |
0 | 185 |
goto error; |
186 |
||
187 |
// ok, ready for access |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
188 |
*image_ptr = image; |
0 | 189 |
|
190 |
return 0; |
|
191 |
||
192 |
error: |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
193 |
pt_image_destroy(image); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
194 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
195 |
return -1; |
0 | 196 |
} |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
197 |
|
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
198 |
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
|
199 |
{ |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
200 |
// XXX: ensure that this was read? |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
201 |
*info_ptr = &image->info; |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
202 |
|
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
203 |
return 0; |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
204 |
} |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
205 |
|
8 | 206 |
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
|
207 |
{ |
8 | 208 |
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
|
209 |
} |
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
210 |
|
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
211 |
int pt_image_update (struct pt_image *image) |
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
212 |
{ |
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
213 |
return pt_image_update_cache(image); |
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
214 |
} |
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
215 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
216 |
void pt_image_destroy (struct pt_image *image) |
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 |
free(image->path); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
219 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
220 |
if (image->cache) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
221 |
pt_cache_destroy(image->cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
222 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
223 |
free(image); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
224 |
} |