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