python/pngtile.pyx
author Tero Marttila <terom@fixme.fi>
Thu, 31 Dec 2009 14:01:37 +0200
changeset 18 f92a24ab046e
parent 17 baf3fe7c6354
permissions -rw-r--r--
add missing lib/tile.*
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
     1
cdef extern from "errno.h" :
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
     2
    extern int errno
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
     3
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
     4
cdef extern from "string.h" :
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
     5
    char* strerror (int err)
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
     6
18
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
     7
cimport stdio
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
     8
cimport stdlib
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
     9
cimport python_string
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
    10
16
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
cdef extern from "Python.h" :
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    int PyFile_Check (object p)
18
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
    13
    stdio.FILE* PyFile_AsFile (object p)
16
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    void PyFile_IncUseCount (object p)
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    void PyFile_DecUseCount (object p)
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
cdef extern from "pngtile.h" :
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    struct pt_ctx :
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
        pass
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    struct pt_image :
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
        pass
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
    enum pt_open_mode :
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
        PT_OPEN_UPDATE
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    enum pt_cache_status :
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
        PT_CACHE_ERROR
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
        PT_CACHE_FRESH
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
        PT_CACHE_NONE
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
        PT_CACHE_STALE
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
    struct pt_image_info :
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
        size_t width, height
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
    struct pt_tile_info :
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
        size_t width, height
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
        size_t x, y
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
        
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
    int pt_image_open (pt_image **image_ptr, pt_ctx *ctx, char *png_path, int cache_mode)
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
    int pt_image_info_func "pt_image_info" (pt_image *image, pt_image_info **info_ptr)
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
    int pt_image_status (pt_image *image)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    43
    int pt_image_update (pt_image *image)
18
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
    44
    int pt_image_tile_file (pt_image *image, pt_tile_info *info, stdio.FILE *out)
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
    45
    int pt_image_tile_mem (pt_image *image, pt_tile_info *info, char **buf_ptr, size_t *len_ptr)
16
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
    void pt_image_destroy (pt_image *image)
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    48
    char* pt_strerror (int err)
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    49
16
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
OPEN_UPDATE = PT_OPEN_UPDATE
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
CACHE_ERROR = PT_CACHE_ERROR
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
CACHE_FRESH = PT_CACHE_FRESH
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
CACHE_NONE = PT_CACHE_NONE
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
CACHE_STALE = PT_CACHE_STALE
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
class Error (BaseException) :
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
    pass
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    59
cdef int trap_err (char *op, int ret) except -1 :
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    60
    if ret < 0 :
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    61
        raise Error("%s: %s: %s" % (op, pt_strerror(ret), strerror(errno)))
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    62
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    63
    else :
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    64
        return ret
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    65
16
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
cdef class Image :
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
    cdef pt_image *image
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
    def __cinit__ (self, char *png_path, int cache_mode = 0) :
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    70
        trap_err("pt_image_open", 
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    71
            pt_image_open(&self.image, NULL, png_path, cache_mode)
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    72
        )
16
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
    
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
    def info (self) :
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
        cdef pt_image_info *image_info
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    76
        
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    77
        trap_err("pt_image_info",
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    78
            pt_image_info_func(self.image, &image_info)
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    79
        )
16
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
        return (image_info.width, image_info.height)
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
    
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
    def status (self) :
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    84
        return trap_err("pt_image_status", 
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    85
            pt_image_status(self.image)
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    86
        )
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    87
    
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    88
    def update (self) :
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    89
        trap_err("pt_image_update", 
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    90
            pt_image_update(self.image)
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
    91
        )
16
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
18
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
    93
    def tile_file (self, size_t width, size_t height, size_t x, size_t y, object out) :
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
    94
        cdef stdio.FILE *outf
16
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
        cdef pt_tile_info ti
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
        if not PyFile_Check(out) :
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
            raise TypeError("out: must be a file object")
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
        outf = PyFile_AsFile(out)
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
        if not outf :
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
            raise TypeError("out: must have a FILE*")
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
    
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
        ti.width = width
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
        ti.height = height
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
        ti.x = x
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
        ti.y = y
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
        
18
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   110
        trap_err("pt_image_tile_file", 
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   111
            pt_image_tile_file(self.image, &ti, outf)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 16
diff changeset
   112
        )
16
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
18
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   114
    def tile_mem (self, size_t width, size_t height, size_t x, size_t y) :
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   115
        cdef pt_tile_info ti
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   116
        cdef char *buf
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   117
        cdef size_t len
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   118
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   119
        ti.width = width
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   120
        ti.height = height
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   121
        ti.x = x
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   122
        ti.y = y
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   123
        
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   124
        # render and return ptr to buffer
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   125
        trap_err("pt_image_tile_mem", 
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   126
            pt_image_tile_mem(self.image, &ti, &buf, &len)
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   127
        )
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   128
        
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   129
        # copy buffer as str...
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   130
        data = python_string.PyString_FromStringAndSize(buf, len)
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   131
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   132
        # drop buffer...
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   133
        stdlib.free(buf)
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   134
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   135
        return data
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   136
16
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   137
    def __dealloc__ (self) :
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   138
        if self.image :
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
            pt_image_destroy(self.image)
6e781cf3d459 move public headers to include/, python stuff to python/ sub-dir
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   140