-
Notifications
You must be signed in to change notification settings - Fork 5
/
contour.go
73 lines (64 loc) · 1.59 KB
/
contour.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
package blurry
/*
#cgo CFLAGS: -I${SRCDIR}/include
#cgo darwin LDFLAGS: -L${SRCDIR}/lib -lruntime_osx -lcontour_osx -ldl -lm
#cgo linux LDFLAGS: -L${SRCDIR}/lib -lruntime_linux -lcontour_linux -ldl -lm
#include <stdlib.h>
#include <string.h>
#ifdef __APPLE__
#include "libcontour_osx.h"
#elif __linux__
#include "libcontour_linux.h"
#endif
#include "buffer.h"
int libcontour(unsigned char *src, int32_t width, int32_t height, uint8_t threshold, uint8_t size, unsigned char *out) {
halide_buffer_t *in_rgba_buf = create_rgba_buffer(src, width, height);
if(in_rgba_buf == NULL){
return 1;
}
halide_buffer_t *out_rgba_buf = create_uint8_array_buffer(out, width, height);
if(out_rgba_buf == NULL){
free_buf(in_rgba_buf);
return 1;
}
int ret = contour(in_rgba_buf, width, height, threshold, size, out_rgba_buf);
free_buf(in_rgba_buf);
free_buf(out_rgba_buf);
return ret;
}
*/
import "C"
import (
"errors"
"image"
)
var (
ErrContour = errors.New("contour cgo call error")
)
func Contour(img *image.RGBA, threshold uint8, size uint8) ([]image.Point, error) {
width, height := wh(img)
buf := GetByteBuf(width * height)
defer PutByteBuf(buf)
ret := C.libcontour(
(*C.uchar)(&img.Pix[0]),
C.int(width),
C.int(height),
C.uchar(threshold),
C.uchar(size),
(*C.uchar)(&buf[0]),
)
if int(ret) != 0 {
return nil, ErrContour
}
points := make([]image.Point, 0, width*height)
for y := 0; y < height; y += 1 {
row := buf[:width]
for x := 0; x < width; x += 1 {
if row[x] == 1 {
points = append(points, image.Pt(x, y))
}
}
buf = buf[width:]
}
return points, nil
}