From 1fe7dcdd23691212e8393559fc9d7d5100981386 Mon Sep 17 00:00:00 2001 From: Nick White Date: Mon, 3 Aug 2020 15:49:55 +0100 Subject: Add more Sum() tests, and further correct bugs found from it The implementation should be bug-free now. Of course, I thought that previously, then found more, so we shall see. There are more tests, though, and they definitely pass, so that's a good start. --- integralimg.go | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'integralimg.go') diff --git a/integralimg.go b/integralimg.go index 922b1ce..a197920 100644 --- a/integralimg.go +++ b/integralimg.go @@ -140,38 +140,43 @@ func highest(a, b int) int { } func (i Image) topLeft(r image.Rectangle) uint64 { - x := highest(r.Min.X, 0) - y := highest(r.Min.Y, 0) - if x == 0 || y == 0 { + b := i.Bounds() + x := r.Min.X - 1 + y := r.Min.Y - 1 + x = lowest(x, b.Max.X - 1) + y = lowest(y, b.Max.Y - 1) + if x < 0 || y < 0 { return 0 } return i[y][x] } func (i Image) topRight(r image.Rectangle) uint64 { - x := lowest(r.Max.X - 1, i.Bounds().Dx() - 1) - y := highest(r.Min.Y, 0) - if x == 0 || y == 0 { + b := i.Bounds() + x := lowest(r.Max.X - 1, b.Max.X - 1) + y := r.Min.Y - 1 + y = lowest(y, b.Max.Y - 1) + if x < 0 || y < 0 { return 0 } return i[y][x] } func (i Image) bottomLeft(r image.Rectangle) uint64 { - x := highest(r.Min.X, 0) - y := lowest(r.Max.Y - 1, i.Bounds().Dy() - 1) - if x == 0 || y == 0 { + b := i.Bounds() + x := r.Min.X - 1 + x = lowest(x, b.Max.X - 1) + y := lowest(r.Max.Y - 1, b.Max.Y - 1) + if x < 0 || y < 0 { return 0 } return i[y][x] } func (i Image) bottomRight(r image.Rectangle) uint64 { - x := lowest(r.Max.X - 1, i.Bounds().Dx() - 1) - y := lowest(r.Max.Y - 1, i.Bounds().Dy() - 1) - if x == 0 || y == 0 { - return 0 - } + b := i.Bounds() + x := lowest(r.Max.X - 1, b.Max.X - 1) + y := lowest(r.Max.Y - 1, b.Max.Y - 1) return i[y][x] } -- cgit v1.2.1-24-ge1ad