-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdct.go
51 lines (43 loc) · 1.02 KB
/
dct.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
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: -ldct2d_darwin
#cgo linux LDFLAGS: -ldct2d_linux
#cgo LDFLAGS: -ldl -lm
#include "dct2d.h"
*/
import "C"
import (
"github.com/pkg/errors"
_ "github.com/benesch/cgosymbolizer"
)
var (
ErrDCT = errors.New("failed to dct")
)
//go:generate go run ./cmd/compile f dct2d dct.cpp
func dct2d(in []byte, width, height int) ([]float32, error) {
out := make([]float32, width*height)
outBuf, err := halideBuffer2DFloat32(out, width, height)
if err != nil {
return nil, errors.WithStack(err)
}
defer halideBufferFree(outBuf)
inBuf, err := halideBuffer2DUint8(in, width, height)
if err != nil {
return nil, errors.WithStack(err)
}
defer halideBufferFree(inBuf)
ret := C.dct2d(
inBuf,
C.int(width),
C.int(height),
outBuf,
)
if ret != C.int(0) {
return nil, errors.WithStack(ErrDCT)
}
return out, nil
}