author | Tero Marttila <terom@fixme.fi> |
Wed, 06 Jan 2010 18:35:49 +0200 | |
changeset 42 | a5bca7b0cd8a |
parent 34 | a387bc77ad52 |
child 52 | 148a120ea7d5 |
permissions | -rw-r--r-- |
0 | 1 |
#ifndef PNGTILE_H |
2 |
#define PNGTILE_H |
|
3 |
||
4 |
/** |
|
5 |
* @file |
|
6 |
* |
|
7 |
* Tile-based access to large PNG images. |
|
8 |
*/ |
|
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
9 |
#include <stddef.h> |
9 | 10 |
#include <stdio.h> // for FILE* |
0 | 11 |
|
12 |
/** |
|
13 |
* "Global" context shared between images |
|
14 |
*/ |
|
15 |
struct pt_ctx; |
|
16 |
||
17 |
/** |
|
18 |
* Per-image state |
|
19 |
*/ |
|
20 |
struct pt_image; |
|
21 |
||
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
22 |
/** Bitmask for pt_image_open modes */ |
15 | 23 |
enum pt_open_mode { |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
24 |
/** Update cache if needed */ |
11 | 25 |
PT_OPEN_UPDATE = 0x01, |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
26 |
|
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
27 |
/** Accept stale cache */ |
15 | 28 |
// TODO: PT_OPEN_STALE = 0x02, |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
29 |
}; |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
30 |
|
8 | 31 |
/** |
32 |
* Values for pt_image_cached |
|
33 |
*/ |
|
34 |
enum pt_cache_status { |
|
35 |
/** Cache status could not be determined */ |
|
36 |
PT_CACHE_ERROR = -1, |
|
37 |
||
38 |
/** Cache is fresh */ |
|
39 |
PT_CACHE_FRESH = 0, |
|
40 |
||
41 |
/** Cache does not exist */ |
|
42 |
PT_CACHE_NONE = 1, |
|
43 |
||
44 |
/** Cache exists, but is stale */ |
|
45 |
PT_CACHE_STALE = 2, |
|
46 |
}; |
|
47 |
||
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
48 |
/** Metadata info for image */ |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
49 |
struct pt_image_info { |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
50 |
/** Dimensions of image */ |
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
51 |
size_t width, height; |
0 | 52 |
}; |
53 |
||
13
294201975f8c
fix clip_x bug, and reject images that are completely out of bounds
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
54 |
/** |
294201975f8c
fix clip_x bug, and reject images that are completely out of bounds
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
55 |
* Info for image tile |
294201975f8c
fix clip_x bug, and reject images that are completely out of bounds
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
56 |
* |
294201975f8c
fix clip_x bug, and reject images that are completely out of bounds
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
57 |
* The tile may safely overlap with the edge of the image, but it should not be entirely outside of the image |
294201975f8c
fix clip_x bug, and reject images that are completely out of bounds
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
58 |
*/ |
9 | 59 |
struct pt_tile_info { |
60 |
/** Dimensions of output image */ |
|
61 |
size_t width, height; |
|
62 |
||
63 |
/** Pixel coordinates of top-left corner */ |
|
64 |
size_t x, y; |
|
65 |
||
34 | 66 |
/** Zoom factor of 2^z (out < zero < in) */ |
67 |
int zoom; |
|
9 | 68 |
}; |
69 |
||
11 | 70 |
/** |
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
71 |
* Construct a new pt_ctx for use with further pt_image's. |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
72 |
* |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
73 |
* @param ctx_ptr returned pt_ctx handle |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
74 |
* @param threads number of worker threads to use for parralel operations, or zero to disable |
11 | 75 |
*/ |
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
76 |
int pt_ctx_new (struct pt_ctx **ctx_ptr, int threads); |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
77 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
78 |
/** |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
79 |
* Shut down the given pt_ctx, waiting for any ongoing/pending operations to finish. |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
80 |
*/ |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
81 |
int pt_ctx_shutdown (struct pt_ctx *ctx); |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
82 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
83 |
/** |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
84 |
* Release the given pt_ctx without waiting for any ongoing operations to finish. |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
85 |
*/ |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
86 |
void pt_ctx_destroy (struct pt_ctx *ctx); |
0 | 87 |
|
88 |
/** |
|
89 |
* Open a new pt_image for use. |
|
90 |
* |
|
91 |
* @param img_ptr returned pt_image handle |
|
92 |
* @param ctx global state to use |
|
93 |
* @param path filesystem path to .png file |
|
11 | 94 |
* @param mode combination of PT_OPEN_* flags |
0 | 95 |
*/ |
5
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
96 |
int pt_image_open (struct pt_image **image_ptr, struct pt_ctx *ctx, const char *png_path, int cache_mode); |
0 | 97 |
|
5
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
98 |
/** |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
99 |
* Get the image's metadata |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
100 |
*/ |
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
101 |
int pt_image_info (struct pt_image *image, const struct pt_image_info **info_ptr); |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
102 |
|
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
103 |
/** |
8 | 104 |
* Check the given image's cache is stale - in other words, if the image needs to be update()'d. |
105 |
* |
|
106 |
* @return one of pt_cache_status |
|
5
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
107 |
*/ |
8 | 108 |
int pt_image_status (struct pt_image *image); |
0 | 109 |
|
5
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
110 |
/** |
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
111 |
* Update the given image's cache. |
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
112 |
*/ |
4b440fa03183
move stale-update logic to main
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
113 |
int pt_image_update (struct pt_image *image); |
0 | 114 |
|
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
115 |
/** |
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
116 |
* Load the image's cache in read-only mode without trying to update it. |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
117 |
*/ |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
118 |
// XXX: rename to pt_image_open? |
25 | 119 |
int pt_image_load (struct pt_image *image); |
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
120 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
121 |
/** |
18 | 122 |
* Render a PNG tile to a FILE*. |
9 | 123 |
* |
124 |
* The PNG data will be written to the given stream, which will be flushed, but not closed. |
|
125 |
*/ |
|
18 | 126 |
int pt_image_tile_file (struct pt_image *image, const struct pt_tile_info *info, FILE *out); |
127 |
||
128 |
/** |
|
129 |
* Render a PNG tile to memory. |
|
130 |
* |
|
131 |
* The PNG data will be written to a malloc'd buffer. |
|
132 |
* |
|
133 |
* @param image render from image's cache |
|
134 |
* @param info tile parameters |
|
135 |
* @param buf_ptr returned heap buffer |
|
136 |
* @param len_ptr returned buffer length |
|
137 |
*/ |
|
138 |
int pt_image_tile_mem (struct pt_image *image, const struct pt_tile_info *info, char **buf_ptr, size_t *len_ptr); |
|
9 | 139 |
|
140 |
/** |
|
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
141 |
* Render a PNG tile to FILE* in a parralel manner. |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
142 |
* |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
143 |
* The PNG data will be written to \a out, which will be fclose()'d once done. |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
144 |
* |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
145 |
* This function may return before the PNG has been rendered. |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
146 |
* |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
147 |
* @param image render from image's cache. The cache must have been opened previously! |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
148 |
* @param info tile parameters |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
149 |
* @param out IO stream to write PNG data to, and close once done |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
150 |
*/ |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
151 |
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
|
152 |
|
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
153 |
/** |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
154 |
* Release the given pt_image without any clean shutdown |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
155 |
*/ |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
156 |
void pt_image_destroy (struct pt_image *image); |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
5
diff
changeset
|
157 |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
158 |
/** |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
159 |
* Error codes returned |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
160 |
*/ |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
161 |
enum pt_error { |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
162 |
PT_SUCCESS = 0, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
163 |
PT_ERR_MEM, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
164 |
|
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
165 |
PT_ERR_PATH, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
166 |
PT_ERR_OPEN_MODE, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
167 |
|
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
168 |
PT_ERR_IMG_STAT, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
169 |
PT_ERR_IMG_FOPEN, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
170 |
|
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
171 |
PT_ERR_PNG_CREATE, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
172 |
PT_ERR_PNG, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
173 |
|
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
174 |
PT_ERR_CACHE_STAT, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
175 |
PT_ERR_CACHE_OPEN_READ, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
176 |
PT_ERR_CACHE_OPEN_TMP, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
177 |
PT_ERR_CACHE_SEEK, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
178 |
PT_ERR_CACHE_READ, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
179 |
PT_ERR_CACHE_WRITE, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
180 |
PT_ERR_CACHE_TRUNC, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
181 |
PT_ERR_CACHE_MMAP, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
182 |
PT_ERR_CACHE_RENAME_TMP, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
183 |
|
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
184 |
PT_ERR_TILE_CLIP, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
185 |
|
19
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
186 |
PT_ERR_PTHREAD_CREATE, |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
187 |
PT_ERR_CTX_SHUTDOWN, |
ebcc49de97d0
implement pt_ctx threadpool and pt_image_tile_async
Tero Marttila <terom@fixme.fi>
parents:
18
diff
changeset
|
188 |
|
34 | 189 |
PT_ERR_ZOOM, |
190 |
||
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
191 |
PT_ERR_MAX, |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
192 |
}; |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
193 |
|
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
194 |
/** |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
195 |
* Translate error code to short description |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
196 |
*/ |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
197 |
const char *pt_strerror (int err); |
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
16
diff
changeset
|
198 |
|
0 | 199 |
#endif |