-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
46 lines (41 loc) · 832 Bytes
/
writer.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
package mangaconv
import (
"archive/zip"
"fmt"
"image"
"image/jpeg"
// for image decoding.
_ "image/png"
"io"
)
func (c *Converter) writeZip(writer io.Writer, deflate bool, pages <-chan page) error {
method := zip.Store
if deflate {
method = zip.Deflate
}
w := zip.NewWriter(writer)
defer w.Close()
for p := range pages {
f, err := w.CreateHeader(&zip.FileHeader{
Name: fmt.Sprintf("%09d.jpg", p.Index),
Method: method,
})
if err != nil {
return err
}
err = saveImg(f, p.Image)
if v, ok := p.Image.(*image.Gray); ok {
c.pool.Put(v)
}
if err != nil {
return err
}
}
return nil
}
func saveImg(target io.Writer, img image.Image) error {
if err := jpeg.Encode(target, img, &jpeg.Options{Quality: 75}); err != nil {
return fmt.Errorf("cannot encode: %w", err)
}
return nil
}