From efdd74f216daa823c81166095788cb2c4957e9f6 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 27 Jan 2024 09:57:46 +0100 Subject: [PATCH 1/6] update deps --- go.mod | 8 ++++---- go.sum | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index bcefd6c..b976d93 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/raff/pdfreader v0.0.0-20220308062436-033e8ac577f0 github.com/schollz/progressbar/v3 v3.14.1 github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e - golang.org/x/image v0.14.0 + golang.org/x/image v0.15.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -23,7 +23,7 @@ require ( github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect github.com/rivo/uniseg v0.4.4 // indirect github.com/stretchr/testify v1.8.4 // indirect - golang.org/x/net v0.19.0 // indirect - golang.org/x/sys v0.15.0 // indirect - golang.org/x/term v0.15.0 // indirect + golang.org/x/net v0.20.0 // indirect + golang.org/x/sys v0.16.0 // indirect + golang.org/x/term v0.16.0 // indirect ) diff --git a/go.sum b/go.sum index 9dd6e76..510d4ea 100644 --- a/go.sum +++ b/go.sum @@ -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/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e h1:IWllFTiDjjLIf2oeKxpIUmtiDV5sn71VgeQgg6vcE7k= github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e/go.mod h1:d7u6HkTYKSv5m6MCKkOQlHwaShTMl3HjqSGW3XtVhXM= -golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4= -golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= -golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= -golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8= +golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= +golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= +golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= 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.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww= -golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= -golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= +golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE= +golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= 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/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From e90a5d8be3a53054b986e725773616b4188a98d2 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 28 Jan 2024 16:42:39 +0100 Subject: [PATCH 2/6] ignore vscode config --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 600d2d3..aafda3a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.vscode \ No newline at end of file +.vscode +.idea From 2442f2edc7112986652033a0aacdfe6c97928746 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 28 Jan 2024 16:43:28 +0100 Subject: [PATCH 3/6] remove unuse attribute --- internal/epub/image/epub_image.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/epub/image/epub_image.go b/internal/epub/image/epub_image.go index 4b84595..d322565 100644 --- a/internal/epub/image/epub_image.go +++ b/internal/epub/image/epub_image.go @@ -15,7 +15,6 @@ type Image struct { Raw image.Image Width int Height int - IsCover bool IsBlank bool DoublePage bool Path string From 51f62588c2777f72e8f234be57fa2f4b42b5bf16 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 28 Jan 2024 16:43:54 +0100 Subject: [PATCH 4/6] add boundaries for autocrop to allow split then crop --- internal/epub/imagefilters/epub_image_filters_autocrop.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/epub/imagefilters/epub_image_filters_autocrop.go b/internal/epub/imagefilters/epub_image_filters_autocrop.go index 9669347..718bcd9 100644 --- a/internal/epub/imagefilters/epub_image_filters_autocrop.go +++ b/internal/epub/imagefilters/epub_image_filters_autocrop.go @@ -8,9 +8,9 @@ import ( ) // Lookup for margin and crop -func AutoCrop(img image.Image, cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom int) gift.Filter { +func AutoCrop(img image.Image, bounds image.Rectangle, cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom int) gift.Filter { return gift.Crop( - findMarging(img, cutRatioOptions{cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom}), + findMargin(img, bounds, cutRatioOptions{cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom}), ) } @@ -25,8 +25,8 @@ type cutRatioOptions struct { Left, Up, Right, Bottom int } -func findMarging(img image.Image, cutRatio cutRatioOptions) image.Rectangle { - imgArea := img.Bounds() +func findMargin(img image.Image, bounds image.Rectangle, cutRatio cutRatioOptions) image.Rectangle { + imgArea := bounds LEFT: for x := imgArea.Min.X; x < imgArea.Max.X; x++ { From 56943aa96cf971f5ea3a98ed1505ee32cb19b2b9 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 28 Jan 2024 16:44:31 +0100 Subject: [PATCH 5/6] remove plural from tasks --- .../epub_image_processor_loader.go | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/internal/epub/imageprocessor/epub_image_processor_loader.go b/internal/epub/imageprocessor/epub_image_processor_loader.go index 3a563a8..4405562 100644 --- a/internal/epub/imageprocessor/epub_image_processor_loader.go +++ b/internal/epub/imageprocessor/epub_image_processor_loader.go @@ -28,7 +28,7 @@ import ( "github.com/raff/pdfreader/pdfread" ) -type tasks struct { +type task struct { Id int Image image.Image Path string @@ -50,7 +50,7 @@ func (e *EPUBImageProcessor) isSupportedImage(path string) bool { } // Load images from input -func (e *EPUBImageProcessor) load() (totalImages int, output chan *tasks, err error) { +func (e *EPUBImageProcessor) load() (totalImages int, output chan *task, err error) { fi, err := os.Stat(e.Input) if err != nil { return @@ -98,7 +98,7 @@ func (e *EPUBImageProcessor) corruptedImage(path, name string) image.Image { } // load a directory of images -func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *tasks, err error) { +func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *task, err error) { images := make([]string, 0) input := filepath.Clean(e.Input) @@ -139,7 +139,7 @@ func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *tasks, err }() // read in parallel and get an image - output = make(chan *tasks, e.Workers) + output = make(chan *task, e.Workers) wg := &sync.WaitGroup{} for j := 0; j < e.WorkersRatio(50); j++ { wg.Add(1) @@ -166,7 +166,7 @@ func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *tasks, err if err != nil { img = e.corruptedImage(p, fn) } - output <- &tasks{ + output <- &task{ Id: job.Id, Image: img, Path: p, @@ -187,7 +187,7 @@ func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *tasks, err } // load a zip file that include images -func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *tasks, err error) { +func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *task, err error) { r, err := zip.OpenReader(e.Input) if err != nil { return @@ -231,7 +231,7 @@ func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *tasks, err } }() - output = make(chan *tasks, e.Workers) + output = make(chan *task, e.Workers) wg := &sync.WaitGroup{} for j := 0; j < e.WorkersRatio(50); j++ { wg.Add(1) @@ -253,7 +253,7 @@ func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *tasks, err if err != nil { img = e.corruptedImage(p, fn) } - output <- &tasks{ + output <- &task{ Id: job.Id, Image: img, Path: p, @@ -273,7 +273,7 @@ func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *tasks, err } // load a rar file that include images -func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *tasks, 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 { @@ -350,7 +350,7 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *tasks, err }() // send file to the queue - output = make(chan *tasks, e.Workers) + output = make(chan *task, e.Workers) wg := &sync.WaitGroup{} for j := 0; j < e.WorkersRatio(50); j++ { wg.Add(1) @@ -372,7 +372,7 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *tasks, err if err != nil { img = e.corruptedImage(p, fn) } - output <- &tasks{ + output <- &task{ Id: job.Id, Image: img, Path: p, @@ -390,7 +390,7 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *tasks, err } // extract image from a pdf -func (e *EPUBImageProcessor) loadPdf() (totalImages int, output chan *tasks, 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") @@ -399,7 +399,7 @@ func (e *EPUBImageProcessor) loadPdf() (totalImages int, output chan *tasks, err totalImages = len(pdf.Pages()) pageFmt := fmt.Sprintf("page %%0%dd", len(fmt.Sprintf("%d", totalImages))) - output = make(chan *tasks) + output = make(chan *task) go func() { defer close(output) defer pdf.Close() @@ -414,7 +414,7 @@ func (e *EPUBImageProcessor) loadPdf() (totalImages int, output chan *tasks, err if err != nil { img = e.corruptedImage("", name) } - output <- &tasks{ + output <- &task{ Id: i, Image: img, Path: "", From 0a60bff6b8cfcf11a6cb09416ba8e93cc24fa0a6 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 28 Jan 2024 16:45:47 +0100 Subject: [PATCH 6/6] reorder transform image for split & crop support --- .../imageprocessor/epub_image_processor.go | 154 ++++++++---------- 1 file changed, 66 insertions(+), 88 deletions(-) diff --git a/internal/epub/imageprocessor/epub_image_processor.go b/internal/epub/imageprocessor/epub_image_processor.go index f0fde37..b52863f 100644 --- a/internal/epub/imageprocessor/epub_image_processor.go +++ b/internal/epub/imageprocessor/epub_image_processor.go @@ -78,43 +78,38 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) { defer wg.Done() for input := range imageInput { - src := input.Image + img := e.transformImage(input, 0, e.Image.Manga) - for part, dst := range e.transformImage(src, input.Id) { - var raw image.Image - if input.Id == 0 && part == 0 { - 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 { + // do not keep double page if requested + if !(img.DoublePage && input.Id > 0 && + e.Options.Image.AutoSplitDoublePage && !e.Options.Image.KeepDoublePageIfSplitted) { + 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) } + // 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 } } @@ -178,14 +173,20 @@ 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(src image.Image, srcId int) []image.Image { - var filters, splitFilters []gift.Filter - var images []image.Image +func (e *EPUBImageProcessor) transformImage(input *task, part int, right bool) *epubimage.Image { + g := gift.New() + src := input.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 if e.Image.Crop.Enabled || e.Image.NoBlankImage { f := epubimagefilters.AutoCrop( src, + g.Bounds(src.Bounds()), e.Image.Crop.Left, e.Image.Crop.Up, e.Image.Crop.Right, @@ -193,41 +194,37 @@ func (e *EPUBImageProcessor) transformImage(src image.Image, srcId int) []image. ) // detect if blank image - size := f.Bounds(src.Bounds()) + size := f.Bounds(srcBounds) isBlank := size.Dx() == 0 && size.Dy() == 0 // crop is enable or if blank image with noblankimage options if e.Image.Crop.Enabled || (e.Image.NoBlankImage && isBlank) { - filters = append(filters, f) - splitFilters = append(splitFilters, f) + g.Add(f) } } - if e.Image.AutoRotate && src.Bounds().Dx() > src.Bounds().Dy() { - filters = append(filters, gift.Rotate90()) + dstBounds := g.Bounds(src.Bounds()) + // Original && Cropped version need to landscape oriented + isDoublePage := srcBounds.Dx() > srcBounds.Dy() && dstBounds.Dx() > dstBounds.Dy() + + if part == 0 && e.Image.AutoRotate && isDoublePage { + g.Add(gift.Rotate90()) } if e.Image.AutoContrast { - f := epubimagefilters.AutoContrast() - filters = append(filters, f) - splitFilters = append(splitFilters, f) + g.Add(epubimagefilters.AutoContrast()) } if e.Image.Contrast != 0 { - f := gift.Contrast(float32(e.Image.Contrast)) - filters = append(filters, f) - splitFilters = append(splitFilters, f) + g.Add(gift.Contrast(float32(e.Image.Contrast))) } if e.Image.Brightness != 0 { - f := gift.Brightness(float32(e.Image.Brightness)) - filters = append(filters, f) - splitFilters = append(splitFilters, f) + g.Add(gift.Brightness(float32(e.Image.Brightness))) } if e.Image.Resize { - f := gift.ResizeToFit(e.Image.View.Width, e.Image.View.Height, gift.LanczosResampling) - filters = append(filters, f) + g.Add(gift.ResizeToFit(e.Image.View.Width, e.Image.View.Height, gift.LanczosResampling)) } if e.Image.GrayScale { @@ -246,48 +243,29 @@ func (e *EPUBImageProcessor) transformImage(src image.Image, srcId int) []image. default: f = gift.Grayscale() } - filters = append(filters, f) - splitFilters = append(splitFilters, f) + g.Add(f) } - filters = append(filters, epubimagefilters.Pixel()) + g.Add(epubimagefilters.Pixel()) - // convert - { - g := gift.New(filters...) - dst := e.createImage(src, g.Bounds(src.Bounds())) - g.Draw(dst, src) - images = append(images, dst) + dst := e.createImage(src, g.Bounds(src.Bounds())) + g.Draw(dst, src) + + return &epubimage.Image{ + Id: input.Id, + Part: part, + 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 {