author | Tero Marttila <terom@fixme.fi> |
Mon, 25 Jan 2010 05:14:00 +0200 | |
changeset 91 | 0bf7878bdf5c |
parent 87 | ecd87b41e884 |
child 104 | b5ae988c78b8 |
permissions | -rw-r--r-- |
30 | 1 |
cdef extern from "errno.h" : |
2 |
extern int errno |
|
3 |
||
4 |
cdef extern from "string.h" : |
|
5 |
char* strerror (int err) |
|
6 |
||
78 | 7 |
void* memset (void *, int, size_t) |
8 |
void* memcpy (void *, void *, size_t) |
|
9 |
||
30 | 10 |
cimport stdio |
11 |
cimport stdlib |
|
12 |
cimport python_string |
|
13 |
||
14 |
cdef extern from "Python.h" : |
|
15 |
int PyFile_Check (object p) |
|
16 |
stdio.FILE* PyFile_AsFile (object p) |
|
17 |
void PyFile_IncUseCount (object p) |
|
18 |
void PyFile_DecUseCount (object p) |
|
19 |
||
20 |
cdef extern from "pngtile.h" : |
|
21 |
struct pt_ctx : |
|
22 |
pass |
|
23 |
||
24 |
struct pt_image : |
|
25 |
pass |
|
26 |
||
27 |
enum pt_open_mode : |
|
78 | 28 |
PT_OPEN_READ # 0 |
30 | 29 |
PT_OPEN_UPDATE |
30 |
||
31 |
enum pt_cache_status : |
|
78 | 32 |
PT_CACHE_ERROR # -1 |
30 | 33 |
PT_CACHE_FRESH |
34 |
PT_CACHE_NONE |
|
35 |
PT_CACHE_STALE |
|
57 | 36 |
PT_CACHE_INCOMPAT |
30 | 37 |
|
38 |
struct pt_image_info : |
|
78 | 39 |
size_t img_width, img_height, img_bpp |
40 |
int image_mtime, cache_mtime, cache_version |
|
41 |
size_t image_bytes, cache_bytes |
|
57 | 42 |
size_t cache_blocks |
43 |
||
44 |
struct pt_image_params : |
|
45 |
int background_color[4] |
|
83 | 46 |
|
30 | 47 |
struct pt_tile_info : |
48 |
size_t width, height |
|
49 |
size_t x, y |
|
34 | 50 |
int zoom |
91
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
51 |
|
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
52 |
ctypedef pt_image_info* const_image_info_ptr "const struct pt_image_info *" |
83 | 53 |
|
54 |
## functions |
|
91
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
55 |
int pt_image_open (pt_image **image_ptr, pt_ctx *ctx, char *png_path, int cache_mode) nogil |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
56 |
int pt_image_info_ "pt_image_info" (pt_image *image, pt_image_info **info_ptr) nogil |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
57 |
int pt_image_status (pt_image *image) nogil |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
58 |
int pt_image_load (pt_image *image) nogil |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
59 |
int pt_image_update (pt_image *image, pt_image_params *params) nogil |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
60 |
int pt_image_tile_file (pt_image *image, pt_tile_info *info, stdio.FILE *out) nogil |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
61 |
int pt_image_tile_mem (pt_image *image, pt_tile_info *info, char **buf_ptr, size_t *len_ptr) nogil |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
62 |
void pt_image_destroy (pt_image *image) nogil |
83 | 63 |
|
64 |
# error code -> name |
|
30 | 65 |
char* pt_strerror (int err) |
66 |
||
78 | 67 |
## constants |
68 |
# Image() |
|
69 |
OPEN_READ = PT_OPEN_READ |
|
70 |
OPEN_UPDATE = PT_OPEN_UPDATE |
|
71 |
||
72 |
# Image.status -> ... |
|
73 |
CACHE_FRESH = PT_CACHE_FRESH |
|
74 |
CACHE_NONE = PT_CACHE_NONE |
|
75 |
CACHE_STALE = PT_CACHE_STALE |
|
76 |
CACHE_INCOMPAT = PT_CACHE_INCOMPAT |
|
30 | 77 |
|
78 |
class Error (BaseException) : |
|
91
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
79 |
""" |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
80 |
Base class for errors raised by pypngtile. |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
81 |
""" |
30 | 82 |
|
91
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
83 |
def __init__ (self, func, err) : |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
84 |
super(Error, self).__init__("%s: %s: %s" % (func, pt_strerror(err), strerror(errno))) |
30 | 85 |
|
86 |
cdef class Image : |
|
78 | 87 |
""" |
88 |
An image file on disk (.png) and an associated .cache file. |
|
89 |
||
90 |
Open the .png file at the given path using the given mode. |
|
91 |
||
92 |
path - filesystem path to .png file |
|
93 |
mode - mode to operate cache in |
|
94 |
OPEN_READ - read-only access to cache |
|
95 |
OPEN_UPDATE - allow .update() |
|
96 |
""" |
|
97 |
||
30 | 98 |
cdef pt_image *image |
99 |
||
78 | 100 |
|
101 |
# open the pt_image |
|
102 |
def __cinit__ (self, char *path, int mode = 0) : |
|
91
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
103 |
cdef int err |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
104 |
|
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
105 |
# open |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
106 |
with nogil : |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
107 |
# XXX: I hope use of path doesn't break... |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
108 |
err = pt_image_open(&self.image, NULL, path, mode) |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
109 |
|
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
110 |
if err : |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
111 |
raise Error("pt_image_open", err) |
78 | 112 |
|
113 |
||
30 | 114 |
def info (self) : |
78 | 115 |
""" |
116 |
Return a dictionary containing various information about the image. |
|
117 |
||
118 |
img_width - pixel dimensions of the source image |
|
119 |
img_height only available if the cache was opened |
|
120 |
img_bpp - bits per pixel for the source image |
|
121 |
||
122 |
image_mtime - last modification timestamp for source image |
|
123 |
image_bytes - size of source image file in bytes |
|
124 |
||
125 |
cache_version - version of cache file available |
|
126 |
cache_mtime - last modification timestamp for cache file |
|
127 |
cache_bytes - size of cache file in bytes |
|
128 |
cache_blocks - size of cache file in disk blocks - 512 bytes / block |
|
129 |
""" |
|
130 |
||
91
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
131 |
cdef const_image_info_ptr infop |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
132 |
cdef int err |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
133 |
|
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
134 |
with nogil : |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
135 |
err = pt_image_info_(self.image, &infop) |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
136 |
|
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
137 |
if err : |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
138 |
raise Error("pt_image_info", err) |
30 | 139 |
|
78 | 140 |
# return as a struct |
91
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
141 |
return infop[0] |
78 | 142 |
|
143 |
||
30 | 144 |
def status (self) : |
78 | 145 |
""" |
146 |
Return a code describing the status of the underlying cache file. |
|
147 |
||
148 |
CACHE_FRESH - the cache file exists and is up-to-date |
|
149 |
CACHE_NONE - the cache file does not exist |
|
150 |
CACHE_STALE - the cache file exists, but is older than the source image |
|
151 |
CACHE_INCOMPAT - the cache file exists, but is incompatible with this version of the library |
|
152 |
""" |
|
153 |
||
91
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
154 |
cdef int ret |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
155 |
|
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
156 |
with nogil : |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
157 |
ret = pt_image_status(self.image) |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
158 |
|
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
159 |
if ret : |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
160 |
raise Error("pt_image_status", ret) |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
161 |
|
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
162 |
return ret |
87 | 163 |
|
164 |
def open (self) : |
|
165 |
""" |
|
166 |
Open the underlying cache file for reading, if available. |
|
167 |
""" |
|
168 |
||
91
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
169 |
cdef int err |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
170 |
|
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
171 |
with nogil : |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
172 |
err = pt_image_load(self.image) |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
173 |
|
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
174 |
if err : |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
175 |
raise Error("pt_image_load", err) |
78 | 176 |
|
177 |
||
178 |
def update (self, background_color = None) : |
|
179 |
""" |
|
180 |
Update the underlying cache file from the source image. |
|
181 |
||
182 |
background_color - skip consecutive pixels that match this byte pattern in output |
|
183 |
||
184 |
Requires that the Image was opened using OPEN_UPDATE. |
|
185 |
""" |
|
186 |
||
187 |
cdef pt_image_params params |
|
188 |
cdef char *bgcolor |
|
91
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
189 |
cdef int err |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
190 |
|
78 | 191 |
memset(¶ms, 0, sizeof(params)) |
192 |
||
193 |
# params |
|
194 |
if background_color : |
|
195 |
# cast |
|
196 |
bgcolor = <char *>background_color |
|
197 |
||
198 |
if 0 >= len(bgcolor) > 4 : |
|
199 |
raise ValueError("background_color must be a str of between 1 and 4 bytes") |
|
200 |
||
201 |
# decode |
|
202 |
memcpy(params.background_color, bgcolor, len(bgcolor)) |
|
30 | 203 |
|
78 | 204 |
# run update |
91
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
205 |
with nogil : |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
206 |
err = pt_image_update(self.image, ¶ms) |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
207 |
|
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
208 |
if err : |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
209 |
raise Error("pt_image_update", err) |
30 | 210 |
|
78 | 211 |
|
34 | 212 |
def tile_file (self, size_t width, size_t height, size_t x, size_t y, int zoom, object out) : |
83 | 213 |
""" |
214 |
Render a region of the source image as a PNG tile to the given output file. |
|
215 |
||
216 |
width - dimensions of the output tile in px |
|
217 |
height |
|
218 |
x - coordinates in the source file |
|
219 |
y |
|
220 |
zoom - zoom level: out = 2**(-zoom) * in |
|
221 |
out - output file |
|
222 |
||
223 |
Note that the given file object MUST be a *real* stdio FILE*, not a fake Python object. |
|
224 |
""" |
|
225 |
||
30 | 226 |
cdef stdio.FILE *outf |
227 |
cdef pt_tile_info ti |
|
91
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
228 |
cdef int err |
30 | 229 |
|
83 | 230 |
memset(&ti, 0, sizeof(ti)) |
231 |
||
232 |
# convert to FILE |
|
30 | 233 |
if not PyFile_Check(out) : |
234 |
raise TypeError("out: must be a file object") |
|
235 |
||
236 |
outf = PyFile_AsFile(out) |
|
237 |
||
238 |
if not outf : |
|
239 |
raise TypeError("out: must have a FILE*") |
|
83 | 240 |
|
241 |
# pack params |
|
30 | 242 |
ti.width = width |
243 |
ti.height = height |
|
244 |
ti.x = x |
|
245 |
ti.y = y |
|
34 | 246 |
ti.zoom = zoom |
30 | 247 |
|
83 | 248 |
# render |
91
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
249 |
with nogil : |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
250 |
err = pt_image_tile_file(self.image, &ti, outf) |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
251 |
|
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
252 |
if err : |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
253 |
raise Error("pt_image_tile_file", err) |
30 | 254 |
|
78 | 255 |
|
34 | 256 |
def tile_mem (self, size_t width, size_t height, size_t x, size_t y, int zoom) : |
83 | 257 |
""" |
258 |
Render a region of the source image as a PNG tile, and return the PNG data a a string. |
|
259 |
||
260 |
width - dimensions of the output tile in px |
|
261 |
height |
|
262 |
x - coordinates in the source file |
|
263 |
y |
|
264 |
zoom - zoom level: out = 2**(-zoom) * in |
|
265 |
""" |
|
266 |
||
30 | 267 |
cdef pt_tile_info ti |
268 |
cdef char *buf |
|
269 |
cdef size_t len |
|
91
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
270 |
cdef int err |
83 | 271 |
|
272 |
memset(&ti, 0, sizeof(ti)) |
|
273 |
||
274 |
# pack params |
|
30 | 275 |
ti.width = width |
276 |
ti.height = height |
|
277 |
ti.x = x |
|
278 |
ti.y = y |
|
34 | 279 |
ti.zoom = zoom |
30 | 280 |
|
83 | 281 |
# render and return via buf/len |
91
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
282 |
with nogil : |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
283 |
err = pt_image_tile_mem(self.image, &ti, &buf, &len) |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
284 |
|
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
285 |
if err : |
0bf7878bdf5c
document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents:
87
diff
changeset
|
286 |
raise Error("pt_image_tile_mem", err) |
30 | 287 |
|
288 |
# copy buffer as str... |
|
289 |
data = python_string.PyString_FromStringAndSize(buf, len) |
|
290 |
||
291 |
# drop buffer... |
|
292 |
stdlib.free(buf) |
|
293 |
||
294 |
return data |
|
295 |
||
78 | 296 |
# release the pt_image |
30 | 297 |
def __dealloc__ (self) : |
298 |
if self.image : |
|
299 |
pt_image_destroy(self.image) |
|
300 |
||
83 | 301 |
self.image = NULL |
302 |