github.com/aloncn/graphics-go@v0.0.1/graphics/detect/integral_test.go (about) 1 // Copyright 2011 The Graphics-Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package detect 6 7 import ( 8 "bytes" 9 "fmt" 10 "image" 11 "testing" 12 ) 13 14 type integralTest struct { 15 x int 16 y int 17 src []uint8 18 res []uint8 19 } 20 21 var integralTests = []integralTest{ 22 { 23 1, 1, 24 []uint8{0x01}, 25 []uint8{0x01}, 26 }, 27 { 28 2, 2, 29 []uint8{ 30 0x01, 0x02, 31 0x03, 0x04, 32 }, 33 []uint8{ 34 0x01, 0x03, 35 0x04, 0x0a, 36 }, 37 }, 38 { 39 4, 4, 40 []uint8{ 41 0x02, 0x03, 0x00, 0x01, 42 0x01, 0x02, 0x01, 0x05, 43 0x01, 0x01, 0x01, 0x01, 44 0x01, 0x01, 0x01, 0x01, 45 }, 46 []uint8{ 47 0x02, 0x05, 0x05, 0x06, 48 0x03, 0x08, 0x09, 0x0f, 49 0x04, 0x0a, 0x0c, 0x13, 50 0x05, 0x0c, 0x0f, 0x17, 51 }, 52 }, 53 } 54 55 func sprintBox(box []byte, width, height int) string { 56 buf := bytes.NewBuffer(nil) 57 i := 0 58 for y := 0; y < height; y++ { 59 for x := 0; x < width; x++ { 60 fmt.Fprintf(buf, " 0x%02x,", box[i]) 61 i++ 62 } 63 buf.WriteByte('\n') 64 } 65 return buf.String() 66 } 67 68 func TestIntegral(t *testing.T) { 69 for i, oc := range integralTests { 70 src := &image.Gray{ 71 Pix: oc.src, 72 Stride: oc.x, 73 Rect: image.Rect(0, 0, oc.x, oc.y), 74 } 75 dst, _ := newIntegrals(src) 76 res := make([]byte, len(dst.pix)) 77 for i, p := range dst.pix { 78 res[i] = byte(p) 79 } 80 81 if !bytes.Equal(res, oc.res) { 82 got := sprintBox(res, oc.x, oc.y) 83 want := sprintBox(oc.res, oc.x, oc.y) 84 t.Errorf("%d: got\n%s\n want\n%s", i, got, want) 85 } 86 } 87 } 88 89 func TestIntegralSum(t *testing.T) { 90 src := &image.Gray{ 91 Pix: []uint8{ 92 0x02, 0x03, 0x00, 0x01, 0x03, 93 0x01, 0x02, 0x01, 0x05, 0x05, 94 0x01, 0x01, 0x01, 0x01, 0x02, 95 0x01, 0x01, 0x01, 0x01, 0x07, 96 0x02, 0x01, 0x00, 0x03, 0x01, 97 }, 98 Stride: 5, 99 Rect: image.Rect(0, 0, 5, 5), 100 } 101 img, _ := newIntegrals(src) 102 103 type sumTest struct { 104 rect image.Rectangle 105 sum uint64 106 } 107 108 var sumTests = []sumTest{ 109 {image.Rect(0, 0, 1, 1), 2}, 110 {image.Rect(0, 0, 2, 1), 5}, 111 {image.Rect(0, 0, 1, 3), 4}, 112 {image.Rect(1, 1, 3, 3), 5}, 113 {image.Rect(2, 2, 4, 4), 4}, 114 {image.Rect(4, 3, 5, 5), 8}, 115 {image.Rect(2, 4, 3, 5), 0}, 116 } 117 118 for _, st := range sumTests { 119 s := img.sum(st.rect) 120 if s != st.sum { 121 t.Errorf("%v: got %d want %d", st.rect, s, st.sum) 122 return 123 } 124 } 125 } 126 127 func TestIntegralSubImage(t *testing.T) { 128 m0 := &image.Gray{ 129 Pix: []uint8{ 130 0x02, 0x03, 0x00, 0x01, 0x03, 131 0x01, 0x02, 0x01, 0x05, 0x05, 132 0x01, 0x04, 0x01, 0x01, 0x02, 133 0x01, 0x02, 0x01, 0x01, 0x07, 134 0x02, 0x01, 0x09, 0x03, 0x01, 135 }, 136 Stride: 5, 137 Rect: image.Rect(0, 0, 5, 5), 138 } 139 b := image.Rect(1, 1, 4, 4) 140 m1 := m0.SubImage(b) 141 mi0, _ := newIntegrals(m0) 142 mi1, _ := newIntegrals(m1) 143 144 sum0 := mi0.sum(b) 145 sum1 := mi1.sum(b) 146 if sum0 != sum1 { 147 t.Errorf("b got %d want %d", sum0, sum1) 148 } 149 150 r0 := image.Rect(2, 2, 4, 4) 151 sum0 = mi0.sum(r0) 152 sum1 = mi1.sum(r0) 153 if sum0 != sum1 { 154 t.Errorf("r0 got %d want %d", sum1, sum0) 155 } 156 }