This repository has been archived by the owner on Oct 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
cgo.go
70 lines (59 loc) · 1.82 KB
/
cgo.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
// Copyright 2017 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package guetzli
/*
#cgo CXXFLAGS: -std=c++11
#cgo CPPFLAGS: -I./internal/guetzli-1.0.1
#cgo CPPFLAGS: -I./internal/guetzli-1.0.1/third_party/butteraugli
#cgo windows LDFLAGS: -Wl,--allow-multiple-definition
#cgo !windows LDFLAGS: -lm
#include "./capi.h"
*/
import "C"
import "unsafe"
func encodeGray(pix []byte, w, h, stride int, quality int) (data []byte, ok bool) {
p := C.guetzliEncodeGray(
(*C.uint8_t)(unsafe.Pointer(&pix[0])),
C.int(w), C.int(h), C.int(stride),
C.float(quality),
)
if p == nil {
return nil, false
}
defer C.guetzli_string_delete(p)
cptr := C.guetzli_string_data(p)
data = make([]byte, int(C.guetzli_string_size(p)))
copy(data, ((*[1 << 30]byte)(unsafe.Pointer(cptr)))[0:len(data):len(data)])
return data, true
}
func encodeRGB(pix []byte, w, h, stride int, quality int) (data []byte, ok bool) {
p := C.guetzliEncodeRGB(
(*C.uint8_t)(unsafe.Pointer(&pix[0])),
C.int(w), C.int(h), C.int(stride),
C.float(quality),
)
if p == nil {
return nil, false
}
defer C.guetzli_string_delete(p)
cptr := C.guetzli_string_data(p)
data = make([]byte, int(C.guetzli_string_size(p)))
copy(data, ((*[1 << 30]byte)(unsafe.Pointer(cptr)))[0:len(data):len(data)])
return data, true
}
func encodeRGBA(pix []byte, w, h, stride int, quality int) (data []byte, ok bool) {
p := C.guetzliEncodeRGBA(
(*C.uint8_t)(unsafe.Pointer(&pix[0])),
C.int(w), C.int(h), C.int(stride),
C.float(quality),
)
if p == nil {
return nil, false
}
defer C.guetzli_string_delete(p)
cptr := C.guetzli_string_data(p)
data = make([]byte, int(C.guetzli_string_size(p)))
copy(data, ((*[1 << 30]byte)(unsafe.Pointer(cptr)))[0:len(data):len(data)])
return data, true
}