auto rotate if necessary

This commit is contained in:
Celogeek 2023-01-15 15:38:15 +01:00
parent dbb93e2f73
commit e8f806141e
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
2 changed files with 21 additions and 6 deletions

View File

@ -7,14 +7,29 @@ import (
"github.com/disintegration/gift"
)
func AutoRotate() gift.Filter {
return &autoRotateFilter{}
func AutoRotate(viewWidth, viewHeight int) gift.Filter {
return &autoRotateFilter{
viewWidth, viewHeight,
}
}
type autoRotateFilter struct{}
type autoRotateFilter struct {
viewWidth, viewHeight int
}
func (p *autoRotateFilter) needRotate(srcBounds image.Rectangle) bool {
width, height := srcBounds.Dx(), srcBounds.Dy()
if width <= height {
return false
}
if width <= p.viewWidth && height <= p.viewHeight {
return false
}
return true
}
func (p *autoRotateFilter) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) {
if srcBounds.Dx() > srcBounds.Dy() {
if p.needRotate(srcBounds) {
dstBounds = gift.Rotate90().Bounds(srcBounds)
} else {
dstBounds = srcBounds
@ -23,7 +38,7 @@ func (p *autoRotateFilter) Bounds(srcBounds image.Rectangle) (dstBounds image.Re
}
func (p *autoRotateFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) {
if src.Bounds().Dx() > src.Bounds().Dy() {
if p.needRotate(src.Bounds()) {
gift.Rotate90().Draw(dst, src, options)
} else {
draw.Draw(dst, dst.Bounds(), src, src.Bounds().Min, draw.Src)

View File

@ -10,7 +10,7 @@ func NewGift(options *ImageOptions) *gift.GIFT {
g.SetParallelization(false)
if options.AutoRotate {
g.Add(filters.AutoRotate())
g.Add(filters.AutoRotate(options.ViewWidth, options.ViewHeight))
}
if options.Contrast != 0 {
g.Add(gift.Contrast(float32(options.Contrast)))