author | Tero Marttila <terom@fixme.fi> |
Mon, 28 Dec 2009 19:54:41 +0200 | |
changeset 2 | 05de54150a4c |
parent 1 | f3cde3db1fef |
child 3 | da7c6dcafb43 |
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" |
0 | 3 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
4 |
#include <stdlib.h> |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
5 |
#include <limits.h> // for _POSIX_PATH_MAX |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
6 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
7 |
#include <png.h> |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
8 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
9 |
static int pt_image_new (struct pt_image **img_ptr, struct pt_ctx *ctx, const char *path) |
0 | 10 |
{ |
11 |
struct pt_image *img; |
|
12 |
||
13 |
// alloc |
|
14 |
if ((img = calloc(1, sizeof(*img))) == NULL) |
|
15 |
return -1; |
|
16 |
||
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
17 |
if ((img->path = strdup(path)) == NULL) |
0 | 18 |
goto error; |
19 |
||
20 |
// init |
|
21 |
img->ctx = ctx; |
|
22 |
||
23 |
// ok |
|
24 |
*img_ptr = img; |
|
25 |
||
26 |
return 0; |
|
27 |
||
28 |
error: |
|
29 |
pt_image_destroy(img); |
|
30 |
||
31 |
return -1; |
|
32 |
} |
|
33 |
||
34 |
/** |
|
35 |
* Open the image's FILE |
|
36 |
*/ |
|
37 |
static int pt_image_open_file (struct pt_image *img, FILE **file_ptr) |
|
38 |
{ |
|
39 |
FILE *fp; |
|
40 |
||
41 |
// open |
|
2 | 42 |
if ((fp = fopen(img->path, "rb")) < 0) |
0 | 43 |
return -1; |
44 |
||
45 |
// ok |
|
46 |
*file_ptr = fp; |
|
47 |
||
48 |
return 0; |
|
49 |
} |
|
50 |
||
51 |
/** |
|
52 |
* Open the PNG image, setting up the I/O and returning the png_structp and png_infop |
|
53 |
*/ |
|
54 |
static int pt_image_open_png (struct pt_image *img, png_structp *png_ptr, png_infop *info_ptr) |
|
55 |
{ |
|
56 |
FILE *fp = NULL; |
|
57 |
png_structp png = NULL; |
|
58 |
png_infop info = NULL; |
|
59 |
||
60 |
// open I/O |
|
61 |
if (pt_image_open_file(img, &fp)) |
|
62 |
goto error; |
|
63 |
||
64 |
// create the struct |
|
65 |
if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL) |
|
66 |
goto error; |
|
67 |
||
68 |
// create the info |
|
69 |
if ((info = png_create_info_struct(png)) == NULL) |
|
70 |
goto error; |
|
71 |
||
72 |
// setup error trap for the I/O |
|
73 |
if (setjmp(png_jmpbuf(png))) |
|
74 |
goto error; |
|
75 |
||
76 |
// setup I/O to FILE |
|
77 |
png_init_io(png, fp); |
|
78 |
||
79 |
// ok |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
80 |
// XXX: what to do with fp? |
0 | 81 |
*png_ptr = png; |
82 |
*info_ptr = info; |
|
83 |
||
84 |
return 0; |
|
85 |
||
86 |
error: |
|
87 |
// cleanup file |
|
88 |
if (fp) fclose(fp); |
|
89 |
||
90 |
// cleanup PNG state |
|
91 |
png_destroy_read_struct(&png, &info, NULL); |
|
92 |
||
93 |
return -1; |
|
94 |
} |
|
95 |
||
96 |
/** |
|
97 |
* Open the PNG image, and write out to the cache |
|
98 |
*/ |
|
99 |
static int pt_image_update_cache (struct pt_image *img) |
|
100 |
{ |
|
101 |
png_structp png; |
|
102 |
png_infop info; |
|
103 |
||
104 |
// open .png |
|
105 |
if (pt_image_open_png(img, &png, &info)) |
|
106 |
return -1; |
|
107 |
||
108 |
// setup error trap |
|
109 |
if (setjmp(png_jmpbuf(png))) |
|
110 |
goto error; |
|
111 |
||
112 |
// read meta-info |
|
113 |
png_read_info(png, info); |
|
114 |
||
115 |
// pass to cache object |
|
116 |
if (pt_cache_update_png(img->cache, png, info)) |
|
117 |
goto error; |
|
118 |
||
119 |
// finish off, ignore trailing data |
|
120 |
png_read_end(png, NULL); |
|
121 |
||
122 |
// clean up |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
123 |
png_destroy_read_struct(&png, &info, NULL); |
0 | 124 |
|
125 |
return 0; |
|
126 |
||
127 |
error: |
|
128 |
// clean up |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
129 |
png_destroy_read_struct(&png, &info, NULL); |
0 | 130 |
|
131 |
return -1; |
|
132 |
} |
|
133 |
||
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
134 |
/** |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
135 |
* 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
|
136 |
* buffer. |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
137 |
*/ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
138 |
static int pt_image_cache_path (struct pt_image *image, char *buf, size_t len) |
0 | 139 |
{ |
2 | 140 |
char *ext; |
141 |
||
142 |
// XXX: be more careful about buf len |
|
143 |
||
144 |
// copy filename |
|
145 |
strncpy(buf, image->path, len); |
|
146 |
||
147 |
// find .ext |
|
148 |
if ((ext = strrchr(buf, '.')) == NULL) |
|
149 |
return -1; |
|
150 |
||
151 |
// change to .cache |
|
152 |
strncpy(ext, ".cache", (buf + len) - ext); |
|
153 |
||
154 |
// hmmk |
|
155 |
return 0; |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
156 |
} |
0 | 157 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
158 |
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
|
159 |
{ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
160 |
struct pt_image *image; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
161 |
char cache_path[_POSIX_PATH_MAX]; |
2 | 162 |
int stale; |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
163 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
164 |
// XXX: verify that the path exists and looks like a PNG file |
0 | 165 |
|
166 |
// alloc |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
167 |
if (pt_image_new(&image, ctx, path)) |
0 | 168 |
return -1; |
1
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 |
// compute cache file path |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
171 |
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
|
172 |
goto error; |
0 | 173 |
|
174 |
// open the cache object for this image |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
175 |
if (pt_cache_open(&image->cache, cache_path, cache_mode)) |
0 | 176 |
goto error; |
177 |
||
178 |
// update if not fresh |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
179 |
// XXX: check cache_mode |
2 | 180 |
if ((stale = pt_cache_stale(image->cache, image->path)) < 0) |
181 |
goto error; |
|
182 |
||
183 |
if (stale) |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
184 |
pt_image_update_cache(image); |
0 | 185 |
|
186 |
// ok, ready for access |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
187 |
*image_ptr = image; |
0 | 188 |
|
189 |
return 0; |
|
190 |
||
191 |
error: |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
192 |
pt_image_destroy(image); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
193 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
194 |
return -1; |
0 | 195 |
} |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
196 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
197 |
void pt_image_destroy (struct pt_image *image) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
198 |
{ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
199 |
free(image->path); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
200 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
201 |
if (image->cache) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
202 |
pt_cache_destroy(image->cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
203 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
204 |
free(image); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
205 |
} |