clip_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2011 The 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. package draw
  5. import (
  6. "image"
  7. "testing"
  8. )
  9. type clipTest struct {
  10. desc string
  11. r, dr, sr, mr image.Rectangle
  12. sp, mp image.Point
  13. nilMask bool
  14. r0 image.Rectangle
  15. sp0, mp0 image.Point
  16. }
  17. var clipTests = []clipTest{
  18. // The following tests all have a nil mask.
  19. {
  20. "basic",
  21. image.Rect(0, 0, 100, 100),
  22. image.Rect(0, 0, 100, 100),
  23. image.Rect(0, 0, 100, 100),
  24. image.ZR,
  25. image.ZP,
  26. image.ZP,
  27. true,
  28. image.Rect(0, 0, 100, 100),
  29. image.ZP,
  30. image.ZP,
  31. },
  32. {
  33. "clip dr",
  34. image.Rect(0, 0, 100, 100),
  35. image.Rect(40, 40, 60, 60),
  36. image.Rect(0, 0, 100, 100),
  37. image.ZR,
  38. image.ZP,
  39. image.ZP,
  40. true,
  41. image.Rect(40, 40, 60, 60),
  42. image.Pt(40, 40),
  43. image.ZP,
  44. },
  45. {
  46. "clip sr",
  47. image.Rect(0, 0, 100, 100),
  48. image.Rect(0, 0, 100, 100),
  49. image.Rect(20, 20, 80, 80),
  50. image.ZR,
  51. image.ZP,
  52. image.ZP,
  53. true,
  54. image.Rect(20, 20, 80, 80),
  55. image.Pt(20, 20),
  56. image.ZP,
  57. },
  58. {
  59. "clip dr and sr",
  60. image.Rect(0, 0, 100, 100),
  61. image.Rect(0, 0, 50, 100),
  62. image.Rect(20, 20, 80, 80),
  63. image.ZR,
  64. image.ZP,
  65. image.ZP,
  66. true,
  67. image.Rect(20, 20, 50, 80),
  68. image.Pt(20, 20),
  69. image.ZP,
  70. },
  71. {
  72. "clip dr and sr, sp outside sr (top-left)",
  73. image.Rect(0, 0, 100, 100),
  74. image.Rect(0, 0, 50, 100),
  75. image.Rect(20, 20, 80, 80),
  76. image.ZR,
  77. image.Pt(15, 8),
  78. image.ZP,
  79. true,
  80. image.Rect(5, 12, 50, 72),
  81. image.Pt(20, 20),
  82. image.ZP,
  83. },
  84. {
  85. "clip dr and sr, sp outside sr (middle-left)",
  86. image.Rect(0, 0, 100, 100),
  87. image.Rect(0, 0, 50, 100),
  88. image.Rect(20, 20, 80, 80),
  89. image.ZR,
  90. image.Pt(15, 66),
  91. image.ZP,
  92. true,
  93. image.Rect(5, 0, 50, 14),
  94. image.Pt(20, 66),
  95. image.ZP,
  96. },
  97. {
  98. "clip dr and sr, sp outside sr (bottom-left)",
  99. image.Rect(0, 0, 100, 100),
  100. image.Rect(0, 0, 50, 100),
  101. image.Rect(20, 20, 80, 80),
  102. image.ZR,
  103. image.Pt(15, 91),
  104. image.ZP,
  105. true,
  106. image.ZR,
  107. image.Pt(15, 91),
  108. image.ZP,
  109. },
  110. {
  111. "clip dr and sr, sp inside sr",
  112. image.Rect(0, 0, 100, 100),
  113. image.Rect(0, 0, 50, 100),
  114. image.Rect(20, 20, 80, 80),
  115. image.ZR,
  116. image.Pt(44, 33),
  117. image.ZP,
  118. true,
  119. image.Rect(0, 0, 36, 47),
  120. image.Pt(44, 33),
  121. image.ZP,
  122. },
  123. // The following tests all have a non-nil mask.
  124. {
  125. "basic mask",
  126. image.Rect(0, 0, 80, 80),
  127. image.Rect(20, 0, 100, 80),
  128. image.Rect(0, 0, 50, 49),
  129. image.Rect(0, 0, 46, 47),
  130. image.ZP,
  131. image.ZP,
  132. false,
  133. image.Rect(20, 0, 46, 47),
  134. image.Pt(20, 0),
  135. image.Pt(20, 0),
  136. },
  137. // TODO(nigeltao): write more tests.
  138. }
  139. func TestClip(t *testing.T) {
  140. dst0 := image.NewRGBA(image.Rect(0, 0, 100, 100))
  141. src0 := image.NewRGBA(image.Rect(0, 0, 100, 100))
  142. mask0 := image.NewRGBA(image.Rect(0, 0, 100, 100))
  143. for _, c := range clipTests {
  144. dst := dst0.SubImage(c.dr).(*image.RGBA)
  145. src := src0.SubImage(c.sr).(*image.RGBA)
  146. var mask image.Image
  147. if !c.nilMask {
  148. mask = mask0.SubImage(c.mr)
  149. }
  150. r, sp, mp := c.r, c.sp, c.mp
  151. clip(dst, &r, src, &sp, mask, &mp)
  152. // Check that the actual results equal the expected results.
  153. if !c.r0.Eq(r) {
  154. t.Errorf("%s: clip rectangle want %v got %v", c.desc, c.r0, r)
  155. continue
  156. }
  157. if !c.sp0.Eq(sp) {
  158. t.Errorf("%s: sp want %v got %v", c.desc, c.sp0, sp)
  159. continue
  160. }
  161. if !c.nilMask {
  162. if !c.mp0.Eq(mp) {
  163. t.Errorf("%s: mp want %v got %v", c.desc, c.mp0, mp)
  164. continue
  165. }
  166. }
  167. // Check that the clipped rectangle is contained by the dst / src / mask
  168. // rectangles, in their respective co-ordinate spaces.
  169. if !r.In(c.dr) {
  170. t.Errorf("%s: c.dr %v does not contain r %v", c.desc, c.dr, r)
  171. }
  172. // sr is r translated into src's co-ordinate space.
  173. sr := r.Add(c.sp.Sub(c.dr.Min))
  174. if !sr.In(c.sr) {
  175. t.Errorf("%s: c.sr %v does not contain sr %v", c.desc, c.sr, sr)
  176. }
  177. if !c.nilMask {
  178. // mr is r translated into mask's co-ordinate space.
  179. mr := r.Add(c.mp.Sub(c.dr.Min))
  180. if !mr.In(c.mr) {
  181. t.Errorf("%s: c.mr %v does not contain mr %v", c.desc, c.mr, mr)
  182. }
  183. }
  184. }
  185. }