summaryrefslogtreecommitdiff
path: root/integralimg_test.go
diff options
context:
space:
mode:
authorNick White <git@njw.name>2020-07-29 18:19:56 +0100
committerNick White <git@njw.name>2020-07-29 18:22:50 +0100
commitd0b28b984bcd1c73dc5782c8d4df2bdc4ab0b310 (patch)
tree16234c71167f804c8a07e838fc49316c2877939d /integralimg_test.go
parentb9e6c0d3ced0f1d56418b8538c79eede0152171b (diff)
Add test of Sum() that compares against a simple implementation for a general Gray16 image
This is what found the bug which was fixed by commit b9e6c0d3.
Diffstat (limited to 'integralimg_test.go')
-rw-r--r--integralimg_test.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/integralimg_test.go b/integralimg_test.go
index 627249f..bd8a983 100644
--- a/integralimg_test.go
+++ b/integralimg_test.go
@@ -52,6 +52,32 @@ func TestSqFromPNG(t *testing.T) {
}
}
+func TestSum(t *testing.T) {
+ f, err := os.Open("testdata/in.png")
+ if err != nil {
+ t.Fatalf("Could not open file %s: %v\n", "testdata/in.png", err)
+ }
+ defer f.Close()
+ img, _, err := image.Decode(f)
+ if err != nil {
+ t.Fatalf("Could not decode image: %v\n", err)
+ }
+ b := img.Bounds()
+
+ imgplus := newGray16Plus(b)
+ integral := NewImage(b)
+
+ 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)
+
+ if sumimg != sumint {
+ t.Errorf("Sum of integral image differs to regular image: regular: %d, integral: %d\n", sumimg, sumint)
+ }
+}
+
func imgsequal(img1, img2 image.Image) bool {
b := img1.Bounds()
if !b.Eq(img2.Bounds()) {
@@ -77,3 +103,24 @@ func imgsequal(img1, img2 image.Image) bool {
}
return true
}
+
+type grayPlus struct {
+ image.Gray16
+}
+
+func newGray16Plus(r image.Rectangle) *grayPlus {
+ var g grayPlus
+ g.Gray16 = *image.NewGray16(r)
+ return &g
+}
+
+func (i grayPlus) sum(r image.Rectangle) uint64 {
+ var sum uint64
+ for y := r.Min.Y; y < r.Max.Y; y++ {
+ for x := r.Min.X; x < r.Max.X; x++ {
+ c := i.Gray16At(x, y).Y
+ sum += uint64(c)
+ }
+ }
+ return sum
+}