mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-24 15:52:38 +02:00
remove pointer from epub processor
This commit is contained in:
parent
a351106eb7
commit
3c582ae52d
@ -20,16 +20,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type EPUBImageProcessor struct {
|
type EPUBImageProcessor struct {
|
||||||
*epuboptions.Options
|
epuboptions.Options
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(o *epuboptions.Options) *EPUBImageProcessor {
|
func New(o epuboptions.Options) EPUBImageProcessor {
|
||||||
return &EPUBImageProcessor{o}
|
return EPUBImageProcessor{o}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load extract and convert images
|
// Load extract and convert images
|
||||||
func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
|
func (e EPUBImageProcessor) Load() (images []epubimage.Image, err error) {
|
||||||
images = make([]*epubimage.Image, 0)
|
images = make([]epubimage.Image, 0)
|
||||||
imageCount, imageInput, err := e.load()
|
imageCount, imageInput, err := e.load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -38,7 +38,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
|
|||||||
// dry run, skip conversion
|
// dry run, skip conversion
|
||||||
if e.Dry {
|
if e.Dry {
|
||||||
for img := range imageInput {
|
for img := range imageInput {
|
||||||
images = append(images, &epubimage.Image{
|
images = append(images, epubimage.Image{
|
||||||
Id: img.Id,
|
Id: img.Id,
|
||||||
Path: img.Path,
|
Path: img.Path,
|
||||||
Name: img.Name,
|
Name: img.Name,
|
||||||
@ -49,7 +49,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
|
|||||||
return images, nil
|
return images, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
imageOutput := make(chan *epubimage.Image)
|
imageOutput := make(chan epubimage.Image)
|
||||||
|
|
||||||
// processing
|
// processing
|
||||||
bar := epubprogress.New(epubprogress.Options{
|
bar := epubprogress.New(epubprogress.Options{
|
||||||
@ -140,7 +140,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
|
|||||||
return images, nil
|
return images, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *EPUBImageProcessor) createImage(src image.Image, r image.Rectangle) draw.Image {
|
func (e EPUBImageProcessor) createImage(src image.Image, r image.Rectangle) draw.Image {
|
||||||
if e.Options.Image.GrayScale {
|
if e.Options.Image.GrayScale {
|
||||||
return image.NewGray(r)
|
return image.NewGray(r)
|
||||||
}
|
}
|
||||||
@ -173,7 +173,7 @@ func (e *EPUBImageProcessor) createImage(src image.Image, r image.Rectangle) dra
|
|||||||
|
|
||||||
// transform image into 1 or 3 images
|
// transform image into 1 or 3 images
|
||||||
// only doublepage with autosplit has 3 versions
|
// only doublepage with autosplit has 3 versions
|
||||||
func (e *EPUBImageProcessor) transformImage(input *task, part int, right bool) *epubimage.Image {
|
func (e EPUBImageProcessor) transformImage(input task, part int, right bool) epubimage.Image {
|
||||||
g := gift.New()
|
g := gift.New()
|
||||||
src := input.Image
|
src := input.Image
|
||||||
srcBounds := src.Bounds()
|
srcBounds := src.Bounds()
|
||||||
@ -262,7 +262,7 @@ func (e *EPUBImageProcessor) transformImage(input *task, part int, right bool) *
|
|||||||
dst := e.createImage(src, g.Bounds(src.Bounds()))
|
dst := e.createImage(src, g.Bounds(src.Bounds()))
|
||||||
g.Draw(dst, src)
|
g.Draw(dst, src)
|
||||||
|
|
||||||
return &epubimage.Image{
|
return epubimage.Image{
|
||||||
Id: input.Id,
|
Id: input.Id,
|
||||||
Part: part,
|
Part: part,
|
||||||
Raw: dst,
|
Raw: dst,
|
||||||
@ -290,7 +290,7 @@ type CoverTitleDataOptions struct {
|
|||||||
BorderSize int
|
BorderSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *EPUBImageProcessor) Cover16LevelOfGray(bounds image.Rectangle) draw.Image {
|
func (e EPUBImageProcessor) Cover16LevelOfGray(bounds image.Rectangle) draw.Image {
|
||||||
return image.NewPaletted(bounds, color.Palette{
|
return image.NewPaletted(bounds, color.Palette{
|
||||||
color.Gray{},
|
color.Gray{},
|
||||||
color.Gray{Y: 0x11},
|
color.Gray{Y: 0x11},
|
||||||
@ -312,7 +312,7 @@ func (e *EPUBImageProcessor) Cover16LevelOfGray(bounds image.Rectangle) draw.Ima
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CoverTitleData create a title page with the cover
|
// CoverTitleData create a title page with the cover
|
||||||
func (e *EPUBImageProcessor) CoverTitleData(o *CoverTitleDataOptions) (*epubzip.ZipImage, error) {
|
func (e EPUBImageProcessor) CoverTitleData(o CoverTitleDataOptions) (epubzip.ZipImage, error) {
|
||||||
// Create a blur version of the cover
|
// Create a blur version of the cover
|
||||||
g := gift.New(epubimagefilters.CoverTitle(o.Text, o.Align, o.PctWidth, o.PctMargin, o.MaxFontSize, o.BorderSize))
|
g := gift.New(epubimagefilters.CoverTitle(o.Text, o.Align, o.PctWidth, o.PctMargin, o.MaxFontSize, o.BorderSize))
|
||||||
var dst draw.Image
|
var dst draw.Image
|
||||||
|
@ -42,7 +42,7 @@ type task struct {
|
|||||||
var errNoImagesFound = errors.New("no images found")
|
var errNoImagesFound = errors.New("no images found")
|
||||||
|
|
||||||
// only accept jpg, png and webp as source file
|
// only accept jpg, png and webp as source file
|
||||||
func (e *EPUBImageProcessor) isSupportedImage(path string) bool {
|
func (e EPUBImageProcessor) isSupportedImage(path string) bool {
|
||||||
switch strings.ToLower(filepath.Ext(path)) {
|
switch strings.ToLower(filepath.Ext(path)) {
|
||||||
case ".jpg", ".jpeg", ".png", ".webp", ".tiff":
|
case ".jpg", ".jpeg", ".png", ".webp", ".tiff":
|
||||||
{
|
{
|
||||||
@ -53,7 +53,7 @@ func (e *EPUBImageProcessor) isSupportedImage(path string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load images from input
|
// Load images from input
|
||||||
func (e *EPUBImageProcessor) load() (totalImages int, output chan *task, err error) {
|
func (e EPUBImageProcessor) load() (totalImages int, output chan task, err error) {
|
||||||
fi, err := os.Stat(e.Input)
|
fi, err := os.Stat(e.Input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -77,7 +77,7 @@ func (e *EPUBImageProcessor) load() (totalImages int, output chan *task, err err
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *EPUBImageProcessor) corruptedImage(path, name string) image.Image {
|
func (e EPUBImageProcessor) corruptedImage(path, name string) image.Image {
|
||||||
var w, h float64 = 1200, 1920
|
var w, h float64 = 1200, 1920
|
||||||
f, _ := truetype.Parse(gomonobold.TTF)
|
f, _ := truetype.Parse(gomonobold.TTF)
|
||||||
face := truetype.NewFace(f, &truetype.Options{Size: 64, DPI: 72})
|
face := truetype.NewFace(f, &truetype.Options{Size: 64, DPI: 72})
|
||||||
@ -101,7 +101,7 @@ func (e *EPUBImageProcessor) corruptedImage(path, name string) image.Image {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// load a directory of images
|
// load a directory of images
|
||||||
func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *task, err error) {
|
func (e EPUBImageProcessor) loadDir() (totalImages int, output chan task, err error) {
|
||||||
images := make([]string, 0)
|
images := make([]string, 0)
|
||||||
|
|
||||||
input := filepath.Clean(e.Input)
|
input := filepath.Clean(e.Input)
|
||||||
@ -133,16 +133,16 @@ func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *task, err
|
|||||||
Id int
|
Id int
|
||||||
Path string
|
Path string
|
||||||
}
|
}
|
||||||
jobs := make(chan *job)
|
jobs := make(chan job)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(jobs)
|
defer close(jobs)
|
||||||
for i, path := range images {
|
for i, path := range images {
|
||||||
jobs <- &job{i, path}
|
jobs <- job{i, path}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// read in parallel and get an image
|
// read in parallel and get an image
|
||||||
output = make(chan *task, e.Workers)
|
output = make(chan task, e.Workers)
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
for range e.WorkersRatio(50) {
|
for range e.WorkersRatio(50) {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
@ -169,7 +169,7 @@ func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *task, err
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
img = e.corruptedImage(p, fn)
|
img = e.corruptedImage(p, fn)
|
||||||
}
|
}
|
||||||
output <- &task{
|
output <- task{
|
||||||
Id: job.Id,
|
Id: job.Id,
|
||||||
Image: img,
|
Image: img,
|
||||||
Path: p,
|
Path: p,
|
||||||
@ -190,7 +190,7 @@ func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *task, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// load a zip file that include images
|
// load a zip file that include images
|
||||||
func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *task, err error) {
|
func (e EPUBImageProcessor) loadCbz() (totalImages int, output chan task, err error) {
|
||||||
r, err := zip.OpenReader(e.Input)
|
r, err := zip.OpenReader(e.Input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -226,15 +226,15 @@ func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *task, err
|
|||||||
Id int
|
Id int
|
||||||
F *zip.File
|
F *zip.File
|
||||||
}
|
}
|
||||||
jobs := make(chan *job)
|
jobs := make(chan job)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(jobs)
|
defer close(jobs)
|
||||||
for _, img := range images {
|
for _, img := range images {
|
||||||
jobs <- &job{indexedNames[img.Name], img}
|
jobs <- job{indexedNames[img.Name], img}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
output = make(chan *task, e.Workers)
|
output = make(chan task, e.Workers)
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
for range e.WorkersRatio(50) {
|
for range e.WorkersRatio(50) {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
@ -256,7 +256,7 @@ func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *task, err
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
img = e.corruptedImage(p, fn)
|
img = e.corruptedImage(p, fn)
|
||||||
}
|
}
|
||||||
output <- &task{
|
output <- task{
|
||||||
Id: job.Id,
|
Id: job.Id,
|
||||||
Image: img,
|
Image: img,
|
||||||
Path: p,
|
Path: p,
|
||||||
@ -276,7 +276,7 @@ func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *task, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// load a rar file that include images
|
// load a rar file that include images
|
||||||
func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *task, err error) {
|
func (e EPUBImageProcessor) loadCbr() (totalImages int, output chan task, err error) {
|
||||||
var isSolid bool
|
var isSolid bool
|
||||||
files, err := rardecode.List(e.Input)
|
files, err := rardecode.List(e.Input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -312,7 +312,7 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *task, err
|
|||||||
Open func() (io.ReadCloser, error)
|
Open func() (io.ReadCloser, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
jobs := make(chan *job)
|
jobs := make(chan job)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(jobs)
|
defer close(jobs)
|
||||||
if isSolid && !e.Dry {
|
if isSolid && !e.Dry {
|
||||||
@ -340,7 +340,7 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *task, err
|
|||||||
utils.Printf("\nerror processing image %s: %s\n", f.Name, rerr)
|
utils.Printf("\nerror processing image %s: %s\n", f.Name, rerr)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
jobs <- &job{i, f.Name, func() (io.ReadCloser, error) {
|
jobs <- job{i, f.Name, func() (io.ReadCloser, error) {
|
||||||
return io.NopCloser(bytes.NewReader(b.Bytes())), nil
|
return io.NopCloser(bytes.NewReader(b.Bytes())), nil
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
@ -348,14 +348,14 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *task, err
|
|||||||
} else {
|
} else {
|
||||||
for _, img := range files {
|
for _, img := range files {
|
||||||
if i, ok := indexedNames[img.Name]; ok {
|
if i, ok := indexedNames[img.Name]; ok {
|
||||||
jobs <- &job{i, img.Name, img.Open}
|
jobs <- job{i, img.Name, img.Open}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// send file to the queue
|
// send file to the queue
|
||||||
output = make(chan *task, e.Workers)
|
output = make(chan task, e.Workers)
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
for range e.WorkersRatio(50) {
|
for range e.WorkersRatio(50) {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
@ -377,7 +377,7 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *task, err
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
img = e.corruptedImage(p, fn)
|
img = e.corruptedImage(p, fn)
|
||||||
}
|
}
|
||||||
output <- &task{
|
output <- task{
|
||||||
Id: job.Id,
|
Id: job.Id,
|
||||||
Image: img,
|
Image: img,
|
||||||
Path: p,
|
Path: p,
|
||||||
@ -395,7 +395,7 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *task, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// extract image from a pdf
|
// extract image from a pdf
|
||||||
func (e *EPUBImageProcessor) loadPdf() (totalImages int, output chan *task, err error) {
|
func (e EPUBImageProcessor) loadPdf() (totalImages int, output chan task, err error) {
|
||||||
pdf := pdfread.Load(e.Input)
|
pdf := pdfread.Load(e.Input)
|
||||||
if pdf == nil {
|
if pdf == nil {
|
||||||
err = fmt.Errorf("can't read pdf")
|
err = fmt.Errorf("can't read pdf")
|
||||||
@ -404,7 +404,7 @@ func (e *EPUBImageProcessor) loadPdf() (totalImages int, output chan *task, err
|
|||||||
|
|
||||||
totalImages = len(pdf.Pages())
|
totalImages = len(pdf.Pages())
|
||||||
pageFmt := fmt.Sprintf("page %%0%dd", len(fmt.Sprintf("%d", totalImages)))
|
pageFmt := fmt.Sprintf("page %%0%dd", len(fmt.Sprintf("%d", totalImages)))
|
||||||
output = make(chan *task)
|
output = make(chan task)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(output)
|
defer close(output)
|
||||||
defer pdf.Close()
|
defer pdf.Close()
|
||||||
@ -419,7 +419,7 @@ func (e *EPUBImageProcessor) loadPdf() (totalImages int, output chan *task, err
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
img = e.corruptedImage("", name)
|
img = e.corruptedImage("", name)
|
||||||
}
|
}
|
||||||
output <- &task{
|
output <- task{
|
||||||
Id: i,
|
Id: i,
|
||||||
Image: img,
|
Image: img,
|
||||||
Path: "",
|
Path: "",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user