From 991e95f02e01c33df6a7542186bb3fa9567f7d46 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 14 Jan 2023 16:39:03 +0100 Subject: [PATCH] move filters --- internal/epub/filters/autorotate.go | 31 +++++++++++++++++++++++++++++ internal/epub/image_filters.go | 25 ++--------------------- 2 files changed, 33 insertions(+), 23 deletions(-) create mode 100644 internal/epub/filters/autorotate.go diff --git a/internal/epub/filters/autorotate.go b/internal/epub/filters/autorotate.go new file mode 100644 index 0000000..cd7e14b --- /dev/null +++ b/internal/epub/filters/autorotate.go @@ -0,0 +1,31 @@ +package filters + +import ( + "image" + "image/draw" + + "github.com/disintegration/gift" +) + +func AutoRotate() *autoRotateFilter { + 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/image_filters.go b/internal/epub/image_filters.go index edfebe6..5345c77 100644 --- a/internal/epub/image_filters.go +++ b/internal/epub/image_filters.go @@ -1,9 +1,7 @@ package epub import ( - "image" - "image/draw" - + "github.com/celogeek/go-comic-converter/internal/epub/filters" "github.com/disintegration/gift" ) @@ -12,7 +10,7 @@ func NewGift(options *ImageOptions) *gift.GIFT { g.SetParallelization(false) if options.AutoRotate { - g.Add(&autoRotateFilter{}) + g.Add(filters.AutoRotate()) } if options.Contrast != 0 { g.Add(gift.Contrast(float32(options.Contrast))) @@ -25,22 +23,3 @@ func NewGift(options *ImageOptions) *gift.GIFT { ) 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) - } -}