-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
44 lines (40 loc) · 898 Bytes
/
decoder.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
package mangaconv
import (
"context"
"fmt"
"image"
"io"
"runtime"
// This adds webp support.
_ "golang.org/x/image/webp"
"golang.org/x/sync/errgroup"
)
// decode reads a channel of raw pages and emits decoded pages.
func decode(ctx context.Context, pages chan<- page, raws <-chan rawPage) error {
errg, ctx := errgroup.WithContext(ctx)
for i := 0; i < runtime.NumCPU(); i++ {
errg.Go(func() error {
for raw := range raws {
img, err := decodeImage(raw.File)
if err != nil {
return fmt.Errorf("cannot decode image number %d: %w", raw.Index, err)
}
select {
case pages <- page{img, raw.Index}:
case <-ctx.Done():
return ctx.Err()
}
}
return nil
})
}
return errg.Wait()
}
func decodeImage(f io.ReadCloser) (image.Image, error) {
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
return nil, err
}
return img, nil
}