-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0halide.go
236 lines (205 loc) · 6.28 KB
/
0halide.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package fastimagehash
/*
#cgo amd64 CFLAGS: -I${SRCDIR}/include/amd64
#cgo arm64 CFLAGS: -I${SRCDIR}/include/arm64
#cgo amd64 LDFLAGS: -L${SRCDIR}/lib/amd64
#cgo arm64 LDFLAGS: -L${SRCDIR}/lib/arm64
#cgo darwin LDFLAGS: -lruntime_darwin
#cgo linux LDFLAGS: -lruntime_linux
#cgo LDFLAGS: -ldl -lm
#include <stdint.h>
#include <stdlib.h>
#include "runtime.h"
const struct halide_type_t halide_int16_t = { halide_type_int, 16, 1 };
const struct halide_type_t halide_int32_t = { halide_type_int, 32, 1 };
const struct halide_type_t halide_uint8_t = { halide_type_uint, 8, 1 };
const struct halide_type_t halide_uint16_t = { halide_type_uint, 16, 1 };
const struct halide_type_t halide_float_t = { halide_type_float, 32, 1 };
const struct halide_type_t halide_double_t = { halide_type_float, 64, 1 };
static void free_halide_buffer(halide_buffer_t *buf) {
if (NULL != buf) {
free(buf->dim);
}
free(buf);
}
static void init_rgba_dim(halide_dimension_t *dim, int32_t width, int32_t height) {
// width
dim[0].min = 0;
dim[0].extent = width;
dim[0].stride = 4;
dim[0].flags = 0;
// height
dim[1].min = 0;
dim[1].extent = height;
dim[1].stride = width * 4;
dim[1].flags = 0;
// channel
dim[2].min = 0;
dim[2].extent = 4;
dim[2].stride = 1;
dim[2].flags = 0;
}
static void init_yuv_dim(halide_dimension_t *dim, int32_t stride, int32_t width, int32_t height) {
// width
dim[0].min = 0;
dim[0].extent = width;
dim[0].stride = 1;
dim[0].flags = 0;
// height
dim[1].min = 0;
dim[1].extent = height;
dim[1].stride = stride;
dim[1].flags = 0;
}
static void init_2d_dim(halide_dimension_t *dim, int32_t width, int32_t height) {
// width
dim[0].min = 0;
dim[0].extent = width;
dim[0].stride = 1;
dim[0].flags = 0;
// height
dim[1].min = 0;
dim[1].extent = height;
dim[1].stride = width;
dim[1].flags = 0;
}
static halide_buffer_t *create_buffer(void *data, halide_dimension_t *dim, int32_t dimensions, struct halide_type_t halide_type) {
halide_buffer_t *buffer = (halide_buffer_t *) malloc(sizeof(halide_buffer_t));
if(buffer == NULL) {
return NULL;
}
memset(buffer, 0, sizeof(halide_buffer_t));
buffer->dimensions = dimensions;
buffer->dim = dim;
buffer->device = 0;
buffer->device_interface = NULL;
buffer->host = data;
buffer->flags = halide_buffer_flag_host_dirty;
buffer->type = halide_type;
return buffer;
}
static halide_buffer_t *create_halide_buffer_rgba(uint8_t *data, int32_t width, int32_t height) {
int32_t dimensions = 3;
halide_dimension_t *dim = (halide_dimension_t *) malloc(dimensions * sizeof(halide_dimension_t));
if(NULL == dim) {
return NULL;
}
memset(dim, 0, dimensions * sizeof(halide_dimension_t));
init_rgba_dim(dim, width, height);
halide_buffer_t *buf = create_buffer(data, dim, dimensions, halide_uint8_t);
if(NULL == buf) {
free(dim);
return NULL;
}
return buf;
}
static halide_buffer_t *create_halide_buffer_yuv(uint8_t *data, int32_t stride, int32_t width, int32_t height) {
int32_t dimensions = 2;
halide_dimension_t *dim = (halide_dimension_t *) malloc(dimensions * sizeof(halide_dimension_t));
if(NULL == dim) {
return NULL;
}
memset(dim, 0, dimensions * sizeof(halide_dimension_t));
init_yuv_dim(dim, stride, width, height);
halide_buffer_t *buf = create_buffer(data, dim, dimensions, halide_uint8_t);
if(NULL == buf) {
free(dim);
return NULL;
}
return buf;
}
static halide_buffer_t *create_halide_buffer_2d_uint8(uint8_t *data, int32_t width, int32_t height) {
int32_t dimensions = 2;
halide_dimension_t *dim = (halide_dimension_t *) malloc(dimensions * sizeof(halide_dimension_t));
if(NULL == dim) {
return NULL;
}
memset(dim, 0, dimensions * sizeof(halide_dimension_t));
init_2d_dim(dim, width, height);
halide_buffer_t *buf = create_buffer(data, dim, dimensions, halide_uint8_t);
if(NULL == buf) {
free(dim);
return NULL;
}
return buf;
}
static halide_buffer_t *create_halide_buffer_2d_float(float *data, int32_t width, int32_t height) {
int32_t dimensions = 2;
halide_dimension_t *dim = (halide_dimension_t *) malloc(dimensions * sizeof(halide_dimension_t));
if(NULL == dim) {
return NULL;
}
memset(dim, 0, dimensions * sizeof(halide_dimension_t));
init_2d_dim(dim, width, height);
halide_buffer_t *buf = create_buffer(data, dim, dimensions, halide_float_t);
if(NULL == buf) {
free(dim);
return NULL;
}
return buf;
}
*/
import "C"
import (
"unsafe"
"github.com/pkg/errors"
_ "github.com/benesch/cgosymbolizer"
)
var (
ErrHalideBufferRGBA = errors.New("failed to create_halide_buffer_rgba")
ErrHalideBufferYUV = errors.New("failed to create_halide_buffer_yuv")
ErrHalideBuffer2DUint8 = errors.New("failed to create_halide_buffer_2d_uint8")
ErrHalideBuffer2DFloat32 = errors.New("failed to create_halide_buffer_2d_float")
)
//go:generate go run ./cmd/download
func init() {
// nop
}
func halideBufferRGBA(data []byte, width, height int) (*C.halide_buffer_t, error) {
buf := unsafe.Pointer(C.create_halide_buffer_rgba(
(*C.uint8_t)(unsafe.Pointer(&data[0])),
C.int(width),
C.int(height),
))
if buf == nil {
return nil, errors.WithStack(ErrHalideBufferRGBA)
}
return (*C.halide_buffer_t)(buf), nil
}
func halideBufferYUV(data []byte, stride, width, height int) (*C.halide_buffer_t, error) {
buf := unsafe.Pointer(C.create_halide_buffer_yuv(
(*C.uint8_t)(unsafe.Pointer(&data[0])),
C.int(stride),
C.int(width),
C.int(height),
))
if buf == nil {
return nil, errors.WithStack(ErrHalideBufferYUV)
}
return (*C.halide_buffer_t)(buf), nil
}
func halideBuffer2DUint8(data []uint8, width, height int) (*C.halide_buffer_t, error) {
buf := unsafe.Pointer(C.create_halide_buffer_2d_uint8(
(*C.uint8_t)(unsafe.Pointer(&data[0])),
C.int(width),
C.int(height),
))
if buf == nil {
return nil, errors.WithStack(ErrHalideBuffer2DUint8)
}
return (*C.halide_buffer_t)(buf), nil
}
func halideBuffer2DFloat32(data []float32, width, height int) (*C.halide_buffer_t, error) {
buf := unsafe.Pointer(C.create_halide_buffer_2d_float(
(*C.float)(unsafe.Pointer(&data[0])),
C.int(width),
C.int(height),
))
if buf == nil {
return nil, errors.WithStack(ErrHalideBuffer2DFloat32)
}
return (*C.halide_buffer_t)(buf), nil
}
func halideBufferFree(buf *C.halide_buffer_t) {
C.free_halide_buffer(buf)
}