load images, simplify generator

This commit is contained in:
Celogeek 2022-12-27 18:19:23 +01:00
parent 95eb3497e7
commit 6dcaf4b7f3
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
6 changed files with 107 additions and 79 deletions

View File

@ -3,7 +3,6 @@ package epub
import ( import (
"archive/zip" "archive/zip"
"fmt" "fmt"
"io"
"io/fs" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
@ -21,16 +20,11 @@ import (
imageconverter "go-comic-converter/internal/image-converter" imageconverter "go-comic-converter/internal/image-converter"
) )
type ImageDetails struct {
*Images
Data io.Reader
Width int
Height int
}
type Images struct { type Images struct {
Id int Id int
Title string Data []byte
Width int
Height int
} }
type EPub struct { type EPub struct {
@ -47,11 +41,11 @@ type EPub struct {
Crop bool Crop bool
LimitMb int LimitMb int
Images []*Images
FirstImageTitle string
Error error Error error
ProcessingImages func() chan *ImageDetails ImagesCount int
ProcessingImages func() chan *Images
TemplateProcessor *template.Template
} }
func NewEpub(path string) *EPub { func NewEpub(path string) *EPub {
@ -60,6 +54,12 @@ func NewEpub(path string) *EPub {
panic(err) panic(err)
} }
tmpl := template.New("parser")
tmpl.Funcs(template.FuncMap{
"mod": func(i, j int) bool { return i%j == 0 },
"zoom": func(s int, z float32) int { return int(float32(s) * z) },
})
return &EPub{ return &EPub{
Path: path, Path: path,
@ -71,6 +71,8 @@ func NewEpub(path string) *EPub {
ViewWidth: 0, ViewWidth: 0,
ViewHeight: 0, ViewHeight: 0,
Quality: 75, Quality: 75,
TemplateProcessor: tmpl,
} }
} }
@ -105,11 +107,17 @@ func (e *EPub) SetLimitMb(l int) *EPub {
return e return e
} }
func (e *EPub) WriteString(wz *zip.Writer, file string, content string) error { func (e *EPub) WriteFile(wz *zip.Writer, file string, data any) error {
return e.WriteBuffer(wz, file, strings.NewReader(content)) var content []byte
switch b := data.(type) {
case string:
content = []byte(b)
case []byte:
content = b
default:
return fmt.Errorf("support string of []byte")
} }
func (e *EPub) WriteBuffer(wz *zip.Writer, file string, content io.Reader) error {
m, err := wz.CreateHeader(&zip.FileHeader{ m, err := wz.CreateHeader(&zip.FileHeader{
Name: file, Name: file,
Modified: time.Now(), Modified: time.Now(),
@ -117,15 +125,12 @@ func (e *EPub) WriteBuffer(wz *zip.Writer, file string, content io.Reader) error
if err != nil { if err != nil {
return err return err
} }
_, err = io.Copy(m, content) _, err = m.Write(content)
return err return err
} }
func (e *EPub) Render(templateString string, data any) string { func (e *EPub) Render(templateString string, data any) string {
tmpl := template.New("parser") tmpl, err := e.TemplateProcessor.Parse(templateString)
tmpl.Funcs(template.FuncMap{"mod": func(i, j int) bool { return i%j == 0 }})
tmpl.Funcs(template.FuncMap{"zoom": func(s int, z float32) int { return int(float32(s) * z) }})
tmpl, err := tmpl.Parse(templateString)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -164,38 +169,40 @@ func (e *EPub) LoadDir(dirname string) *EPub {
} }
sort.Strings(images) sort.Strings(images)
titleFormat := fmt.Sprintf("%%0%dd", len(fmt.Sprint(len(images)-1))) e.ImagesCount = len(images)
for i := range images {
e.Images = append(e.Images, &Images{
Id: i,
Title: fmt.Sprintf(titleFormat, i),
})
}
type Todo struct { type Todo struct {
*Images Id int
Path string Path string
} }
todo := make(chan *Todo) todo := make(chan *Todo)
e.ProcessingImages = func() chan *ImageDetails { e.ProcessingImages = func() chan *Images {
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
results := make(chan *ImageDetails) results := make(chan *Images)
for i := 0; i < runtime.NumCPU(); i++ { for i := 0; i < runtime.NumCPU(); i++ {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
for task := range todo { for task := range todo {
data, w, h := imageconverter.Convert(task.Path, e.Crop, e.ViewWidth, e.ViewHeight, e.Quality) data, w, h := imageconverter.Convert(task.Path, e.Crop, e.ViewWidth, e.ViewHeight, e.Quality)
results <- &ImageDetails{task.Images, data, w, h} results <- &Images{
task.Id,
data,
w,
h,
}
} }
}() }()
} }
go func() { go func() {
for i, path := range images { for i, path := range images {
todo <- &Todo{e.Images[i], path} if i == 0 {
todo <- &Todo{i, path}
} else {
todo <- &Todo{i, path}
}
} }
close(todo) close(todo)
wg.Wait() wg.Wait()
@ -204,8 +211,6 @@ func (e *EPub) LoadDir(dirname string) *EPub {
return results return results
} }
e.FirstImageTitle = e.Images[0].Title
return e return e
} }
@ -219,35 +224,59 @@ func (e *EPub) Write() error {
return err return err
} }
zipContent := [][]string{ images := make([]*Images, e.ImagesCount)
totalSize := 0
bar := progressbar.Default(int64(e.ImagesCount), "Processing")
for img := range e.ProcessingImages() {
images[img.Id] = img
totalSize += len(img.Data)
bar.Add(1)
}
bar.Close()
cover := images[0]
images = images[1:]
fmt.Println(len(images))
fmt.Println("Total Size:", totalSize)
type ZipContent struct {
Name string
Content any
}
zipContent := []ZipContent{
{"mimetype", TEMPLATE_MIME_TYPE}, {"mimetype", TEMPLATE_MIME_TYPE},
{"META-INF/container.xml", gohtml.Format(TEMPLATE_CONTAINER)}, {"META-INF/container.xml", gohtml.Format(TEMPLATE_CONTAINER)},
{"OEBPS/content.opf", e.Render(TEMPLATE_CONTENT, e)}, {"OEBPS/content.opf", e.Render(TEMPLATE_CONTENT, map[string]any{"Info": e, "Images": images})},
{"OEBPS/toc.ncx", e.Render(TEMPLATE_TOC, e)}, {"OEBPS/toc.ncx", e.Render(TEMPLATE_TOC, map[string]any{"Info": e, "Images": images})},
{"OEBPS/nav.xhtml", e.Render(TEMPLATE_NAV, e)}, {"OEBPS/nav.xhtml", e.Render(TEMPLATE_NAV, map[string]any{"Info": e, "Images": images})},
{"OEBPS/Text/style.css", TEMPLATE_STYLE}, {"OEBPS/Text/style.css", TEMPLATE_STYLE},
{"OEBPS/Text/cover.xhtml", e.Render(TEMPLATE_TEXT, map[string]any{
"Id": "cover",
"Width": cover.Width,
"Height": cover.Height,
})},
{"OEBPS/Images/cover.jpg", cover.Data},
} }
wz := zip.NewWriter(w) wz := zip.NewWriter(w)
defer wz.Close() defer wz.Close()
for _, content := range zipContent { for _, content := range zipContent {
if err := e.WriteString(wz, content[0], content[1]); err != nil { if err := e.WriteFile(wz, content.Name, content.Content); err != nil {
return err return err
} }
} }
bar := progressbar.Default(int64(len(e.Images)), "Processing") for _, img := range images {
defer bar.Close() text := fmt.Sprintf("OEBPS/Text/%d.xhtml", img.Id)
for img := range e.ProcessingImages() { image := fmt.Sprintf("OEBPS/Images/%d.jpg", img.Id)
text := fmt.Sprintf("OEBPS/Text/%s.xhtml", img.Title) if err := e.WriteFile(wz, text, e.Render(TEMPLATE_TEXT, img)); err != nil {
image := fmt.Sprintf("OEBPS/Images/%s.jpg", img.Title)
if err := e.WriteString(wz, text, e.Render(TEMPLATE_TEXT, img)); err != nil {
return err return err
} }
if err := e.WriteBuffer(wz, image, img.Data); err != nil { if err := e.WriteFile(wz, image, img.Data); err != nil {
return err return err
} }
bar.Add(1)
} }
return nil return nil

View File

@ -1,15 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<package version="3.0" unique-identifier="BookID" xmlns="http://www.idpf.org/2007/opf"> <package version="3.0" unique-identifier="BookID" xmlns="http://www.idpf.org/2007/opf">
<metadata xmlns:opf="http://www.idpf.org/2007/opf" xmlns:dc="http://purl.org/dc/elements/1.1/"> <metadata xmlns:opf="http://www.idpf.org/2007/opf" xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>{{ .Title }}</dc:title> <dc:title>{{ .Info.Title }}</dc:title>
<dc:language>en-US</dc:language> <dc:language>en-US</dc:language>
<dc:identifier id="BookID">urn:uuid:{{ .UID }}</dc:identifier> <dc:identifier id="BookID">urn:uuid:{{ .Info.UID }}</dc:identifier>
<dc:contributor id="contributor">GO Comic Converter</dc:contributor> <dc:contributor id="contributor">GO Comic Converter</dc:contributor>
<dc:creator>GO Comic Converter</dc:creator> <dc:creator>GO Comic Converter</dc:creator>
<meta property="dcterms:modified">{{ .UpdatedAt }}</meta> <meta property="dcterms:modified">{{ .Info.UpdatedAt }}</meta>
<meta name="cover" content="cover"/> <meta name="cover" content="cover"/>
<meta name="fixed-layout" content="true"/> <meta name="fixed-layout" content="true"/>
<meta name="original-resolution" content="{{ .ViewWidth }}x{{ .ViewHeight }}"/> <meta name="original-resolution" content="{{ .Info.ViewWidth }}x{{ .Info.ViewHeight }}"/>
<meta name="book-type" content="comic"/> <meta name="book-type" content="comic"/>
<meta name="primary-writing-mode" content="horizontal-lr"/> <meta name="primary-writing-mode" content="horizontal-lr"/>
<meta name="zero-gutter" content="true"/> <meta name="zero-gutter" content="true"/>
@ -22,20 +22,20 @@
<manifest> <manifest>
<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/> <item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>
<item id="nav" href="nav.xhtml" properties="nav" media-type="application/xhtml+xml"/> <item id="nav" href="nav.xhtml" properties="nav" media-type="application/xhtml+xml"/>
<item id="page_cover" href="Text/cover.xhtml" media-type="application/xhtml+xml"/>
<item id="img_cover" href="Images/cover.jpg" media-type="image/jpeg" properties="cover-image"/>
{{ range .Images }} {{ range .Images }}
{{ if eq .Id 0 }} <item id="page_{{ .Id }}" href="Text/{{ .Id }}.xhtml" media-type="application/xhtml+xml"/>
<item id="cover" href="Images/{{ .Title }}.jpg" media-type="image/jpeg" properties="cover-image"/> <item id="img_{{ .Id }}" href="Images/{{ .Id }}.jpg" media-type="image/jpeg"/>
{{ end }}
<item id="page_{{ .Id }}" href="Text/{{ .Title }}.xhtml" media-type="application/xhtml+xml"/>
<item id="img_{{ .Id }}" href="Images/{{ .Title }}.jpg" media-type="image/jpeg"/>
{{ end }} {{ end }}
</manifest> </manifest>
<spine page-progression-direction="ltr" toc="ncx"> <spine page-progression-direction="ltr" toc="ncx">
{{ range .Images }} <itemref idref="page_cover" linear="yes" properties="page-spread-left"/>
{{ if mod .Id 2 }} {{ range $idx, $ := .Images }}
<itemref idref="page_{{ .Id }}" linear="yes" properties="page-spread-left"/> {{ if mod $idx 2 }}
<itemref idref="page_{{ $.Id }}" linear="yes" properties="page-spread-right"/>
{{ else }} {{ else }}
<itemref idref="page_{{ .Id }}" linear="yes" properties="page-spread-right"/> <itemref idref="page_{{ $.Id }}" linear="yes" properties="page-spread-left"/>
{{ end }} {{ end }}
{{ end }} {{ end }}
</spine> </spine>

View File

@ -2,18 +2,18 @@
<!DOCTYPE html> <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head> <head>
<title>{{ .Title }}</title> <title>{{ .Info.Title }}</title>
<meta charset="utf-8"/> <meta charset="utf-8"/>
</head> </head>
<body> <body>
<nav xmlns:epub="http://www.idpf.org/2007/ops" epub:type="toc" id="toc"> <nav xmlns:epub="http://www.idpf.org/2007/ops" epub:type="toc" id="toc">
<ol> <ol>
<li><a href="Text/{{ .FirstImageTitle }}.xhtml">{{ .Title }}</a></li> <li><a href="Text/cover.xhtml">{{ .Info.Title }}</a></li>
</ol> </ol>
</nav> </nav>
<nav epub:type="page-list"> <nav epub:type="page-list">
<ol> <ol>
<li><a href="Text/{{ .FirstImageTitle }}.xhtml">{{ .Title }}</a></li> <li><a href="Text/cover.xhtml">{{ .Info.Title }}</a></li>
</ol> </ol>
</nav> </nav>
</body> </body>

View File

@ -2,13 +2,13 @@
<!DOCTYPE html> <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head> <head>
<title>{{ .Title }}</title> <title>Page {{ .Id }}</title>
<link href="style.css" type="text/css" rel="stylesheet"/> <link href="style.css" type="text/css" rel="stylesheet"/>
<meta name="viewport" content="width={{ .Width }}, height={{ .Height }}"/> <meta name="viewport" content="width={{ .Width }}, height={{ .Height }}"/>
</head> </head>
<body style=""> <body style="">
<div style="text-align:center;top:0.0%;"> <div style="text-align:center;top:0.0%;">
<img width="{{ .Width }}" height="{{ .Height }}" src="../Images/{{ .Title }}.jpg"/> <img width="{{ .Width }}" height="{{ .Height }}" src="../Images/{{ .Id }}.jpg"/>
</div> </div>
<div id="PV"> <div id="PV">
<div id="PV-TL"> <div id="PV-TL">
@ -25,16 +25,16 @@
</div> </div>
</div> </div>
<div class="PV-P" id="PV-TL-P" style=""> <div class="PV-P" id="PV-TL-P" style="">
<img style="position:absolute;left:0;top:0;" src="../Images/{{ .Title }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/> <img style="position:absolute;left:0;top:0;" src="../Images/{{ .Id }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/>
</div> </div>
<div class="PV-P" id="PV-TR-P" style=""> <div class="PV-P" id="PV-TR-P" style="">
<img style="position:absolute;right:0;top:0;" src="../Images/{{ .Title }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/> <img style="position:absolute;right:0;top:0;" src="../Images/{{ .Id }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/>
</div> </div>
<div class="PV-P" id="PV-BL-P" style=""> <div class="PV-P" id="PV-BL-P" style="">
<img style="position:absolute;left:0;bottom:0;" src="../Images/{{ .Title }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/> <img style="position:absolute;left:0;bottom:0;" src="../Images/{{ .Id }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/>
</div> </div>
<div class="PV-P" id="PV-BR-P" style=""> <div class="PV-P" id="PV-BR-P" style="">
<img style="position:absolute;right:0;bottom:0;" src="../Images/{{ .Title }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/> <img style="position:absolute;right:0;bottom:0;" src="../Images/{{ .Id }}.jpg" width="{{ zoom .Width 1.5 }}" height="{{ zoom .Height 1.5 }}"/>
</div> </div>
</body> </body>
</html> </html>

View File

@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<ncx version="2005-1" xml:lang="en-US" xmlns="http://www.daisy.org/z3986/2005/ncx/"> <ncx version="2005-1" xml:lang="en-US" xmlns="http://www.daisy.org/z3986/2005/ncx/">
<head> <head>
<meta name="dtb:uid" content="urn:uuid:{{ .UID }}"/> <meta name="dtb:uid" content="urn:uuid:{{ .Info.UID }}"/>
<meta name="dtb:depth" content="1"/> <meta name="dtb:depth" content="1"/>
<meta name="dtb:totalPageCount" content="0"/> <meta name="dtb:totalPageCount" content="0"/>
<meta name="dtb:maxPageNumber" content="0"/> <meta name="dtb:maxPageNumber" content="0"/>
<meta name="generated" content="true"/> <meta name="generated" content="true"/>
</head> </head>
<docTitle><text>{{ .Title }}</text></docTitle> <docTitle><text>{{ .Info.Title }}</text></docTitle>
<navMap> <navMap>
<navPoint id="Text"><navLabel><text>{{ .Title }}</text></navLabel><content src="Text/{{ .FirstImageTitle }}.xhtml"/></navPoint> <navPoint id="Text"><navLabel><text>{{ .Info.Title }}</text></navLabel><content src="Text/cover.xhtml"/></navPoint>
</navMap> </navMap>
</ncx> </ncx>

View File

@ -5,7 +5,6 @@ import (
"image" "image"
"image/color" "image/color"
"image/jpeg" "image/jpeg"
"io"
"os" "os"
"golang.org/x/image/draw" "golang.org/x/image/draw"
@ -117,13 +116,13 @@ func Resize(img *image.Gray, w, h int) *image.Gray {
return newImg return newImg
} }
func Get(img *image.Gray, quality int) io.Reader { func Get(img *image.Gray, quality int) []byte {
b := bytes.NewBuffer([]byte{}) b := bytes.NewBuffer([]byte{})
err := jpeg.Encode(b, img, &jpeg.Options{Quality: quality}) err := jpeg.Encode(b, img, &jpeg.Options{Quality: quality})
if err != nil { if err != nil {
panic(err) panic(err)
} }
return b return b.Bytes()
} }
func Save(img *image.Gray, output string, quality int) { func Save(img *image.Gray, output string, quality int) {
@ -143,7 +142,7 @@ func Save(img *image.Gray, output string, quality int) {
} }
} }
func Convert(path string, crop bool, w, h int, quality int) (io.Reader, int, int) { func Convert(path string, crop bool, w, h int, quality int) ([]byte, int, int) {
img := Load(path) img := Load(path)
if crop { if crop {
img = CropMarging(img) img = CropMarging(img)