From 356391a36c9f802455e0594e50cb475aea44a4fe Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 21 Oct 2020 10:38:10 +0900 Subject: [PATCH] mipmap: Bug fix: Scale could be Inf/0 and caused a forever loop Fixes #1398 --- image_test.go | 21 +++++++++++++++++++++ internal/mipmap/mipmap.go | 25 ++++++++++++++++++++----- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/image_test.go b/image_test.go index 33be215d3..237cf6874 100644 --- a/image_test.go +++ b/image_test.go @@ -2128,3 +2128,24 @@ func TestImageZeroTriangle(t *testing.T) { is := []uint16{} dst.DrawTriangles(vs, is, src, nil) } + +// Issue #1398 +func TestImageDrawImageTooBigScale(t *testing.T) { + dst := NewImage(1, 1) + src := NewImage(1, 1) + + op := &DrawImageOptions{} + op.GeoM.Scale(1e20, 1e20) + dst.DrawImage(src, op) +} + +// Issue #1398 +func TestImageDrawImageTooSmallScale(t *testing.T) { + dst := NewImage(1, 1) + src := NewImage(1, 1) + + op := &DrawImageOptions{} + op.Filter = FilterLinear + op.GeoM.Scale(1e-10, 1e-10) + dst.DrawImage(src, op) +} diff --git a/internal/mipmap/mipmap.go b/internal/mipmap/mipmap.go index 7de1b68be..8b6dca068 100644 --- a/internal/mipmap/mipmap.go +++ b/internal/mipmap/mipmap.go @@ -277,6 +277,8 @@ func (m *Mipmap) disposeMipmaps() { // mipmapLevel returns an appropriate mipmap level for the given distance. func mipmapLevelFromDistance(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1 float32, filter driver.Filter) int { + const maxScale = 6 + if filter == driver.FilterScreen { return 0 } @@ -288,6 +290,19 @@ func mipmapLevelFromDistance(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1 float32, fil } scale := d / s + // Scale can be infinite when the specified scale is extremely big (#1398). + if math.IsInf(float64(scale), 0) { + if filter == driver.FilterNearest { + return -maxScale + } + return 0 + } + + // Scale can be zero when the specified scale is extremely small (#1398). + if scale == 0 { + return 0 + } + // Use 'negative' mipmap to render edges correctly (#611, #907). // It looks like 128 is the enlargement factor that causes edge missings to pass the test TestImageStretch, // but we use 32 here for environments where the float precision is low (#1044, #1270). @@ -316,10 +331,10 @@ func mipmapLevelFromDistance(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1 float32, fil } } - // If tooBigScale is 64, level -6 means that the maximum scale is 64 * 2^6 = 4096. This should be + // If tooBigScale is 32, level -6 means that the maximum scale is 32 * 2^6 = 2048. This should be // enough. - if level < -6 { - level = -6 + if level < -maxScale { + level = -maxScale } return level } @@ -352,8 +367,8 @@ func mipmapLevelFromDistance(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1 float32, fil } } - if level > 6 { - level = 6 + if level > maxScale { + level = maxScale } return level