author | Tero Marttila <terom@fixme.fi> |
Tue, 29 Dec 2009 15:34:18 +0200 | |
changeset 15 | 01de253f3bbf |
parent 10 | 6806a90d934f |
child 52 | 148a120ea7d5 |
permissions | -rw-r--r-- |
0 | 1 |
#ifndef PNGTILE_CACHE_H |
2 |
#define PNGTILE_CACHE_H |
|
3 |
||
4 |
/** |
|
5 |
* @file |
|
6 |
* |
|
7 |
* Internal image cache implementation |
|
8 |
*/ |
|
9 |
#include "image.h" |
|
10 |
||
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
11 |
#include <stdint.h> |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
12 |
#include <stdbool.h> |
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 |
|
0 | 16 |
/** |
17 |
* State for cache access |
|
18 |
*/ |
|
19 |
struct pt_cache { |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
20 |
/** Filesystem path to cache file */ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
21 |
char *path; |
0 | 22 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
23 |
/** The mode we are operating in, bitmask of PT_IMG_* */ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
24 |
int mode; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
25 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
26 |
/** Opened file */ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
27 |
int fd; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
28 |
|
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
29 |
/** The mmap'd header */ |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
30 |
struct pt_cache_header *header; |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
31 |
|
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
32 |
/** Memory-mapped file data, starting at PT_CACHE_HEADER_SIZE */ |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
33 |
uint8_t *data; |
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
34 |
|
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
35 |
/** Size of the data segment in bytes, starting at PT_CACHE_HEADER_SIZE */ |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
36 |
size_t size; |
0 | 37 |
}; |
38 |
||
39 |
/** |
|
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
40 |
* Size used to store the cache header |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
41 |
*/ |
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
42 |
#define PT_CACHE_HEADER_SIZE 4096 |
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 |
/** |
0 | 45 |
* On-disk header |
46 |
*/ |
|
47 |
struct pt_cache_header { |
|
48 |
/** Pixel dimensions of image */ |
|
49 |
uint32_t width, height; |
|
50 |
||
51 |
/** Pixel format */ |
|
52 |
uint8_t bit_depth, color_type; |
|
53 |
||
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
2
diff
changeset
|
54 |
/** Number of png_color entries that follow */ |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
2
diff
changeset
|
55 |
uint16_t num_palette; |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
2
diff
changeset
|
56 |
|
15 | 57 |
/** Number of bytes per row */ |
0 | 58 |
uint32_t row_bytes; |
9 | 59 |
|
60 |
/** Number of bytes per pixel */ |
|
61 |
uint8_t col_bytes; |
|
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
2
diff
changeset
|
62 |
|
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
2
diff
changeset
|
63 |
/** Palette entries, up to 256 entries used */ |
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
2
diff
changeset
|
64 |
png_color palette[PNG_MAX_PALETTE_LENGTH]; |
0 | 65 |
}; |
66 |
||
67 |
/** |
|
68 |
* Construct the image cache info object associated with the given image. |
|
69 |
*/ |
|
9 | 70 |
int pt_cache_new (struct pt_cache **cache_ptr, const char *path, int mode); |
0 | 71 |
|
72 |
/** |
|
8 | 73 |
* Verify if the cached data eixsts, or has become stale compared to the given original file. |
74 |
* |
|
75 |
* @return one of pt_cache_status; <0 on error, 0 if fresh, >0 otherwise |
|
0 | 76 |
*/ |
8 | 77 |
int pt_cache_status (struct pt_cache *cache, const char *img_path); |
0 | 78 |
|
79 |
/** |
|
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
|
80 |
* Get info for the cached image, open it if not 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
|
81 |
*/ |
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 |
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
|
83 |
|
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
|
84 |
/** |
0 | 85 |
* Update the cache data from the given PNG image. |
86 |
*/ |
|
87 |
int pt_cache_update_png (struct pt_cache *cache, png_structp png, png_infop info); |
|
88 |
||
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
89 |
/** |
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
|
90 |
* Open the existing .cache for use. If already opened, does nothing. |
9 | 91 |
*/ |
92 |
int pt_cache_open (struct pt_cache *cache); |
|
93 |
||
94 |
/** |
|
95 |
* Render out a PNG tile as given, into the established png object, up to (but not including) the png_write_end. |
|
96 |
* |
|
97 |
* If the cache is not yet open, this will open it |
|
98 |
*/ |
|
99 |
int pt_cache_tile_png (struct pt_cache *cache, png_structp png, png_infop info, const struct pt_tile_info *ti); |
|
100 |
||
101 |
/** |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
102 |
* Release all resources associated with the given cache object without any cleanup. |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
103 |
*/ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
104 |
void pt_cache_destroy (struct pt_cache *cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
105 |
|
0 | 106 |
#endif |