transform_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. package imaging
  2. import (
  3. "image"
  4. "image/color"
  5. "testing"
  6. )
  7. func TestFlipH(t *testing.T) {
  8. td := []struct {
  9. desc string
  10. src image.Image
  11. want *image.NRGBA
  12. }{
  13. {
  14. "FlipH 2x3",
  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.NRGBA{
  25. Rect: image.Rect(0, 0, 2, 3),
  26. Stride: 2 * 4,
  27. Pix: []uint8{
  28. 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33,
  29. 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
  30. 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
  31. },
  32. },
  33. },
  34. }
  35. for _, d := range td {
  36. got := FlipH(d.src)
  37. want := d.want
  38. if !compareNRGBA(got, want, 0) {
  39. t.Errorf("test [%s] failed: %#v", d.desc, got)
  40. }
  41. }
  42. }
  43. func BenchmarkFlipH(b *testing.B) {
  44. b.ReportAllocs()
  45. for i := 0; i < b.N; i++ {
  46. FlipH(testdataBranchesJPG)
  47. }
  48. }
  49. func TestFlipV(t *testing.T) {
  50. td := []struct {
  51. desc string
  52. src image.Image
  53. want *image.NRGBA
  54. }{
  55. {
  56. "FlipV 2x3",
  57. &image.NRGBA{
  58. Rect: image.Rect(-1, -1, 1, 2),
  59. Stride: 2 * 4,
  60. Pix: []uint8{
  61. 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
  62. 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
  63. 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
  64. },
  65. },
  66. &image.NRGBA{
  67. Rect: image.Rect(0, 0, 2, 3),
  68. Stride: 2 * 4,
  69. Pix: []uint8{
  70. 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
  71. 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
  72. 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
  73. },
  74. },
  75. },
  76. }
  77. for _, d := range td {
  78. got := FlipV(d.src)
  79. want := d.want
  80. if !compareNRGBA(got, want, 0) {
  81. t.Errorf("test [%s] failed: %#v", d.desc, got)
  82. }
  83. }
  84. }
  85. func BenchmarkFlipV(b *testing.B) {
  86. b.ReportAllocs()
  87. for i := 0; i < b.N; i++ {
  88. FlipV(testdataBranchesJPG)
  89. }
  90. }
  91. func TestTranspose(t *testing.T) {
  92. td := []struct {
  93. desc string
  94. src image.Image
  95. want *image.NRGBA
  96. }{
  97. {
  98. "Transpose 2x3",
  99. &image.NRGBA{
  100. Rect: image.Rect(-1, -1, 1, 2),
  101. Stride: 2 * 4,
  102. Pix: []uint8{
  103. 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
  104. 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
  105. 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
  106. },
  107. },
  108. &image.NRGBA{
  109. Rect: image.Rect(0, 0, 3, 2),
  110. Stride: 3 * 4,
  111. Pix: []uint8{
  112. 0x00, 0x11, 0x22, 0x33, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00,
  113. 0xcc, 0xdd, 0xee, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  114. },
  115. },
  116. },
  117. }
  118. for _, d := range td {
  119. got := Transpose(d.src)
  120. want := d.want
  121. if !compareNRGBA(got, want, 0) {
  122. t.Errorf("test [%s] failed: %#v", d.desc, got)
  123. }
  124. }
  125. }
  126. func BenchmarkTranspose(b *testing.B) {
  127. b.ReportAllocs()
  128. for i := 0; i < b.N; i++ {
  129. Transpose(testdataBranchesJPG)
  130. }
  131. }
  132. func TestTransverse(t *testing.T) {
  133. td := []struct {
  134. desc string
  135. src image.Image
  136. want *image.NRGBA
  137. }{
  138. {
  139. "Transverse 2x3",
  140. &image.NRGBA{
  141. Rect: image.Rect(-1, -1, 1, 2),
  142. Stride: 2 * 4,
  143. Pix: []uint8{
  144. 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
  145. 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
  146. 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
  147. },
  148. },
  149. &image.NRGBA{
  150. Rect: image.Rect(0, 0, 3, 2),
  151. Stride: 3 * 4,
  152. Pix: []uint8{
  153. 0x00, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xcc, 0xdd, 0xee, 0xff,
  154. 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x11, 0x22, 0x33,
  155. },
  156. },
  157. },
  158. }
  159. for _, d := range td {
  160. got := Transverse(d.src)
  161. want := d.want
  162. if !compareNRGBA(got, want, 0) {
  163. t.Errorf("test [%s] failed: %#v", d.desc, got)
  164. }
  165. }
  166. }
  167. func BenchmarkTransverse(b *testing.B) {
  168. b.ReportAllocs()
  169. for i := 0; i < b.N; i++ {
  170. Transverse(testdataBranchesJPG)
  171. }
  172. }
  173. func TestRotate90(t *testing.T) {
  174. td := []struct {
  175. desc string
  176. src image.Image
  177. want *image.NRGBA
  178. }{
  179. {
  180. "Rotate90 2x3",
  181. &image.NRGBA{
  182. Rect: image.Rect(-1, -1, 1, 2),
  183. Stride: 2 * 4,
  184. Pix: []uint8{
  185. 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
  186. 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
  187. 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
  188. },
  189. },
  190. &image.NRGBA{
  191. Rect: image.Rect(0, 0, 3, 2),
  192. Stride: 3 * 4,
  193. Pix: []uint8{
  194. 0xcc, 0xdd, 0xee, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  195. 0x00, 0x11, 0x22, 0x33, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00,
  196. },
  197. },
  198. },
  199. }
  200. for _, d := range td {
  201. got := Rotate90(d.src)
  202. want := d.want
  203. if !compareNRGBA(got, want, 0) {
  204. t.Errorf("test [%s] failed: %#v", d.desc, got)
  205. }
  206. }
  207. }
  208. func BenchmarkRotate90(b *testing.B) {
  209. b.ReportAllocs()
  210. for i := 0; i < b.N; i++ {
  211. Rotate90(testdataBranchesJPG)
  212. }
  213. }
  214. func TestRotate180(t *testing.T) {
  215. td := []struct {
  216. desc string
  217. src image.Image
  218. want *image.NRGBA
  219. }{
  220. {
  221. "Rotate180 2x3",
  222. &image.NRGBA{
  223. Rect: image.Rect(-1, -1, 1, 2),
  224. Stride: 2 * 4,
  225. Pix: []uint8{
  226. 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
  227. 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
  228. 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
  229. },
  230. },
  231. &image.NRGBA{
  232. Rect: image.Rect(0, 0, 2, 3),
  233. Stride: 2 * 4,
  234. Pix: []uint8{
  235. 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
  236. 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
  237. 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33,
  238. },
  239. },
  240. },
  241. }
  242. for _, d := range td {
  243. got := Rotate180(d.src)
  244. want := d.want
  245. if !compareNRGBA(got, want, 0) {
  246. t.Errorf("test [%s] failed: %#v", d.desc, got)
  247. }
  248. }
  249. }
  250. func BenchmarkRotate180(b *testing.B) {
  251. b.ReportAllocs()
  252. for i := 0; i < b.N; i++ {
  253. Rotate180(testdataBranchesJPG)
  254. }
  255. }
  256. func TestRotate270(t *testing.T) {
  257. td := []struct {
  258. desc string
  259. src image.Image
  260. want *image.NRGBA
  261. }{
  262. {
  263. "Rotate270 2x3",
  264. &image.NRGBA{
  265. Rect: image.Rect(-1, -1, 1, 2),
  266. Stride: 2 * 4,
  267. Pix: []uint8{
  268. 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff,
  269. 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
  270. 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
  271. },
  272. },
  273. &image.NRGBA{
  274. Rect: image.Rect(0, 0, 3, 2),
  275. Stride: 3 * 4,
  276. Pix: []uint8{
  277. 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x11, 0x22, 0x33,
  278. 0x00, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xcc, 0xdd, 0xee, 0xff,
  279. },
  280. },
  281. },
  282. }
  283. for _, d := range td {
  284. got := Rotate270(d.src)
  285. want := d.want
  286. if !compareNRGBA(got, want, 0) {
  287. t.Errorf("test [%s] failed: %#v", d.desc, got)
  288. }
  289. }
  290. }
  291. func BenchmarkRotate270(b *testing.B) {
  292. b.ReportAllocs()
  293. for i := 0; i < b.N; i++ {
  294. Rotate270(testdataBranchesJPG)
  295. }
  296. }
  297. func TestRotate(t *testing.T) {
  298. testCases := []struct {
  299. desc string
  300. src image.Image
  301. angle float64
  302. bg color.Color
  303. want *image.NRGBA
  304. }{
  305. {
  306. "Rotate 0",
  307. &image.NRGBA{
  308. Rect: image.Rect(-1, -1, 3, 3),
  309. Stride: 4 * 4,
  310. Pix: []uint8{
  311. 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  312. 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
  313. 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
  314. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  315. },
  316. },
  317. 0,
  318. color.Black,
  319. &image.NRGBA{
  320. Rect: image.Rect(0, 0, 4, 4),
  321. Stride: 4 * 4,
  322. Pix: []uint8{
  323. 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  324. 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
  325. 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
  326. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  327. },
  328. },
  329. },
  330. {
  331. "Rotate 90",
  332. &image.NRGBA{
  333. Rect: image.Rect(-1, -1, 3, 3),
  334. Stride: 4 * 4,
  335. Pix: []uint8{
  336. 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  337. 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
  338. 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
  339. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  340. },
  341. },
  342. 90,
  343. color.Black,
  344. &image.NRGBA{
  345. Rect: image.Rect(0, 0, 4, 4),
  346. Stride: 4 * 4,
  347. Pix: []uint8{
  348. 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  349. 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  350. 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  351. 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  352. },
  353. },
  354. },
  355. {
  356. "Rotate 180",
  357. &image.NRGBA{
  358. Rect: image.Rect(-1, -1, 3, 3),
  359. Stride: 4 * 4,
  360. Pix: []uint8{
  361. 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  362. 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
  363. 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
  364. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  365. },
  366. },
  367. 180,
  368. color.Black,
  369. &image.NRGBA{
  370. Rect: image.Rect(0, 0, 4, 4),
  371. Stride: 4 * 4,
  372. Pix: []uint8{
  373. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  374. 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
  375. 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
  376. 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  377. },
  378. },
  379. },
  380. {
  381. "Rotate 45",
  382. &image.NRGBA{
  383. Rect: image.Rect(-1, -1, 3, 3),
  384. Stride: 4 * 4,
  385. Pix: []uint8{
  386. 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  387. 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
  388. 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
  389. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  390. },
  391. },
  392. 45,
  393. color.Black,
  394. &image.NRGBA{
  395. Rect: image.Rect(0, 0, 6, 6),
  396. Stride: 6 * 4,
  397. Pix: []uint8{
  398. 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x61, 0x00, 0x00, 0xff, 0x58, 0x08, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff,
  399. 0x00, 0x00, 0x00, 0xff, 0x61, 0x00, 0x00, 0xff, 0xe9, 0x16, 0x00, 0xff, 0x35, 0xca, 0x00, 0xff, 0x00, 0x30, 0x30, 0xff, 0x00, 0x00, 0x00, 0xff,
  400. 0x61, 0x00, 0x00, 0xff, 0xe9, 0x16, 0x00, 0xff, 0x35, 0xca, 0x00, 0xff, 0x00, 0x80, 0x80, 0xff, 0x35, 0x35, 0xff, 0xff, 0x58, 0x58, 0x61, 0xff,
  401. 0x58, 0x08, 0x00, 0xff, 0x35, 0xca, 0x00, 0xff, 0x00, 0x80, 0x80, 0xff, 0x35, 0x35, 0xff, 0xff, 0xe9, 0xe9, 0xff, 0xff, 0x61, 0x61, 0x61, 0xff,
  402. 0x00, 0x00, 0x00, 0xff, 0x00, 0x30, 0x30, 0xff, 0x35, 0x35, 0xff, 0xff, 0xe9, 0xe9, 0xff, 0xff, 0x61, 0x61, 0x61, 0xff, 0x00, 0x00, 0x00, 0xff,
  403. 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x58, 0x58, 0x61, 0xff, 0x61, 0x61, 0x61, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff,
  404. },
  405. },
  406. },
  407. {
  408. "Rotate 0x0",
  409. &image.NRGBA{
  410. Rect: image.Rect(0, 0, 0, 0),
  411. Stride: 0,
  412. Pix: []uint8{},
  413. },
  414. 123,
  415. color.Black,
  416. &image.NRGBA{
  417. Rect: image.Rect(0, 0, 0, 0),
  418. Stride: 0,
  419. Pix: []uint8{},
  420. },
  421. },
  422. {
  423. "Rotate -90",
  424. &image.NRGBA{
  425. Rect: image.Rect(-1, -1, 0, 1),
  426. Stride: 1 * 4,
  427. Pix: []uint8{
  428. 0xff, 0x00, 0x00, 0xff,
  429. 0x00, 0xff, 0x00, 0xff,
  430. },
  431. },
  432. -90,
  433. color.Black,
  434. &image.NRGBA{
  435. Rect: image.Rect(0, 0, 2, 1),
  436. Stride: 2 * 4,
  437. Pix: []uint8{
  438. 0x00, 0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  439. },
  440. },
  441. },
  442. {
  443. "Rotate -360*10",
  444. &image.NRGBA{
  445. Rect: image.Rect(-1, -1, 0, 1),
  446. Stride: 1 * 4,
  447. Pix: []uint8{
  448. 0x00, 0xff, 0x00, 0xff,
  449. 0xff, 0x00, 0x00, 0xff,
  450. },
  451. },
  452. -360 * 10,
  453. color.Black,
  454. &image.NRGBA{
  455. Rect: image.Rect(0, 0, 1, 2),
  456. Stride: 1 * 4,
  457. Pix: []uint8{
  458. 0x00, 0xff, 0x00, 0xff,
  459. 0xff, 0x00, 0x00, 0xff,
  460. },
  461. },
  462. },
  463. {
  464. "Rotate -360*10 + 90",
  465. &image.NRGBA{
  466. Rect: image.Rect(-1, -1, 0, 1),
  467. Stride: 1 * 4,
  468. Pix: []uint8{
  469. 0xff, 0x00, 0x00, 0xff,
  470. 0x00, 0xff, 0x00, 0xff,
  471. },
  472. },
  473. -360*10 + 90,
  474. color.Black,
  475. &image.NRGBA{
  476. Rect: image.Rect(0, 0, 2, 1),
  477. Stride: 2 * 4,
  478. Pix: []uint8{
  479. 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
  480. },
  481. },
  482. },
  483. {
  484. "Rotate -360*10 + 180",
  485. &image.NRGBA{
  486. Rect: image.Rect(-1, -1, 0, 1),
  487. Stride: 1 * 4,
  488. Pix: []uint8{
  489. 0xff, 0x00, 0x00, 0xff,
  490. 0x00, 0xff, 0x00, 0xff,
  491. },
  492. },
  493. -360*10 + 180,
  494. color.Black,
  495. &image.NRGBA{
  496. Rect: image.Rect(0, 0, 1, 2),
  497. Stride: 1 * 4,
  498. Pix: []uint8{
  499. 0x00, 0xff, 0x00, 0xff,
  500. 0xff, 0x00, 0x00, 0xff,
  501. },
  502. },
  503. },
  504. {
  505. "Rotate -360*10 + 270",
  506. &image.NRGBA{
  507. Rect: image.Rect(-1, -1, 0, 1),
  508. Stride: 1 * 4,
  509. Pix: []uint8{
  510. 0xff, 0x00, 0x00, 0xff,
  511. 0x00, 0xff, 0x00, 0xff,
  512. },
  513. },
  514. -360*10 + 270,
  515. color.Black,
  516. &image.NRGBA{
  517. Rect: image.Rect(0, 0, 2, 1),
  518. Stride: 2 * 4,
  519. Pix: []uint8{
  520. 0x00, 0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  521. },
  522. },
  523. },
  524. {
  525. "Rotate 360*10",
  526. &image.NRGBA{
  527. Rect: image.Rect(-1, -1, 0, 1),
  528. Stride: 1 * 4,
  529. Pix: []uint8{
  530. 0x00, 0xff, 0x00, 0xff,
  531. 0xff, 0x00, 0x00, 0xff,
  532. },
  533. },
  534. 360 * 10,
  535. color.Black,
  536. &image.NRGBA{
  537. Rect: image.Rect(0, 0, 1, 2),
  538. Stride: 1 * 4,
  539. Pix: []uint8{
  540. 0x00, 0xff, 0x00, 0xff,
  541. 0xff, 0x00, 0x00, 0xff,
  542. },
  543. },
  544. },
  545. {
  546. "Rotate 360*10 + 90",
  547. &image.NRGBA{
  548. Rect: image.Rect(-1, -1, 0, 1),
  549. Stride: 1 * 4,
  550. Pix: []uint8{
  551. 0xff, 0x00, 0x00, 0xff,
  552. 0x00, 0xff, 0x00, 0xff,
  553. },
  554. },
  555. 360*10 + 90,
  556. color.Black,
  557. &image.NRGBA{
  558. Rect: image.Rect(0, 0, 2, 1),
  559. Stride: 2 * 4,
  560. Pix: []uint8{
  561. 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
  562. },
  563. },
  564. },
  565. {
  566. "Rotate 360*10 + 180",
  567. &image.NRGBA{
  568. Rect: image.Rect(-1, -1, 0, 1),
  569. Stride: 1 * 4,
  570. Pix: []uint8{
  571. 0xff, 0x00, 0x00, 0xff,
  572. 0x00, 0xff, 0x00, 0xff,
  573. },
  574. },
  575. 360*10 + 180,
  576. color.Black,
  577. &image.NRGBA{
  578. Rect: image.Rect(0, 0, 1, 2),
  579. Stride: 1 * 4,
  580. Pix: []uint8{
  581. 0x00, 0xff, 0x00, 0xff,
  582. 0xff, 0x00, 0x00, 0xff,
  583. },
  584. },
  585. },
  586. {
  587. "Rotate 360*10 + 270",
  588. &image.NRGBA{
  589. Rect: image.Rect(-1, -1, 0, 1),
  590. Stride: 1 * 4,
  591. Pix: []uint8{
  592. 0xff, 0x00, 0x00, 0xff,
  593. 0x00, 0xff, 0x00, 0xff,
  594. },
  595. },
  596. 360*10 + 270,
  597. color.Black,
  598. &image.NRGBA{
  599. Rect: image.Rect(0, 0, 2, 1),
  600. Stride: 2 * 4,
  601. Pix: []uint8{
  602. 0x00, 0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  603. },
  604. },
  605. },
  606. }
  607. for _, test := range testCases {
  608. got := Rotate(test.src, test.angle, test.bg)
  609. want := test.want
  610. if !compareNRGBA(got, want, 0) {
  611. t.Errorf("test [%s] failed: %#v", test.desc, got)
  612. }
  613. }
  614. }
  615. func BenchmarkRotate(b *testing.B) {
  616. b.ReportAllocs()
  617. for i := 0; i < b.N; i++ {
  618. Rotate(testdataBranchesJPG, 30, color.Transparent)
  619. }
  620. }