vector: fix overflow when rasterizing wide lines.

Change-Id: Iea92b74ca9533de2ef17534ee3acf4f40c3d03ef
Reviewed-on: https://go-review.googlesource.com/30899
Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
Nigel Tao 2016-10-13 18:47:49 +11:00
parent 8edbaf3f9e
commit beb9675609
2 changed files with 24 additions and 3 deletions

View File

@ -176,9 +176,9 @@ func (z *Rasterizer) fixedLineTo(b f32.Vec2) {
// //
// Convert to int64 to avoid overflow. Without that, // Convert to int64 to avoid overflow. Without that,
// TestRasterizePolygon fails. // TestRasterizePolygon fails.
D := int64(twoOverS << ϕ) D := int64(twoOverS) << ϕ
D -= int64((fxOneAndAHalf - x0f) << (ϕ + 1)) D -= int64((fxOneAndAHalf - x0f)) << (ϕ + 1)
D -= int64((x1i - x0i - 3) << (2*ϕ + 1)) D -= int64((x1i - x0i - 3)) << (2*ϕ + 1)
D -= int64(x1fSquared) D -= int64(x1fSquared)
D *= int64(d) D *= int64(d)
D /= int64(twoOverS) D /= int64(twoOverS)

View File

@ -125,6 +125,27 @@ func TestRasterizeAlmostAxisAligned(t *testing.T) {
} }
} }
func TestRasterizeWideAlmostHorizontalLines(t *testing.T) {
var z Rasterizer
for i := uint(3); i < 16; i++ {
x := float32(int(1 << i))
z.Reset(8, 8)
z.MoveTo(f32.Vec2{-x, 3})
z.LineTo(f32.Vec2{+x, 4})
z.LineTo(f32.Vec2{+x, 6})
z.LineTo(f32.Vec2{-x, 6})
z.ClosePath()
dst := image.NewAlpha(z.Bounds())
z.Draw(dst, dst.Bounds(), image.Opaque, image.Point{})
if err := checkCornersCenter(dst); err != nil {
t.Errorf("i=%d: %v", i, err)
}
}
}
// checkCornersCenter checks that the corners of the image are all 0x00 and the // checkCornersCenter checks that the corners of the image are all 0x00 and the
// center is 0xff. // center is 0xff.
func checkCornersCenter(m *image.Alpha) error { func checkCornersCenter(m *image.Alpha) error {