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 {
|
||||
*epuboptions.Options
|
||||
epuboptions.Options
|
||||
}
|
||||
|
||||
func New(o *epuboptions.Options) *EPUBImageProcessor {
|
||||
return &EPUBImageProcessor{o}
|
||||
func New(o epuboptions.Options) EPUBImageProcessor {
|
||||
return EPUBImageProcessor{o}
|
||||
}
|
||||
|
||||
// Load extract and convert images
|
||||
func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
|
||||
images = make([]*epubimage.Image, 0)
|
||||
func (e EPUBImageProcessor) Load() (images []epubimage.Image, err error) {
|
||||
images = make([]epubimage.Image, 0)
|
||||
imageCount, imageInput, err := e.load()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -38,7 +38,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
|
||||
// dry run, skip conversion
|
||||
if e.Dry {
|
||||
for img := range imageInput {
|
||||
images = append(images, &epubimage.Image{
|
||||
images = append(images, epubimage.Image{
|
||||
Id: img.Id,
|
||||
Path: img.Path,
|
||||
Name: img.Name,
|
||||
@ -49,7 +49,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
|
||||
return images, nil
|
||||
}
|
||||
|
||||
imageOutput := make(chan *epubimage.Image)
|
||||
imageOutput := make(chan epubimage.Image)
|
||||
|
||||
// processing
|
||||
bar := epubprogress.New(epubprogress.Options{
|
||||
@ -140,7 +140,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
|
||||
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 {
|
||||
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
|
||||
// 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()
|
||||
src := input.Image
|
||||
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()))
|
||||
g.Draw(dst, src)
|
||||
|
||||
return &epubimage.Image{
|
||||
return epubimage.Image{
|
||||
Id: input.Id,
|
||||
Part: part,
|
||||
Raw: dst,
|
||||
@ -290,7 +290,7 @@ type CoverTitleDataOptions struct {
|
||||
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{
|
||||
color.Gray{},
|
||||
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
|
||||
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
|
||||
g := gift.New(epubimagefilters.CoverTitle(o.Text, o.Align, o.PctWidth, o.PctMargin, o.MaxFontSize, o.BorderSize))
|
||||
var dst draw.Image
|
||||
|
@ -42,7 +42,7 @@ type task struct {
|
||||
var errNoImagesFound = errors.New("no images found")
|
||||
|
||||
// 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)) {
|
||||
case ".jpg", ".jpeg", ".png", ".webp", ".tiff":
|
||||
{
|
||||
@ -53,7 +53,7 @@ func (e *EPUBImageProcessor) isSupportedImage(path string) bool {
|
||||
}
|
||||
|
||||
// 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)
|
||||
if err != nil {
|
||||
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
|
||||
f, _ := truetype.Parse(gomonobold.TTF)
|
||||
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
|
||||
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)
|
||||
|
||||
input := filepath.Clean(e.Input)
|
||||
@ -133,16 +133,16 @@ func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *task, err
|
||||
Id int
|
||||
Path string
|
||||
}
|
||||
jobs := make(chan *job)
|
||||
jobs := make(chan job)
|
||||
go func() {
|
||||
defer close(jobs)
|
||||
for i, path := range images {
|
||||
jobs <- &job{i, path}
|
||||
jobs <- job{i, path}
|
||||
}
|
||||
}()
|
||||
|
||||
// read in parallel and get an image
|
||||
output = make(chan *task, e.Workers)
|
||||
output = make(chan task, e.Workers)
|
||||
wg := &sync.WaitGroup{}
|
||||
for range e.WorkersRatio(50) {
|
||||
wg.Add(1)
|
||||
@ -169,7 +169,7 @@ func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *task, err
|
||||
if err != nil {
|
||||
img = e.corruptedImage(p, fn)
|
||||
}
|
||||
output <- &task{
|
||||
output <- task{
|
||||
Id: job.Id,
|
||||
Image: img,
|
||||
Path: p,
|
||||
@ -190,7 +190,7 @@ func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *task, err
|
||||
}
|
||||
|
||||
// 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)
|
||||
if err != nil {
|
||||
return
|
||||
@ -226,15 +226,15 @@ func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *task, err
|
||||
Id int
|
||||
F *zip.File
|
||||
}
|
||||
jobs := make(chan *job)
|
||||
jobs := make(chan job)
|
||||
go func() {
|
||||
defer close(jobs)
|
||||
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{}
|
||||
for range e.WorkersRatio(50) {
|
||||
wg.Add(1)
|
||||
@ -256,7 +256,7 @@ func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *task, err
|
||||
if err != nil {
|
||||
img = e.corruptedImage(p, fn)
|
||||
}
|
||||
output <- &task{
|
||||
output <- task{
|
||||
Id: job.Id,
|
||||
Image: img,
|
||||
Path: p,
|
||||
@ -276,7 +276,7 @@ func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *task, err
|
||||
}
|
||||
|
||||
// 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
|
||||
files, err := rardecode.List(e.Input)
|
||||
if err != nil {
|
||||
@ -312,7 +312,7 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *task, err
|
||||
Open func() (io.ReadCloser, error)
|
||||
}
|
||||
|
||||
jobs := make(chan *job)
|
||||
jobs := make(chan job)
|
||||
go func() {
|
||||
defer close(jobs)
|
||||
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)
|
||||
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
|
||||
}}
|
||||
}
|
||||
@ -348,14 +348,14 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *task, err
|
||||
} else {
|
||||
for _, img := range files {
|
||||
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
|
||||
output = make(chan *task, e.Workers)
|
||||
output = make(chan task, e.Workers)
|
||||
wg := &sync.WaitGroup{}
|
||||
for range e.WorkersRatio(50) {
|
||||
wg.Add(1)
|
||||
@ -377,7 +377,7 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *task, err
|
||||
if err != nil {
|
||||
img = e.corruptedImage(p, fn)
|
||||
}
|
||||
output <- &task{
|
||||
output <- task{
|
||||
Id: job.Id,
|
||||
Image: img,
|
||||
Path: p,
|
||||
@ -395,7 +395,7 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *task, err
|
||||
}
|
||||
|
||||
// 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)
|
||||
if pdf == nil {
|
||||
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())
|
||||
pageFmt := fmt.Sprintf("page %%0%dd", len(fmt.Sprintf("%d", totalImages)))
|
||||
output = make(chan *task)
|
||||
output = make(chan task)
|
||||
go func() {
|
||||
defer close(output)
|
||||
defer pdf.Close()
|
||||
@ -419,7 +419,7 @@ func (e *EPUBImageProcessor) loadPdf() (totalImages int, output chan *task, err
|
||||
if err != nil {
|
||||
img = e.corruptedImage("", name)
|
||||
}
|
||||
output <- &task{
|
||||
output <- task{
|
||||
Id: i,
|
||||
Image: img,
|
||||
Path: "",
|
||||
|
Loading…
x
Reference in New Issue
Block a user