vips_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package bimg
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path"
  6. "testing"
  7. )
  8. func TestVipsRead(t *testing.T) {
  9. files := []struct {
  10. name string
  11. expected ImageType
  12. }{
  13. {"test.jpg", JPEG},
  14. {"test.png", PNG},
  15. {"test.webp", WEBP},
  16. }
  17. for _, file := range files {
  18. image, imageType, _ := vipsRead(readImage(file.name))
  19. if image == nil {
  20. t.Fatal("Empty image")
  21. }
  22. if imageType != file.expected {
  23. t.Fatal("Invalid image type")
  24. }
  25. }
  26. }
  27. func TestVipsSave(t *testing.T) {
  28. types := [...]ImageType{JPEG, PNG, WEBP}
  29. for _, typ := range types {
  30. image, _, _ := vipsRead(readImage("test.jpg"))
  31. options := vipsSaveOptions{Quality: 95, Type: typ, StripMetadata: true}
  32. buf, err := vipsSave(image, options)
  33. if err != nil {
  34. t.Fatalf("Cannot save the image as '%v'", ImageTypes[typ])
  35. }
  36. if len(buf) == 0 {
  37. t.Fatalf("Empty saved '%v' image", ImageTypes[typ])
  38. }
  39. }
  40. }
  41. func TestVipsSaveTiff(t *testing.T) {
  42. if !IsTypeSupportedSave(TIFF) {
  43. t.Skipf("Format %#v is not supported", ImageTypes[TIFF])
  44. }
  45. image, _, _ := vipsRead(readImage("test.jpg"))
  46. options := vipsSaveOptions{Quality: 95, Type: TIFF}
  47. buf, _ := vipsSave(image, options)
  48. if len(buf) == 0 {
  49. t.Fatalf("Empty saved '%v' image", ImageTypes[TIFF])
  50. }
  51. }
  52. func TestVipsRotate(t *testing.T) {
  53. files := []struct {
  54. name string
  55. rotate Angle
  56. }{
  57. {"test.jpg", D90},
  58. {"test_square.jpg", D45},
  59. }
  60. for _, file := range files {
  61. image, _, _ := vipsRead(readImage(file.name))
  62. newImg, err := vipsRotate(image, file.rotate)
  63. if err != nil {
  64. t.Fatal("Cannot rotate the image")
  65. }
  66. buf, _ := vipsSave(newImg, vipsSaveOptions{Quality: 95})
  67. if len(buf) == 0 {
  68. t.Fatal("Empty image")
  69. }
  70. }
  71. }
  72. func TestVipsZoom(t *testing.T) {
  73. image, _, _ := vipsRead(readImage("test.jpg"))
  74. newImg, err := vipsZoom(image, 1)
  75. if err != nil {
  76. t.Fatal("Cannot save the image")
  77. }
  78. buf, _ := vipsSave(newImg, vipsSaveOptions{Quality: 95})
  79. if len(buf) == 0 {
  80. t.Fatal("Empty image")
  81. }
  82. }
  83. func TestVipsWatermark(t *testing.T) {
  84. image, _, _ := vipsRead(readImage("test.jpg"))
  85. watermark := Watermark{
  86. Text: "Copy me if you can",
  87. Font: "sans bold 12",
  88. Opacity: 0.5,
  89. Width: 200,
  90. DPI: 100,
  91. Margin: 100,
  92. Background: Color{255, 255, 255},
  93. }
  94. newImg, err := vipsWatermark(image, watermark)
  95. if err != nil {
  96. t.Errorf("Cannot add watermark: %s", err)
  97. }
  98. buf, _ := vipsSave(newImg, vipsSaveOptions{Quality: 95})
  99. if len(buf) == 0 {
  100. t.Fatal("Empty image")
  101. }
  102. }
  103. func TestVipsWatermarkWithImage(t *testing.T) {
  104. image, _, _ := vipsRead(readImage("test.jpg"))
  105. watermark := readImage("transparent.png")
  106. options := WatermarkImage{Left: 100, Top: 100, Opacity: 1.0, Buf: watermark}
  107. newImg, err := vipsDrawWatermark(image, options)
  108. if err != nil {
  109. t.Errorf("Cannot add watermark: %s", err)
  110. }
  111. buf, _ := vipsSave(newImg, vipsSaveOptions{Quality: 95})
  112. if len(buf) == 0 {
  113. t.Fatal("Empty image")
  114. }
  115. }
  116. func TestVipsImageType(t *testing.T) {
  117. imgType := vipsImageType(readImage("test.jpg"))
  118. if imgType != JPEG {
  119. t.Fatal("Invalid image type")
  120. }
  121. }
  122. func TestVipsImageTypeInvalid(t *testing.T) {
  123. imgType := vipsImageType([]byte("vip"))
  124. if imgType != UNKNOWN {
  125. t.Fatal("Invalid image type")
  126. }
  127. }
  128. func TestVipsMemory(t *testing.T) {
  129. mem := VipsMemory()
  130. if mem.Memory < 1024 {
  131. t.Fatal("Invalid memory")
  132. }
  133. if mem.Allocations == 0 {
  134. t.Fatal("Invalid memory allocations")
  135. }
  136. }
  137. func readImage(file string) []byte {
  138. img, _ := os.Open(path.Join("fixtures", file))
  139. buf, _ := ioutil.ReadAll(img)
  140. defer img.Close()
  141. return buf
  142. }