Skip to content

Commit d5a3ccd

Browse files
committed
Add Init()
1 parent 5eebe9b commit d5a3ccd

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

jpegli.go

+6
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ func Encode(w io.Writer, m image.Image, o ...*EncodingOptions) error {
166166
return nil
167167
}
168168

169+
// Init initializes wazero runtime and compiles the module.
170+
// There is no need to explicitly call this function, first Decode/Encode will initialize the runtime.
171+
func Init() {
172+
initOnce()
173+
}
174+
169175
func imageToRGBA(src image.Image) *image.RGBA {
170176
if dst, ok := src.(*image.RGBA); ok {
171177
return dst

jpegli_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ var testRgba []byte
3030
//go:embed testdata/cmyk.jpg
3131
var testCmyk []byte
3232

33+
func init() {
34+
jpegli.Init()
35+
}
36+
3337
func TestDecode(t *testing.T) {
3438
img, err := jpegli.Decode(bytes.NewReader(testJpg))
3539
if err != nil {
@@ -235,7 +239,7 @@ func TestEncodeSync(t *testing.T) {
235239
t.Error(err)
236240
}
237241

238-
for i := 0; i < 100; i++ {
242+
for i := 0; i < 10; i++ {
239243
wg.Add(1)
240244
go func() {
241245
ch <- true

jpegli_wazero.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"image/color"
1111
"io"
1212
"os"
13+
"sync"
1314
"unsafe"
1415

1516
"github.com/tetratelabs/wazero"
@@ -28,6 +29,8 @@ const (
2829
)
2930

3031
func decode(r io.Reader, configOnly, fancyUpsampling, blockSmoothing, arithCode bool, dctMethod DCTMethod, tw, th int) (image.Image, image.Config, error) {
32+
initOnce()
33+
3134
var err error
3235
var cfg image.Config
3336
var data []byte
@@ -217,6 +220,8 @@ func decode(r io.Reader, configOnly, fancyUpsampling, blockSmoothing, arithCode
217220
func encode(w io.Writer, m image.Image, quality, chromaSubsampling, progressiveLevel int, optimizeCoding, adaptiveQuantization,
218221
standardQuantTables, fancyDownsampling bool, dctMethod DCTMethod) error {
219222

223+
initOnce()
224+
220225
var data []byte
221226
var colorspace int
222227
var chroma int
@@ -336,9 +341,11 @@ var (
336341
rt wazero.Runtime
337342
cm wazero.CompiledModule
338343
mc wazero.ModuleConfig
344+
345+
initOnce = sync.OnceFunc(initialize)
339346
)
340347

341-
func init() {
348+
func initialize() {
342349
ctx := context.Background()
343350
rt = wazero.NewRuntime(ctx)
344351

0 commit comments

Comments
 (0)