author | Tero Marttila <terom@paivola.fi> |
Sun, 14 Sep 2014 17:19:28 +0300 | |
changeset 134 | 08a0056f6175 |
parent 133 | 67f956b71bdf |
child 170 | c756bbcbc102 |
permissions | -rw-r--r-- |
133
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
1 |
from libc.errno cimport ( |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
2 |
errno, |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
3 |
) |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
4 |
from libc.string cimport ( |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
5 |
strerror, |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
6 |
memset, |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
7 |
memcpy, |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
8 |
) |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
9 |
from libc.stdio cimport ( |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
10 |
FILE, |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
11 |
) |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
12 |
from libc.stdlib cimport ( |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
13 |
free, |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
14 |
) |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
15 |
from cpython.string cimport ( |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
16 |
PyString_FromStringAndSize, |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
17 |
) |
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
18 |
from pypngtile cimport * |
30 | 19 |
|
20 |
cdef extern from "Python.h" : |
|
21 |
int PyFile_Check (object p) |
|
133
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
22 |
FILE* PyFile_AsFile (object p) |
30 | 23 |
|
78 | 24 |
## constants |
25 |
# Image() |
|
26 |
OPEN_READ = PT_OPEN_READ |
|
27 |
OPEN_UPDATE = PT_OPEN_UPDATE |
|
28 |
||
29 |
# Image.status -> ... |
|
30 |
CACHE_FRESH = PT_CACHE_FRESH |
|
31 |
CACHE_NONE = PT_CACHE_NONE |
|
32 |
CACHE_STALE = PT_CACHE_STALE |
|
33 |
CACHE_INCOMPAT = PT_CACHE_INCOMPAT |
|
30 | 34 |
|
134
08a0056f6175
pypngtile: use Exception as base class, not BaseException
Tero Marttila <terom@paivola.fi>
parents:
133
diff
changeset
|
35 |
class Error (Exception) : |
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
|
36 |
""" |
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
|
37 |
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
|
38 |
""" |
30 | 39 |
|
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
|
40 |
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
|
41 |
super(Error, self).__init__("%s: %s: %s" % (func, pt_strerror(err), strerror(errno))) |
30 | 42 |
|
43 |
cdef class Image : |
|
78 | 44 |
""" |
45 |
An image file on disk (.png) and an associated .cache file. |
|
46 |
||
47 |
Open the .png file at the given path using the given mode. |
|
48 |
||
49 |
path - filesystem path to .png file |
|
50 |
mode - mode to operate cache in |
|
51 |
OPEN_READ - read-only access to cache |
|
52 |
OPEN_UPDATE - allow .update() |
|
53 |
""" |
|
54 |
||
30 | 55 |
cdef pt_image *image |
56 |
||
104
b5ae988c78b8
add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents:
91
diff
changeset
|
57 |
# XXX: should really be a pt_image property... |
b5ae988c78b8
add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents:
91
diff
changeset
|
58 |
cdef readonly object path |
b5ae988c78b8
add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents:
91
diff
changeset
|
59 |
|
78 | 60 |
|
61 |
# open the pt_image |
|
62 |
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
|
63 |
cdef int err |
104
b5ae988c78b8
add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents:
91
diff
changeset
|
64 |
|
b5ae988c78b8
add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents:
91
diff
changeset
|
65 |
# store |
b5ae988c78b8
add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents:
91
diff
changeset
|
66 |
self.path = path |
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
|
67 |
|
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
|
68 |
# 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
|
69 |
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
|
70 |
# 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
|
71 |
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
|
72 |
|
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
|
73 |
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
|
74 |
raise Error("pt_image_open", err) |
78 | 75 |
|
76 |
||
30 | 77 |
def info (self) : |
78 | 78 |
""" |
79 |
Return a dictionary containing various information about the image. |
|
80 |
||
81 |
img_width - pixel dimensions of the source image |
|
82 |
img_height only available if the cache was opened |
|
83 |
img_bpp - bits per pixel for the source image |
|
84 |
||
85 |
image_mtime - last modification timestamp for source image |
|
86 |
image_bytes - size of source image file in bytes |
|
87 |
||
88 |
cache_version - version of cache file available |
|
89 |
cache_mtime - last modification timestamp for cache file |
|
90 |
cache_bytes - size of cache file in bytes |
|
91 |
cache_blocks - size of cache file in disk blocks - 512 bytes / block |
|
92 |
""" |
|
93 |
||
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
|
94 |
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
|
95 |
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
|
96 |
|
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
|
97 |
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
|
98 |
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
|
99 |
|
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
|
100 |
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
|
101 |
raise Error("pt_image_info", err) |
30 | 102 |
|
78 | 103 |
# 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
|
104 |
return infop[0] |
78 | 105 |
|
106 |
||
30 | 107 |
def status (self) : |
78 | 108 |
""" |
109 |
Return a code describing the status of the underlying cache file. |
|
110 |
||
111 |
CACHE_FRESH - the cache file exists and is up-to-date |
|
112 |
CACHE_NONE - the cache file does not exist |
|
113 |
CACHE_STALE - the cache file exists, but is older than the source image |
|
114 |
CACHE_INCOMPAT - the cache file exists, but is incompatible with this version of the library |
|
115 |
""" |
|
116 |
||
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
|
117 |
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
|
118 |
|
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
|
119 |
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
|
120 |
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
|
121 |
|
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
|
122 |
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
|
123 |
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
|
124 |
|
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
|
125 |
return ret |
87 | 126 |
|
127 |
def open (self) : |
|
128 |
""" |
|
129 |
Open the underlying cache file for reading, if available. |
|
130 |
""" |
|
131 |
||
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
|
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_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
|
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_load", err) |
78 | 139 |
|
140 |
||
141 |
def update (self, background_color = None) : |
|
142 |
""" |
|
143 |
Update the underlying cache file from the source image. |
|
144 |
||
145 |
background_color - skip consecutive pixels that match this byte pattern in output |
|
146 |
||
147 |
Requires that the Image was opened using OPEN_UPDATE. |
|
148 |
""" |
|
149 |
||
150 |
cdef pt_image_params params |
|
151 |
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
|
152 |
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
|
153 |
|
78 | 154 |
memset(¶ms, 0, sizeof(params)) |
155 |
||
156 |
# params |
|
157 |
if background_color : |
|
158 |
# cast |
|
159 |
bgcolor = <char *>background_color |
|
160 |
||
161 |
if 0 >= len(bgcolor) > 4 : |
|
162 |
raise ValueError("background_color must be a str of between 1 and 4 bytes") |
|
163 |
||
164 |
# decode |
|
165 |
memcpy(params.background_color, bgcolor, len(bgcolor)) |
|
30 | 166 |
|
78 | 167 |
# 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
|
168 |
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
|
169 |
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
|
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 |
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
|
172 |
raise Error("pt_image_update", err) |
30 | 173 |
|
78 | 174 |
|
34 | 175 |
def tile_file (self, size_t width, size_t height, size_t x, size_t y, int zoom, object out) : |
83 | 176 |
""" |
177 |
Render a region of the source image as a PNG tile to the given output file. |
|
178 |
||
179 |
width - dimensions of the output tile in px |
|
180 |
height |
|
181 |
x - coordinates in the source file |
|
182 |
y |
|
183 |
zoom - zoom level: out = 2**(-zoom) * in |
|
184 |
out - output file |
|
185 |
||
133
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
186 |
Note that the given file object MUST be a *real* FILE*, not a fake Python object. |
83 | 187 |
""" |
188 |
||
133
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
189 |
cdef FILE *outf |
30 | 190 |
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
|
191 |
cdef int err |
30 | 192 |
|
83 | 193 |
memset(&ti, 0, sizeof(ti)) |
194 |
||
195 |
# convert to FILE |
|
30 | 196 |
if not PyFile_Check(out) : |
197 |
raise TypeError("out: must be a file object") |
|
198 |
||
199 |
outf = PyFile_AsFile(out) |
|
200 |
||
201 |
if not outf : |
|
202 |
raise TypeError("out: must have a FILE*") |
|
83 | 203 |
|
204 |
# pack params |
|
30 | 205 |
ti.width = width |
206 |
ti.height = height |
|
207 |
ti.x = x |
|
208 |
ti.y = y |
|
34 | 209 |
ti.zoom = zoom |
30 | 210 |
|
83 | 211 |
# 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
|
212 |
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
|
213 |
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
|
214 |
|
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
|
215 |
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
|
216 |
raise Error("pt_image_tile_file", err) |
30 | 217 |
|
78 | 218 |
|
34 | 219 |
def tile_mem (self, size_t width, size_t height, size_t x, size_t y, int zoom) : |
83 | 220 |
""" |
221 |
Render a region of the source image as a PNG tile, and return the PNG data a a string. |
|
222 |
||
223 |
width - dimensions of the output tile in px |
|
224 |
height |
|
225 |
x - coordinates in the source file |
|
226 |
y |
|
227 |
zoom - zoom level: out = 2**(-zoom) * in |
|
228 |
""" |
|
229 |
||
30 | 230 |
cdef pt_tile_info ti |
231 |
cdef char *buf |
|
232 |
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
|
233 |
cdef int err |
83 | 234 |
|
235 |
memset(&ti, 0, sizeof(ti)) |
|
236 |
||
237 |
# pack params |
|
30 | 238 |
ti.width = width |
239 |
ti.height = height |
|
240 |
ti.x = x |
|
241 |
ti.y = y |
|
34 | 242 |
ti.zoom = zoom |
30 | 243 |
|
83 | 244 |
# 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
|
245 |
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
|
246 |
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
|
247 |
|
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
|
248 |
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
|
249 |
raise Error("pt_image_tile_mem", err) |
30 | 250 |
|
251 |
# copy buffer as str... |
|
133
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
252 |
data = PyString_FromStringAndSize(buf, len) |
30 | 253 |
|
254 |
# drop buffer... |
|
133
67f956b71bdf
python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents:
104
diff
changeset
|
255 |
free(buf) |
30 | 256 |
|
257 |
return data |
|
258 |
||
78 | 259 |
# release the pt_image |
30 | 260 |
def __dealloc__ (self) : |
261 |
if self.image : |
|
262 |
pt_image_destroy(self.image) |
|
263 |
||
83 | 264 |
self.image = NULL |
265 |