summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--integralimg.go33
-rw-r--r--integralimg_test.go22
2 files changed, 37 insertions, 18 deletions
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]
}
diff --git a/integralimg_test.go b/integralimg_test.go
index bd8a983..35b353d 100644
--- a/integralimg_test.go
+++ b/integralimg_test.go
@@ -70,11 +70,25 @@ func TestSum(t *testing.T) {
draw.Draw(imgplus, b, img, b.Min, draw.Src)
draw.Draw(integral, b, img, b.Min, draw.Src)
- sumimg := imgplus.sum(b)
- sumint := integral.Sum(b)
+ cases := []struct {
+ name string
+ r image.Rectangle
+ }{
+ {"fullimage", b},
+ {"small", image.Rect(1, 1, 5, 5)},
+ {"toobig", image.Rect(0, 0, 2000, b.Dy())},
+ {"toosmall", image.Rect(-1, -1, 4, 5)},
+ {"small2", image.Rect(0, 0, 4, 4)},
+ }
- if sumimg != sumint {
- t.Errorf("Sum of integral image differs to regular image: regular: %d, integral: %d\n", sumimg, sumint)
+ for _, c := range cases{
+ t.Run(c.name, func(t *testing.T) {
+ sumimg := imgplus.sum(c.r)
+ sumint := integral.Sum(c.r)
+ if sumimg != sumint {
+ t.Errorf("Sum of integral image differs to regular image: regular: %d, integral: %d\n", sumimg, sumint)
+ }
+ })
}
}