-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
58 lines (46 loc) · 1.18 KB
/
util.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
package webp
//#include "webp_util.h"
import "C"
import (
"io"
"sync"
"unsafe"
)
//WebPPicture pointer -> Writer for the WebPPicture
var wh *writeHandler = &writeHandler{
wm: make(map[unsafe.Pointer]io.Writer),
}
type writeHandler struct {
m sync.Mutex
wm map[unsafe.Pointer]io.Writer
}
func (h *writeHandler) add(p unsafe.Pointer, w io.Writer) {
h.m.Lock()
defer h.m.Unlock()
h.wm[p] = w
}
func (h *writeHandler) get(p unsafe.Pointer) io.Writer {
h.m.Lock()
defer h.m.Unlock()
return h.wm[p]
}
func (h *writeHandler) del(p unsafe.Pointer) {
h.m.Lock()
defer h.m.Unlock()
delete(h.wm, p)
}
//writeTo is a Go implementation of the C function we defined in our webp_util header file
//the purpose of this is to be able to use this callback function and write to a native Go writer.
//We map a writer to the WebPPicture's pointer so we can grab it when this function is called
//export writeTo
func writeTo(data *C.uint8_t, data_size C.size_t, picture *C.struct_WebPPicture) C.int {
w := wh.get(unsafe.Pointer(picture))
if w != nil {
read, err := w.Write(C.GoBytes(unsafe.Pointer(data), C.int(data_size)))
if err != nil {
return 0
}
return C.int(read)
}
return 0
}