resize_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. package imaging
  2. import (
  3. "fmt"
  4. "image"
  5. "testing"
  6. )
  7. func TestResize(t *testing.T) {
  8. td := []struct {
  9. desc string
  10. src image.Image
  11. w, h int
  12. f ResampleFilter
  13. want *image.NRGBA
  14. }{
  15. {
  16. "Resize 2x2 1x1 box",
  17. &image.NRGBA{
  18. Rect: image.Rect(-1, -1, 1, 1),
  19. Stride: 2 * 4,
  20. Pix: []uint8{
  21. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  22. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  23. },
  24. },
  25. 1, 1,
  26. Box,
  27. &image.NRGBA{
  28. Rect: image.Rect(0, 0, 1, 1),
  29. Stride: 1 * 4,
  30. Pix: []uint8{0x55, 0x55, 0x55, 0xc0},
  31. },
  32. },
  33. {
  34. "Resize 2x2 2x2 box",
  35. &image.NRGBA{
  36. Rect: image.Rect(-1, -1, 1, 1),
  37. Stride: 2 * 4,
  38. Pix: []uint8{
  39. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  40. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  41. },
  42. },
  43. 2, 2,
  44. Box,
  45. &image.NRGBA{
  46. Rect: image.Rect(0, 0, 2, 2),
  47. Stride: 2 * 4,
  48. Pix: []uint8{
  49. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  50. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  51. },
  52. },
  53. },
  54. {
  55. "Resize 3x1 1x1 nearest",
  56. &image.NRGBA{
  57. Rect: image.Rect(-1, -1, 2, 0),
  58. Stride: 3 * 4,
  59. Pix: []uint8{
  60. 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  61. },
  62. },
  63. 1, 1,
  64. NearestNeighbor,
  65. &image.NRGBA{
  66. Rect: image.Rect(0, 0, 1, 1),
  67. Stride: 1 * 4,
  68. Pix: []uint8{0x00, 0xff, 0x00, 0xff},
  69. },
  70. },
  71. {
  72. "Resize 2x2 0x4 box",
  73. &image.NRGBA{
  74. Rect: image.Rect(-1, -1, 1, 1),
  75. Stride: 2 * 4,
  76. Pix: []uint8{
  77. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  78. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  79. },
  80. },
  81. 0, 4,
  82. Box,
  83. &image.NRGBA{
  84. Rect: image.Rect(0, 0, 4, 4),
  85. Stride: 4 * 4,
  86. Pix: []uint8{
  87. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  88. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  89. 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
  90. 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
  91. },
  92. },
  93. },
  94. {
  95. "Resize 2x2 4x0 linear",
  96. &image.NRGBA{
  97. Rect: image.Rect(-1, -1, 1, 1),
  98. Stride: 2 * 4,
  99. Pix: []uint8{
  100. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  101. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  102. },
  103. },
  104. 4, 0,
  105. Linear,
  106. &image.NRGBA{
  107. Rect: image.Rect(0, 0, 4, 4),
  108. Stride: 4 * 4,
  109. Pix: []uint8{
  110. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x40, 0xff, 0x00, 0x00, 0xbf, 0xff, 0x00, 0x00, 0xff,
  111. 0x00, 0xff, 0x00, 0x40, 0x6e, 0x6d, 0x25, 0x70, 0xb0, 0x14, 0x3b, 0xcf, 0xbf, 0x00, 0x40, 0xff,
  112. 0x00, 0xff, 0x00, 0xbf, 0x14, 0xb0, 0x3b, 0xcf, 0x33, 0x33, 0x99, 0xef, 0x40, 0x00, 0xbf, 0xff,
  113. 0x00, 0xff, 0x00, 0xff, 0x00, 0xbf, 0x40, 0xff, 0x00, 0x40, 0xbf, 0xff, 0x00, 0x00, 0xff, 0xff,
  114. },
  115. },
  116. },
  117. {
  118. "Resize 0x0 1x1 box",
  119. &image.NRGBA{
  120. Rect: image.Rect(-1, -1, -1, -1),
  121. Stride: 0,
  122. Pix: []uint8{},
  123. },
  124. 1, 1,
  125. Box,
  126. &image.NRGBA{},
  127. },
  128. {
  129. "Resize 2x2 0x0 box",
  130. &image.NRGBA{
  131. Rect: image.Rect(-1, -1, 1, 1),
  132. Stride: 2 * 4,
  133. Pix: []uint8{
  134. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  135. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  136. },
  137. },
  138. 0, 0,
  139. Box,
  140. &image.NRGBA{},
  141. },
  142. {
  143. "Resize 2x2 -1x0 box",
  144. &image.NRGBA{
  145. Rect: image.Rect(-1, -1, 1, 1),
  146. Stride: 2 * 4,
  147. Pix: []uint8{
  148. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  149. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  150. },
  151. },
  152. -1, 0,
  153. Box,
  154. &image.NRGBA{},
  155. },
  156. }
  157. for _, d := range td {
  158. got := Resize(d.src, d.w, d.h, d.f)
  159. want := d.want
  160. if !compareNRGBA(got, want, 0) {
  161. t.Errorf("test [%s] failed: %#v", d.desc, got)
  162. }
  163. }
  164. for i, filter := range []ResampleFilter{
  165. NearestNeighbor,
  166. Box,
  167. Linear,
  168. Hermite,
  169. MitchellNetravali,
  170. CatmullRom,
  171. BSpline,
  172. Gaussian,
  173. Lanczos,
  174. Hann,
  175. Hamming,
  176. Blackman,
  177. Bartlett,
  178. Welch,
  179. Cosine,
  180. } {
  181. src := image.NewNRGBA(image.Rect(-1, -1, 2, 3))
  182. got := Resize(src, 5, 6, filter)
  183. want := image.NewNRGBA(image.Rect(0, 0, 5, 6))
  184. if !compareNRGBA(got, want, 0) {
  185. t.Errorf("test [Resize all filters #%d] failed: %#v", i, got)
  186. }
  187. if filter.Kernel != nil {
  188. x := filter.Kernel(filter.Support + 0.0001)
  189. if x != 0 {
  190. t.Errorf("test [ResampleFilter edge cases #%d] failed: %f", i, x)
  191. }
  192. }
  193. }
  194. bcs2 := bcspline(2, 1, 0)
  195. if bcs2 != 0 {
  196. t.Errorf("test [bcspline 2] failed: %f", bcs2)
  197. }
  198. }
  199. func TestResizeGolden(t *testing.T) {
  200. for name, filter := range map[string]ResampleFilter{
  201. "out_resize_nearest.png": NearestNeighbor,
  202. "out_resize_linear.png": Linear,
  203. "out_resize_catrom.png": CatmullRom,
  204. "out_resize_lanczos.png": Lanczos,
  205. } {
  206. got := Resize(testdataBranchesPNG, 150, 0, filter)
  207. want, err := Open("testdata/" + name)
  208. if err != nil {
  209. t.Errorf("Open: %v", err)
  210. }
  211. if !compareNRGBA(got, toNRGBA(want), 0) {
  212. t.Errorf("resulting image differs from golden: %s", name)
  213. }
  214. }
  215. }
  216. func TestFit(t *testing.T) {
  217. td := []struct {
  218. desc string
  219. src image.Image
  220. w, h int
  221. f ResampleFilter
  222. want *image.NRGBA
  223. }{
  224. {
  225. "Fit 2x2 1x10 box",
  226. &image.NRGBA{
  227. Rect: image.Rect(-1, -1, 1, 1),
  228. Stride: 2 * 4,
  229. Pix: []uint8{
  230. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  231. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  232. },
  233. },
  234. 1, 10,
  235. Box,
  236. &image.NRGBA{
  237. Rect: image.Rect(0, 0, 1, 1),
  238. Stride: 1 * 4,
  239. Pix: []uint8{0x55, 0x55, 0x55, 0xc0},
  240. },
  241. },
  242. {
  243. "Fit 2x2 10x1 box",
  244. &image.NRGBA{
  245. Rect: image.Rect(-1, -1, 1, 1),
  246. Stride: 2 * 4,
  247. Pix: []uint8{
  248. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  249. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  250. },
  251. },
  252. 10, 1,
  253. Box,
  254. &image.NRGBA{
  255. Rect: image.Rect(0, 0, 1, 1),
  256. Stride: 1 * 4,
  257. Pix: []uint8{0x55, 0x55, 0x55, 0xc0},
  258. },
  259. },
  260. {
  261. "Fit 2x2 10x10 box",
  262. &image.NRGBA{
  263. Rect: image.Rect(-1, -1, 1, 1),
  264. Stride: 2 * 4,
  265. Pix: []uint8{
  266. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  267. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  268. },
  269. },
  270. 10, 10,
  271. Box,
  272. &image.NRGBA{
  273. Rect: image.Rect(0, 0, 2, 2),
  274. Stride: 2 * 4,
  275. Pix: []uint8{
  276. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  277. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  278. },
  279. },
  280. },
  281. {
  282. "Fit 0x0 1x1 box",
  283. &image.NRGBA{
  284. Rect: image.Rect(-1, -1, -1, -1),
  285. Stride: 0,
  286. Pix: []uint8{},
  287. },
  288. 1, 1,
  289. Box,
  290. &image.NRGBA{},
  291. },
  292. {
  293. "Fit 2x2 0x0 box",
  294. &image.NRGBA{
  295. Rect: image.Rect(-1, -1, 1, 1),
  296. Stride: 2 * 4,
  297. Pix: []uint8{
  298. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  299. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  300. },
  301. },
  302. 0, 0,
  303. Box,
  304. &image.NRGBA{},
  305. },
  306. {
  307. "Fit 2x2 -1x0 box",
  308. &image.NRGBA{
  309. Rect: image.Rect(-1, -1, 1, 1),
  310. Stride: 2 * 4,
  311. Pix: []uint8{
  312. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  313. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  314. },
  315. },
  316. -1, 0,
  317. Box,
  318. &image.NRGBA{},
  319. },
  320. }
  321. for _, d := range td {
  322. got := Fit(d.src, d.w, d.h, d.f)
  323. want := d.want
  324. if !compareNRGBA(got, want, 0) {
  325. t.Errorf("test [%s] failed: %#v", d.desc, got)
  326. }
  327. }
  328. }
  329. func TestFill(t *testing.T) {
  330. td := []struct {
  331. desc string
  332. src image.Image
  333. w, h int
  334. a Anchor
  335. f ResampleFilter
  336. want *image.NRGBA
  337. }{
  338. {
  339. "Fill 4x4 2x2 Center Nearest",
  340. &image.NRGBA{
  341. Rect: image.Rect(-1, -1, 3, 3),
  342. Stride: 4 * 4,
  343. Pix: []uint8{
  344. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  345. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  346. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  347. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  348. },
  349. },
  350. 2, 2,
  351. Center,
  352. NearestNeighbor,
  353. &image.NRGBA{
  354. Rect: image.Rect(0, 0, 2, 2),
  355. Stride: 2 * 4,
  356. Pix: []uint8{
  357. 0x14, 0x15, 0x16, 0x17, 0x1c, 0x1d, 0x1e, 0x1f,
  358. 0x34, 0x35, 0x36, 0x37, 0x3c, 0x3d, 0x3e, 0x3f,
  359. },
  360. },
  361. },
  362. {
  363. "Fill 4x4 1x4 TopLeft Nearest",
  364. &image.NRGBA{
  365. Rect: image.Rect(-1, -1, 3, 3),
  366. Stride: 4 * 4,
  367. Pix: []uint8{
  368. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  369. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  370. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  371. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  372. },
  373. },
  374. 1, 4,
  375. TopLeft,
  376. NearestNeighbor,
  377. &image.NRGBA{
  378. Rect: image.Rect(0, 0, 1, 4),
  379. Stride: 1 * 4,
  380. Pix: []uint8{
  381. 0x00, 0x01, 0x02, 0x03,
  382. 0x10, 0x11, 0x12, 0x13,
  383. 0x20, 0x21, 0x22, 0x23,
  384. 0x30, 0x31, 0x32, 0x33,
  385. },
  386. },
  387. },
  388. {
  389. "Fill 4x4 8x2 Bottom Nearest",
  390. &image.NRGBA{
  391. Rect: image.Rect(-1, -1, 3, 3),
  392. Stride: 4 * 4,
  393. Pix: []uint8{
  394. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  395. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  396. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  397. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  398. },
  399. },
  400. 8, 2,
  401. Bottom,
  402. NearestNeighbor,
  403. &image.NRGBA{
  404. Rect: image.Rect(0, 0, 8, 2),
  405. Stride: 8 * 4,
  406. Pix: []uint8{
  407. 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,
  408. 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,
  409. },
  410. },
  411. },
  412. {
  413. "Fill 4x4 2x8 Top Nearest",
  414. &image.NRGBA{
  415. Rect: image.Rect(-1, -1, 3, 3),
  416. Stride: 4 * 4,
  417. Pix: []uint8{
  418. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  419. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  420. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  421. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  422. },
  423. },
  424. 2, 8,
  425. Top,
  426. NearestNeighbor,
  427. &image.NRGBA{
  428. Rect: image.Rect(0, 0, 2, 8),
  429. Stride: 2 * 4,
  430. Pix: []uint8{
  431. 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  432. 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  433. 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  434. 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  435. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
  436. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
  437. 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  438. 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
  439. },
  440. },
  441. },
  442. {
  443. "Fill 4x4 4x4 TopRight Box",
  444. &image.NRGBA{
  445. Rect: image.Rect(-1, -1, 3, 3),
  446. Stride: 4 * 4,
  447. Pix: []uint8{
  448. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  449. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  450. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  451. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  452. },
  453. },
  454. 4, 4,
  455. TopRight,
  456. Box,
  457. &image.NRGBA{
  458. Rect: image.Rect(0, 0, 4, 4),
  459. Stride: 4 * 4,
  460. Pix: []uint8{
  461. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  462. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  463. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  464. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  465. },
  466. },
  467. },
  468. {
  469. "Fill 4x4 0x4 Left Box",
  470. &image.NRGBA{
  471. Rect: image.Rect(-1, -1, 3, 3),
  472. Stride: 4 * 4,
  473. Pix: []uint8{
  474. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  475. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  476. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  477. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  478. },
  479. },
  480. 0, 4,
  481. Left,
  482. Box,
  483. &image.NRGBA{},
  484. },
  485. {
  486. "Fill 0x0 4x4 Right Box",
  487. &image.NRGBA{},
  488. 4, 4,
  489. Right,
  490. Box,
  491. &image.NRGBA{},
  492. },
  493. }
  494. for _, d := range td {
  495. got := Fill(d.src, d.w, d.h, d.a, d.f)
  496. want := d.want
  497. if !compareNRGBA(got, want, 0) {
  498. t.Errorf("test [%s] failed: %#v", d.desc, got)
  499. }
  500. }
  501. }
  502. func TestThumbnail(t *testing.T) {
  503. td := []struct {
  504. desc string
  505. src image.Image
  506. w, h int
  507. f ResampleFilter
  508. want *image.NRGBA
  509. }{
  510. {
  511. "Thumbnail 6x2 1x1 box",
  512. &image.NRGBA{
  513. Rect: image.Rect(-1, -1, 5, 1),
  514. Stride: 6 * 4,
  515. Pix: []uint8{
  516. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  517. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  518. },
  519. },
  520. 1, 1,
  521. Box,
  522. &image.NRGBA{
  523. Rect: image.Rect(0, 0, 1, 1),
  524. Stride: 1 * 4,
  525. Pix: []uint8{0x55, 0x55, 0x55, 0xc0},
  526. },
  527. },
  528. {
  529. "Thumbnail 2x6 1x1 box",
  530. &image.NRGBA{
  531. Rect: image.Rect(-1, -1, 1, 5),
  532. Stride: 2 * 4,
  533. Pix: []uint8{
  534. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  535. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  536. 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
  537. 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
  538. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  539. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  540. },
  541. },
  542. 1, 1,
  543. Box,
  544. &image.NRGBA{
  545. Rect: image.Rect(0, 0, 1, 1),
  546. Stride: 1 * 4,
  547. Pix: []uint8{0x55, 0x55, 0x55, 0xc0},
  548. },
  549. },
  550. {
  551. "Thumbnail 1x3 2x2 box",
  552. &image.NRGBA{
  553. Rect: image.Rect(-1, -1, 0, 2),
  554. Stride: 1 * 4,
  555. Pix: []uint8{
  556. 0x00, 0x00, 0x00, 0x00,
  557. 0xff, 0x00, 0x00, 0xff,
  558. 0xff, 0xff, 0xff, 0xff,
  559. },
  560. },
  561. 2, 2,
  562. Box,
  563. &image.NRGBA{
  564. Rect: image.Rect(0, 0, 2, 2),
  565. Stride: 2 * 4,
  566. Pix: []uint8{
  567. 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  568. 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
  569. },
  570. },
  571. },
  572. }
  573. for _, d := range td {
  574. got := Thumbnail(d.src, d.w, d.h, d.f)
  575. want := d.want
  576. if !compareNRGBA(got, want, 0) {
  577. t.Errorf("test [%s] failed: %#v", d.desc, got)
  578. }
  579. }
  580. }
  581. func BenchmarkResize(b *testing.B) {
  582. for _, dir := range []string{"Down", "Up"} {
  583. for _, filter := range []string{"NearestNeighbor", "Linear", "CatmullRom", "Lanczos"} {
  584. for _, format := range []string{"JPEG", "PNG"} {
  585. var size int
  586. switch dir {
  587. case "Down":
  588. size = 100
  589. case "Up":
  590. size = 1000
  591. }
  592. var f ResampleFilter
  593. switch filter {
  594. case "NearestNeighbor":
  595. f = NearestNeighbor
  596. case "Linear":
  597. f = Linear
  598. case "CatmullRom":
  599. f = CatmullRom
  600. case "Lanczos":
  601. f = Lanczos
  602. }
  603. var img image.Image
  604. switch format {
  605. case "JPEG":
  606. img = testdataBranchesJPG
  607. case "PNG":
  608. img = testdataBranchesPNG
  609. }
  610. b.Run(fmt.Sprintf("%s %s %s", dir, filter, format), func(b *testing.B) {
  611. b.ReportAllocs()
  612. for i := 0; i < b.N; i++ {
  613. Resize(img, size, size, f)
  614. }
  615. })
  616. }
  617. }
  618. }
  619. }