support io reader for converter

This commit is contained in:
Celogeek 2022-12-29 17:30:35 +01:00
parent 99b80b248c
commit 222e02f377
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
2 changed files with 39 additions and 14 deletions

View File

@ -169,6 +169,11 @@ func (e *EPub) LoadCBZ(path string) *EPub {
r.Close()
return e
}
sort.SliceStable(images, func(i, j int) bool {
return strings.Compare(images[i].Name, images[j].Name) < 0
})
e.ImagesCount = len(images)
type Todo struct {
@ -179,7 +184,7 @@ func (e *EPub) LoadCBZ(path string) *EPub {
todo := make(chan *Todo)
e.ProcessingImages = func() chan *Image {
defer r.Close()
// defer r.Close()
wg := &sync.WaitGroup{}
results := make(chan *Image)
for i := 0; i < runtime.NumCPU(); i++ {
@ -187,8 +192,27 @@ func (e *EPub) LoadCBZ(path string) *EPub {
go func() {
defer wg.Done()
for task := range todo {
fmt.Println(task.FZ.Name)
// TODO
reader, err := task.FZ.Open()
if err != nil {
panic(err)
}
data, w, h := imageconverter.Convert(
reader,
e.Crop,
e.ViewWidth,
e.ViewHeight,
e.Quality,
)
name := fmt.Sprintf("OEBPS/Images/%d.jpg", task.Id)
if task.Id == 0 {
name = "OEBPS/Images/cover.jpg"
}
results <- &Image{
task.Id,
NewImageData(name, data),
w,
h,
}
}
}()
}
@ -198,6 +222,7 @@ func (e *EPub) LoadCBZ(path string) *EPub {
}
close(todo)
wg.Wait()
r.Close()
close(results)
}()
@ -261,8 +286,12 @@ func (e *EPub) LoadDir(dirname string) *EPub {
go func() {
defer wg.Done()
for task := range todo {
reader, err := os.Open(task.Path)
if err != nil {
panic(err)
}
data, w, h := imageconverter.Convert(
task.Path,
reader,
e.Crop,
e.ViewWidth,
e.ViewHeight,

View File

@ -5,19 +5,15 @@ import (
"image"
"image/color"
"image/jpeg"
"io"
"os"
"golang.org/x/image/draw"
)
func Load(file string) *image.Gray {
f, err := os.Open(file)
if err != nil {
panic(err)
}
defer f.Close()
img, _, err := image.Decode(f)
func Load(reader io.ReadCloser) *image.Gray {
defer reader.Close()
img, _, err := image.Decode(reader)
if err != nil {
panic(err)
}
@ -142,8 +138,8 @@ func Save(img *image.Gray, output string, quality int) {
}
}
func Convert(path string, crop bool, w, h int, quality int) ([]byte, int, int) {
img := Load(path)
func Convert(reader io.ReadCloser, crop bool, w, h int, quality int) ([]byte, int, int) {
img := Load(reader)
if crop {
img = CropMarging(img)
}