mipmap: Bug fix: Scale could be Inf/0 and caused a forever loop

Fixes #1398
This commit is contained in:
Hajime Hoshi 2020-10-21 10:38:10 +09:00
parent 75232e315c
commit 356391a36c
2 changed files with 41 additions and 5 deletions

View File

@ -2128,3 +2128,24 @@ func TestImageZeroTriangle(t *testing.T) {
is := []uint16{} is := []uint16{}
dst.DrawTriangles(vs, is, src, nil) 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)
}

View File

@ -277,6 +277,8 @@ func (m *Mipmap) disposeMipmaps() {
// mipmapLevel returns an appropriate mipmap level for the given distance. // 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 { func mipmapLevelFromDistance(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1 float32, filter driver.Filter) int {
const maxScale = 6
if filter == driver.FilterScreen { if filter == driver.FilterScreen {
return 0 return 0
} }
@ -288,6 +290,19 @@ func mipmapLevelFromDistance(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1 float32, fil
} }
scale := d / s 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). // 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, // 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). // 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. // enough.
if level < -6 { if level < -maxScale {
level = -6 level = -maxScale
} }
return level return level
} }
@ -352,8 +367,8 @@ func mipmapLevelFromDistance(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1 float32, fil
} }
} }
if level > 6 { if level > maxScale {
level = 6 level = maxScale
} }
return level return level