resize_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. package imaging
  2. import (
  3. "fmt"
  4. "image"
  5. "path/filepath"
  6. "testing"
  7. )
  8. func TestResize(t *testing.T) {
  9. testCases := []struct {
  10. name string
  11. src image.Image
  12. w, h int
  13. f ResampleFilter
  14. want *image.NRGBA
  15. }{
  16. {
  17. "Resize 2x2 1x1 box",
  18. &image.NRGBA{
  19. Rect: image.Rect(-1, -1, 1, 1),
  20. Stride: 2 * 4,
  21. Pix: []uint8{
  22. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  23. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  24. },
  25. },
  26. 1, 1,
  27. Box,
  28. &image.NRGBA{
  29. Rect: image.Rect(0, 0, 1, 1),
  30. Stride: 1 * 4,
  31. Pix: []uint8{0x55, 0x55, 0x55, 0xc0},
  32. },
  33. },
  34. {
  35. "Resize 2x2 1x2 box",
  36. &image.NRGBA{
  37. Rect: image.Rect(-1, -1, 1, 1),
  38. Stride: 2 * 4,
  39. Pix: []uint8{
  40. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  41. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  42. },
  43. },
  44. 1, 2,
  45. Box,
  46. &image.NRGBA{
  47. Rect: image.Rect(0, 0, 1, 2),
  48. Stride: 1 * 4,
  49. Pix: []uint8{
  50. 0xff, 0x00, 0x00, 0x80,
  51. 0x00, 0x80, 0x80, 0xff,
  52. },
  53. },
  54. },
  55. {
  56. "Resize 2x2 2x1 box",
  57. &image.NRGBA{
  58. Rect: image.Rect(-1, -1, 1, 1),
  59. Stride: 2 * 4,
  60. Pix: []uint8{
  61. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  62. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  63. },
  64. },
  65. 2, 1,
  66. Box,
  67. &image.NRGBA{
  68. Rect: image.Rect(0, 0, 2, 1),
  69. Stride: 2 * 4,
  70. Pix: []uint8{
  71. 0x00, 0xff, 0x00, 0x80, 0x80, 0x00, 0x80, 0xff,
  72. },
  73. },
  74. },
  75. {
  76. "Resize 2x2 2x2 box",
  77. &image.NRGBA{
  78. Rect: image.Rect(-1, -1, 1, 1),
  79. Stride: 2 * 4,
  80. Pix: []uint8{
  81. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  82. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  83. },
  84. },
  85. 2, 2,
  86. Box,
  87. &image.NRGBA{
  88. Rect: image.Rect(0, 0, 2, 2),
  89. Stride: 2 * 4,
  90. Pix: []uint8{
  91. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  92. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  93. },
  94. },
  95. },
  96. {
  97. "Resize 3x1 1x1 nearest",
  98. &image.NRGBA{
  99. Rect: image.Rect(-1, -1, 2, 0),
  100. Stride: 3 * 4,
  101. Pix: []uint8{
  102. 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  103. },
  104. },
  105. 1, 1,
  106. NearestNeighbor,
  107. &image.NRGBA{
  108. Rect: image.Rect(0, 0, 1, 1),
  109. Stride: 1 * 4,
  110. Pix: []uint8{0x00, 0xff, 0x00, 0xff},
  111. },
  112. },
  113. {
  114. "Resize 2x2 0x4 box",
  115. &image.NRGBA{
  116. Rect: image.Rect(-1, -1, 1, 1),
  117. Stride: 2 * 4,
  118. Pix: []uint8{
  119. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  120. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  121. },
  122. },
  123. 0, 4,
  124. Box,
  125. &image.NRGBA{
  126. Rect: image.Rect(0, 0, 4, 4),
  127. Stride: 4 * 4,
  128. Pix: []uint8{
  129. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  130. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  131. 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
  132. 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
  133. },
  134. },
  135. },
  136. {
  137. "Resize 2x2 4x0 linear",
  138. &image.NRGBA{
  139. Rect: image.Rect(-1, -1, 1, 1),
  140. Stride: 2 * 4,
  141. Pix: []uint8{
  142. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  143. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  144. },
  145. },
  146. 4, 0,
  147. Linear,
  148. &image.NRGBA{
  149. Rect: image.Rect(0, 0, 4, 4),
  150. Stride: 4 * 4,
  151. Pix: []uint8{
  152. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x40, 0xff, 0x00, 0x00, 0xbf, 0xff, 0x00, 0x00, 0xff,
  153. 0x00, 0xff, 0x00, 0x40, 0x6e, 0x6d, 0x25, 0x70, 0xb0, 0x14, 0x3b, 0xcf, 0xbf, 0x00, 0x40, 0xff,
  154. 0x00, 0xff, 0x00, 0xbf, 0x14, 0xb0, 0x3b, 0xcf, 0x33, 0x33, 0x99, 0xef, 0x40, 0x00, 0xbf, 0xff,
  155. 0x00, 0xff, 0x00, 0xff, 0x00, 0xbf, 0x40, 0xff, 0x00, 0x40, 0xbf, 0xff, 0x00, 0x00, 0xff, 0xff,
  156. },
  157. },
  158. },
  159. {
  160. "Resize 0x0 1x1 box",
  161. &image.NRGBA{
  162. Rect: image.Rect(-1, -1, -1, -1),
  163. Stride: 0,
  164. Pix: []uint8{},
  165. },
  166. 1, 1,
  167. Box,
  168. &image.NRGBA{},
  169. },
  170. {
  171. "Resize 2x2 0x0 box",
  172. &image.NRGBA{
  173. Rect: image.Rect(-1, -1, 1, 1),
  174. Stride: 2 * 4,
  175. Pix: []uint8{
  176. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  177. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  178. },
  179. },
  180. 0, 0,
  181. Box,
  182. &image.NRGBA{},
  183. },
  184. {
  185. "Resize 2x2 -1x0 box",
  186. &image.NRGBA{
  187. Rect: image.Rect(-1, -1, 1, 1),
  188. Stride: 2 * 4,
  189. Pix: []uint8{
  190. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  191. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  192. },
  193. },
  194. -1, 0,
  195. Box,
  196. &image.NRGBA{},
  197. },
  198. }
  199. for _, tc := range testCases {
  200. t.Run(tc.name, func(t *testing.T) {
  201. got := Resize(tc.src, tc.w, tc.h, tc.f)
  202. if !compareNRGBA(got, tc.want, 0) {
  203. t.Fatalf("got result %#v want %#v", got, tc.want)
  204. }
  205. })
  206. }
  207. }
  208. func TestResampleFilters(t *testing.T) {
  209. for _, filter := range []ResampleFilter{
  210. NearestNeighbor,
  211. Box,
  212. Linear,
  213. Hermite,
  214. MitchellNetravali,
  215. CatmullRom,
  216. BSpline,
  217. Gaussian,
  218. Lanczos,
  219. Hann,
  220. Hamming,
  221. Blackman,
  222. Bartlett,
  223. Welch,
  224. Cosine,
  225. } {
  226. t.Run("", func(t *testing.T) {
  227. src := image.NewNRGBA(image.Rect(-1, -1, 2, 3))
  228. got := Resize(src, 5, 6, filter)
  229. want := image.NewNRGBA(image.Rect(0, 0, 5, 6))
  230. if !compareNRGBA(got, want, 0) {
  231. t.Fatalf("got result %#v want %#v", got, want)
  232. }
  233. if filter.Kernel != nil {
  234. if x := filter.Kernel(filter.Support + 0.0001); x != 0 {
  235. t.Fatalf("got kernel value %f want 0", x)
  236. }
  237. }
  238. })
  239. }
  240. }
  241. func TestResizeGolden(t *testing.T) {
  242. for name, filter := range map[string]ResampleFilter{
  243. "out_resize_nearest.png": NearestNeighbor,
  244. "out_resize_linear.png": Linear,
  245. "out_resize_catrom.png": CatmullRom,
  246. "out_resize_lanczos.png": Lanczos,
  247. } {
  248. got := Resize(testdataBranchesPNG, 150, 0, filter)
  249. want, err := Open("testdata/" + name)
  250. if err != nil {
  251. t.Fatalf("failed to open image: %v", err)
  252. }
  253. if !compareNRGBAGolden(got, toNRGBA(want)) {
  254. t.Fatalf("resulting image differs from golden: %s", name)
  255. }
  256. }
  257. }
  258. func TestFit(t *testing.T) {
  259. testCases := []struct {
  260. name string
  261. src image.Image
  262. w, h int
  263. f ResampleFilter
  264. want *image.NRGBA
  265. }{
  266. {
  267. "Fit 2x2 1x10 box",
  268. &image.NRGBA{
  269. Rect: image.Rect(-1, -1, 1, 1),
  270. Stride: 2 * 4,
  271. Pix: []uint8{
  272. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  273. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  274. },
  275. },
  276. 1, 10,
  277. Box,
  278. &image.NRGBA{
  279. Rect: image.Rect(0, 0, 1, 1),
  280. Stride: 1 * 4,
  281. Pix: []uint8{0x55, 0x55, 0x55, 0xc0},
  282. },
  283. },
  284. {
  285. "Fit 2x2 10x1 box",
  286. &image.NRGBA{
  287. Rect: image.Rect(-1, -1, 1, 1),
  288. Stride: 2 * 4,
  289. Pix: []uint8{
  290. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  291. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  292. },
  293. },
  294. 10, 1,
  295. Box,
  296. &image.NRGBA{
  297. Rect: image.Rect(0, 0, 1, 1),
  298. Stride: 1 * 4,
  299. Pix: []uint8{0x55, 0x55, 0x55, 0xc0},
  300. },
  301. },
  302. {
  303. "Fit 2x2 10x10 box",
  304. &image.NRGBA{
  305. Rect: image.Rect(-1, -1, 1, 1),
  306. Stride: 2 * 4,
  307. Pix: []uint8{
  308. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  309. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  310. },
  311. },
  312. 10, 10,
  313. Box,
  314. &image.NRGBA{
  315. Rect: image.Rect(0, 0, 2, 2),
  316. Stride: 2 * 4,
  317. Pix: []uint8{
  318. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  319. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  320. },
  321. },
  322. },
  323. {
  324. "Fit 0x0 1x1 box",
  325. &image.NRGBA{
  326. Rect: image.Rect(-1, -1, -1, -1),
  327. Stride: 0,
  328. Pix: []uint8{},
  329. },
  330. 1, 1,
  331. Box,
  332. &image.NRGBA{},
  333. },
  334. {
  335. "Fit 2x2 0x0 box",
  336. &image.NRGBA{
  337. Rect: image.Rect(-1, -1, 1, 1),
  338. Stride: 2 * 4,
  339. Pix: []uint8{
  340. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  341. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  342. },
  343. },
  344. 0, 0,
  345. Box,
  346. &image.NRGBA{},
  347. },
  348. {
  349. "Fit 2x2 -1x0 box",
  350. &image.NRGBA{
  351. Rect: image.Rect(-1, -1, 1, 1),
  352. Stride: 2 * 4,
  353. Pix: []uint8{
  354. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  355. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  356. },
  357. },
  358. -1, 0,
  359. Box,
  360. &image.NRGBA{},
  361. },
  362. }
  363. for _, tc := range testCases {
  364. t.Run(tc.name, func(t *testing.T) {
  365. got := Fit(tc.src, tc.w, tc.h, tc.f)
  366. if !compareNRGBA(got, tc.want, 0) {
  367. t.Fatalf("got result %#v want %#v", got, tc.want)
  368. }
  369. })
  370. }
  371. }
  372. func TestFitGolden(t *testing.T) {
  373. got := Fit(testdataBranchesPNG, 150, 150, Box)
  374. name := filepath.Join("testdata", "out_fit.png")
  375. want, err := Open(name)
  376. if err != nil {
  377. t.Fatalf("failed to open image: %v", err)
  378. }
  379. if !compareNRGBAGolden(got, toNRGBA(want)) {
  380. t.Fatalf("resulting image differs from golden: %s", name)
  381. }
  382. }
  383. func TestFill(t *testing.T) {
  384. testCases := []struct {
  385. name string
  386. src image.Image
  387. w, h int
  388. a Anchor
  389. f ResampleFilter
  390. want *image.NRGBA
  391. }{
  392. {
  393. "Fill 4x4 4x4 TopRight Box",
  394. &image.NRGBA{
  395. Rect: image.Rect(-1, -1, 3, 3),
  396. Stride: 4 * 4,
  397. Pix: []uint8{
  398. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  399. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  400. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  401. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  402. },
  403. },
  404. 4, 4,
  405. TopRight,
  406. Box,
  407. &image.NRGBA{
  408. Rect: image.Rect(0, 0, 4, 4),
  409. Stride: 4 * 4,
  410. Pix: []uint8{
  411. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  412. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  413. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  414. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  415. },
  416. },
  417. },
  418. {
  419. "Fill 4x4 0x4 Left Box",
  420. &image.NRGBA{
  421. Rect: image.Rect(-1, -1, 3, 3),
  422. Stride: 4 * 4,
  423. Pix: []uint8{
  424. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  425. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  426. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  427. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  428. },
  429. },
  430. 0, 4,
  431. Left,
  432. Box,
  433. &image.NRGBA{},
  434. },
  435. {
  436. "Fill 0x0 4x4 Right Box",
  437. &image.NRGBA{},
  438. 4, 4,
  439. Right,
  440. Box,
  441. &image.NRGBA{},
  442. },
  443. {
  444. "Fill 100x200 20x10 Center Linear",
  445. image.NewRGBA(image.Rect(0, 0, 100, 200)),
  446. 20, 10,
  447. Center,
  448. Linear,
  449. image.NewNRGBA(image.Rect(0, 0, 20, 10)),
  450. },
  451. {
  452. "Fill 10x20 20x10 Center Linear",
  453. image.NewRGBA(image.Rect(0, 0, 10, 20)),
  454. 20, 10,
  455. Center,
  456. Linear,
  457. image.NewNRGBA(image.Rect(0, 0, 20, 10)),
  458. },
  459. }
  460. for _, tc := range testCases {
  461. t.Run(tc.name, func(t *testing.T) {
  462. got := Fill(tc.src, tc.w, tc.h, tc.a, tc.f)
  463. if !compareNRGBA(got, tc.want, 0) {
  464. t.Fatalf("got result %#v want %#v", got, tc.want)
  465. }
  466. })
  467. }
  468. }
  469. func TestFillGolden(t *testing.T) {
  470. anchorPoints := map[string]Anchor{
  471. "left": Left,
  472. "center": Center,
  473. "right": Right,
  474. }
  475. for apName, ap := range anchorPoints {
  476. got := Fill(testdataBranchesPNG, 150, 150, ap, Box)
  477. name := filepath.Join("testdata", "out_fill_"+apName+".png")
  478. want, err := Open(name)
  479. if err != nil {
  480. t.Fatalf("failed to open image: %v", err)
  481. }
  482. if !compareNRGBAGolden(got, toNRGBA(want)) {
  483. t.Fatalf("resulting image differs from golden: %s", name)
  484. }
  485. }
  486. }
  487. func TestResizeAndCrop(t *testing.T) {
  488. testCases := []struct {
  489. name string
  490. src image.Image
  491. w, h int
  492. a Anchor
  493. f ResampleFilter
  494. want *image.NRGBA
  495. }{
  496. {
  497. "resizeAndCrop 4x4 2x2 Center Nearest",
  498. &image.NRGBA{
  499. Rect: image.Rect(-1, -1, 3, 3),
  500. Stride: 4 * 4,
  501. Pix: []uint8{
  502. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  503. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  504. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  505. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  506. },
  507. },
  508. 2, 2,
  509. Center,
  510. NearestNeighbor,
  511. &image.NRGBA{
  512. Rect: image.Rect(0, 0, 2, 2),
  513. Stride: 2 * 4,
  514. Pix: []uint8{
  515. 0x14, 0x15, 0x16, 0x17, 0x1c, 0x1d, 0x1e, 0x1f,
  516. 0x34, 0x35, 0x36, 0x37, 0x3c, 0x3d, 0x3e, 0x3f,
  517. },
  518. },
  519. },
  520. {
  521. "resizeAndCrop 4x4 1x4 TopLeft Nearest",
  522. &image.NRGBA{
  523. Rect: image.Rect(-1, -1, 3, 3),
  524. Stride: 4 * 4,
  525. Pix: []uint8{
  526. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  527. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  528. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  529. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  530. },
  531. },
  532. 1, 4,
  533. TopLeft,
  534. NearestNeighbor,
  535. &image.NRGBA{
  536. Rect: image.Rect(0, 0, 1, 4),
  537. Stride: 1 * 4,
  538. Pix: []uint8{
  539. 0x00, 0x01, 0x02, 0x03,
  540. 0x10, 0x11, 0x12, 0x13,
  541. 0x20, 0x21, 0x22, 0x23,
  542. 0x30, 0x31, 0x32, 0x33,
  543. },
  544. },
  545. },
  546. {
  547. "resizeAndCrop 4x4 8x2 Bottom Nearest",
  548. &image.NRGBA{
  549. Rect: image.Rect(-1, -1, 3, 3),
  550. Stride: 4 * 4,
  551. Pix: []uint8{
  552. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  553. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  554. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  555. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  556. },
  557. },
  558. 8, 2,
  559. Bottom,
  560. NearestNeighbor,
  561. &image.NRGBA{
  562. Rect: image.Rect(0, 0, 8, 2),
  563. Stride: 8 * 4,
  564. Pix: []uint8{
  565. 0x30, 0x31, 0x32, 0x33, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x3c, 0x3d, 0x3e, 0x3f,
  566. 0x30, 0x31, 0x32, 0x33, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x3c, 0x3d, 0x3e, 0x3f,
  567. },
  568. },
  569. },
  570. {
  571. "resizeAndCrop 4x4 2x8 Top Nearest",
  572. &image.NRGBA{
  573. Rect: image.Rect(-1, -1, 3, 3),
  574. Stride: 4 * 4,
  575. Pix: []uint8{
  576. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  577. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  578. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  579. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  580. },
  581. },
  582. 2, 8,
  583. Top,
  584. NearestNeighbor,
  585. &image.NRGBA{
  586. Rect: image.Rect(0, 0, 2, 8),
  587. Stride: 2 * 4,
  588. Pix: []uint8{
  589. 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  590. 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  591. 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  592. 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  593. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
  594. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
  595. 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  596. 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  597. },
  598. },
  599. },
  600. {
  601. "resizeAndCrop 4x4 4x4 TopRight Box",
  602. &image.NRGBA{
  603. Rect: image.Rect(-1, -1, 3, 3),
  604. Stride: 4 * 4,
  605. Pix: []uint8{
  606. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  607. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  608. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  609. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  610. },
  611. },
  612. 4, 4,
  613. TopRight,
  614. Box,
  615. &image.NRGBA{
  616. Rect: image.Rect(0, 0, 4, 4),
  617. Stride: 4 * 4,
  618. Pix: []uint8{
  619. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  620. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  621. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  622. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  623. },
  624. },
  625. },
  626. }
  627. for _, tc := range testCases {
  628. t.Run(tc.name, func(t *testing.T) {
  629. got := resizeAndCrop(tc.src, tc.w, tc.h, tc.a, tc.f)
  630. if !compareNRGBA(got, tc.want, 0) {
  631. t.Fatalf("got result %#v want %#v", got, tc.want)
  632. }
  633. })
  634. }
  635. }
  636. func TestCropAndResize(t *testing.T) {
  637. testCases := []struct {
  638. name string
  639. src image.Image
  640. w, h int
  641. a Anchor
  642. f ResampleFilter
  643. want *image.NRGBA
  644. }{
  645. {
  646. "cropAndResize 4x4 2x2 Center Nearest",
  647. &image.NRGBA{
  648. Rect: image.Rect(-1, -1, 3, 3),
  649. Stride: 4 * 4,
  650. Pix: []uint8{
  651. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  652. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  653. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  654. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  655. },
  656. },
  657. 2, 2,
  658. Center,
  659. NearestNeighbor,
  660. &image.NRGBA{
  661. Rect: image.Rect(0, 0, 2, 2),
  662. Stride: 2 * 4,
  663. Pix: []uint8{
  664. 0x14, 0x15, 0x16, 0x17, 0x1c, 0x1d, 0x1e, 0x1f,
  665. 0x34, 0x35, 0x36, 0x37, 0x3c, 0x3d, 0x3e, 0x3f,
  666. },
  667. },
  668. },
  669. {
  670. "cropAndResize 4x4 1x4 TopLeft Nearest",
  671. &image.NRGBA{
  672. Rect: image.Rect(-1, -1, 3, 3),
  673. Stride: 4 * 4,
  674. Pix: []uint8{
  675. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  676. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  677. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  678. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  679. },
  680. },
  681. 1, 4,
  682. TopLeft,
  683. NearestNeighbor,
  684. &image.NRGBA{
  685. Rect: image.Rect(0, 0, 1, 4),
  686. Stride: 1 * 4,
  687. Pix: []uint8{
  688. 0x00, 0x01, 0x02, 0x03,
  689. 0x10, 0x11, 0x12, 0x13,
  690. 0x20, 0x21, 0x22, 0x23,
  691. 0x30, 0x31, 0x32, 0x33,
  692. },
  693. },
  694. },
  695. {
  696. "cropAndResize 4x4 8x2 Bottom Nearest",
  697. &image.NRGBA{
  698. Rect: image.Rect(-1, -1, 3, 3),
  699. Stride: 4 * 4,
  700. Pix: []uint8{
  701. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  702. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  703. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  704. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  705. },
  706. },
  707. 8, 2,
  708. Bottom,
  709. NearestNeighbor,
  710. &image.NRGBA{
  711. Rect: image.Rect(0, 0, 8, 2),
  712. Stride: 8 * 4,
  713. Pix: []uint8{
  714. 0x30, 0x31, 0x32, 0x33, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x3c, 0x3d, 0x3e, 0x3f,
  715. 0x30, 0x31, 0x32, 0x33, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x3c, 0x3d, 0x3e, 0x3f,
  716. },
  717. },
  718. },
  719. {
  720. "cropAndResize 4x4 2x8 Top Nearest",
  721. &image.NRGBA{
  722. Rect: image.Rect(-1, -1, 3, 3),
  723. Stride: 4 * 4,
  724. Pix: []uint8{
  725. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  726. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  727. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  728. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  729. },
  730. },
  731. 2, 8,
  732. Top,
  733. NearestNeighbor,
  734. &image.NRGBA{
  735. Rect: image.Rect(0, 0, 2, 8),
  736. Stride: 2 * 4,
  737. Pix: []uint8{
  738. 0x04, 0x05, 0x06, 0x07, 0x04, 0x05, 0x06, 0x07,
  739. 0x04, 0x05, 0x06, 0x07, 0x04, 0x05, 0x06, 0x07,
  740. 0x14, 0x15, 0x16, 0x17, 0x14, 0x15, 0x16, 0x17,
  741. 0x14, 0x15, 0x16, 0x17, 0x14, 0x15, 0x16, 0x17,
  742. 0x24, 0x25, 0x26, 0x27, 0x24, 0x25, 0x26, 0x27,
  743. 0x24, 0x25, 0x26, 0x27, 0x24, 0x25, 0x26, 0x27,
  744. 0x34, 0x35, 0x36, 0x37, 0x34, 0x35, 0x36, 0x37,
  745. 0x34, 0x35, 0x36, 0x37, 0x34, 0x35, 0x36, 0x37,
  746. },
  747. },
  748. },
  749. {
  750. "cropAndResize 4x4 4x4 TopRight Box",
  751. &image.NRGBA{
  752. Rect: image.Rect(-1, -1, 3, 3),
  753. Stride: 4 * 4,
  754. Pix: []uint8{
  755. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  756. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  757. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  758. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  759. },
  760. },
  761. 4, 4,
  762. TopRight,
  763. Box,
  764. &image.NRGBA{
  765. Rect: image.Rect(0, 0, 4, 4),
  766. Stride: 4 * 4,
  767. Pix: []uint8{
  768. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  769. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  770. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  771. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  772. },
  773. },
  774. },
  775. }
  776. for _, tc := range testCases {
  777. t.Run(tc.name, func(t *testing.T) {
  778. got := cropAndResize(tc.src, tc.w, tc.h, tc.a, tc.f)
  779. if !compareNRGBA(got, tc.want, 0) {
  780. t.Fatalf("got result %#v want %#v", got, tc.want)
  781. }
  782. })
  783. }
  784. }
  785. func TestThumbnail(t *testing.T) {
  786. testCases := []struct {
  787. name string
  788. src image.Image
  789. w, h int
  790. f ResampleFilter
  791. want *image.NRGBA
  792. }{
  793. {
  794. "Thumbnail 6x2 1x1 box",
  795. &image.NRGBA{
  796. Rect: image.Rect(-1, -1, 5, 1),
  797. Stride: 6 * 4,
  798. Pix: []uint8{
  799. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  800. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  801. },
  802. },
  803. 1, 1,
  804. Box,
  805. &image.NRGBA{
  806. Rect: image.Rect(0, 0, 1, 1),
  807. Stride: 1 * 4,
  808. Pix: []uint8{0x55, 0x55, 0x55, 0xc0},
  809. },
  810. },
  811. {
  812. "Thumbnail 2x6 1x1 box",
  813. &image.NRGBA{
  814. Rect: image.Rect(-1, -1, 1, 5),
  815. Stride: 2 * 4,
  816. Pix: []uint8{
  817. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  818. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  819. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  820. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  821. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  822. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  823. },
  824. },
  825. 1, 1,
  826. Box,
  827. &image.NRGBA{
  828. Rect: image.Rect(0, 0, 1, 1),
  829. Stride: 1 * 4,
  830. Pix: []uint8{0x55, 0x55, 0x55, 0xc0},
  831. },
  832. },
  833. {
  834. "Thumbnail 1x3 2x2 box",
  835. &image.NRGBA{
  836. Rect: image.Rect(-1, -1, 0, 2),
  837. Stride: 1 * 4,
  838. Pix: []uint8{
  839. 0x00, 0x00, 0x00, 0x00,
  840. 0xff, 0x00, 0x00, 0xff,
  841. 0xff, 0xff, 0xff, 0xff,
  842. },
  843. },
  844. 2, 2,
  845. Box,
  846. &image.NRGBA{
  847. Rect: image.Rect(0, 0, 2, 2),
  848. Stride: 2 * 4,
  849. Pix: []uint8{
  850. 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  851. 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  852. },
  853. },
  854. },
  855. }
  856. for _, tc := range testCases {
  857. t.Run(tc.name, func(t *testing.T) {
  858. got := Thumbnail(tc.src, tc.w, tc.h, tc.f)
  859. if !compareNRGBA(got, tc.want, 0) {
  860. t.Fatalf("got result %#v want %#v", got, tc.want)
  861. }
  862. })
  863. }
  864. }
  865. func BenchmarkResize(b *testing.B) {
  866. for _, dir := range []string{"Down", "Up"} {
  867. for _, filter := range []string{"NearestNeighbor", "Linear", "CatmullRom", "Lanczos"} {
  868. for _, format := range []string{"JPEG", "PNG"} {
  869. var size int
  870. switch dir {
  871. case "Down":
  872. size = 100
  873. case "Up":
  874. size = 1000
  875. }
  876. var f ResampleFilter
  877. switch filter {
  878. case "NearestNeighbor":
  879. f = NearestNeighbor
  880. case "Linear":
  881. f = Linear
  882. case "CatmullRom":
  883. f = CatmullRom
  884. case "Lanczos":
  885. f = Lanczos
  886. }
  887. var img image.Image
  888. switch format {
  889. case "JPEG":
  890. img = testdataBranchesJPG
  891. case "PNG":
  892. img = testdataBranchesPNG
  893. }
  894. b.Run(fmt.Sprintf("%s %s %s", dir, filter, format), func(b *testing.B) {
  895. b.ReportAllocs()
  896. for i := 0; i < b.N; i++ {
  897. Resize(img, size, size, f)
  898. }
  899. })
  900. }
  901. }
  902. }
  903. }
  904. func BenchmarkFill(b *testing.B) {
  905. for _, dir := range []string{"Vertical", "Horizontal"} {
  906. for _, filter := range []string{"NearestNeighbor", "Linear", "CatmullRom", "Lanczos"} {
  907. for _, format := range []string{"JPEG", "PNG"} {
  908. var width, height int
  909. switch dir {
  910. case "Vertical":
  911. width = 100
  912. height = 1000
  913. case "Horizontal":
  914. width = 1000
  915. height = 100
  916. }
  917. var f ResampleFilter
  918. switch filter {
  919. case "NearestNeighbor":
  920. f = NearestNeighbor
  921. case "Linear":
  922. f = Linear
  923. case "CatmullRom":
  924. f = CatmullRom
  925. case "Lanczos":
  926. f = Lanczos
  927. }
  928. var img image.Image
  929. switch format {
  930. case "JPEG":
  931. img = testdataBranchesJPG
  932. case "PNG":
  933. img = testdataBranchesPNG
  934. }
  935. b.Run(fmt.Sprintf("%s %s %s", dir, filter, format), func(b *testing.B) {
  936. b.ReportAllocs()
  937. for i := 0; i < b.N; i++ {
  938. Fill(img, width, height, Center, f)
  939. }
  940. })
  941. }
  942. }
  943. }
  944. }