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

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

@ -1,15 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/">
<dc:title>{{ .Title }}</dc:title>
<dc:title>{{ .Info.Title }}</dc:title>
<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: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="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="primary-writing-mode" content="horizontal-lr"/>
<meta name="zero-gutter" content="true"/>
@ -22,20 +22,20 @@
<manifest>
<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="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 }}
{{ if eq .Id 0 }}
<item id="cover" href="Images/{{ .Title }}.jpg" media-type="image/jpeg" properties="cover-image"/>
{{ 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"/>
<item id="page_{{ .Id }}" href="Text/{{ .Id }}.xhtml" media-type="application/xhtml+xml"/>
<item id="img_{{ .Id }}" href="Images/{{ .Id }}.jpg" media-type="image/jpeg"/>
{{ end }}
</manifest>
<spine page-progression-direction="ltr" toc="ncx">
{{ range .Images }}
{{ if mod .Id 2 }}
<itemref idref="page_{{ .Id }}" linear="yes" properties="page-spread-left"/>
<itemref idref="page_cover" linear="yes" properties="page-spread-left"/>
{{ range $idx, $ := .Images }}
{{ if mod $idx 2 }}
<itemref idref="page_{{ $.Id }}" linear="yes" properties="page-spread-right"/>
{{ else }}
<itemref idref="page_{{ .Id }}" linear="yes" properties="page-spread-right"/>
<itemref idref="page_{{ $.Id }}" linear="yes" properties="page-spread-left"/>
{{ end }}
{{ end }}
</spine>

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

@ -2,13 +2,13 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>{{ .Title }}</title>
<title>Page {{ .Id }}</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
<meta name="viewport" content="width={{ .Width }}, height={{ .Height }}"/>
</head>
<body style="">
<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 id="PV">
<div id="PV-TL">
@ -25,16 +25,16 @@
</div>
</div>
<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 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 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 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>
</body>
</html>

@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<ncx version="2005-1" xml:lang="en-US" xmlns="http://www.daisy.org/z3986/2005/ncx/">
<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:totalPageCount" content="0"/>
<meta name="dtb:maxPageNumber" content="0"/>
<meta name="generated" content="true"/>
</head>
<docTitle><text>{{ .Title }}</text></docTitle>
<docTitle><text>{{ .Info.Title }}</text></docTitle>
<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>
</ncx>

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