python/pypngtile.pyx
changeset 83 a1e8fa84a9fb
parent 78 a3aaf5c23454
child 87 ecd87b41e884
equal deleted inserted replaced
82:c65afb0ce169 83:a1e8fa84a9fb
    41         size_t image_bytes, cache_bytes
    41         size_t image_bytes, cache_bytes
    42         size_t cache_blocks
    42         size_t cache_blocks
    43 
    43 
    44     struct pt_image_params :
    44     struct pt_image_params :
    45         int background_color[4]
    45         int background_color[4]
    46 
    46     
    47     struct pt_tile_info :
    47     struct pt_tile_info :
    48         size_t width, height
    48         size_t width, height
    49         size_t x, y
    49         size_t x, y
    50         int zoom
    50         int zoom
    51 
    51     
       
    52     ## functions
    52     int pt_image_open (pt_image **image_ptr, pt_ctx *ctx, char *png_path, int cache_mode)
    53     int pt_image_open (pt_image **image_ptr, pt_ctx *ctx, char *png_path, int cache_mode)
    53     int pt_image_info_func "pt_image_info" (pt_image *image, pt_image_info **info_ptr)
    54     int pt_image_info_ "pt_image_info" (pt_image *image, pt_image_info **info_ptr)
    54     int pt_image_status (pt_image *image)
    55     int pt_image_status (pt_image *image)
    55     int pt_image_update (pt_image *image, pt_image_params *params)
    56     int pt_image_update (pt_image *image, pt_image_params *params)
    56     int pt_image_tile_file (pt_image *image, pt_tile_info *info, stdio.FILE *out)
    57     int pt_image_tile_file (pt_image *image, pt_tile_info *info, stdio.FILE *out)
    57     int pt_image_tile_mem (pt_image *image, pt_tile_info *info, char **buf_ptr, size_t *len_ptr)
    58     int pt_image_tile_mem (pt_image *image, pt_tile_info *info, char **buf_ptr, size_t *len_ptr)
    58     void pt_image_destroy (pt_image *image)
    59     void pt_image_destroy (pt_image *image)
    59 
    60     
       
    61     # error code -> name
    60     char* pt_strerror (int err)
    62     char* pt_strerror (int err)
    61 
    63 
    62 ## constants
    64 ## constants
    63 # Image()
    65 # Image()
    64 OPEN_READ       = PT_OPEN_READ
    66 OPEN_READ       = PT_OPEN_READ
    71 CACHE_INCOMPAT  = PT_CACHE_INCOMPAT
    73 CACHE_INCOMPAT  = PT_CACHE_INCOMPAT
    72 
    74 
    73 class Error (BaseException) :
    75 class Error (BaseException) :
    74     pass
    76     pass
    75 
    77 
       
    78 # raise Error if the given return value is <0
    76 cdef int trap_err (char *op, int ret) except -1 :
    79 cdef int trap_err (char *op, int ret) except -1 :
    77     if ret < 0 :
    80     if ret < 0 :
    78         raise Error("%s: %s: %s" % (op, pt_strerror(ret), strerror(errno)))
    81         raise Error("%s: %s: %s" % (op, pt_strerror(ret), strerror(errno)))
    79 
    82 
    80     else :
    83     else :
   120         """
   123         """
   121 
   124 
   122         cdef pt_image_info *info
   125         cdef pt_image_info *info
   123         
   126         
   124         trap_err("pt_image_info",
   127         trap_err("pt_image_info",
   125             pt_image_info_func(self.image, &info)
   128             pt_image_info_(self.image, &info)
   126         )
   129         )
   127 
   130 
   128         # return as a struct
   131         # return as a struct
   129         return info[0]
   132         return info[0]
   130 
   133 
   173             pt_image_update(self.image, &params)
   176             pt_image_update(self.image, &params)
   174         )
   177         )
   175 
   178 
   176 
   179 
   177     def tile_file (self, size_t width, size_t height, size_t x, size_t y, int zoom, object out) :
   180     def tile_file (self, size_t width, size_t height, size_t x, size_t y, int zoom, object out) :
       
   181         """
       
   182             Render a region of the source image as a PNG tile to the given output file.
       
   183 
       
   184             width       - dimensions of the output tile in px
       
   185             height      
       
   186             x           - coordinates in the source file
       
   187             y
       
   188             zoom        - zoom level: out = 2**(-zoom) * in
       
   189             out         - output file
       
   190 
       
   191             Note that the given file object MUST be a *real* stdio FILE*, not a fake Python object.
       
   192         """
       
   193 
   178         cdef stdio.FILE *outf
   194         cdef stdio.FILE *outf
   179         cdef pt_tile_info ti
   195         cdef pt_tile_info ti
   180 
   196 
       
   197         memset(&ti, 0, sizeof(ti))
       
   198         
       
   199         # convert to FILE
   181         if not PyFile_Check(out) :
   200         if not PyFile_Check(out) :
   182             raise TypeError("out: must be a file object")
   201             raise TypeError("out: must be a file object")
   183 
   202 
   184         outf = PyFile_AsFile(out)
   203         outf = PyFile_AsFile(out)
   185 
   204 
   186         if not outf :
   205         if not outf :
   187             raise TypeError("out: must have a FILE*")
   206             raise TypeError("out: must have a FILE*")
   188     
   207         
       
   208         # pack params
   189         ti.width = width
   209         ti.width = width
   190         ti.height = height
   210         ti.height = height
   191         ti.x = x
   211         ti.x = x
   192         ti.y = y
   212         ti.y = y
   193         ti.zoom = zoom
   213         ti.zoom = zoom
   194         
   214         
       
   215         # render
   195         trap_err("pt_image_tile_file", 
   216         trap_err("pt_image_tile_file", 
   196             pt_image_tile_file(self.image, &ti, outf)
   217             pt_image_tile_file(self.image, &ti, outf)
   197         )
   218         )
   198 
   219 
   199 
   220 
   200     def tile_mem (self, size_t width, size_t height, size_t x, size_t y, int zoom) :
   221     def tile_mem (self, size_t width, size_t height, size_t x, size_t y, int zoom) :
       
   222         """
       
   223             Render a region of the source image as a PNG tile, and return the PNG data a a string.
       
   224 
       
   225             width       - dimensions of the output tile in px
       
   226             height      
       
   227             x           - coordinates in the source file
       
   228             y
       
   229             zoom        - zoom level: out = 2**(-zoom) * in
       
   230         """
       
   231 
   201         cdef pt_tile_info ti
   232         cdef pt_tile_info ti
   202         cdef char *buf
   233         cdef char *buf
   203         cdef size_t len
   234         cdef size_t len
   204 
   235         
       
   236         memset(&ti, 0, sizeof(ti))
       
   237         
       
   238         # pack params
   205         ti.width = width
   239         ti.width = width
   206         ti.height = height
   240         ti.height = height
   207         ti.x = x
   241         ti.x = x
   208         ti.y = y
   242         ti.y = y
   209         ti.zoom = zoom
   243         ti.zoom = zoom
   210         
   244         
   211         # render and return ptr to buffer
   245         # render and return via buf/len
   212         trap_err("pt_image_tile_mem", 
   246         trap_err("pt_image_tile_mem", 
   213             pt_image_tile_mem(self.image, &ti, &buf, &len)
   247             pt_image_tile_mem(self.image, &ti, &buf, &len)
   214         )
   248         )
   215         
   249         
   216         # copy buffer as str...
   250         # copy buffer as str...
   224     # release the pt_image
   258     # release the pt_image
   225     def __dealloc__ (self) :
   259     def __dealloc__ (self) :
   226         if self.image :
   260         if self.image :
   227             pt_image_destroy(self.image)
   261             pt_image_destroy(self.image)
   228 
   262 
       
   263             self.image = NULL
       
   264