python/pypngtile.pyx
author Tero Marttila <terom@fixme.fi>
Mon, 25 Jan 2010 02:03:25 +0200
changeset 61 31650ef395d3
parent 57 06258920cec8
child 78 a3aaf5c23454
permissions -rw-r--r--
use pt_png_decode_direct if no background color is set..
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
cdef extern from "errno.h" :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    extern int errno
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
cdef extern from "string.h" :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
    char* strerror (int err)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
cimport stdio
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
cimport stdlib
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
cimport python_string
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
cdef extern from "Python.h" :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    int PyFile_Check (object p)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
    stdio.FILE* PyFile_AsFile (object p)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    void PyFile_IncUseCount (object p)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    void PyFile_DecUseCount (object p)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
cdef extern from "pngtile.h" :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    struct pt_ctx :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
        pass
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    struct pt_image :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
        pass
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
    enum pt_open_mode :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
        PT_OPEN_UPDATE
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    enum pt_cache_status :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
        PT_CACHE_ERROR
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
        PT_CACHE_FRESH
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
        PT_CACHE_NONE
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
        PT_CACHE_STALE
57
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    32
        PT_CACHE_INCOMPAT
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
    struct pt_image_info :
57
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    35
        size_t img_width, img_height
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    36
        int img_mtime, cache_mtime, cache_version
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    37
        size_t img_bytes, cache_bytes
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    38
        size_t cache_blocks
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    39
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    40
    struct pt_image_params :
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    41
        int background_color[4]
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
    struct pt_tile_info :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
        size_t width, height
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
        size_t x, y
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    46
        int zoom
57
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    47
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
    int pt_image_open (pt_image **image_ptr, pt_ctx *ctx, char *png_path, int cache_mode)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
    int pt_image_info_func "pt_image_info" (pt_image *image, pt_image_info **info_ptr)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
    int pt_image_status (pt_image *image)
57
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    51
    int pt_image_update (pt_image *image, pt_image_params *params)
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
    int pt_image_tile_file (pt_image *image, pt_tile_info *info, stdio.FILE *out)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
    int pt_image_tile_mem (pt_image *image, pt_tile_info *info, char **buf_ptr, size_t *len_ptr)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
    void pt_image_destroy (pt_image *image)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
    char* pt_strerror (int err)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
OPEN_UPDATE = PT_OPEN_UPDATE
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
CACHE_ERROR = PT_CACHE_ERROR
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
CACHE_FRESH = PT_CACHE_FRESH
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
CACHE_NONE = PT_CACHE_NONE
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
CACHE_STALE = PT_CACHE_STALE
57
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    63
CACHE_INCOMPAT = PT_CACHE_INCOMPAT
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
class Error (BaseException) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
    pass
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
cdef int trap_err (char *op, int ret) except -1 :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
    if ret < 0 :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
        raise Error("%s: %s: %s" % (op, pt_strerror(ret), strerror(errno)))
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
    else :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
        return ret
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
cdef class Image :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
    cdef pt_image *image
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
    def __cinit__ (self, char *png_path, int cache_mode = 0) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
        trap_err("pt_image_open", 
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
            pt_image_open(&self.image, NULL, png_path, cache_mode)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
        )
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
    
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
    def info (self) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
        cdef pt_image_info *image_info
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
        
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
        trap_err("pt_image_info",
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
            pt_image_info_func(self.image, &image_info)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
        )
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
57
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    90
        return (image_info.img_width, image_info.img_height)
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
    
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
    def status (self) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
        return trap_err("pt_image_status", 
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
            pt_image_status(self.image)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
        )
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
    
57
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    97
    # XXX: support params
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
    def update (self) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
        trap_err("pt_image_update", 
57
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   100
            pt_image_update(self.image, NULL)
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
        )
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   103
    def tile_file (self, size_t width, size_t height, size_t x, size_t y, int zoom, object out) :
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
        cdef stdio.FILE *outf
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
        cdef pt_tile_info ti
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
        if not PyFile_Check(out) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
            raise TypeError("out: must be a file object")
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
        outf = PyFile_AsFile(out)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
        if not outf :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
            raise TypeError("out: must have a FILE*")
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
    
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
        ti.width = width
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
        ti.height = height
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
        ti.x = x
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
        ti.y = y
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   119
        ti.zoom = zoom
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   120
        
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
        trap_err("pt_image_tile_file", 
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   122
            pt_image_tile_file(self.image, &ti, outf)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   123
        )
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   124
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   125
    def tile_mem (self, size_t width, size_t height, size_t x, size_t y, int zoom) :
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   126
        cdef pt_tile_info ti
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   127
        cdef char *buf
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   128
        cdef size_t len
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   129
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   130
        ti.width = width
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   131
        ti.height = height
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
        ti.x = x
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   133
        ti.y = y
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   134
        ti.zoom = zoom
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   135
        
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   136
        # render and return ptr to buffer
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   137
        trap_err("pt_image_tile_mem", 
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   138
            pt_image_tile_mem(self.image, &ti, &buf, &len)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
        )
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   140
        
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   141
        # copy buffer as str...
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   142
        data = python_string.PyString_FromStringAndSize(buf, len)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   144
        # drop buffer...
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   145
        stdlib.free(buf)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   146
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   147
        return data
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   148
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   149
    def __dealloc__ (self) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   150
        if self.image :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   151
            pt_image_destroy(self.image)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   152