python/pypngtile.pyx
author Tero Marttila <terom@fixme.fi>
Mon, 25 Jan 2010 04:06:44 +0200
changeset 78 a3aaf5c23454
parent 57 06258920cec8
child 83 a1e8fa84a9fb
permissions -rw-r--r--
improve pypngtile
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
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
     7
    void* memset (void *, int, size_t)
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
     8
    void* memcpy (void *, void *, size_t)
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
     9
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
cimport stdio
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
cimport stdlib
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
cimport python_string
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
cdef extern from "Python.h" :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    int PyFile_Check (object p)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    stdio.FILE* PyFile_AsFile (object p)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
    void PyFile_IncUseCount (object p)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    void PyFile_DecUseCount (object p)
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 "pngtile.h" :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    struct pt_ctx :
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
    struct pt_image :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
        pass
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_open_mode :
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    28
        PT_OPEN_READ    # 0
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
        PT_OPEN_UPDATE
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
    enum pt_cache_status :
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    32
        PT_CACHE_ERROR  # -1
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
        PT_CACHE_FRESH
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
        PT_CACHE_NONE
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
        PT_CACHE_STALE
57
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    36
        PT_CACHE_INCOMPAT
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
    struct pt_image_info :
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    39
        size_t img_width, img_height, img_bpp
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    40
        int image_mtime, cache_mtime, cache_version
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    41
        size_t image_bytes, cache_bytes
57
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    42
        size_t cache_blocks
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    43
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    44
    struct pt_image_params :
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    45
        int background_color[4]
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
    struct pt_tile_info :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
        size_t width, height
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
        size_t x, y
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
    50
        int zoom
57
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    51
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
    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
    53
    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
    54
    int pt_image_status (pt_image *image)
57
06258920cec8 update pypngtile.pyx
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
    55
    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
    56
    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
    57
    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
    58
    void pt_image_destroy (pt_image *image)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
    char* pt_strerror (int err)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    62
## constants
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    63
# Image()
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    64
OPEN_READ       = PT_OPEN_READ
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    65
OPEN_UPDATE     = PT_OPEN_UPDATE
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    66
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    67
# Image.status -> ...
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    68
CACHE_FRESH     = PT_CACHE_FRESH
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    69
CACHE_NONE      = PT_CACHE_NONE
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    70
CACHE_STALE     = PT_CACHE_STALE
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    71
CACHE_INCOMPAT  = PT_CACHE_INCOMPAT
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
class Error (BaseException) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
    pass
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
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
    77
    if ret < 0 :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
        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
    79
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
    else :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
        return ret
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
cdef class Image :
78
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
        An image file on disk (.png) and an associated .cache file.
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    86
        
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    87
        Open the .png file at the given path using the given mode.
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    88
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    89
        path        - filesystem path to .png file
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    90
        mode        - mode to operate cache in
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    91
            OPEN_READ       - read-only access to cache
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    92
            OPEN_UPDATE     - allow .update()
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    93
    """
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    94
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
    cdef pt_image *image
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    97
    
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    98
    # open the pt_image
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
    99
    def __cinit__ (self, char *path, int mode = 0) :
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
        trap_err("pt_image_open", 
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   101
            pt_image_open(&self.image, NULL, path, mode)
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
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   104
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
    def info (self) :
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   106
        """
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   107
            Return a dictionary containing various information about the image.
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
            img_width           - pixel dimensions of the source image
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   110
            img_height            only available if the cache was opened
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   111
            img_bpp             - bits per pixel for the source image
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   112
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   113
            image_mtime         - last modification timestamp for source image
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   114
            image_bytes         - size of source image file in bytes
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
            cache_version       - version of cache file available
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   117
            cache_mtime         - last modification timestamp for cache file
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   118
            cache_bytes         - size of cache file in bytes
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   119
            cache_blocks        - size of cache file in disk blocks - 512 bytes / block
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   120
        """
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   121
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   122
        cdef pt_image_info *info
30
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
        trap_err("pt_image_info",
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   125
            pt_image_info_func(self.image, &info)
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   126
        )
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   127
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   128
        # return as a struct
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   129
        return info[0]
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   130
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   131
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
    def status (self) :
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   133
        """
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   134
            Return a code describing the status of the underlying cache file.
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   135
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   136
            CACHE_FRESH         - the cache file exists and is up-to-date
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   137
            CACHE_NONE          - the cache file does not exist
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   138
            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
   139
            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
   140
        """
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   141
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   142
        return trap_err("pt_image_status", 
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
            pt_image_status(self.image)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   144
        )
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   145
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
    def update (self, background_color = None) :
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
            Update the underlying cache file from the source image.
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   150
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   151
            background_color    - skip consecutive pixels that match this byte pattern in output
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   152
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   153
            Requires that the Image was opened using OPEN_UPDATE.
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   154
        """
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
        cdef pt_image_params params
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   157
        cdef char *bgcolor
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   158
        memset(&params, 0, sizeof(params))
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   159
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   160
        # params
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   161
        if background_color :
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   162
            # cast
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   163
            bgcolor = <char *>background_color
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   164
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   165
            if 0 >= len(bgcolor) > 4 :
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   166
                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
   167
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   168
            # decode 
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   169
            memcpy(params.background_color, bgcolor, len(bgcolor))
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   170
    
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   171
        # run update
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   172
        trap_err("pt_image_update", 
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   173
            pt_image_update(self.image, &params)
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   174
        )
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   175
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   176
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   177
    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
   178
        cdef stdio.FILE *outf
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   179
        cdef pt_tile_info ti
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   180
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   181
        if not PyFile_Check(out) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   182
            raise TypeError("out: must be a file object")
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   183
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   184
        outf = PyFile_AsFile(out)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   185
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   186
        if not outf :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   187
            raise TypeError("out: must have a FILE*")
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   188
    
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   189
        ti.width = width
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   190
        ti.height = height
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   191
        ti.x = x
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   192
        ti.y = y
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   193
        ti.zoom = zoom
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   194
        
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   195
        trap_err("pt_image_tile_file", 
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   196
            pt_image_tile_file(self.image, &ti, outf)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   197
        )
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   198
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   199
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   200
    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
   201
        cdef pt_tile_info ti
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   202
        cdef char *buf
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   203
        cdef size_t len
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   204
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
        
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   211
        # render and return ptr to buffer
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   212
        trap_err("pt_image_tile_mem", 
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   213
            pt_image_tile_mem(self.image, &ti, &buf, &len)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   214
        )
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   215
        
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   216
        # copy buffer as str...
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   217
        data = python_string.PyString_FromStringAndSize(buf, len)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   218
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   219
        # drop buffer...
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   220
        stdlib.free(buf)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   221
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   222
        return data
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   223
78
a3aaf5c23454 improve pypngtile
Tero Marttila <terom@fixme.fi>
parents: 57
diff changeset
   224
    # release the pt_image
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   225
    def __dealloc__ (self) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   226
        if self.image :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   227
            pt_image_destroy(self.image)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   228