author | Tero Marttila <terom@fixme.fi> |
Tue, 26 Jan 2010 01:37:01 +0200 | |
changeset 104 | b5ae988c78b8 |
parent 91 | 0bf7878bdf5c |
child 133 | 67f956b71bdf |
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 |
||
104
b5ae988c78b8
add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents:
91
diff
changeset
|
100 |
# XXX: should really be a pt_image property... |
b5ae988c78b8
add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents:
91
diff
changeset
|
101 |
cdef readonly object path |
b5ae988c78b8
add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents:
91
diff
changeset
|
102 |
|
78 | 103 |
|
104 |
# open the pt_image |
|
105 |
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
|
106 |
cdef int err |
104
b5ae988c78b8
add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents:
91
diff
changeset
|
107 |
|
b5ae988c78b8
add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents:
91
diff
changeset
|
108 |
# store |
b5ae988c78b8
add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents:
91
diff
changeset
|
109 |
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
|
110 |
|
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 |
# 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
|
112 |
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
|
113 |
# 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
|
114 |
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
|
115 |
|
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
|
116 |
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
|
117 |
raise Error("pt_image_open", err) |
78 | 118 |
|
119 |
||
30 | 120 |
def info (self) : |
78 | 121 |
""" |
122 |
Return a dictionary containing various information about the image. |
|
123 |
||
124 |
img_width - pixel dimensions of the source image |
|
125 |
img_height only available if the cache was opened |
|
126 |
img_bpp - bits per pixel for the source image |
|
127 |
||
128 |
image_mtime - last modification timestamp for source image |
|
129 |
image_bytes - size of source image file in bytes |
|
130 |
||
131 |
cache_version - version of cache file available |
|
132 |
cache_mtime - last modification timestamp for cache file |
|
133 |
cache_bytes - size of cache file in bytes |
|
134 |
cache_blocks - size of cache file in disk blocks - 512 bytes / block |
|
135 |
""" |
|
136 |
||
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
|
137 |
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
|
138 |
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
|
139 |
|
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
|
140 |
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
|
141 |
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
|
142 |
|
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 |
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
|
144 |
raise Error("pt_image_info", err) |
30 | 145 |
|
78 | 146 |
# 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
|
147 |
return infop[0] |
78 | 148 |
|
149 |
||
30 | 150 |
def status (self) : |
78 | 151 |
""" |
152 |
Return a code describing the status of the underlying cache file. |
|
153 |
||
154 |
CACHE_FRESH - the cache file exists and is up-to-date |
|
155 |
CACHE_NONE - the cache file does not exist |
|
156 |
CACHE_STALE - the cache file exists, but is older than the source image |
|
157 |
CACHE_INCOMPAT - the cache file exists, but is incompatible with this version of the library |
|
158 |
""" |
|
159 |
||
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
|
160 |
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
|
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 |
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
|
163 |
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
|
164 |
|
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
|
165 |
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
|
166 |
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
|
167 |
|
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 |
return ret |
87 | 169 |
|
170 |
def open (self) : |
|
171 |
""" |
|
172 |
Open the underlying cache file for reading, if available. |
|
173 |
""" |
|
174 |
||
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
|
175 |
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
|
176 |
|
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
|
177 |
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
|
178 |
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
|
179 |
|
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 |
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
|
181 |
raise Error("pt_image_load", err) |
78 | 182 |
|
183 |
||
184 |
def update (self, background_color = None) : |
|
185 |
""" |
|
186 |
Update the underlying cache file from the source image. |
|
187 |
||
188 |
background_color - skip consecutive pixels that match this byte pattern in output |
|
189 |
||
190 |
Requires that the Image was opened using OPEN_UPDATE. |
|
191 |
""" |
|
192 |
||
193 |
cdef pt_image_params params |
|
194 |
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
|
195 |
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
|
196 |
|
78 | 197 |
memset(¶ms, 0, sizeof(params)) |
198 |
||
199 |
# params |
|
200 |
if background_color : |
|
201 |
# cast |
|
202 |
bgcolor = <char *>background_color |
|
203 |
||
204 |
if 0 >= len(bgcolor) > 4 : |
|
205 |
raise ValueError("background_color must be a str of between 1 and 4 bytes") |
|
206 |
||
207 |
# decode |
|
208 |
memcpy(params.background_color, bgcolor, len(bgcolor)) |
|
30 | 209 |
|
78 | 210 |
# 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
|
211 |
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
|
212 |
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
|
213 |
|
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 |
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
|
215 |
raise Error("pt_image_update", err) |
30 | 216 |
|
78 | 217 |
|
34 | 218 |
def tile_file (self, size_t width, size_t height, size_t x, size_t y, int zoom, object out) : |
83 | 219 |
""" |
220 |
Render a region of the source image as a PNG tile to the given output file. |
|
221 |
||
222 |
width - dimensions of the output tile in px |
|
223 |
height |
|
224 |
x - coordinates in the source file |
|
225 |
y |
|
226 |
zoom - zoom level: out = 2**(-zoom) * in |
|
227 |
out - output file |
|
228 |
||
229 |
Note that the given file object MUST be a *real* stdio FILE*, not a fake Python object. |
|
230 |
""" |
|
231 |
||
30 | 232 |
cdef stdio.FILE *outf |
233 |
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
|
234 |
cdef int err |
30 | 235 |
|
83 | 236 |
memset(&ti, 0, sizeof(ti)) |
237 |
||
238 |
# convert to FILE |
|
30 | 239 |
if not PyFile_Check(out) : |
240 |
raise TypeError("out: must be a file object") |
|
241 |
||
242 |
outf = PyFile_AsFile(out) |
|
243 |
||
244 |
if not outf : |
|
245 |
raise TypeError("out: must have a FILE*") |
|
83 | 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 |
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_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
|
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_file", err) |
30 | 260 |
|
78 | 261 |
|
34 | 262 |
def tile_mem (self, size_t width, size_t height, size_t x, size_t y, int zoom) : |
83 | 263 |
""" |
264 |
Render a region of the source image as a PNG tile, and return the PNG data a a string. |
|
265 |
||
266 |
width - dimensions of the output tile in px |
|
267 |
height |
|
268 |
x - coordinates in the source file |
|
269 |
y |
|
270 |
zoom - zoom level: out = 2**(-zoom) * in |
|
271 |
""" |
|
272 |
||
30 | 273 |
cdef pt_tile_info ti |
274 |
cdef char *buf |
|
275 |
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
|
276 |
cdef int err |
83 | 277 |
|
278 |
memset(&ti, 0, sizeof(ti)) |
|
279 |
||
280 |
# pack params |
|
30 | 281 |
ti.width = width |
282 |
ti.height = height |
|
283 |
ti.x = x |
|
284 |
ti.y = y |
|
34 | 285 |
ti.zoom = zoom |
30 | 286 |
|
83 | 287 |
# 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
|
288 |
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
|
289 |
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
|
290 |
|
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
|
291 |
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
|
292 |
raise Error("pt_image_tile_mem", err) |
30 | 293 |
|
294 |
# copy buffer as str... |
|
295 |
data = python_string.PyString_FromStringAndSize(buf, len) |
|
296 |
||
297 |
# drop buffer... |
|
298 |
stdlib.free(buf) |
|
299 |
||
300 |
return data |
|
301 |
||
78 | 302 |
# release the pt_image |
30 | 303 |
def __dealloc__ (self) : |
304 |
if self.image : |
|
305 |
pt_image_destroy(self.image) |
|
306 |
||
83 | 307 |
self.image = NULL |
308 |