30
|
1 |
cdef extern from "errno.h" :
|
|
2 |
extern int errno
|
|
3 |
|
|
4 |
cdef extern from "string.h" :
|
|
5 |
char* strerror (int err)
|
|
6 |
|
|
7 |
cimport stdio
|
|
8 |
cimport stdlib
|
|
9 |
cimport python_string
|
|
10 |
|
|
11 |
cdef extern from "Python.h" :
|
|
12 |
int PyFile_Check (object p)
|
|
13 |
stdio.FILE* PyFile_AsFile (object p)
|
|
14 |
void PyFile_IncUseCount (object p)
|
|
15 |
void PyFile_DecUseCount (object p)
|
|
16 |
|
|
17 |
cdef extern from "pngtile.h" :
|
|
18 |
struct pt_ctx :
|
|
19 |
pass
|
|
20 |
|
|
21 |
struct pt_image :
|
|
22 |
pass
|
|
23 |
|
|
24 |
enum pt_open_mode :
|
|
25 |
PT_OPEN_UPDATE
|
|
26 |
|
|
27 |
enum pt_cache_status :
|
|
28 |
PT_CACHE_ERROR
|
|
29 |
PT_CACHE_FRESH
|
|
30 |
PT_CACHE_NONE
|
|
31 |
PT_CACHE_STALE
|
57
|
32 |
PT_CACHE_INCOMPAT
|
30
|
33 |
|
|
34 |
struct pt_image_info :
|
57
|
35 |
size_t img_width, img_height
|
|
36 |
int img_mtime, cache_mtime, cache_version
|
|
37 |
size_t img_bytes, cache_bytes
|
|
38 |
size_t cache_blocks
|
|
39 |
|
|
40 |
struct pt_image_params :
|
|
41 |
int background_color[4]
|
30
|
42 |
|
|
43 |
struct pt_tile_info :
|
|
44 |
size_t width, height
|
|
45 |
size_t x, y
|
34
|
46 |
int zoom
|
57
|
47 |
|
30
|
48 |
int pt_image_open (pt_image **image_ptr, pt_ctx *ctx, char *png_path, int cache_mode)
|
|
49 |
int pt_image_info_func "pt_image_info" (pt_image *image, pt_image_info **info_ptr)
|
|
50 |
int pt_image_status (pt_image *image)
|
57
|
51 |
int pt_image_update (pt_image *image, pt_image_params *params)
|
30
|
52 |
int pt_image_tile_file (pt_image *image, pt_tile_info *info, stdio.FILE *out)
|
|
53 |
int pt_image_tile_mem (pt_image *image, pt_tile_info *info, char **buf_ptr, size_t *len_ptr)
|
|
54 |
void pt_image_destroy (pt_image *image)
|
|
55 |
|
|
56 |
char* pt_strerror (int err)
|
|
57 |
|
|
58 |
OPEN_UPDATE = PT_OPEN_UPDATE
|
|
59 |
CACHE_ERROR = PT_CACHE_ERROR
|
|
60 |
CACHE_FRESH = PT_CACHE_FRESH
|
|
61 |
CACHE_NONE = PT_CACHE_NONE
|
|
62 |
CACHE_STALE = PT_CACHE_STALE
|
57
|
63 |
CACHE_INCOMPAT = PT_CACHE_INCOMPAT
|
30
|
64 |
|
|
65 |
class Error (BaseException) :
|
|
66 |
pass
|
|
67 |
|
|
68 |
cdef int trap_err (char *op, int ret) except -1 :
|
|
69 |
if ret < 0 :
|
|
70 |
raise Error("%s: %s: %s" % (op, pt_strerror(ret), strerror(errno)))
|
|
71 |
|
|
72 |
else :
|
|
73 |
return ret
|
|
74 |
|
|
75 |
cdef class Image :
|
|
76 |
cdef pt_image *image
|
|
77 |
|
|
78 |
def __cinit__ (self, char *png_path, int cache_mode = 0) :
|
|
79 |
trap_err("pt_image_open",
|
|
80 |
pt_image_open(&self.image, NULL, png_path, cache_mode)
|
|
81 |
)
|
|
82 |
|
|
83 |
def info (self) :
|
|
84 |
cdef pt_image_info *image_info
|
|
85 |
|
|
86 |
trap_err("pt_image_info",
|
|
87 |
pt_image_info_func(self.image, &image_info)
|
|
88 |
)
|
|
89 |
|
57
|
90 |
return (image_info.img_width, image_info.img_height)
|
30
|
91 |
|
|
92 |
def status (self) :
|
|
93 |
return trap_err("pt_image_status",
|
|
94 |
pt_image_status(self.image)
|
|
95 |
)
|
|
96 |
|
57
|
97 |
# XXX: support params
|
30
|
98 |
def update (self) :
|
|
99 |
trap_err("pt_image_update",
|
57
|
100 |
pt_image_update(self.image, NULL)
|
30
|
101 |
)
|
|
102 |
|
34
|
103 |
def tile_file (self, size_t width, size_t height, size_t x, size_t y, int zoom, object out) :
|
30
|
104 |
cdef stdio.FILE *outf
|
|
105 |
cdef pt_tile_info ti
|
|
106 |
|
|
107 |
if not PyFile_Check(out) :
|
|
108 |
raise TypeError("out: must be a file object")
|
|
109 |
|
|
110 |
outf = PyFile_AsFile(out)
|
|
111 |
|
|
112 |
if not outf :
|
|
113 |
raise TypeError("out: must have a FILE*")
|
|
114 |
|
|
115 |
ti.width = width
|
|
116 |
ti.height = height
|
|
117 |
ti.x = x
|
|
118 |
ti.y = y
|
34
|
119 |
ti.zoom = zoom
|
30
|
120 |
|
|
121 |
trap_err("pt_image_tile_file",
|
|
122 |
pt_image_tile_file(self.image, &ti, outf)
|
|
123 |
)
|
|
124 |
|
34
|
125 |
def tile_mem (self, size_t width, size_t height, size_t x, size_t y, int zoom) :
|
30
|
126 |
cdef pt_tile_info ti
|
|
127 |
cdef char *buf
|
|
128 |
cdef size_t len
|
|
129 |
|
|
130 |
ti.width = width
|
|
131 |
ti.height = height
|
|
132 |
ti.x = x
|
|
133 |
ti.y = y
|
34
|
134 |
ti.zoom = zoom
|
30
|
135 |
|
|
136 |
# render and return ptr to buffer
|
|
137 |
trap_err("pt_image_tile_mem",
|
|
138 |
pt_image_tile_mem(self.image, &ti, &buf, &len)
|
|
139 |
)
|
|
140 |
|
|
141 |
# copy buffer as str...
|
|
142 |
data = python_string.PyString_FromStringAndSize(buf, len)
|
|
143 |
|
|
144 |
# drop buffer...
|
|
145 |
stdlib.free(buf)
|
|
146 |
|
|
147 |
return data
|
|
148 |
|
|
149 |
def __dealloc__ (self) :
|
|
150 |
if self.image :
|
|
151 |
pt_image_destroy(self.image)
|
|
152 |
|