From d0b28b984bcd1c73dc5782c8d4df2bdc4ab0b310 Mon Sep 17 00:00:00 2001 From: Nick White Date: Wed, 29 Jul 2020 18:19:56 +0100 Subject: 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. --- integralimg_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) 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 +} -- cgit v1.2.1-24-ge1ad