|
1 cdef extern from "stdio.h" : |
|
2 struct FILE : |
|
3 pass |
|
4 |
|
5 cdef extern from "Python.h" : |
|
6 int PyFile_Check (object p) |
|
7 FILE* PyFile_AsFile (object p) |
|
8 void PyFile_IncUseCount (object p) |
|
9 void PyFile_DecUseCount (object p) |
|
10 |
|
11 cdef extern from "pngtile.h" : |
|
12 struct pt_ctx : |
|
13 pass |
|
14 |
|
15 struct pt_image : |
|
16 pass |
|
17 |
|
18 enum pt_open_mode : |
|
19 PT_OPEN_UPDATE |
|
20 |
|
21 enum pt_cache_status : |
|
22 PT_CACHE_ERROR |
|
23 PT_CACHE_FRESH |
|
24 PT_CACHE_NONE |
|
25 PT_CACHE_STALE |
|
26 |
|
27 struct pt_image_info : |
|
28 size_t width, height |
|
29 |
|
30 struct pt_tile_info : |
|
31 size_t width, height |
|
32 size_t x, y |
|
33 |
|
34 int pt_image_open (pt_image **image_ptr, pt_ctx *ctx, char *png_path, int cache_mode) |
|
35 int pt_image_info_func "pt_image_info" (pt_image *image, pt_image_info **info_ptr) |
|
36 int pt_image_status (pt_image *image) |
|
37 int pt_image_tile (pt_image *image, pt_tile_info *info, FILE *out) |
|
38 void pt_image_destroy (pt_image *image) |
|
39 |
|
40 OPEN_UPDATE = PT_OPEN_UPDATE |
|
41 CACHE_ERROR = PT_CACHE_ERROR |
|
42 CACHE_FRESH = PT_CACHE_FRESH |
|
43 CACHE_NONE = PT_CACHE_NONE |
|
44 CACHE_STALE = PT_CACHE_STALE |
|
45 |
|
46 class Error (BaseException) : |
|
47 pass |
|
48 |
|
49 cdef class Image : |
|
50 cdef pt_image *image |
|
51 |
|
52 def __cinit__ (self, char *png_path, int cache_mode = 0) : |
|
53 if pt_image_open(&self.image, NULL, png_path, cache_mode) < 0 : |
|
54 raise Error("pt_image_open: " + png_path) |
|
55 |
|
56 def info (self) : |
|
57 cdef pt_image_info *image_info |
|
58 |
|
59 if pt_image_info_func(self.image, &image_info) < 0 : |
|
60 raise Error("pt_image_info") |
|
61 |
|
62 return (image_info.width, image_info.height) |
|
63 |
|
64 def status (self) : |
|
65 cdef int status = pt_image_status(self.image) |
|
66 |
|
67 if status < 0 : |
|
68 raise Error("pt_image_status") |
|
69 |
|
70 else : |
|
71 return status |
|
72 |
|
73 def tile (self, size_t width, size_t height, size_t x, size_t y, object out) : |
|
74 cdef FILE *outf |
|
75 cdef pt_tile_info ti |
|
76 |
|
77 if not PyFile_Check(out) : |
|
78 raise TypeError("out: must be a file object") |
|
79 |
|
80 outf = PyFile_AsFile(out) |
|
81 |
|
82 if not outf : |
|
83 raise TypeError("out: must have a FILE*") |
|
84 |
|
85 ti.width = width |
|
86 ti.height = height |
|
87 ti.x = x |
|
88 ti.y = y |
|
89 |
|
90 if pt_image_tile(self.image, &ti, outf) < 0 : |
|
91 raise Error("pt_image_tile") |
|
92 |
|
93 def __dealloc__ (self) : |
|
94 if self.image : |
|
95 pt_image_destroy(self.image) |
|
96 |