upscale or downscale image automatically

will make panel view works for small image
This commit is contained in:
Celogeek 2023-03-04 12:47:50 +01:00
parent 0360e297b8
commit ef8e88c56b
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
2 changed files with 48 additions and 1 deletions

View File

@ -0,0 +1,47 @@
package filters
import (
"image"
"image/draw"
"github.com/disintegration/gift"
)
func Resize(viewWidth, viewHeight int, resampling gift.Resampling) gift.Filter {
return &resizeFilter{
viewWidth, viewHeight, resampling,
}
}
type resizeFilter struct {
viewWidth, viewHeight int
resampling gift.Resampling
}
func (p *resizeFilter) Bounds(srcBounds image.Rectangle) image.Rectangle {
w, h := p.viewWidth, p.viewHeight
srcw, srch := srcBounds.Dx(), srcBounds.Dy()
if w <= 0 || h <= 0 || srcw <= 0 || srch <= 0 {
return image.Rect(0, 0, 0, 0)
}
wratio := float64(srcw) / float64(w)
hratio := float64(srch) / float64(h)
var dstw, dsth int
if wratio > hratio {
dstw = w
dsth = int(float64(srch)/wratio + 0.5)
} else {
dsth = h
dstw = int(float64(srcw)/hratio + 0.5)
}
return image.Rect(0, 0, dstw, dsth)
}
func (p *resizeFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) {
b := p.Bounds(src.Bounds())
gift.Resize(b.Dx(), b.Dy(), p.resampling).Draw(dst, src, options)
}

View File

@ -19,7 +19,7 @@ func NewGift(options *ImageOptions) *gift.GIFT {
g.Add(gift.Brightness(float32(options.Brightness)))
}
g.Add(
gift.ResizeToFit(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
filters.Pixel(),
)
return g