-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrayscale.go
55 lines (45 loc) · 1.13 KB
/
grayscale.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
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: -lgrayscale_darwin
#cgo linux LDFLAGS: -lgrayscale_linux
#cgo LDFLAGS: -ldl -lm
#include "grayscale.h"
*/
import "C"
import (
"image"
"github.com/pkg/errors"
_ "github.com/benesch/cgosymbolizer"
)
var (
ErrGrayscale = errors.New("failed to grayscale")
)
//go:generate go run ./cmd/compile f grayscale grayscale.cpp
func grayscale(in *image.RGBA) (*image.RGBA, error) {
width, height := in.Rect.Dx(), in.Rect.Dy()
inBuf, err := halideBufferRGBA(in.Pix, width, height)
if err != nil {
return nil, errors.WithStack(err)
}
defer halideBufferFree(inBuf)
out := image.NewRGBA(image.Rect(0, 0, width, height))
outBuf, err := halideBufferRGBA(out.Pix, width, height)
if err != nil {
return nil, errors.WithStack(err)
}
defer halideBufferFree(outBuf)
ret := C.grayscale(
inBuf,
C.int(width),
C.int(height),
outBuf,
)
if ret != C.int(0) {
return nil, errors.WithStack(ErrGrayscale)
}
return out, nil
}