transform_test.go 16 KB

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