tools_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package imaging
  2. import (
  3. "image"
  4. "testing"
  5. )
  6. func TestCrop(t *testing.T) {
  7. td := []struct {
  8. desc string
  9. src image.Image
  10. r image.Rectangle
  11. want *image.NRGBA
  12. }{
  13. {
  14. "Crop 2x3 2x1",
  15. &image.NRGBA{
  16. Rect: image.Rect(-1, -1, 1, 2),
  17. Stride: 2 * 4,
  18. Pix: []uint8{
  19. 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
  20. 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
  21. 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
  22. },
  23. },
  24. image.Rect(-1, 0, 1, 1),
  25. &image.NRGBA{
  26. Rect: image.Rect(0, 0, 2, 1),
  27. Stride: 2 * 4,
  28. Pix: []uint8{
  29. 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
  30. },
  31. },
  32. },
  33. }
  34. for _, d := range td {
  35. got := Crop(d.src, d.r)
  36. want := d.want
  37. if !compareNRGBA(got, want, 0) {
  38. t.Errorf("test [%s] failed: %#v", d.desc, got)
  39. }
  40. }
  41. }
  42. func TestCropCenter(t *testing.T) {
  43. td := []struct {
  44. desc string
  45. src image.Image
  46. w, h int
  47. want *image.NRGBA
  48. }{
  49. {
  50. "CropCenter 2x3 2x1",
  51. &image.NRGBA{
  52. Rect: image.Rect(-1, -1, 1, 2),
  53. Stride: 2 * 4,
  54. Pix: []uint8{
  55. 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
  56. 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
  57. 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
  58. },
  59. },
  60. 2, 1,
  61. &image.NRGBA{
  62. Rect: image.Rect(0, 0, 2, 1),
  63. Stride: 2 * 4,
  64. Pix: []uint8{
  65. 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
  66. },
  67. },
  68. },
  69. }
  70. for _, d := range td {
  71. got := CropCenter(d.src, d.w, d.h)
  72. want := d.want
  73. if !compareNRGBA(got, want, 0) {
  74. t.Errorf("test [%s] failed: %#v", d.desc, got)
  75. }
  76. }
  77. }
  78. func TestPaste(t *testing.T) {
  79. td := []struct {
  80. desc string
  81. src1 image.Image
  82. src2 image.Image
  83. p image.Point
  84. want *image.NRGBA
  85. }{
  86. {
  87. "Paste 2x3 2x1",
  88. &image.NRGBA{
  89. Rect: image.Rect(-1, -1, 1, 2),
  90. Stride: 2 * 4,
  91. Pix: []uint8{
  92. 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
  93. 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
  94. 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
  95. },
  96. },
  97. &image.NRGBA{
  98. Rect: image.Rect(1, 1, 3, 2),
  99. Stride: 2 * 4,
  100. Pix: []uint8{
  101. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  102. },
  103. },
  104. image.Pt(-1, 0),
  105. &image.NRGBA{
  106. Rect: image.Rect(0, 0, 2, 3),
  107. Stride: 2 * 4,
  108. Pix: []uint8{
  109. 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
  110. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  111. 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
  112. },
  113. },
  114. },
  115. }
  116. for _, d := range td {
  117. got := Paste(d.src1, d.src2, d.p)
  118. want := d.want
  119. if !compareNRGBA(got, want, 0) {
  120. t.Errorf("test [%s] failed: %#v", d.desc, got)
  121. }
  122. }
  123. }
  124. func TestPasteCenter(t *testing.T) {
  125. td := []struct {
  126. desc string
  127. src1 image.Image
  128. src2 image.Image
  129. want *image.NRGBA
  130. }{
  131. {
  132. "PasteCenter 2x3 2x1",
  133. &image.NRGBA{
  134. Rect: image.Rect(-1, -1, 1, 2),
  135. Stride: 2 * 4,
  136. Pix: []uint8{
  137. 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
  138. 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
  139. 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
  140. },
  141. },
  142. &image.NRGBA{
  143. Rect: image.Rect(1, 1, 3, 2),
  144. Stride: 2 * 4,
  145. Pix: []uint8{
  146. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  147. },
  148. },
  149. &image.NRGBA{
  150. Rect: image.Rect(0, 0, 2, 3),
  151. Stride: 2 * 4,
  152. Pix: []uint8{
  153. 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
  154. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  155. 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
  156. },
  157. },
  158. },
  159. }
  160. for _, d := range td {
  161. got := PasteCenter(d.src1, d.src2)
  162. want := d.want
  163. if !compareNRGBA(got, want, 0) {
  164. t.Errorf("test [%s] failed: %#v", d.desc, got)
  165. }
  166. }
  167. }
  168. func TestOverlay(t *testing.T) {
  169. td := []struct {
  170. desc string
  171. src1 image.Image
  172. src2 image.Image
  173. p image.Point
  174. a float64
  175. want *image.NRGBA
  176. }{
  177. {
  178. "Overlay 2x3 2x1 1.0",
  179. &image.NRGBA{
  180. Rect: image.Rect(-1, -1, 1, 2),
  181. Stride: 2 * 4,
  182. Pix: []uint8{
  183. 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
  184. 0x60, 0x00, 0x90, 0xff, 0xff, 0x00, 0x99, 0x7f,
  185. 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
  186. },
  187. },
  188. &image.NRGBA{
  189. Rect: image.Rect(1, 1, 3, 2),
  190. Stride: 2 * 4,
  191. Pix: []uint8{
  192. 0x20, 0x40, 0x80, 0x7f, 0xaa, 0xbb, 0xcc, 0xff,
  193. },
  194. },
  195. image.Pt(-1, 0),
  196. 1.0,
  197. &image.NRGBA{
  198. Rect: image.Rect(0, 0, 2, 3),
  199. Stride: 2 * 4,
  200. Pix: []uint8{
  201. 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
  202. 0x40, 0x1f, 0x88, 0xff, 0xaa, 0xbb, 0xcc, 0xff,
  203. 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
  204. },
  205. },
  206. },
  207. {
  208. "Overlay 2x2 2x2 0.5",
  209. &image.NRGBA{
  210. Rect: image.Rect(-1, -1, 1, 1),
  211. Stride: 2 * 4,
  212. Pix: []uint8{
  213. 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
  214. 0x00, 0x00, 0xff, 0xff, 0x20, 0x20, 0x20, 0x00,
  215. },
  216. },
  217. &image.NRGBA{
  218. Rect: image.Rect(-1, -1, 1, 1),
  219. Stride: 2 * 4,
  220. Pix: []uint8{
  221. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  222. 0xff, 0xff, 0x00, 0xff, 0x20, 0x20, 0x20, 0xff,
  223. },
  224. },
  225. image.Pt(-1, -1),
  226. 0.5,
  227. &image.NRGBA{
  228. Rect: image.Rect(0, 0, 2, 2),
  229. Stride: 2 * 4,
  230. Pix: []uint8{
  231. 0xff, 0x7f, 0x7f, 0xff, 0x00, 0xff, 0x00, 0xff,
  232. 0x7f, 0x7f, 0x7f, 0xff, 0x20, 0x20, 0x20, 0x7f,
  233. },
  234. },
  235. },
  236. }
  237. for _, d := range td {
  238. got := Overlay(d.src1, d.src2, d.p, d.a)
  239. want := d.want
  240. if !compareNRGBA(got, want, 1) {
  241. t.Errorf("test [%s] failed: %#v", d.desc, got)
  242. }
  243. }
  244. }