From 2b7582e776f64d5e3a424bb7e5064ce53b47ab4e Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Wed, 26 Apr 2023 20:56:02 +0200 Subject: [PATCH] move auto crop to filters --- .../epub_filters_autocrop.go} | 19 ++++------ .../epub/filters/epub_filters_autorotate.go | 35 ------------------- ...=> epub_filters_crop_split_double_page.go} | 0 internal/epub/image/epub_image_filters.go | 20 +++++++++-- .../imageprocessing/epub_image_processing.go | 26 +++++++------- 5 files changed, 36 insertions(+), 64 deletions(-) rename internal/epub/{imageprocessing/epub_image_processing_helpers.go => filters/epub_filters_autocrop.go} (85%) delete mode 100644 internal/epub/filters/epub_filters_autorotate.go rename internal/epub/filters/{epub_filters_crop.go => epub_filters_crop_split_double_page.go} (100%) diff --git a/internal/epub/imageprocessing/epub_image_processing_helpers.go b/internal/epub/filters/epub_filters_autocrop.go similarity index 85% rename from internal/epub/imageprocessing/epub_image_processing_helpers.go rename to internal/epub/filters/epub_filters_autocrop.go index 3514897..467013c 100644 --- a/internal/epub/imageprocessing/epub_image_processing_helpers.go +++ b/internal/epub/filters/epub_filters_autocrop.go @@ -1,21 +1,16 @@ -package epubimageprocessing +package epubfilters import ( "image" "image/color" - "path/filepath" - "strings" + + "github.com/disintegration/gift" ) -// only accept jpg, png and webp as source file -func isSupportedImage(path string) bool { - switch strings.ToLower(filepath.Ext(path)) { - case ".jpg", ".jpeg", ".png", ".webp": - { - return true - } - } - return false +func AutoCrop(img image.Image, cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom int) gift.Filter { + return gift.Crop( + findMarging(img, cutRatioOptions{cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom}), + ) } // check if the color is blank enough diff --git a/internal/epub/filters/epub_filters_autorotate.go b/internal/epub/filters/epub_filters_autorotate.go deleted file mode 100644 index 1460c01..0000000 --- a/internal/epub/filters/epub_filters_autorotate.go +++ /dev/null @@ -1,35 +0,0 @@ -/* -Rotate image if the source width > height. -*/ -package epubfilters - -import ( - "image" - "image/draw" - - "github.com/disintegration/gift" -) - -func AutoRotate() gift.Filter { - return &autoRotateFilter{} -} - -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/filters/epub_filters_crop.go b/internal/epub/filters/epub_filters_crop_split_double_page.go similarity index 100% rename from internal/epub/filters/epub_filters_crop.go rename to internal/epub/filters/epub_filters_crop_split_double_page.go diff --git a/internal/epub/image/epub_image_filters.go b/internal/epub/image/epub_image_filters.go index f44f2e3..b21520b 100644 --- a/internal/epub/image/epub_image_filters.go +++ b/internal/epub/image/epub_image_filters.go @@ -1,24 +1,38 @@ package epubimage import ( + "image" + epubfilters "github.com/celogeek/go-comic-converter/v2/internal/epub/filters" "github.com/disintegration/gift" ) // create filter to apply to the source -func NewGift(options *Options) *gift.GIFT { +func NewGift(img image.Image, options *Options) *gift.GIFT { g := gift.New() g.SetParallelization(false) - if options.AutoRotate { - g.Add(epubfilters.AutoRotate()) + if options.Crop { + g.Add(epubfilters.AutoCrop( + img, + options.CropRatioLeft, + options.CropRatioUp, + options.CropRatioRight, + options.CropRatioBottom, + )) } + if options.AutoRotate && img.Bounds().Dx() > img.Bounds().Dy() { + g.Add(gift.Rotate90()) + } + if options.Contrast != 0 { g.Add(gift.Contrast(float32(options.Contrast))) } + if options.Brightness != 0 { g.Add(gift.Brightness(float32(options.Brightness))) } + g.Add( epubfilters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling), epubfilters.Pixel(), diff --git a/internal/epub/imageprocessing/epub_image_processing.go b/internal/epub/imageprocessing/epub_image_processing.go index 06aa993..96684b8 100644 --- a/internal/epub/imageprocessing/epub_image_processing.go +++ b/internal/epub/imageprocessing/epub_image_processing.go @@ -29,6 +29,17 @@ type tasks struct { Name string } +// only accept jpg, png and webp as source file +func isSupportedImage(path string) bool { + switch strings.ToLower(filepath.Ext(path)) { + case ".jpg", ".jpeg", ".png", ".webp": + { + return true + } + } + return false +} + // extract and convert images func LoadImages(o *Options) ([]*epubimage.Image, error) { images := make([]*epubimage.Image, 0) @@ -101,20 +112,7 @@ func LoadImages(o *Options) ([]*epubimage.Image, error) { os.Exit(1) } - if o.Image.Crop { - g := gift.New(gift.Crop(findMarging(src, cutRatioOptions{ - Left: o.Image.CropRatioLeft, - Up: o.Image.CropRatioUp, - Right: o.Image.CropRatioRight, - Bottom: o.Image.CropRatioBottom, - }))) - newSrc := image.NewNRGBA(g.Bounds(src.Bounds())) - g.Draw(newSrc, src) - src = newSrc - } - - g := epubimage.NewGift(o.Image) - + g := epubimage.NewGift(src, o.Image) // Convert image dst := image.NewGray(g.Bounds(src.Bounds())) g.Draw(dst, src)