From 3524ce6d043596c17d1b0c96d482315fc61ae76c Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Tue, 3 Jan 2023 19:58:19 +0100 Subject: [PATCH] add workers and autorotate options --- internal/epub/core.go | 2 ++ internal/epub/image_filters.go | 46 +++++++++++++++++++++++++++++++ internal/epub/image_processing.go | 21 ++++++-------- main.go | 11 ++++++++ 4 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 internal/epub/image_filters.go diff --git a/internal/epub/core.go b/internal/epub/core.go index 1c6cef0..09602f5 100644 --- a/internal/epub/core.go +++ b/internal/epub/core.go @@ -20,6 +20,8 @@ type ImageOptions struct { Palette color.Palette Brightness int Contrast int + AutoRotate bool + Workers int } type EpubOptions struct { diff --git a/internal/epub/image_filters.go b/internal/epub/image_filters.go new file mode 100644 index 0000000..edfebe6 --- /dev/null +++ b/internal/epub/image_filters.go @@ -0,0 +1,46 @@ +package epub + +import ( + "image" + "image/draw" + + "github.com/disintegration/gift" +) + +func NewGift(options *ImageOptions) *gift.GIFT { + g := gift.New() + g.SetParallelization(false) + + if options.AutoRotate { + g.Add(&autoRotateFilter{}) + } + if options.Contrast != 0 { + g.Add(gift.Contrast(float32(options.Contrast))) + } + if options.Brightness != 0 { + g.Add(gift.Brightness(float32(options.Brightness))) + } + g.Add( + gift.ResizeToFit(options.ViewWidth, options.ViewHeight, gift.LanczosResampling), + ) + return g +} + +type autoRotateFilter struct{} + +func (p *autoRotateFilter) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) { + if srcBounds.Dx() > srcBounds.Dy() { + dstBounds = gift.Rotate90().Bounds(srcBounds) + } else { + dstBounds = srcBounds + } + return +} + +func (p *autoRotateFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) { + if src.Bounds().Dx() > src.Bounds().Dy() { + gift.Rotate90().Draw(dst, src, options) + } else { + draw.Draw(dst, dst.Bounds(), src, src.Bounds().Min, draw.Src) + } +} diff --git a/internal/epub/image_processing.go b/internal/epub/image_processing.go index 01c36ae..3589542 100644 --- a/internal/epub/image_processing.go +++ b/internal/epub/image_processing.go @@ -11,13 +11,11 @@ import ( "io/fs" "os" "path/filepath" - "runtime" "sort" "strings" "sync" "github.com/disintegration/gift" - "github.com/nwaples/rardecode" pdfimage "github.com/raff/pdfreader/image" "github.com/raff/pdfreader/pdfread" @@ -122,9 +120,8 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) { // processing wg := &sync.WaitGroup{} - bar := NewBar(imageCount, "Processing", 1, 2) - for i := 0; i < runtime.NumCPU(); i++ { + for i := 0; i < options.Workers; i++ { wg.Add(1) go func() { defer wg.Done() @@ -136,14 +133,14 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) { panic(err) } - // prepare filter - g := gift.New( - gift.Crop(findMarging(src)), - gift.Contrast(float32(options.Contrast)), - gift.Brightness(float32(options.Brightness)), - gift.ResizeToFit(options.ViewWidth, options.ViewHeight, gift.LanczosResampling), - ) - g.SetParallelization(false) + if options.AutoRotate { + g := gift.New(gift.Crop(findMarging(src))) + newSrc := image.NewNRGBA(g.Bounds(src.Bounds())) + g.Draw(newSrc, src) + src = newSrc + } + + g := NewGift(options) // Convert image dst := image.NewPaletted(g.Bounds(src.Bounds()), options.Palette) diff --git a/main.go b/main.go index 315fd0b..0234099 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "image/color" "os" "path/filepath" + "runtime" "strings" "github.com/celogeek/go-comic-converter/internal/epub" @@ -65,6 +66,8 @@ type Option struct { NoCrop bool Brightness int Contrast int + AutoRotate bool + Workers int LimitMb int } @@ -95,7 +98,9 @@ Options: Crop : %v Brightness: %d Contrast : %d + AutoRotate: %v LimitMb : %s + Workers : %d `, o.Input, o.Output, @@ -106,7 +111,9 @@ Options: !o.NoCrop, o.Brightness, o.Contrast, + o.AutoRotate, limitmb, + o.Workers, ) } @@ -132,7 +139,9 @@ func main() { flag.BoolVar(&opt.NoCrop, "nocrop", false, "Disable cropping") flag.IntVar(&opt.Brightness, "brightness", 0, "Brightness readjustement: between -100 and 100, > 0 lighter, < 0 darker") flag.IntVar(&opt.Contrast, "contrast", 0, "Contrast readjustement: between -100 and 100, > 0 more contrast, < 0 less contrast") + flag.BoolVar(&opt.AutoRotate, "autorotate", false, "Auto Rotate page when width > height") flag.IntVar(&opt.LimitMb, "limitmb", 0, "Limit size of the ePub: Default nolimit (0), Minimum 20") + flag.IntVar(&opt.Workers, "workers", runtime.NumCPU(), "Number of workers") flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage of %s:\n", filepath.Base(os.Args[0])) flag.PrintDefaults() @@ -229,6 +238,8 @@ func main() { Palette: profile.Palette, Brightness: opt.Brightness, Contrast: opt.Contrast, + AutoRotate: opt.AutoRotate, + Workers: opt.Workers, }, }).Write(); err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err)