python/pngtile.pyx
changeset 17 baf3fe7c6354
parent 16 6e781cf3d459
child 18 f92a24ab046e
equal deleted inserted replaced
16:6e781cf3d459 17:baf3fe7c6354
     1 cdef extern from "stdio.h" :
     1 cdef extern from "stdio.h" :
     2     struct FILE :
     2     struct FILE :
     3         pass
     3         pass
       
     4 
       
     5 cdef extern from "errno.h" :
       
     6     extern int errno
       
     7 
       
     8 cdef extern from "string.h" :
       
     9     char* strerror (int err)
     4 
    10 
     5 cdef extern from "Python.h" :
    11 cdef extern from "Python.h" :
     6     int PyFile_Check (object p)
    12     int PyFile_Check (object p)
     7     FILE* PyFile_AsFile (object p)
    13     FILE* PyFile_AsFile (object p)
     8     void PyFile_IncUseCount (object p)
    14     void PyFile_IncUseCount (object p)
    32         size_t x, y
    38         size_t x, y
    33         
    39         
    34     int pt_image_open (pt_image **image_ptr, pt_ctx *ctx, char *png_path, int cache_mode)
    40     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)
    41     int pt_image_info_func "pt_image_info" (pt_image *image, pt_image_info **info_ptr)
    36     int pt_image_status (pt_image *image)
    42     int pt_image_status (pt_image *image)
       
    43     int pt_image_update (pt_image *image)
    37     int pt_image_tile (pt_image *image, pt_tile_info *info, FILE *out)
    44     int pt_image_tile (pt_image *image, pt_tile_info *info, FILE *out)
    38     void pt_image_destroy (pt_image *image)
    45     void pt_image_destroy (pt_image *image)
       
    46 
       
    47     char* pt_strerror (int err)
    39 
    48 
    40 OPEN_UPDATE = PT_OPEN_UPDATE
    49 OPEN_UPDATE = PT_OPEN_UPDATE
    41 CACHE_ERROR = PT_CACHE_ERROR
    50 CACHE_ERROR = PT_CACHE_ERROR
    42 CACHE_FRESH = PT_CACHE_FRESH
    51 CACHE_FRESH = PT_CACHE_FRESH
    43 CACHE_NONE = PT_CACHE_NONE
    52 CACHE_NONE = PT_CACHE_NONE
    44 CACHE_STALE = PT_CACHE_STALE
    53 CACHE_STALE = PT_CACHE_STALE
    45 
    54 
    46 class Error (BaseException) :
    55 class Error (BaseException) :
    47     pass
    56     pass
    48 
    57 
       
    58 cdef int trap_err (char *op, int ret) except -1 :
       
    59     if ret < 0 :
       
    60         raise Error("%s: %s: %s" % (op, pt_strerror(ret), strerror(errno)))
       
    61 
       
    62     else :
       
    63         return ret
       
    64 
    49 cdef class Image :
    65 cdef class Image :
    50     cdef pt_image *image
    66     cdef pt_image *image
    51 
    67 
    52     def __cinit__ (self, char *png_path, int cache_mode = 0) :
    68     def __cinit__ (self, char *png_path, int cache_mode = 0) :
    53         if pt_image_open(&self.image, NULL, png_path, cache_mode) < 0 :
    69         trap_err("pt_image_open", 
    54             raise Error("pt_image_open: " + png_path)
    70             pt_image_open(&self.image, NULL, png_path, cache_mode)
       
    71         )
    55     
    72     
    56     def info (self) :
    73     def info (self) :
    57         cdef pt_image_info *image_info
    74         cdef pt_image_info *image_info
    58 
    75         
    59         if pt_image_info_func(self.image, &image_info) < 0 :
    76         trap_err("pt_image_info",
    60             raise Error("pt_image_info")
    77             pt_image_info_func(self.image, &image_info)
       
    78         )
    61 
    79 
    62         return (image_info.width, image_info.height)
    80         return (image_info.width, image_info.height)
    63     
    81     
    64     def status (self) :
    82     def status (self) :
    65         cdef int status = pt_image_status(self.image)
    83         return trap_err("pt_image_status", 
    66         
    84             pt_image_status(self.image)
    67         if status < 0 :
    85         )
    68             raise Error("pt_image_status")
    86     
    69 
    87     def update (self) :
    70         else :
    88         trap_err("pt_image_update", 
    71             return status
    89             pt_image_update(self.image)
       
    90         )
    72 
    91 
    73     def tile (self, size_t width, size_t height, size_t x, size_t y, object out) :
    92     def tile (self, size_t width, size_t height, size_t x, size_t y, object out) :
    74         cdef FILE *outf
    93         cdef FILE *outf
    75         cdef pt_tile_info ti
    94         cdef pt_tile_info ti
    76 
    95 
    85         ti.width = width
   104         ti.width = width
    86         ti.height = height
   105         ti.height = height
    87         ti.x = x
   106         ti.x = x
    88         ti.y = y
   107         ti.y = y
    89         
   108         
    90         if pt_image_tile(self.image, &ti, outf) < 0 :
   109         trap_err("pt_image_tile", 
    91             raise Error("pt_image_tile")
   110             pt_image_tile(self.image, &ti, outf)
       
   111         )
    92 
   112 
    93     def __dealloc__ (self) :
   113     def __dealloc__ (self) :
    94         if self.image :
   114         if self.image :
    95             pt_image_destroy(self.image)
   115             pt_image_destroy(self.image)
    96 
   116