Source file
src/image/draw/clip_test.go
1
2
3
4
5 package draw
6
7 import (
8 "image"
9 "testing"
10 )
11
12 type clipTest struct {
13 desc string
14 r, dr, sr, mr image.Rectangle
15 sp, mp image.Point
16 nilMask bool
17 r0 image.Rectangle
18 sp0, mp0 image.Point
19 }
20
21 var clipTests = []clipTest{
22
23 {
24 "basic",
25 image.Rect(0, 0, 100, 100),
26 image.Rect(0, 0, 100, 100),
27 image.Rect(0, 0, 100, 100),
28 image.Rectangle{},
29 image.Point{},
30 image.Point{},
31 true,
32 image.Rect(0, 0, 100, 100),
33 image.Point{},
34 image.Point{},
35 },
36 {
37 "clip dr",
38 image.Rect(0, 0, 100, 100),
39 image.Rect(40, 40, 60, 60),
40 image.Rect(0, 0, 100, 100),
41 image.Rectangle{},
42 image.Point{},
43 image.Point{},
44 true,
45 image.Rect(40, 40, 60, 60),
46 image.Pt(40, 40),
47 image.Point{},
48 },
49 {
50 "clip sr",
51 image.Rect(0, 0, 100, 100),
52 image.Rect(0, 0, 100, 100),
53 image.Rect(20, 20, 80, 80),
54 image.Rectangle{},
55 image.Point{},
56 image.Point{},
57 true,
58 image.Rect(20, 20, 80, 80),
59 image.Pt(20, 20),
60 image.Point{},
61 },
62 {
63 "clip dr and sr",
64 image.Rect(0, 0, 100, 100),
65 image.Rect(0, 0, 50, 100),
66 image.Rect(20, 20, 80, 80),
67 image.Rectangle{},
68 image.Point{},
69 image.Point{},
70 true,
71 image.Rect(20, 20, 50, 80),
72 image.Pt(20, 20),
73 image.Point{},
74 },
75 {
76 "clip dr and sr, sp outside sr (top-left)",
77 image.Rect(0, 0, 100, 100),
78 image.Rect(0, 0, 50, 100),
79 image.Rect(20, 20, 80, 80),
80 image.Rectangle{},
81 image.Pt(15, 8),
82 image.Point{},
83 true,
84 image.Rect(5, 12, 50, 72),
85 image.Pt(20, 20),
86 image.Point{},
87 },
88 {
89 "clip dr and sr, sp outside sr (middle-left)",
90 image.Rect(0, 0, 100, 100),
91 image.Rect(0, 0, 50, 100),
92 image.Rect(20, 20, 80, 80),
93 image.Rectangle{},
94 image.Pt(15, 66),
95 image.Point{},
96 true,
97 image.Rect(5, 0, 50, 14),
98 image.Pt(20, 66),
99 image.Point{},
100 },
101 {
102 "clip dr and sr, sp outside sr (bottom-left)",
103 image.Rect(0, 0, 100, 100),
104 image.Rect(0, 0, 50, 100),
105 image.Rect(20, 20, 80, 80),
106 image.Rectangle{},
107 image.Pt(15, 91),
108 image.Point{},
109 true,
110 image.Rectangle{},
111 image.Pt(15, 91),
112 image.Point{},
113 },
114 {
115 "clip dr and sr, sp inside sr",
116 image.Rect(0, 0, 100, 100),
117 image.Rect(0, 0, 50, 100),
118 image.Rect(20, 20, 80, 80),
119 image.Rectangle{},
120 image.Pt(44, 33),
121 image.Point{},
122 true,
123 image.Rect(0, 0, 36, 47),
124 image.Pt(44, 33),
125 image.Point{},
126 },
127
128
129 {
130 "basic mask",
131 image.Rect(0, 0, 80, 80),
132 image.Rect(20, 0, 100, 80),
133 image.Rect(0, 0, 50, 49),
134 image.Rect(0, 0, 46, 47),
135 image.Point{},
136 image.Point{},
137 false,
138 image.Rect(20, 0, 46, 47),
139 image.Pt(20, 0),
140 image.Pt(20, 0),
141 },
142 {
143 "clip sr and mr",
144 image.Rect(0, 0, 100, 100),
145 image.Rect(0, 0, 100, 100),
146 image.Rect(23, 23, 55, 86),
147 image.Rect(44, 44, 87, 58),
148 image.Pt(10, 10),
149 image.Pt(11, 11),
150 false,
151 image.Rect(33, 33, 45, 47),
152 image.Pt(43, 43),
153 image.Pt(44, 44),
154 },
155 }
156
157 func TestClip(t *testing.T) {
158 dst0 := image.NewRGBA(image.Rect(0, 0, 100, 100))
159 src0 := image.NewRGBA(image.Rect(0, 0, 100, 100))
160 mask0 := image.NewRGBA(image.Rect(0, 0, 100, 100))
161 for _, c := range clipTests {
162 dst := dst0.SubImage(c.dr).(*image.RGBA)
163 src := src0.SubImage(c.sr).(*image.RGBA)
164 r, sp, mp := c.r, c.sp, c.mp
165 if c.nilMask {
166 clip(dst, &r, src, &sp, nil, nil)
167 } else {
168 clip(dst, &r, src, &sp, mask0.SubImage(c.mr), &mp)
169 }
170
171
172 if !c.r0.Eq(r) {
173 t.Errorf("%s: clip rectangle want %v got %v", c.desc, c.r0, r)
174 continue
175 }
176 if !c.sp0.Eq(sp) {
177 t.Errorf("%s: sp want %v got %v", c.desc, c.sp0, sp)
178 continue
179 }
180 if !c.nilMask {
181 if !c.mp0.Eq(mp) {
182 t.Errorf("%s: mp want %v got %v", c.desc, c.mp0, mp)
183 continue
184 }
185 }
186
187
188
189 if !r.In(c.dr) {
190 t.Errorf("%s: c.dr %v does not contain r %v", c.desc, c.dr, r)
191 }
192
193 sr := r.Add(c.sp.Sub(c.dr.Min))
194 if !sr.In(c.sr) {
195 t.Errorf("%s: c.sr %v does not contain sr %v", c.desc, c.sr, sr)
196 }
197 if !c.nilMask {
198
199 mr := r.Add(c.mp.Sub(c.dr.Min))
200 if !mr.In(c.mr) {
201 t.Errorf("%s: c.mr %v does not contain mr %v", c.desc, c.mr, mr)
202 }
203 }
204 }
205 }
206
View as plain text