-
Notifications
You must be signed in to change notification settings - Fork 3
/
zbar.go
51 lines (40 loc) · 953 Bytes
/
zbar.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 zbar
import (
"context"
"image"
"sync"
_ "embed"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/imports/emscripten"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)
var (
//go:embed zbar.wasm
zbarWasm []byte
ctx = context.Background()
pool = sync.Pool{New: func() any { return NewScanner() }}
)
func newZbarInstance() (wazero.Runtime, api.Module) {
r := wazero.NewRuntime(ctx)
wasi_snapshot_preview1.MustInstantiate(ctx, r)
emscripten.MustInstantiate(ctx, r)
zbar := must(r.Instantiate(ctx, zbarWasm))
return r, zbar
}
func ReadAll(img image.Image) ([][]byte, error) {
s := pool.Get().(*Scanner)
defer pool.Put(s)
return s.ReadAll(img)
}
func Read(img image.Image) (*Reader, error) {
s := pool.Get().(*Scanner)
defer pool.Put(s)
return s.Reader(img)
}
func must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}