Browse Source

Fix over-shrinking during scale-on-load

DarthSim 1 year ago
parent
commit
cdde8918c1
2 changed files with 7 additions and 5 deletions
  1. 1 0
      CHANGELOG.md
  2. 6 5
      processing/scale_on_load.go

+ 1 - 0
CHANGELOG.md

@@ -11,6 +11,7 @@
 ### Fix
 - (pro) Fixed false-positive video detections.
 - Fix parsing some TIFFs.
+- Fix over-shrinking during scale-on-load.
 
 ## [3.23.0] - 2024-03-11
 ### Add

+ 6 - 5
processing/scale_on_load.go

@@ -32,9 +32,7 @@ func canScaleOnLoad(pctx *pipelineContext, imgdata *imagedata.ImageData, scale f
 		imgdata.Type == imagetype.AVIF
 }
 
-func calcJpegShink(scale float64, imgtype imagetype.Type) int {
-	shrink := int(1.0 / scale)
-
+func calcJpegShink(shrink float64) int {
 	switch {
 	case shrink >= 8:
 		return 8
@@ -48,7 +46,10 @@ func calcJpegShink(scale float64, imgtype imagetype.Type) int {
 }
 
 func scaleOnLoad(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
-	prescale := math.Max(pctx.wscale, pctx.hscale)
+	wshrink := float64(pctx.srcWidth) / float64(imath.Scale(pctx.srcWidth, pctx.wscale))
+	hshrink := float64(pctx.srcHeight) / float64(imath.Scale(pctx.srcHeight, pctx.hscale))
+	preshrink := math.Min(wshrink, hshrink)
+	prescale := 1.0 / preshrink
 
 	if !canScaleOnLoad(pctx, imgdata, prescale) {
 		return nil
@@ -76,7 +77,7 @@ func scaleOnLoad(pctx *pipelineContext, img *vips.Image, po *options.ProcessingO
 		pctx.angle = angle
 		pctx.flip = flip
 	} else {
-		jpegShrink := calcJpegShink(prescale, pctx.imgtype)
+		jpegShrink := calcJpegShink(preshrink)
 
 		if pctx.imgtype == imagetype.JPEG && jpegShrink == 1 {
 			return nil