parallel processing

This commit is contained in:
Celogeek 2022-12-26 16:21:31 +01:00
parent e1dd2f22a3
commit 8181c13492
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
2 changed files with 51 additions and 46 deletions

97
main.go
View File

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
comicconverter "go-comic-converter/internal/comic-converter" imageconverter "go-comic-converter/internal/image-converter"
"io/fs" "io/fs"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -13,51 +13,69 @@ import (
"github.com/bmaupin/go-epub" "github.com/bmaupin/go-epub"
) )
type Todo struct { type File struct {
Input string Path string
Output string Name string
Title string
Data string
InternalPath string
} }
func addImages(doc *epub.Epub, imagesPath string) { func addImages(doc *epub.Epub, imagesPath []string) {
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
todos := make(chan Todo, runtime.NumCPU()) todos := make(chan string, runtime.NumCPU())
imageResult := make(chan *File)
wg.Add(runtime.NumCPU()) wg.Add(runtime.NumCPU())
for i := 0; i < runtime.NumCPU(); i++ { for i := 0; i < runtime.NumCPU(); i++ {
go func() { go func() {
defer wg.Done() defer wg.Done()
for todo := range todos { for imagePath := range todos {
fmt.Printf("Processing %s\n", todo.Input) name := filepath.Base(imagePath)
comicconverter.Save( ext := filepath.Ext(name)
comicconverter.Resize( title := name[0 : len(name)-len(ext)]
comicconverter.CropMarging( imageResult <- &File{
comicconverter.Load(todo.Input), Path: imagePath,
), 1860, 2480), todo.Output, 75, Name: name,
) Title: title,
Data: imageconverter.Convert(imagePath, true, 1860, 2480, 75),
}
} }
}() }()
} }
go func() {
dirname := "/Users/vincent/Downloads/Bleach T01 (Tite KUBO) [eBook officiel 1920]" for _, imagePath := range imagesPath {
filepath.WalkDir(dirname, func(path string, d fs.DirEntry, err error) error { todos <- imagePath
if d.IsDir() {
return nil
} }
input := path close(todos)
ext := filepath.Ext(path) wg.Wait()
if strings.ToLower(ext) != ".jpg" { close(imageResult)
return nil }()
}
output := fmt.Sprintf("%s_gray%s", input[0:len(input)-len(ext)], ext)
todos <- Todo{input, output} results := make([]*File, 0)
for result := range imageResult {
return nil fmt.Println(result.Name)
internalPath, _ := doc.AddImage(result.Data, result.Name)
result.InternalPath = internalPath
results = append(results, result)
}
sort.SliceStable(results, func(i, j int) bool {
return strings.Compare(
results[i].Path, results[j].Path,
) < 0
}) })
for i, result := range results {
close(todos) if i == 0 {
doc.SetCover(result.InternalPath, "")
wg.Wait() } else {
doc.AddSection(
fmt.Sprintf("<img src=\"%s\" />", result.InternalPath),
result.Title,
fmt.Sprintf("%s.xhtml", result.Title),
"../css/cover.css",
)
}
}
} }
func getImages(dirname string) []string { func getImages(dirname string) []string {
@ -83,20 +101,7 @@ func main() {
doc := epub.NewEpub("Bleach T01 (Tite KUBO) [eBook officiel 1920]") doc := epub.NewEpub("Bleach T01 (Tite KUBO) [eBook officiel 1920]")
doc.SetAuthor("Bachelier Vincent") doc.SetAuthor("Bachelier Vincent")
for i, imagePath := range imagesPath { addImages(doc, imagesPath)
fmt.Printf("%04d / %04d\n", i+1, len(imagesPath))
name := filepath.Base(imagePath)
ext := filepath.Ext(name)
title := name[0 : len(name)-len(ext)]
img := comicconverter.Convert(imagePath, true, 1860, 2480, 75)
if i == 0 {
doc.SetCover(img, "")
} else {
imgPath, _ := doc.AddImage(img, name)
doc.AddSection(fmt.Sprintf("<img src=\"%s\" />", imgPath), title, fmt.Sprintf("%s.xhtml", title), "../css/cover.css")
}
}
if err := doc.Write("/Users/vincent/Downloads/test.epub"); err != nil { if err := doc.Write("/Users/vincent/Downloads/test.epub"); err != nil {
panic(err) panic(err)