imagefn - flip_test.go
1 package imagefn
2
3 import (
4 "image"
5 "image/color"
6 "testing"
7 )
8
9 func testImage(a, b image.Image) bool {
10 ab := a.Bounds()
11 bb := b.Bounds()
12 dx := ab.Dx()
13 dy := ab.Dy()
14 if dx != bb.Dx() || dy != bb.Dy() {
15 return false
16 }
17 for j := 0; j < dy; j++ {
18 for i := 0; i < dx; i++ {
19 ar, ag, ab, aa := a.At(ab.Min.X+i, ab.Min.Y+j).RGBA()
20 br, bg, bb, ba := b.At(bb.Min.X+i, bb.Min.Y+j).RGBA()
21 if ar != br || ag != bg || ab != bb || aa != ba {
22 return false
23 }
24 }
25 }
26 return true
27 }
28
29 func newGray(w, h int, pix ...uint8) *image.Gray {
30 g := image.NewGray(image.Rect(0, 0, w, h))
31 Loop:
32 for y := 0; y < h; y++ {
33 for x := 0; x < w; x++ {
34 g.SetGray(x, y, color.Gray{pix[0]})
35 pix = pix[1:]
36 if len(pix) == 0 {
37 break Loop
38 }
39 }
40 }
41 return g
42 }
43
44 func TestFlipX(t *testing.T) {
45 for n, test := range [...]struct {
46 In, Out *image.Gray
47 SubImage image.Rectangle
48 }{
49 {
50 newGray(1, 1, 1),
51 newGray(1, 1, 1),
52 image.Rect(0, 0, 1, 1),
53 },
54 {
55 newGray(1, 2, 0, 1),
56 newGray(1, 2, 0, 1),
57 image.Rect(0, 1, 1, 2),
58 },
59 {
60 newGray(2, 1, 0, 1),
61 newGray(2, 1, 1, 0),
62 image.Rect(0, 0, 1, 1),
63 },
64 {
65 newGray(3, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8),
66 newGray(3, 3, 2, 1, 0, 5, 4, 3, 8, 7, 6),
67 image.Rect(1, 1, 3, 3),
68 },
69 } {
70 fx := FlipX(test.In)
71 if !testImage(fx, test.Out) {
72 t.Errorf("test %d: images do not match", n+1)
73 }
74 if !testImage(SubImage(fx, test.SubImage), test.Out.SubImage(test.SubImage)) {
75 t.Errorf("test %d: subimages do not match", n+1)
76 }
77 }
78 }
79
80 func TestFlipY(t *testing.T) {
81 for n, test := range []struct {
82 In, Out *image.Gray
83 SubImage image.Rectangle
84 }{
85 {
86 newGray(1, 1, 1),
87 newGray(1, 1, 1),
88 image.Rect(0, 0, 1, 1),
89 },
90 {
91 newGray(2, 1, 0, 1),
92 newGray(2, 1, 0, 1),
93 image.Rect(0, 1, 2, 1),
94 },
95 {
96 newGray(1, 2, 0, 1),
97 newGray(1, 2, 1, 0),
98 image.Rect(0, 0, 1, 1),
99 },
100 {
101 newGray(3, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8),
102 newGray(3, 3, 6, 7, 8, 3, 4, 5, 0, 1, 2),
103 image.Rect(1, 1, 3, 3),
104 },
105 } {
106 fy := FlipY(test.In)
107 if !testImage(fy, test.Out) {
108 t.Errorf("test %d: images do not match", n+1)
109 }
110 if !testImage(SubImage(fy, test.SubImage), test.Out.SubImage(test.SubImage)) {
111 t.Errorf("test %d: subimages do not match", n+1)
112 }
113 }
114 }