python/pypngtile.pyx
changeset 133 67f956b71bdf
parent 104 b5ae988c78b8
child 134 08a0056f6175
equal deleted inserted replaced
132:0260aeca943c 133:67f956b71bdf
     1 cdef extern from "errno.h" :
     1 from libc.errno cimport (
     2     extern int errno
     2         errno,
     3 
     3 )
     4 cdef extern from "string.h" :
     4 from libc.string cimport (
     5     char* strerror (int err)
     5         strerror,
     6 
     6         memset,
     7     void* memset (void *, int, size_t)
     7         memcpy,
     8     void* memcpy (void *, void *, size_t)
     8 )
     9 
     9 from libc.stdio cimport (
    10 cimport stdio
    10         FILE,
    11 cimport stdlib
    11 )
    12 cimport python_string
    12 from libc.stdlib cimport (
       
    13         free,
       
    14 )
       
    15 from cpython.string cimport (
       
    16     PyString_FromStringAndSize,
       
    17 )
       
    18 from pypngtile cimport *
    13 
    19 
    14 cdef extern from "Python.h" :
    20 cdef extern from "Python.h" :
    15     int PyFile_Check (object p)
    21     int PyFile_Check (object p)
    16     stdio.FILE* PyFile_AsFile (object p)
    22     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 :
       
    28         PT_OPEN_READ    # 0
       
    29         PT_OPEN_UPDATE
       
    30 
       
    31     enum pt_cache_status :
       
    32         PT_CACHE_ERROR  # -1
       
    33         PT_CACHE_FRESH
       
    34         PT_CACHE_NONE
       
    35         PT_CACHE_STALE
       
    36         PT_CACHE_INCOMPAT
       
    37 
       
    38     struct pt_image_info :
       
    39         size_t img_width, img_height, img_bpp
       
    40         int image_mtime, cache_mtime, cache_version
       
    41         size_t image_bytes, cache_bytes
       
    42         size_t cache_blocks
       
    43 
       
    44     struct pt_image_params :
       
    45         int background_color[4]
       
    46     
       
    47     struct pt_tile_info :
       
    48         size_t width, height
       
    49         size_t x, y
       
    50         int zoom
       
    51 
       
    52     ctypedef pt_image_info* const_image_info_ptr "const struct pt_image_info *"
       
    53     
       
    54     ## functions
       
    55     int pt_image_open (pt_image **image_ptr, pt_ctx *ctx, char *png_path, int cache_mode) nogil
       
    56     int pt_image_info_ "pt_image_info" (pt_image *image, pt_image_info **info_ptr) nogil
       
    57     int pt_image_status (pt_image *image) nogil
       
    58     int pt_image_load (pt_image *image) nogil
       
    59     int pt_image_update (pt_image *image, pt_image_params *params) nogil
       
    60     int pt_image_tile_file (pt_image *image, pt_tile_info *info, stdio.FILE *out) nogil
       
    61     int pt_image_tile_mem (pt_image *image, pt_tile_info *info, char **buf_ptr, size_t *len_ptr) nogil
       
    62     void pt_image_destroy (pt_image *image) nogil
       
    63     
       
    64     # error code -> name
       
    65     char* pt_strerror (int err)
       
    66 
    23 
    67 ## constants
    24 ## constants
    68 # Image()
    25 # Image()
    69 OPEN_READ       = PT_OPEN_READ
    26 OPEN_READ       = PT_OPEN_READ
    70 OPEN_UPDATE     = PT_OPEN_UPDATE
    27 OPEN_UPDATE     = PT_OPEN_UPDATE
   224             x           - coordinates in the source file
   181             x           - coordinates in the source file
   225             y
   182             y
   226             zoom        - zoom level: out = 2**(-zoom) * in
   183             zoom        - zoom level: out = 2**(-zoom) * in
   227             out         - output file
   184             out         - output file
   228 
   185 
   229             Note that the given file object MUST be a *real* stdio FILE*, not a fake Python object.
   186             Note that the given file object MUST be a *real* FILE*, not a fake Python object.
   230         """
   187         """
   231 
   188 
   232         cdef stdio.FILE *outf
   189         cdef FILE *outf
   233         cdef pt_tile_info ti
   190         cdef pt_tile_info ti
   234         cdef int err
   191         cdef int err
   235 
   192 
   236         memset(&ti, 0, sizeof(ti))
   193         memset(&ti, 0, sizeof(ti))
   237         
   194         
   290 
   247 
   291         if err :
   248         if err :
   292             raise Error("pt_image_tile_mem", err)
   249             raise Error("pt_image_tile_mem", err)
   293         
   250         
   294         # copy buffer as str...
   251         # copy buffer as str...
   295         data = python_string.PyString_FromStringAndSize(buf, len)
   252         data = PyString_FromStringAndSize(buf, len)
   296 
   253 
   297         # drop buffer...
   254         # drop buffer...
   298         stdlib.free(buf)
   255         free(buf)
   299 
   256 
   300         return data
   257         return data
   301 
   258 
   302     # release the pt_image
   259     # release the pt_image
   303     def __dealloc__ (self) :
   260     def __dealloc__ (self) :