image_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. package bimg
  2. import (
  3. "fmt"
  4. "path"
  5. "testing"
  6. )
  7. func TestImageResize(t *testing.T) {
  8. buf, err := initImage("test.jpg").Resize(300, 240)
  9. if err != nil {
  10. t.Errorf("Cannot process the image: %#v", err)
  11. }
  12. err = assertSize(buf, 300, 240)
  13. if err != nil {
  14. t.Error(err)
  15. }
  16. Write("fixtures/test_resize_out.jpg", buf)
  17. }
  18. func TestImageGifResize(t *testing.T) {
  19. _, err := initImage("test.gif").Resize(300, 240)
  20. if err == nil {
  21. t.Errorf("GIF shouldn't be saved within VIPS")
  22. }
  23. }
  24. func TestImagePdfResize(t *testing.T) {
  25. _, err := initImage("test.pdf").Resize(300, 240)
  26. if err == nil {
  27. t.Errorf("PDF cannot be saved within VIPS")
  28. }
  29. }
  30. func TestImageSvgResize(t *testing.T) {
  31. _, err := initImage("test.svg").Resize(300, 240)
  32. if err == nil {
  33. t.Errorf("SVG cannot be saved within VIPS")
  34. }
  35. }
  36. func TestImageGifToJpeg(t *testing.T) {
  37. if VipsMajorVersion >= 8 && VipsMinorVersion > 2 {
  38. i := initImage("test.gif")
  39. options := Options{
  40. Type: JPEG,
  41. }
  42. buf, err := i.Process(options)
  43. if err != nil {
  44. t.Errorf("Cannot process the image: %#v", err)
  45. }
  46. Write("fixtures/test_gif.jpg", buf)
  47. }
  48. }
  49. func TestImagePdfToJpeg(t *testing.T) {
  50. if VipsMajorVersion >= 8 && VipsMinorVersion > 2 {
  51. i := initImage("test.pdf")
  52. options := Options{
  53. Type: JPEG,
  54. }
  55. buf, err := i.Process(options)
  56. if err != nil {
  57. t.Errorf("Cannot process the image: %#v", err)
  58. }
  59. Write("fixtures/test_pdf.jpg", buf)
  60. }
  61. }
  62. func TestImageSvgToJpeg(t *testing.T) {
  63. if VipsMajorVersion >= 8 && VipsMinorVersion > 2 {
  64. i := initImage("test.svg")
  65. options := Options{
  66. Type: JPEG,
  67. }
  68. buf, err := i.Process(options)
  69. if err != nil {
  70. t.Errorf("Cannot process the image: %#v", err)
  71. }
  72. Write("fixtures/test_svg.jpg", buf)
  73. }
  74. }
  75. func TestImageResizeAndCrop(t *testing.T) {
  76. buf, err := initImage("test.jpg").ResizeAndCrop(300, 200)
  77. if err != nil {
  78. t.Errorf("Cannot process the image: %#v", err)
  79. }
  80. err = assertSize(buf, 300, 200)
  81. if err != nil {
  82. t.Error(err)
  83. }
  84. Write("fixtures/test_resize_crop_out.jpg", buf)
  85. }
  86. func TestImageExtract(t *testing.T) {
  87. buf, err := initImage("test.jpg").Extract(100, 100, 300, 200)
  88. if err != nil {
  89. t.Errorf("Cannot process the image: %s", err)
  90. }
  91. err = assertSize(buf, 300, 200)
  92. if err != nil {
  93. t.Error(err)
  94. }
  95. Write("fixtures/test_extract_out.jpg", buf)
  96. }
  97. func TestImageExtractZero(t *testing.T) {
  98. buf, err := initImage("test.jpg").Extract(0, 0, 300, 200)
  99. if err != nil {
  100. t.Errorf("Cannot process the image: %s", err)
  101. }
  102. err = assertSize(buf, 300, 200)
  103. if err != nil {
  104. t.Error(err)
  105. }
  106. Write("fixtures/test_extract_zero_out.jpg", buf)
  107. }
  108. func TestImageEnlarge(t *testing.T) {
  109. buf, err := initImage("test.png").Enlarge(500, 375)
  110. if err != nil {
  111. t.Errorf("Cannot process the image: %#v", err)
  112. }
  113. err = assertSize(buf, 500, 375)
  114. if err != nil {
  115. t.Error(err)
  116. }
  117. Write("fixtures/test_enlarge_out.jpg", buf)
  118. }
  119. func TestImageEnlargeAndCrop(t *testing.T) {
  120. buf, err := initImage("test.png").EnlargeAndCrop(800, 480)
  121. if err != nil {
  122. t.Errorf("Cannot process the image: %#v", err)
  123. }
  124. err = assertSize(buf, 800, 480)
  125. if err != nil {
  126. t.Error(err)
  127. }
  128. Write("fixtures/test_enlarge_crop_out.jpg", buf)
  129. }
  130. func TestImageCrop(t *testing.T) {
  131. buf, err := initImage("test.jpg").Crop(800, 600, GravityNorth)
  132. if err != nil {
  133. t.Errorf("Cannot process the image: %s", err)
  134. }
  135. err = assertSize(buf, 800, 600)
  136. if err != nil {
  137. t.Error(err)
  138. }
  139. Write("fixtures/test_crop_out.jpg", buf)
  140. }
  141. func TestImageCropByWidth(t *testing.T) {
  142. buf, err := initImage("test.jpg").CropByWidth(600)
  143. if err != nil {
  144. t.Errorf("Cannot process the image: %s", err)
  145. }
  146. err = assertSize(buf, 600, 1050)
  147. if err != nil {
  148. t.Error(err)
  149. }
  150. Write("fixtures/test_crop_width_out.jpg", buf)
  151. }
  152. func TestImageCropByHeight(t *testing.T) {
  153. buf, err := initImage("test.jpg").CropByHeight(300)
  154. if err != nil {
  155. t.Errorf("Cannot process the image: %s", err)
  156. }
  157. err = assertSize(buf, 1680, 300)
  158. if err != nil {
  159. t.Error(err)
  160. }
  161. Write("fixtures/test_crop_height_out.jpg", buf)
  162. }
  163. func TestImageThumbnail(t *testing.T) {
  164. buf, err := initImage("test.jpg").Thumbnail(100)
  165. if err != nil {
  166. t.Errorf("Cannot process the image: %s", err)
  167. }
  168. err = assertSize(buf, 100, 100)
  169. if err != nil {
  170. t.Error(err)
  171. }
  172. Write("fixtures/test_thumbnail_out.jpg", buf)
  173. }
  174. func TestImageWatermark(t *testing.T) {
  175. image := initImage("test.jpg")
  176. _, err := image.Crop(800, 600, GravityNorth)
  177. if err != nil {
  178. t.Errorf("Cannot process the image: %#v", err)
  179. }
  180. buf, err := image.Watermark(Watermark{
  181. Text: "Copy me if you can",
  182. Opacity: 0.5,
  183. Width: 200,
  184. DPI: 100,
  185. Background: Color{255, 255, 255},
  186. })
  187. if err != nil {
  188. t.Error(err)
  189. }
  190. err = assertSize(buf, 800, 600)
  191. if err != nil {
  192. t.Error(err)
  193. }
  194. if DetermineImageType(buf) != JPEG {
  195. t.Fatal("Image is not jpeg")
  196. }
  197. Write("fixtures/test_watermark_text_out.jpg", buf)
  198. }
  199. func TestImageWatermarkWithImage(t *testing.T) {
  200. image := initImage("test.jpg")
  201. watermark, _ := imageBuf("transparent.png")
  202. _, err := image.Crop(800, 600, GravityNorth)
  203. if err != nil {
  204. t.Errorf("Cannot process the image: %#v", err)
  205. }
  206. buf, err := image.WatermarkImage(WatermarkImage{Left: 100, Top: 100, Buf: watermark})
  207. if err != nil {
  208. t.Error(err)
  209. }
  210. err = assertSize(buf, 800, 600)
  211. if err != nil {
  212. t.Error(err)
  213. }
  214. if DetermineImageType(buf) != JPEG {
  215. t.Fatal("Image is not jpeg")
  216. }
  217. Write("fixtures/test_watermark_image_out.jpg", buf)
  218. }
  219. func TestImageWatermarkNoReplicate(t *testing.T) {
  220. image := initImage("test.jpg")
  221. _, err := image.Crop(800, 600, GravityNorth)
  222. if err != nil {
  223. t.Errorf("Cannot process the image: %s", err)
  224. }
  225. buf, err := image.Watermark(Watermark{
  226. Text: "Copy me if you can",
  227. Opacity: 0.5,
  228. Width: 200,
  229. DPI: 100,
  230. NoReplicate: true,
  231. Background: Color{255, 255, 255},
  232. })
  233. if err != nil {
  234. t.Error(err)
  235. }
  236. err = assertSize(buf, 800, 600)
  237. if err != nil {
  238. t.Error(err)
  239. }
  240. if DetermineImageType(buf) != JPEG {
  241. t.Fatal("Image is not jpeg")
  242. }
  243. Write("fixtures/test_watermark_replicate_out.jpg", buf)
  244. }
  245. func TestImageZoom(t *testing.T) {
  246. image := initImage("test.jpg")
  247. _, err := image.Extract(100, 100, 400, 300)
  248. if err != nil {
  249. t.Errorf("Cannot extract the image: %s", err)
  250. }
  251. buf, err := image.Zoom(1)
  252. if err != nil {
  253. t.Errorf("Cannot process the image: %s", err)
  254. }
  255. err = assertSize(buf, 800, 600)
  256. if err != nil {
  257. t.Error(err)
  258. }
  259. Write("fixtures/test_zoom_out.jpg", buf)
  260. }
  261. func TestImageFlip(t *testing.T) {
  262. buf, err := initImage("test.jpg").Flip()
  263. if err != nil {
  264. t.Errorf("Cannot process the image: %#v", err)
  265. }
  266. Write("fixtures/test_flip_out.jpg", buf)
  267. }
  268. func TestImageFlop(t *testing.T) {
  269. buf, err := initImage("test.jpg").Flop()
  270. if err != nil {
  271. t.Errorf("Cannot process the image: %#v", err)
  272. }
  273. Write("fixtures/test_flop_out.jpg", buf)
  274. }
  275. func TestImageRotate(t *testing.T) {
  276. buf, err := initImage("test_flip_out.jpg").Rotate(90)
  277. if err != nil {
  278. t.Errorf("Cannot process the image: %#v", err)
  279. }
  280. Write("fixtures/test_image_rotate_out.jpg", buf)
  281. }
  282. func TestImageConvert(t *testing.T) {
  283. buf, err := initImage("test.jpg").Convert(PNG)
  284. if err != nil {
  285. t.Errorf("Cannot process the image: %#v", err)
  286. }
  287. Write("fixtures/test_image_convert_out.png", buf)
  288. }
  289. func TestTransparentImageConvert(t *testing.T) {
  290. image := initImage("transparent.png")
  291. options := Options{
  292. Type: JPEG,
  293. Background: Color{255, 255, 255},
  294. }
  295. buf, err := image.Process(options)
  296. if err != nil {
  297. t.Errorf("Cannot process the image: %#v", err)
  298. }
  299. Write("fixtures/test_transparent_image_convert_out.jpg", buf)
  300. }
  301. func TestImageMetadata(t *testing.T) {
  302. data, err := initImage("test.png").Metadata()
  303. if err != nil {
  304. t.Errorf("Cannot process the image: %#v", err)
  305. }
  306. if data.Alpha != true {
  307. t.Fatal("Invalid alpha channel")
  308. }
  309. if data.Size.Width != 400 {
  310. t.Fatal("Invalid width size")
  311. }
  312. if data.Type != "png" {
  313. t.Fatal("Invalid image type")
  314. }
  315. }
  316. func TestInterpretation(t *testing.T) {
  317. interpretation, err := initImage("test.jpg").Interpretation()
  318. if err != nil {
  319. t.Errorf("Cannot process the image: %#v", err)
  320. }
  321. if interpretation != InterpretationSRGB {
  322. t.Errorf("Invalid interpretation: %d", interpretation)
  323. }
  324. }
  325. func TestImageColourspace(t *testing.T) {
  326. tests := []struct {
  327. file string
  328. interpretation Interpretation
  329. }{
  330. {"test.jpg", InterpretationSRGB},
  331. {"test.jpg", InterpretationBW},
  332. }
  333. for _, test := range tests {
  334. buf, err := initImage(test.file).Colourspace(test.interpretation)
  335. if err != nil {
  336. t.Errorf("Cannot process the image: %#v", err)
  337. }
  338. interpretation, err := ImageInterpretation(buf)
  339. if interpretation != test.interpretation {
  340. t.Errorf("Invalid colourspace")
  341. }
  342. }
  343. }
  344. func TestImageColourspaceIsSupported(t *testing.T) {
  345. supported, err := initImage("test.jpg").ColourspaceIsSupported()
  346. if err != nil {
  347. t.Errorf("Cannot process the image: %#v", err)
  348. }
  349. if supported != true {
  350. t.Errorf("Non-supported colourspace")
  351. }
  352. }
  353. func TestFluentInterface(t *testing.T) {
  354. image := initImage("test.jpg")
  355. _, err := image.CropByWidth(300)
  356. if err != nil {
  357. t.Errorf("Cannot process the image: %#v", err)
  358. }
  359. _, err = image.Flip()
  360. if err != nil {
  361. t.Errorf("Cannot process the image: %#v", err)
  362. }
  363. _, err = image.Convert(PNG)
  364. if err != nil {
  365. t.Errorf("Cannot process the image: %#v", err)
  366. }
  367. data, _ := image.Metadata()
  368. if data.Alpha != false {
  369. t.Fatal("Invalid alpha channel")
  370. }
  371. if data.Size.Width != 300 {
  372. t.Fatal("Invalid width size")
  373. }
  374. if data.Type != "png" {
  375. t.Fatal("Invalid image type")
  376. }
  377. Write("fixtures/test_image_fluent_out.png", image.Image())
  378. }
  379. func TestImageSmartCrop(t *testing.T) {
  380. if !(VipsMajorVersion >= 8 && VipsMinorVersion >= 5) {
  381. t.Skipf("Skipping this test, libvips doesn't meet version requirement %s >= 8.5", VipsVersion)
  382. }
  383. i := initImage("northern_cardinal_bird.jpg")
  384. buf, err := i.SmartCrop(300, 300)
  385. if err != nil {
  386. t.Errorf("Cannot process the image: %#v", err)
  387. }
  388. err = assertSize(buf, 300, 300)
  389. if err != nil {
  390. t.Error(err)
  391. }
  392. Write("fixtures/test_smart_crop.jpg", buf)
  393. }
  394. func TestImageTrim(t *testing.T) {
  395. if !(VipsMajorVersion >= 8 && VipsMinorVersion >= 6) {
  396. t.Skipf("Skipping this test, libvips doesn't meet version requirement %s >= 8.6", VipsVersion)
  397. }
  398. i := initImage("transparent.png")
  399. buf, err := i.Trim()
  400. if err != nil {
  401. t.Errorf("Cannot process the image: %#v", err)
  402. }
  403. err = assertSize(buf, 250, 208)
  404. if err != nil {
  405. t.Errorf("The image wasn't trimmed.")
  406. }
  407. Write("fixtures/transparent_trim.png", buf)
  408. }
  409. func TestImageLength(t *testing.T) {
  410. i := initImage("test.jpg")
  411. actual := i.Length()
  412. expected := 53653
  413. if expected != actual {
  414. t.Errorf("Size in Bytes of the image doesn't correspond. %d != %d", expected, actual)
  415. }
  416. }
  417. func initImage(file string) *Image {
  418. buf, _ := imageBuf(file)
  419. return NewImage(buf)
  420. }
  421. func imageBuf(file string) ([]byte, error) {
  422. return Read(path.Join("fixtures", file))
  423. }
  424. func assertSize(buf []byte, width, height int) error {
  425. size, err := NewImage(buf).Size()
  426. if err != nil {
  427. return err
  428. }
  429. if size.Width != width || size.Height != height {
  430. return fmt.Errorf("Invalid image size: %dx%d", size.Width, size.Height)
  431. }
  432. return nil
  433. }