transform_test.go 5.7 KB

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