python/pypngtile.pyx
author Tero Marttila <terom@paivola.fi>
Sun, 14 Sep 2014 17:19:28 +0300
changeset 134 08a0056f6175
parent 133 67f956b71bdf
child 170 c756bbcbc102
permissions -rw-r--r--
pypngtile: use Exception as base class, not BaseException
133
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
     1
from libc.errno cimport (
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
     2
        errno,
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
     3
)
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
     4
from libc.string cimport (
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
     5
        strerror,
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
     6
        memset,
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
     7
        memcpy,
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
     8
)
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
     9
from libc.stdio cimport (
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
    10
        FILE,
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
    11
)
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
    12
from libc.stdlib cimport (
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
    13
        free,
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
    14
)
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
    15
from cpython.string cimport (
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
    16
    PyString_FromStringAndSize,
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
    17
)
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
    18
from pypngtile cimport *
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
cdef extern from "Python.h" :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    int PyFile_Check (object p)
133
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
    22
    FILE* PyFile_AsFile (object p)
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    24
## constants
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    25
# Image()
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    26
OPEN_READ       = PT_OPEN_READ
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    27
OPEN_UPDATE     = PT_OPEN_UPDATE
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    28
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    29
# Image.status -> ...
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    30
CACHE_FRESH     = PT_CACHE_FRESH
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    31
CACHE_NONE      = PT_CACHE_NONE
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    32
CACHE_STALE     = PT_CACHE_STALE
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    33
CACHE_INCOMPAT  = PT_CACHE_INCOMPAT
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
134
08a0056f6175 pypngtile: use Exception as base class, not BaseException
Tero Marttila <terom@paivola.fi>
parents: 133
diff changeset
    35
class Error (Exception) :
91
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    36
    """
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    37
        Base class for errors raised by pypngtile.
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    38
    """
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
91
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    40
    def __init__ (self, func, err) :
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    41
        super(Error, self).__init__("%s: %s: %s" % (func, pt_strerror(err), strerror(errno)))
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
cdef class Image :
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    44
    """
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    45
        An image file on disk (.png) and an associated .cache file.
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    46
        
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    47
        Open the .png file at the given path using the given mode.
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    48
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    49
        path        - filesystem path to .png file
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    50
        mode        - mode to operate cache in
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    51
            OPEN_READ       - read-only access to cache
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    52
            OPEN_UPDATE     - allow .update()
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    53
    """
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    54
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
    cdef pt_image *image
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
104
b5ae988c78b8 add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
    57
    # XXX: should really be a pt_image property...
b5ae988c78b8 add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
    58
    cdef readonly object path
b5ae988c78b8 add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
    59
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    60
    
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    61
    # open the pt_image
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    62
    def __cinit__ (self, char *path, int mode = 0) :
91
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    63
        cdef int err
104
b5ae988c78b8 add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
    64
b5ae988c78b8 add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
    65
        # store
b5ae988c78b8 add .path attr to pypngtile.Image
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
    66
        self.path = path
91
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    67
        
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    68
        # open
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    69
        with nogil :
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    70
            # XXX: I hope use of path doesn't break...
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    71
            err = pt_image_open(&self.image, NULL, path, mode)
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    72
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    73
        if err :
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    74
            raise Error("pt_image_open", err)
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    75
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    76
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
    def info (self) :
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    78
        """
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    79
            Return a dictionary containing various information about the image.
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    80
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    81
            img_width           - pixel dimensions of the source image
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    82
            img_height            only available if the cache was opened
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    83
            img_bpp             - bits per pixel for the source image
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    84
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    85
            image_mtime         - last modification timestamp for source image
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    86
            image_bytes         - size of source image file in bytes
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    87
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    88
            cache_version       - version of cache file available
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    89
            cache_mtime         - last modification timestamp for cache file
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    90
            cache_bytes         - size of cache file in bytes
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    91
            cache_blocks        - size of cache file in disk blocks - 512 bytes / block
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    92
        """
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    93
91
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    94
        cdef const_image_info_ptr infop
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    95
        cdef int err
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    96
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    97
        with nogil :
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    98
            err = pt_image_info_(self.image, &infop)
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
    99
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   100
        if err :
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   101
            raise Error("pt_image_info", err)
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
        
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   103
        # return as a struct
91
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   104
        return infop[0]
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   105
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   106
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
    def status (self) :
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   108
        """
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   109
            Return a code describing the status of the underlying cache file.
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   110
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   111
            CACHE_FRESH         - the cache file exists and is up-to-date
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   112
            CACHE_NONE          - the cache file does not exist
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   113
            CACHE_STALE         - the cache file exists, but is older than the source image
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   114
            CACHE_INCOMPAT      - the cache file exists, but is incompatible with this version of the library
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   115
        """
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   116
91
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   117
        cdef int ret
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   118
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   119
        with nogil :
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   120
            ret = pt_image_status(self.image)
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   121
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   122
        if ret :
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   123
            raise Error("pt_image_status", ret)
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   124
        
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   125
        return ret
87
ecd87b41e884 pyx: Image.open
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   126
ecd87b41e884 pyx: Image.open
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   127
    def open (self) :
ecd87b41e884 pyx: Image.open
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   128
        """
ecd87b41e884 pyx: Image.open
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   129
            Open the underlying cache file for reading, if available.
ecd87b41e884 pyx: Image.open
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   130
        """
ecd87b41e884 pyx: Image.open
Tero Marttila <terom@fixme.fi>
parents: 83
diff changeset
   131
91
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   132
        cdef int err
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   133
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   134
        with nogil :
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   135
            err = pt_image_load(self.image)
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   136
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   137
        if err :
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   138
            raise Error("pt_image_load", err)
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   139
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   140
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   141
    def update (self, background_color = None) :
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   142
        """
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   143
            Update the underlying cache file from the source image.
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   144
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   145
            background_color    - skip consecutive pixels that match this byte pattern in output
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   146
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   147
            Requires that the Image was opened using OPEN_UPDATE.
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   148
        """
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   149
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   150
        cdef pt_image_params params
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   151
        cdef char *bgcolor
91
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   152
        cdef int err
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   153
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   154
        memset(&params, 0, sizeof(params))
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   155
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   156
        # params
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   157
        if background_color :
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   158
            # cast
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   159
            bgcolor = <char *>background_color
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   160
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   161
            if 0 >= len(bgcolor) > 4 :
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   162
                raise ValueError("background_color must be a str of between 1 and 4 bytes")
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   163
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   164
            # decode 
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   165
            memcpy(params.background_color, bgcolor, len(bgcolor))
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   166
    
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   167
        # run update
91
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   168
        with nogil :
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   169
            err = pt_image_update(self.image, &params)
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   170
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   171
        if err :
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   172
            raise Error("pt_image_update", err)
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   173
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   174
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   175
    def tile_file (self, size_t width, size_t height, size_t x, size_t y, int zoom, object out) :
83
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   176
        """
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   177
            Render a region of the source image as a PNG tile to the given output file.
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   178
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   179
            width       - dimensions of the output tile in px
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   180
            height      
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   181
            x           - coordinates in the source file
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   182
            y
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   183
            zoom        - zoom level: out = 2**(-zoom) * in
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   184
            out         - output file
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   185
133
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
   186
            Note that the given file object MUST be a *real* FILE*, not a fake Python object.
83
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   187
        """
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   188
133
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
   189
        cdef FILE *outf
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   190
        cdef pt_tile_info ti
91
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   191
        cdef int err
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   192
83
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   193
        memset(&ti, 0, sizeof(ti))
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   194
        
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   195
        # convert to FILE
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   196
        if not PyFile_Check(out) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   197
            raise TypeError("out: must be a file object")
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   198
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   199
        outf = PyFile_AsFile(out)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   200
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   201
        if not outf :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   202
            raise TypeError("out: must have a FILE*")
83
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   203
        
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   204
        # pack params
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   205
        ti.width = width
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   206
        ti.height = height
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   207
        ti.x = x
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   208
        ti.y = y
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   209
        ti.zoom = zoom
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   210
        
83
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   211
        # render
91
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   212
        with nogil :
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   213
            err = pt_image_tile_file(self.image, &ti, outf)
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   214
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   215
        if err :
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   216
            raise Error("pt_image_tile_file", err)
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   217
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   218
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   219
    def tile_mem (self, size_t width, size_t height, size_t x, size_t y, int zoom) :
83
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   220
        """
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   221
            Render a region of the source image as a PNG tile, and return the PNG data a a string.
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   222
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   223
            width       - dimensions of the output tile in px
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   224
            height      
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   225
            x           - coordinates in the source file
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   226
            y
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   227
            zoom        - zoom level: out = 2**(-zoom) * in
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   228
        """
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   229
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   230
        cdef pt_tile_info ti
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   231
        cdef char *buf
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   232
        cdef size_t len
91
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   233
        cdef int err
83
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   234
        
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   235
        memset(&ti, 0, sizeof(ti))
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   236
        
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   237
        # pack params
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   238
        ti.width = width
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   239
        ti.height = height
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   240
        ti.x = x
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   241
        ti.y = y
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   242
        ti.zoom = zoom
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   243
        
83
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   244
        # render and return via buf/len
91
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   245
        with nogil :
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   246
            err = pt_image_tile_mem(self.image, &ti, &buf, &len)
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   247
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   248
        if err :
0bf7878bdf5c document pt_image_tile_* as semi-threadsafe, and nogil pypngtile.pyx (and get rid of trap_err, ugh). Also fix constness warning
Tero Marttila <terom@fixme.fi>
parents: 87
diff changeset
   249
            raise Error("pt_image_tile_mem", err)
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   250
        
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   251
        # copy buffer as str...
133
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
   252
        data = PyString_FromStringAndSize(buf, len)
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   253
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   254
        # drop buffer...
133
67f956b71bdf python/pypngtile: cleanup cython using modern cimports
Tero Marttila <terom@paivola.fi>
parents: 104
diff changeset
   255
        free(buf)
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   256
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   257
        return data
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   258
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   259
    # release the pt_image
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   260
    def __dealloc__ (self) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   261
        if self.image :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   262
            pt_image_destroy(self.image)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   263
83
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   264
            self.image = NULL
a1e8fa84a9fb docfix tile_* funcs
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   265