resize_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. package bimg
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "image"
  6. "image/jpeg"
  7. "io/ioutil"
  8. "os"
  9. "path"
  10. "strconv"
  11. "testing"
  12. )
  13. func TestResize(t *testing.T) {
  14. options := Options{Width: 800, Height: 600}
  15. buf, _ := Read("fixtures/test.jpg")
  16. newImg, err := Resize(buf, options)
  17. if err != nil {
  18. t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
  19. }
  20. if DetermineImageType(newImg) != JPEG {
  21. t.Fatal("Image is not jpeg")
  22. }
  23. size, _ := Size(newImg)
  24. if size.Height != options.Height || size.Width != options.Width {
  25. t.Fatalf("Invalid image size: %dx%d", size.Width, size.Height)
  26. }
  27. Write("fixtures/test_out.jpg", newImg)
  28. }
  29. func TestResizeVerticalImage(t *testing.T) {
  30. tests := []struct {
  31. format ImageType
  32. options Options
  33. }{
  34. {JPEG, Options{Width: 800, Height: 600}},
  35. {JPEG, Options{Width: 1000, Height: 1000}},
  36. {JPEG, Options{Width: 1000, Height: 1500}},
  37. {JPEG, Options{Width: 1000}},
  38. {JPEG, Options{Height: 1500}},
  39. {JPEG, Options{Width: 100, Height: 50}},
  40. {JPEG, Options{Width: 2000, Height: 2000}},
  41. {JPEG, Options{Width: 500, Height: 1000}},
  42. {JPEG, Options{Width: 500}},
  43. {JPEG, Options{Height: 500}},
  44. {JPEG, Options{Crop: true, Width: 500, Height: 1000}},
  45. {JPEG, Options{Crop: true, Enlarge: true, Width: 2000, Height: 1400}},
  46. {JPEG, Options{Enlarge: true, Force: true, Width: 2000, Height: 2000}},
  47. {JPEG, Options{Force: true, Width: 2000, Height: 2000}},
  48. }
  49. buf, _ := Read("fixtures/vertical.jpg")
  50. for _, test := range tests {
  51. image, err := Resize(buf, test.options)
  52. if err != nil {
  53. t.Errorf("Resize(imgData, %#v) error: %#v", test.options, err)
  54. }
  55. if DetermineImageType(image) != test.format {
  56. t.Fatalf("Image format is invalid. Expected: %#v", test.format)
  57. }
  58. size, _ := Size(image)
  59. if test.options.Height > 0 && size.Height != test.options.Height {
  60. t.Fatalf("Invalid height: %d", size.Height)
  61. }
  62. if test.options.Width > 0 && size.Width != test.options.Width {
  63. t.Fatalf("Invalid width: %d", size.Width)
  64. }
  65. Write("fixtures/test_vertical_"+strconv.Itoa(test.options.Width)+"x"+strconv.Itoa(test.options.Height)+"_out.jpg", image)
  66. }
  67. }
  68. func TestResizeCustomSizes(t *testing.T) {
  69. tests := []struct {
  70. format ImageType
  71. options Options
  72. }{
  73. {JPEG, Options{Width: 800, Height: 600}},
  74. {JPEG, Options{Width: 1000, Height: 1000}},
  75. {JPEG, Options{Width: 100, Height: 50}},
  76. {JPEG, Options{Width: 2000, Height: 2000}},
  77. {JPEG, Options{Width: 500, Height: 1000}},
  78. {JPEG, Options{Width: 500}},
  79. {JPEG, Options{Height: 500}},
  80. {JPEG, Options{Crop: true, Width: 500, Height: 1000}},
  81. {JPEG, Options{Crop: true, Enlarge: true, Width: 2000, Height: 1400}},
  82. {JPEG, Options{Enlarge: true, Force: true, Width: 2000, Height: 2000}},
  83. {JPEG, Options{Force: true, Width: 2000, Height: 2000}},
  84. }
  85. buf, _ := Read("fixtures/test.jpg")
  86. for _, test := range tests {
  87. image, err := Resize(buf, test.options)
  88. if err != nil {
  89. t.Errorf("Resize(imgData, %#v) error: %#v", test.options, err)
  90. }
  91. if DetermineImageType(image) != test.format {
  92. t.Fatalf("Image format is invalid. Expected: %#v", test.format)
  93. }
  94. size, _ := Size(image)
  95. if test.options.Height > 0 && size.Height != test.options.Height {
  96. t.Fatalf("Invalid height: %d", size.Height)
  97. }
  98. if test.options.Width > 0 && size.Width != test.options.Width {
  99. t.Fatalf("Invalid width: %d", size.Width)
  100. }
  101. }
  102. }
  103. func TestResizePrecision(t *testing.T) {
  104. // see https://github.com/h2non/bimg/issues/99
  105. img := image.NewGray16(image.Rect(0, 0, 1920, 1080))
  106. input := &bytes.Buffer{}
  107. jpeg.Encode(input, img, nil)
  108. opts := Options{Width: 300}
  109. newImg, err := Resize(input.Bytes(), opts)
  110. if err != nil {
  111. t.Fatalf("Resize(imgData, %#v) error: %#v", opts, err)
  112. }
  113. size, _ := Size(newImg)
  114. if size.Width != opts.Width {
  115. t.Fatalf("Invalid width: %d", size.Width)
  116. }
  117. }
  118. func TestRotate(t *testing.T) {
  119. options := Options{Width: 800, Height: 600, Rotate: 270, Crop: true}
  120. buf, _ := Read("fixtures/test.jpg")
  121. newImg, err := Resize(buf, options)
  122. if err != nil {
  123. t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
  124. }
  125. if DetermineImageType(newImg) != JPEG {
  126. t.Error("Image is not jpeg")
  127. }
  128. size, _ := Size(newImg)
  129. if size.Width != options.Width || size.Height != options.Height {
  130. t.Errorf("Invalid image size: %dx%d", size.Width, size.Height)
  131. }
  132. Write("fixtures/test_rotate_out.jpg", newImg)
  133. }
  134. func TestInvalidRotateDegrees(t *testing.T) {
  135. options := Options{Width: 800, Height: 600, Rotate: 111, Crop: true}
  136. buf, _ := Read("fixtures/test.jpg")
  137. newImg, err := Resize(buf, options)
  138. if err != nil {
  139. t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
  140. }
  141. if DetermineImageType(newImg) != JPEG {
  142. t.Errorf("Image is not jpeg")
  143. }
  144. size, _ := Size(newImg)
  145. if size.Width != options.Width || size.Height != options.Height {
  146. t.Errorf("Invalid image size: %dx%d", size.Width, size.Height)
  147. }
  148. Write("fixtures/test_rotate_invalid_out.jpg", newImg)
  149. }
  150. func TestCorruptedImage(t *testing.T) {
  151. options := Options{Width: 800, Height: 600}
  152. buf, _ := Read("fixtures/corrupt.jpg")
  153. newImg, err := Resize(buf, options)
  154. if err != nil {
  155. t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
  156. }
  157. if DetermineImageType(newImg) != JPEG {
  158. t.Fatal("Image is not jpeg")
  159. }
  160. size, _ := Size(newImg)
  161. if size.Height != options.Height || size.Width != options.Width {
  162. t.Fatalf("Invalid image size: %dx%d", size.Width, size.Height)
  163. }
  164. Write("fixtures/test_corrupt_out.jpg", newImg)
  165. }
  166. func TestNoColorProfile(t *testing.T) {
  167. options := Options{Width: 800, Height: 600, NoProfile: true}
  168. buf, _ := Read("fixtures/test.jpg")
  169. newImg, err := Resize(buf, options)
  170. if err != nil {
  171. t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
  172. }
  173. metadata, err := Metadata(newImg)
  174. if metadata.Profile == true {
  175. t.Fatal("Invalid profile data")
  176. }
  177. size, _ := Size(newImg)
  178. if size.Height != options.Height || size.Width != options.Width {
  179. t.Fatalf("Invalid image size: %dx%d", size.Width, size.Height)
  180. }
  181. }
  182. func TestEmbedExtendColor(t *testing.T) {
  183. options := Options{Width: 400, Height: 600, Crop: false, Embed: true, Extend: ExtendWhite, Background: Color{255, 20, 10}}
  184. buf, _ := Read("fixtures/test_issue.jpg")
  185. newImg, err := Resize(buf, options)
  186. if err != nil {
  187. t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
  188. }
  189. size, _ := Size(newImg)
  190. if size.Height != options.Height || size.Width != options.Width {
  191. t.Fatalf("Invalid image size: %dx%d", size.Width, size.Height)
  192. }
  193. Write("fixtures/test_extend_white_out.jpg", newImg)
  194. }
  195. func TestEmbedExtendWithCustomColor(t *testing.T) {
  196. options := Options{Width: 400, Height: 600, Crop: false, Embed: true, Extend: 5, Background: Color{255, 20, 10}}
  197. buf, _ := Read("fixtures/test_issue.jpg")
  198. newImg, err := Resize(buf, options)
  199. if err != nil {
  200. t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
  201. }
  202. size, _ := Size(newImg)
  203. if size.Height != options.Height || size.Width != options.Width {
  204. t.Fatalf("Invalid image size: %dx%d", size.Width, size.Height)
  205. }
  206. Write("fixtures/test_extend_background_out.jpg", newImg)
  207. }
  208. func TestGaussianBlur(t *testing.T) {
  209. options := Options{Width: 800, Height: 600, GaussianBlur: GaussianBlur{Sigma: 5}}
  210. buf, _ := Read("fixtures/test.jpg")
  211. newImg, err := Resize(buf, options)
  212. if err != nil {
  213. t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
  214. }
  215. size, _ := Size(newImg)
  216. if size.Height != options.Height || size.Width != options.Width {
  217. t.Fatalf("Invalid image size: %dx%d", size.Width, size.Height)
  218. }
  219. Write("fixtures/test_gaussian_out.jpg", newImg)
  220. }
  221. func TestSharpen(t *testing.T) {
  222. options := Options{Width: 800, Height: 600, Sharpen: Sharpen{Radius: 1, X1: 1.5, Y2: 20, Y3: 50, M1: 1, M2: 2}}
  223. buf, _ := Read("fixtures/test.jpg")
  224. newImg, err := Resize(buf, options)
  225. if err != nil {
  226. t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
  227. }
  228. size, _ := Size(newImg)
  229. if size.Height != options.Height || size.Width != options.Width {
  230. t.Fatalf("Invalid image size: %dx%d", size.Width, size.Height)
  231. }
  232. Write("fixtures/test_sharpen_out.jpg", newImg)
  233. }
  234. func TestExtractWithDefaultAxis(t *testing.T) {
  235. options := Options{AreaWidth: 200, AreaHeight: 200}
  236. buf, _ := Read("fixtures/test.jpg")
  237. newImg, err := Resize(buf, options)
  238. if err != nil {
  239. t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
  240. }
  241. size, _ := Size(newImg)
  242. if size.Height != options.AreaHeight || size.Width != options.AreaWidth {
  243. t.Fatalf("Invalid image size: %dx%d", size.Width, size.Height)
  244. }
  245. Write("fixtures/test_extract_defaults_out.jpg", newImg)
  246. }
  247. func TestExtractCustomAxis(t *testing.T) {
  248. options := Options{Top: 100, Left: 100, AreaWidth: 200, AreaHeight: 200}
  249. buf, _ := Read("fixtures/test.jpg")
  250. newImg, err := Resize(buf, options)
  251. if err != nil {
  252. t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
  253. }
  254. size, _ := Size(newImg)
  255. if size.Height != options.AreaHeight || size.Width != options.AreaWidth {
  256. t.Fatalf("Invalid image size: %dx%d", size.Width, size.Height)
  257. }
  258. Write("fixtures/test_extract_custom_axis_out.jpg", newImg)
  259. }
  260. func TestConvert(t *testing.T) {
  261. width, height := 300, 240
  262. formats := [3]ImageType{PNG, WEBP, JPEG}
  263. files := []string{
  264. "test.jpg",
  265. "test.png",
  266. "test.webp",
  267. }
  268. for _, file := range files {
  269. img, err := os.Open("fixtures/" + file)
  270. if err != nil {
  271. t.Fatal(err)
  272. }
  273. buf, err := ioutil.ReadAll(img)
  274. if err != nil {
  275. t.Fatal(err)
  276. }
  277. img.Close()
  278. for _, format := range formats {
  279. options := Options{Width: width, Height: height, Crop: true, Type: format}
  280. newImg, err := Resize(buf, options)
  281. if err != nil {
  282. t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
  283. }
  284. if DetermineImageType(newImg) != format {
  285. t.Fatal("Image is not png")
  286. }
  287. size, _ := Size(newImg)
  288. if size.Height != height || size.Width != width {
  289. t.Fatalf("Invalid image size: %dx%d", size.Width, size.Height)
  290. }
  291. }
  292. }
  293. }
  294. func TestResizePngWithTransparency(t *testing.T) {
  295. width, height := 300, 240
  296. options := Options{Width: width, Height: height, Crop: true}
  297. img, err := os.Open("fixtures/transparent.png")
  298. if err != nil {
  299. t.Fatal(err)
  300. }
  301. defer img.Close()
  302. buf, err := ioutil.ReadAll(img)
  303. if err != nil {
  304. t.Fatal(err)
  305. }
  306. newImg, err := Resize(buf, options)
  307. if err != nil {
  308. t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
  309. }
  310. if DetermineImageType(newImg) != PNG {
  311. t.Fatal("Image is not png")
  312. }
  313. size, _ := Size(newImg)
  314. if size.Height != height || size.Width != width {
  315. t.Fatal("Invalid image size")
  316. }
  317. Write("fixtures/transparent_out.png", newImg)
  318. }
  319. func TestIfBothSmartCropOptionsAreIdentical(t *testing.T) {
  320. if !(VipsMajorVersion >= 8 && VipsMinorVersion > 4) {
  321. t.Skipf("Skipping this test, libvips doesn't meet version requirement %s > 8.4", VipsVersion)
  322. }
  323. benchmarkOptions := Options{Width: 100, Height: 100, Crop: true}
  324. smartCropOptions := Options{Width: 100, Height: 100, Crop: true, SmartCrop: true}
  325. gravityOptions := Options{Width: 100, Height: 100, Crop: true, Gravity: GravitySmart}
  326. testImg, err := os.Open("fixtures/northern_cardinal_bird.jpg")
  327. if err != nil {
  328. t.Fatal(err)
  329. }
  330. defer testImg.Close()
  331. testImgByte, err := ioutil.ReadAll(testImg)
  332. if err != nil {
  333. t.Fatal(err)
  334. }
  335. scImg, err := Resize(testImgByte, smartCropOptions)
  336. if err != nil {
  337. t.Fatal(err)
  338. }
  339. gImg, err := Resize(testImgByte, gravityOptions)
  340. if err != nil {
  341. t.Fatal(err)
  342. }
  343. benchmarkImg, err := Resize(testImgByte, benchmarkOptions)
  344. if err != nil {
  345. t.Fatal(err)
  346. }
  347. sch, gh, bh := md5.Sum(scImg), md5.Sum(gImg), md5.Sum(benchmarkImg)
  348. if gh == bh || sch == bh {
  349. t.Error("Expected both options produce a different result from a standard crop.")
  350. }
  351. if sch != gh {
  352. t.Errorf("Expected both options to result in the same output, %x != %x", sch, gh)
  353. }
  354. }
  355. func runBenchmarkResize(file string, o Options, b *testing.B) {
  356. buf, _ := Read(path.Join("fixtures", file))
  357. for n := 0; n < b.N; n++ {
  358. Resize(buf, o)
  359. }
  360. }
  361. func BenchmarkRotateJpeg(b *testing.B) {
  362. options := Options{Rotate: 180}
  363. runBenchmarkResize("test.jpg", options, b)
  364. }
  365. func BenchmarkResizeLargeJpeg(b *testing.B) {
  366. options := Options{
  367. Width: 800,
  368. Height: 600,
  369. }
  370. runBenchmarkResize("test.jpg", options, b)
  371. }
  372. func BenchmarkResizePng(b *testing.B) {
  373. options := Options{
  374. Width: 200,
  375. Height: 200,
  376. }
  377. runBenchmarkResize("test.png", options, b)
  378. }
  379. func BenchmarkResizeWebP(b *testing.B) {
  380. options := Options{
  381. Width: 200,
  382. Height: 200,
  383. }
  384. runBenchmarkResize("test.webp", options, b)
  385. }
  386. func BenchmarkConvertToJpeg(b *testing.B) {
  387. options := Options{Type: JPEG}
  388. runBenchmarkResize("test.png", options, b)
  389. }
  390. func BenchmarkConvertToPng(b *testing.B) {
  391. options := Options{Type: PNG}
  392. runBenchmarkResize("test.jpg", options, b)
  393. }
  394. func BenchmarkConvertToWebp(b *testing.B) {
  395. options := Options{Type: WEBP}
  396. runBenchmarkResize("test.jpg", options, b)
  397. }
  398. func BenchmarkCropJpeg(b *testing.B) {
  399. options := Options{
  400. Width: 800,
  401. Height: 600,
  402. }
  403. runBenchmarkResize("test.jpg", options, b)
  404. }
  405. func BenchmarkCropPng(b *testing.B) {
  406. options := Options{
  407. Width: 800,
  408. Height: 600,
  409. }
  410. runBenchmarkResize("test.png", options, b)
  411. }
  412. func BenchmarkCropWebP(b *testing.B) {
  413. options := Options{
  414. Width: 800,
  415. Height: 600,
  416. }
  417. runBenchmarkResize("test.webp", options, b)
  418. }
  419. func BenchmarkExtractJpeg(b *testing.B) {
  420. options := Options{
  421. Top: 100,
  422. Left: 50,
  423. AreaWidth: 600,
  424. AreaHeight: 480,
  425. }
  426. runBenchmarkResize("test.jpg", options, b)
  427. }
  428. func BenchmarkExtractPng(b *testing.B) {
  429. options := Options{
  430. Top: 100,
  431. Left: 50,
  432. AreaWidth: 600,
  433. AreaHeight: 480,
  434. }
  435. runBenchmarkResize("test.png", options, b)
  436. }
  437. func BenchmarkExtractWebp(b *testing.B) {
  438. options := Options{
  439. Top: 100,
  440. Left: 50,
  441. AreaWidth: 600,
  442. AreaHeight: 480,
  443. }
  444. runBenchmarkResize("test.webp", options, b)
  445. }
  446. func BenchmarkZoomJpeg(b *testing.B) {
  447. options := Options{Zoom: 1}
  448. runBenchmarkResize("test.jpg", options, b)
  449. }
  450. func BenchmarkZoomPng(b *testing.B) {
  451. options := Options{Zoom: 1}
  452. runBenchmarkResize("test.png", options, b)
  453. }
  454. func BenchmarkZoomWebp(b *testing.B) {
  455. options := Options{Zoom: 1}
  456. runBenchmarkResize("test.webp", options, b)
  457. }
  458. func BenchmarkWatermarkJpeg(b *testing.B) {
  459. options := Options{
  460. Watermark: Watermark{
  461. Text: "Chuck Norris (c) 2315",
  462. Opacity: 0.25,
  463. Width: 200,
  464. DPI: 100,
  465. Margin: 150,
  466. Font: "sans bold 12",
  467. Background: Color{255, 255, 255},
  468. },
  469. }
  470. runBenchmarkResize("test.jpg", options, b)
  471. }
  472. func BenchmarkWatermarPng(b *testing.B) {
  473. options := Options{
  474. Watermark: Watermark{
  475. Text: "Chuck Norris (c) 2315",
  476. Opacity: 0.25,
  477. Width: 200,
  478. DPI: 100,
  479. Margin: 150,
  480. Font: "sans bold 12",
  481. Background: Color{255, 255, 255},
  482. },
  483. }
  484. runBenchmarkResize("test.png", options, b)
  485. }
  486. func BenchmarkWatermarWebp(b *testing.B) {
  487. options := Options{
  488. Watermark: Watermark{
  489. Text: "Chuck Norris (c) 2315",
  490. Opacity: 0.25,
  491. Width: 200,
  492. DPI: 100,
  493. Margin: 150,
  494. Font: "sans bold 12",
  495. Background: Color{255, 255, 255},
  496. },
  497. }
  498. runBenchmarkResize("test.webp", options, b)
  499. }
  500. func BenchmarkWatermarkImageJpeg(b *testing.B) {
  501. watermark := readFile("transparent.png")
  502. options := Options{
  503. WatermarkImage: WatermarkImage{
  504. Buf: watermark,
  505. Opacity: 0.25,
  506. Left: 100,
  507. Top: 100,
  508. },
  509. }
  510. runBenchmarkResize("test.jpg", options, b)
  511. }
  512. func BenchmarkWatermarImagePng(b *testing.B) {
  513. watermark := readFile("transparent.png")
  514. options := Options{
  515. WatermarkImage: WatermarkImage{
  516. Buf: watermark,
  517. Opacity: 0.25,
  518. Left: 100,
  519. Top: 100,
  520. },
  521. }
  522. runBenchmarkResize("test.png", options, b)
  523. }
  524. func BenchmarkWatermarImageWebp(b *testing.B) {
  525. watermark := readFile("transparent.png")
  526. options := Options{
  527. WatermarkImage: WatermarkImage{
  528. Buf: watermark,
  529. Opacity: 0.25,
  530. Left: 100,
  531. Top: 100,
  532. },
  533. }
  534. runBenchmarkResize("test.webp", options, b)
  535. }