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

View File

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