python/pngtile.pyx
changeset 18 f92a24ab046e
parent 17 baf3fe7c6354
equal deleted inserted replaced
17:baf3fe7c6354 18:f92a24ab046e
     1 cdef extern from "stdio.h" :
       
     2     struct FILE :
       
     3         pass
       
     4 
       
     5 cdef extern from "errno.h" :
     1 cdef extern from "errno.h" :
     6     extern int errno
     2     extern int errno
     7 
     3 
     8 cdef extern from "string.h" :
     4 cdef extern from "string.h" :
     9     char* strerror (int err)
     5     char* strerror (int err)
    10 
     6 
       
     7 cimport stdio
       
     8 cimport stdlib
       
     9 cimport python_string
       
    10 
    11 cdef extern from "Python.h" :
    11 cdef extern from "Python.h" :
    12     int PyFile_Check (object p)
    12     int PyFile_Check (object p)
    13     FILE* PyFile_AsFile (object p)
    13     stdio.FILE* PyFile_AsFile (object p)
    14     void PyFile_IncUseCount (object p)
    14     void PyFile_IncUseCount (object p)
    15     void PyFile_DecUseCount (object p)
    15     void PyFile_DecUseCount (object p)
    16 
    16 
    17 cdef extern from "pngtile.h" :
    17 cdef extern from "pngtile.h" :
    18     struct pt_ctx :
    18     struct pt_ctx :
    39         
    39         
    40     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)
    41     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)
    42     int pt_image_status (pt_image *image)
    42     int pt_image_status (pt_image *image)
    43     int pt_image_update (pt_image *image)
    43     int pt_image_update (pt_image *image)
    44     int pt_image_tile (pt_image *image, pt_tile_info *info, FILE *out)
    44     int pt_image_tile_file (pt_image *image, pt_tile_info *info, stdio.FILE *out)
       
    45     int pt_image_tile_mem (pt_image *image, pt_tile_info *info, char **buf_ptr, size_t *len_ptr)
    45     void pt_image_destroy (pt_image *image)
    46     void pt_image_destroy (pt_image *image)
    46 
    47 
    47     char* pt_strerror (int err)
    48     char* pt_strerror (int err)
    48 
    49 
    49 OPEN_UPDATE = PT_OPEN_UPDATE
    50 OPEN_UPDATE = PT_OPEN_UPDATE
    87     def update (self) :
    88     def update (self) :
    88         trap_err("pt_image_update", 
    89         trap_err("pt_image_update", 
    89             pt_image_update(self.image)
    90             pt_image_update(self.image)
    90         )
    91         )
    91 
    92 
    92     def tile (self, size_t width, size_t height, size_t x, size_t y, object out) :
    93     def tile_file (self, size_t width, size_t height, size_t x, size_t y, object out) :
    93         cdef FILE *outf
    94         cdef stdio.FILE *outf
    94         cdef pt_tile_info ti
    95         cdef pt_tile_info ti
    95 
    96 
    96         if not PyFile_Check(out) :
    97         if not PyFile_Check(out) :
    97             raise TypeError("out: must be a file object")
    98             raise TypeError("out: must be a file object")
    98 
    99 
   104         ti.width = width
   105         ti.width = width
   105         ti.height = height
   106         ti.height = height
   106         ti.x = x
   107         ti.x = x
   107         ti.y = y
   108         ti.y = y
   108         
   109         
   109         trap_err("pt_image_tile", 
   110         trap_err("pt_image_tile_file", 
   110             pt_image_tile(self.image, &ti, outf)
   111             pt_image_tile_file(self.image, &ti, outf)
   111         )
   112         )
       
   113 
       
   114     def tile_mem (self, size_t width, size_t height, size_t x, size_t y) :
       
   115         cdef pt_tile_info ti
       
   116         cdef char *buf
       
   117         cdef size_t len
       
   118 
       
   119         ti.width = width
       
   120         ti.height = height
       
   121         ti.x = x
       
   122         ti.y = y
       
   123         
       
   124         # render and return ptr to buffer
       
   125         trap_err("pt_image_tile_mem", 
       
   126             pt_image_tile_mem(self.image, &ti, &buf, &len)
       
   127         )
       
   128         
       
   129         # copy buffer as str...
       
   130         data = python_string.PyString_FromStringAndSize(buf, len)
       
   131 
       
   132         # drop buffer...
       
   133         stdlib.free(buf)
       
   134 
       
   135         return data
   112 
   136 
   113     def __dealloc__ (self) :
   137     def __dealloc__ (self) :
   114         if self.image :
   138         if self.image :
   115             pt_image_destroy(self.image)
   139             pt_image_destroy(self.image)
   116 
   140