Compare commits

..

No commits in common. "58ea72ce82b362538a38b004040973f92d51dda9" and "6c92597d8cd4480fcf9d14b69ab7ad69418afede" have entirely different histories.

7 changed files with 120 additions and 98 deletions

3
.gitignore vendored
View File

@ -1,2 +1 @@
.vscode .vscode
.idea

8
go.mod
View File

@ -12,7 +12,7 @@ require (
github.com/raff/pdfreader v0.0.0-20220308062436-033e8ac577f0 github.com/raff/pdfreader v0.0.0-20220308062436-033e8ac577f0
github.com/schollz/progressbar/v3 v3.14.1 github.com/schollz/progressbar/v3 v3.14.1
github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e
golang.org/x/image v0.15.0 golang.org/x/image v0.14.0
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )
@ -23,7 +23,7 @@ require (
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/rivo/uniseg v0.4.4 // indirect github.com/rivo/uniseg v0.4.4 // indirect
github.com/stretchr/testify v1.8.4 // indirect github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/net v0.20.0 // indirect golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.16.0 // indirect golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.16.0 // indirect golang.org/x/term v0.15.0 // indirect
) )

16
go.sum
View File

@ -39,17 +39,17 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e h1:IWllFTiDjjLIf2oeKxpIUmtiDV5sn71VgeQgg6vcE7k= github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e h1:IWllFTiDjjLIf2oeKxpIUmtiDV5sn71VgeQgg6vcE7k=
github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e/go.mod h1:d7u6HkTYKSv5m6MCKkOQlHwaShTMl3HjqSGW3XtVhXM= github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e/go.mod h1:d7u6HkTYKSv5m6MCKkOQlHwaShTMl3HjqSGW3XtVhXM=
golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8= golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww= golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE= golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@ -15,6 +15,7 @@ type Image struct {
Raw image.Image Raw image.Image
Width int Width int
Height int Height int
IsCover bool
IsBlank bool IsBlank bool
DoublePage bool DoublePage bool
Path string Path string

View File

@ -8,9 +8,9 @@ import (
) )
// Lookup for margin and crop // Lookup for margin and crop
func AutoCrop(img image.Image, bounds image.Rectangle, cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom int) gift.Filter { func AutoCrop(img image.Image, cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom int) gift.Filter {
return gift.Crop( return gift.Crop(
findMargin(img, bounds, cutRatioOptions{cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom}), findMarging(img, cutRatioOptions{cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom}),
) )
} }
@ -25,8 +25,8 @@ type cutRatioOptions struct {
Left, Up, Right, Bottom int Left, Up, Right, Bottom int
} }
func findMargin(img image.Image, bounds image.Rectangle, cutRatio cutRatioOptions) image.Rectangle { func findMarging(img image.Image, cutRatio cutRatioOptions) image.Rectangle {
imgArea := bounds imgArea := img.Bounds()
LEFT: LEFT:
for x := imgArea.Min.X; x < imgArea.Max.X; x++ { for x := imgArea.Min.X; x < imgArea.Max.X; x++ {

View File

@ -78,38 +78,43 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
defer wg.Done() defer wg.Done()
for input := range imageInput { for input := range imageInput {
img := e.transformImage(input, 0, e.Image.Manga) src := input.Image
// do not keep double page if requested for part, dst := range e.transformImage(src, input.Id) {
if !(img.DoublePage && input.Id > 0 && var raw image.Image
e.Options.Image.AutoSplitDoublePage && !e.Options.Image.KeepDoublePageIfSplitted) { if input.Id == 0 && part == 0 {
if err = imgStorage.Add(img.EPUBImgPath(), img.Raw, e.Image.Quality); err != nil { raw = dst
}
img := &epubimage.Image{
Id: input.Id,
Part: part,
Raw: raw,
Width: dst.Bounds().Dx(),
Height: dst.Bounds().Dy(),
IsCover: input.Id == 0 && part == 0,
IsBlank: dst.Bounds().Dx() == 1 && dst.Bounds().Dy() == 1,
DoublePage: part == 0 && src.Bounds().Dx() > src.Bounds().Dy(),
Path: input.Path,
Name: input.Name,
Format: e.Image.Format,
OriginalAspectRatio: float64(src.Bounds().Dy()) / float64(src.Bounds().Dx()),
Error: input.Error,
}
// do not keep double page if requested
if !img.IsCover &&
img.DoublePage &&
e.Options.Image.AutoSplitDoublePage &&
!e.Options.Image.KeepDoublePageIfSplitted {
continue
}
if err = imgStorage.Add(img.EPUBImgPath(), dst, e.Image.Quality); err != nil {
bar.Close() bar.Close()
fmt.Fprintf(os.Stderr, "error with %s: %s", input.Name, err) fmt.Fprintf(os.Stderr, "error with %s: %s", input.Name, err)
os.Exit(1) os.Exit(1)
} }
// do not keep raw image except for cover
if img.Id > 0 {
img.Raw = nil
}
imageOutput <- img
}
// DOUBLE PAGE
if !e.Image.AutoSplitDoublePage || // No split required
!img.DoublePage || // Not a double page
(e.Image.HasCover && img.Id == 0) { // Cover
continue
}
for i, b := range []bool{e.Image.Manga, !e.Image.Manga} {
img = e.transformImage(input, i+1, b)
if err = imgStorage.Add(img.EPUBImgPath(), img.Raw, e.Image.Quality); err != nil {
bar.Close()
fmt.Fprintf(os.Stderr, "error with %s: %s", input.Name, err)
os.Exit(1)
}
img.Raw = nil
imageOutput <- img imageOutput <- img
} }
} }
@ -173,20 +178,14 @@ 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(src image.Image, srcId int) []image.Image {
g := gift.New() var filters, splitFilters []gift.Filter
src := input.Image var images []image.Image
srcBounds := src.Bounds()
if part > 0 {
g.Add(epubimagefilters.CropSplitDoublePage(right))
}
// Lookup for margin if crop is enable or if we want to remove blank image // Lookup for margin if crop is enable or if we want to remove blank image
if e.Image.Crop.Enabled || e.Image.NoBlankImage { if e.Image.Crop.Enabled || e.Image.NoBlankImage {
f := epubimagefilters.AutoCrop( f := epubimagefilters.AutoCrop(
src, src,
g.Bounds(src.Bounds()),
e.Image.Crop.Left, e.Image.Crop.Left,
e.Image.Crop.Up, e.Image.Crop.Up,
e.Image.Crop.Right, e.Image.Crop.Right,
@ -194,37 +193,41 @@ func (e *EPUBImageProcessor) transformImage(input *task, part int, right bool) *
) )
// detect if blank image // detect if blank image
size := f.Bounds(srcBounds) size := f.Bounds(src.Bounds())
isBlank := size.Dx() == 0 && size.Dy() == 0 isBlank := size.Dx() == 0 && size.Dy() == 0
// crop is enable or if blank image with noblankimage options // crop is enable or if blank image with noblankimage options
if e.Image.Crop.Enabled || (e.Image.NoBlankImage && isBlank) { if e.Image.Crop.Enabled || (e.Image.NoBlankImage && isBlank) {
g.Add(f) filters = append(filters, f)
splitFilters = append(splitFilters, f)
} }
} }
dstBounds := g.Bounds(src.Bounds()) if e.Image.AutoRotate && src.Bounds().Dx() > src.Bounds().Dy() {
// Original && Cropped version need to landscape oriented filters = append(filters, gift.Rotate90())
isDoublePage := srcBounds.Dx() > srcBounds.Dy() && dstBounds.Dx() > dstBounds.Dy()
if part == 0 && e.Image.AutoRotate && isDoublePage {
g.Add(gift.Rotate90())
} }
if e.Image.AutoContrast { if e.Image.AutoContrast {
g.Add(epubimagefilters.AutoContrast()) f := epubimagefilters.AutoContrast()
filters = append(filters, f)
splitFilters = append(splitFilters, f)
} }
if e.Image.Contrast != 0 { if e.Image.Contrast != 0 {
g.Add(gift.Contrast(float32(e.Image.Contrast))) f := gift.Contrast(float32(e.Image.Contrast))
filters = append(filters, f)
splitFilters = append(splitFilters, f)
} }
if e.Image.Brightness != 0 { if e.Image.Brightness != 0 {
g.Add(gift.Brightness(float32(e.Image.Brightness))) f := gift.Brightness(float32(e.Image.Brightness))
filters = append(filters, f)
splitFilters = append(splitFilters, f)
} }
if e.Image.Resize { if e.Image.Resize {
g.Add(gift.ResizeToFit(e.Image.View.Width, e.Image.View.Height, gift.LanczosResampling)) f := gift.ResizeToFit(e.Image.View.Width, e.Image.View.Height, gift.LanczosResampling)
filters = append(filters, f)
} }
if e.Image.GrayScale { if e.Image.GrayScale {
@ -243,29 +246,48 @@ func (e *EPUBImageProcessor) transformImage(input *task, part int, right bool) *
default: default:
f = gift.Grayscale() f = gift.Grayscale()
} }
g.Add(f) filters = append(filters, f)
splitFilters = append(splitFilters, f)
} }
g.Add(epubimagefilters.Pixel()) filters = append(filters, epubimagefilters.Pixel())
dst := e.createImage(src, g.Bounds(src.Bounds())) // convert
g.Draw(dst, src) {
g := gift.New(filters...)
return &epubimage.Image{ dst := e.createImage(src, g.Bounds(src.Bounds()))
Id: input.Id, g.Draw(dst, src)
Part: part, images = append(images, dst)
Raw: dst,
Width: dst.Bounds().Dx(),
Height: dst.Bounds().Dy(),
IsBlank: dst.Bounds().Dx() == 1 && dst.Bounds().Dy() == 1,
DoublePage: isDoublePage,
Path: input.Path,
Name: input.Name,
Format: e.Image.Format,
OriginalAspectRatio: float64(src.Bounds().Dy()) / float64(src.Bounds().Dx()),
Error: input.Error,
} }
// auto split off
if !e.Image.AutoSplitDoublePage {
return images
}
// portrait, no need to split
if src.Bounds().Dx() <= src.Bounds().Dy() {
return images
}
// cover
if e.Image.HasCover && srcId == 0 {
return images
}
// convert double page
for _, b := range []bool{e.Image.Manga, !e.Image.Manga} {
g := gift.New(splitFilters...)
g.Add(epubimagefilters.CropSplitDoublePage(b))
if e.Image.Resize {
g.Add(gift.ResizeToFit(e.Image.View.Width, e.Image.View.Height, gift.LanczosResampling))
}
dst := e.createImage(src, g.Bounds(src.Bounds()))
g.Draw(dst, src)
images = append(images, dst)
}
return images
} }
type CoverTitleDataOptions struct { type CoverTitleDataOptions struct {

View File

@ -28,7 +28,7 @@ import (
"github.com/raff/pdfreader/pdfread" "github.com/raff/pdfreader/pdfread"
) )
type task struct { type tasks struct {
Id int Id int
Image image.Image Image image.Image
Path string Path string
@ -50,7 +50,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 *tasks, err error) {
fi, err := os.Stat(e.Input) fi, err := os.Stat(e.Input)
if err != nil { if err != nil {
return return
@ -98,7 +98,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 *tasks, err error) {
images := make([]string, 0) images := make([]string, 0)
input := filepath.Clean(e.Input) input := filepath.Clean(e.Input)
@ -139,7 +139,7 @@ func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *task, err
}() }()
// read in parallel and get an image // read in parallel and get an image
output = make(chan *task, e.Workers) output = make(chan *tasks, e.Workers)
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
for j := 0; j < e.WorkersRatio(50); j++ { for j := 0; j < e.WorkersRatio(50); j++ {
wg.Add(1) wg.Add(1)
@ -166,7 +166,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 <- &tasks{
Id: job.Id, Id: job.Id,
Image: img, Image: img,
Path: p, Path: p,
@ -187,7 +187,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 *tasks, err error) {
r, err := zip.OpenReader(e.Input) r, err := zip.OpenReader(e.Input)
if err != nil { if err != nil {
return return
@ -231,7 +231,7 @@ func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *task, err
} }
}() }()
output = make(chan *task, e.Workers) output = make(chan *tasks, e.Workers)
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
for j := 0; j < e.WorkersRatio(50); j++ { for j := 0; j < e.WorkersRatio(50); j++ {
wg.Add(1) wg.Add(1)
@ -253,7 +253,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 <- &tasks{
Id: job.Id, Id: job.Id,
Image: img, Image: img,
Path: p, Path: p,
@ -273,7 +273,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 *tasks, 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 {
@ -350,7 +350,7 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *task, err
}() }()
// send file to the queue // send file to the queue
output = make(chan *task, e.Workers) output = make(chan *tasks, e.Workers)
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
for j := 0; j < e.WorkersRatio(50); j++ { for j := 0; j < e.WorkersRatio(50); j++ {
wg.Add(1) wg.Add(1)
@ -372,7 +372,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 <- &tasks{
Id: job.Id, Id: job.Id,
Image: img, Image: img,
Path: p, Path: p,
@ -390,7 +390,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 *tasks, 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")
@ -399,7 +399,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 *tasks)
go func() { go func() {
defer close(output) defer close(output)
defer pdf.Close() defer pdf.Close()
@ -414,7 +414,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 <- &tasks{
Id: i, Id: i,
Image: img, Image: img,
Path: "", Path: "",