-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage.go
154 lines (120 loc) · 3.69 KB
/
image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package pngtile
/*
#include <stdlib.h>
#include "pngtile.h"
static char** new_strarray(int size) {
return calloc(sizeof(char *), size);
}
static void set_strarray(char **a, int n, char *s) {
a[n] = s;
}
static void free_strarray(char **a, int size) {
for (int i = 0; i < size; i++)
free(a[i]);
free(a);
}
*/
import "C"
import "unsafe"
// mode: OPEN_*
func imageOpen(cachePath string) (*Image, error) {
var image Image
var c_cache_path = C.CString(cachePath)
defer C.free(unsafe.Pointer(c_cache_path))
if ret, err := C.pt_image_new(&image.pt_image, c_cache_path); ret < 0 {
return nil, makeError("pt_image_new", ret, err)
}
return &image, nil
}
type Image struct {
pt_image *C.struct_pt_image
}
// return: CACHE_*
func (image *Image) Status(path string) (CacheStatus, error) {
var c_path = C.CString(path)
defer C.free(unsafe.Pointer(c_path))
if ret, err := C.pt_image_status(image.pt_image, c_path); ret < 0 {
return CACHE_ERROR, makeError("pt_image_status", ret, err)
} else {
return CacheStatus(ret), nil
}
}
func (image *Image) Info() (ImageInfo, error) {
var image_info C.struct_pt_image_info
var cache_info C.struct_pt_cache_info
if ret, err := C.pt_image_info(image.pt_image, &cache_info, &image_info); ret < 0 {
return ImageInfo{}, makeError("pt_image_open", ret, err)
}
return makeImageCacheInfo(&cache_info, &image_info), nil
}
// Open image cache in read-only mode
func (image *Image) Open() error {
if ret, err := C.pt_image_open(image.pt_image); ret < 0 {
return makeError("pt_image_open", ret, err)
}
return nil
}
// Open image cache in update mode,
func (image *Image) Update(path string, params ImageParams) error {
var image_params = params.c_struct()
var c_path = C.CString(path)
defer C.free(unsafe.Pointer(c_path))
if ret, err := C.pt_image_update(image.pt_image, c_path, &image_params); ret < 0 {
return makeError("pt_image_update", ret, err)
}
return nil
}
// Open image cache in update mode,
func (image *Image) UpdateParts(format ImageFormat, paths [][]string, params ImageParams) error {
var image_params = params.c_struct()
var c_paths_count = C.int(len(paths) * len(paths[0]))
var c_paths = C.new_strarray(c_paths_count)
defer C.free_strarray(c_paths, c_paths_count)
var c_parts = C.struct_pt_image_parts{
format: uint32(format),
rows: C.uint(len(paths)),
cols: C.uint(len(paths[0])),
paths: c_paths,
}
for row, rowPaths := range paths {
for col, path := range rowPaths {
C.set_strarray(c_paths, C.int(row*len(rowPaths)+col), C.CString(path))
}
}
if ret, err := C.pt_image_update_parts(image.pt_image, &c_parts, &image_params); ret < 0 {
return makeError("pt_image_update", ret, err)
}
return nil
}
// Render tile to PNG image
func (image *Image) Tile(params TileParams) ([]byte, error) {
var tile_params = params.c_struct()
var tile_buf *C.char
var tile_size C.size_t
if ret, err := C.pt_image_tile_mem(image.pt_image, &tile_params, &tile_buf, &tile_size); ret < 0 {
return nil, makeError("pt_image_tile_mem", ret, err)
} else {
defer C.free(unsafe.Pointer(tile_buf))
}
return C.GoBytes(unsafe.Pointer(tile_buf), C.int(tile_size)), nil // XXX: overflow?
}
// Close image, and destroy it to release resources.
// The Image is no longer usable, even after error returns.
func (image *Image) Close() error {
if image.pt_image == nil {
return nil // XXX: error?
}
defer image.destroy()
if ret, err := C.pt_image_close(image.pt_image); ret < 0 {
return makeError("pt_image_close", ret, err)
}
return nil
}
// Abort if open, release.
// Safe to re-call.
func (image *Image) destroy() {
if image.pt_image != nil {
C.pt_image_destroy(image.pt_image)
image.pt_image = nil
}
}