-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.go
52 lines (42 loc) · 1.04 KB
/
library.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
package main
import (
"C"
"log"
)
import (
"bytes"
"fmt"
"image/png"
"os"
"unsafe"
diff "github.com/olegfedoseev/image-diff"
)
func main() {}
//export helloWorld
func helloWorld() {
log.Println("hello World")
}
//export compare
func compare(a_data *C.uchar, a_size C.int, b_data *C.uchar, b_size C.int) *C.char {
a := (*[1 << 30]byte)(unsafe.Pointer(a_data))[:a_size:a_size]
b := (*[1 << 30]byte)(unsafe.Pointer(b_data))[:b_size:b_size]
a_img, err := png.Decode(bytes.NewReader(a))
if err != nil {
return C.CString(fmt.Sprintf("err: a_img decoding: %s", err))
}
b_img, err := png.Decode(bytes.NewReader(b))
if err != nil {
return C.CString(fmt.Sprintf("err: b_img decoding: %s", err))
}
diff, percent, err := diff.CompareImages(a_img, b_img)
if err != nil {
return C.CString(fmt.Sprintf("err: diffing: %s", err))
}
f, err := os.Create("diff.png")
if err != nil {
return C.CString(fmt.Sprintf("err: error opening result file: %s", err))
}
png.Encode(f, diff)
f.Close()
return C.CString(fmt.Sprintf("res: %f", percent))
}