From 40fe09b15e737dd69082a41c5b2c06929489d49c Mon Sep 17 00:00:00 2001 From: DarthSim Date: Mon, 16 Sep 2019 23:09:28 +0600 Subject: [PATCH] More accurate scale calculation --- process.go | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/process.go b/process.go index b8afad12..aff34e7d 100644 --- a/process.go +++ b/process.go @@ -39,15 +39,24 @@ func extractMeta(img *vipsImage) (int, int, int, bool) { } func calcScale(width, height int, po *processingOptions, imgtype imageType) float64 { - var scale float64 + var shrink float64 srcW, srcH := float64(width), float64(height) + dstW, dstH := float64(po.Width), float64(po.Height) - if (po.Width == 0 || po.Width == width) && (po.Height == 0 || po.Height == height) { - scale = 1 + if po.Width == 0 { + dstW = srcW + } + + if po.Height == 0 { + dstH = srcH + } + + if dstW == srcW && dstH == srcH { + shrink = 1 } else { - wr := float64(po.Width) / srcW - hr := float64(po.Height) / srcH + wshrink := srcW / dstW + hshrink := srcH / dstH rt := po.Resize @@ -64,31 +73,31 @@ func calcScale(width, height int, po *processingOptions, imgtype imageType) floa switch { case po.Width == 0: - scale = hr + shrink = hshrink case po.Height == 0: - scale = wr + shrink = wshrink case rt == resizeFit: - scale = math.Min(wr, hr) + shrink = math.Max(wshrink, hshrink) default: - scale = math.Max(wr, hr) + shrink = math.Min(wshrink, hshrink) } } - if !po.Enlarge && scale > 1 && imgtype != imageTypeSVG { - scale = 1 + if !po.Enlarge && shrink < 1 && imgtype != imageTypeSVG { + shrink = 1 } - scale *= po.Dpr + shrink /= po.Dpr - if srcW*scale < 1 { - scale = 1 / srcW + if shrink > srcW { + shrink = srcW } - if srcH*scale < 1 { - scale = 1 / srcH + if shrink > srcH { + shrink = srcH } - return scale + return 1.0 / shrink } func canScaleOnLoad(imgtype imageType, scale float64) bool {