forked from h2non/bimg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvips_test.go
129 lines (109 loc) · 2.42 KB
/
vips_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package bimg
import (
"io/ioutil"
"os"
"path"
"testing"
)
func TestVipsRead(t *testing.T) {
files := []struct {
name string
expected ImageType
}{
{"test.jpg", JPEG},
{"test.png", PNG},
{"test.webp", WEBP},
}
for _, file := range files {
image, imageType, _ := vipsRead(readImage(file.name))
if image == nil {
t.Fatal("Empty image")
}
if imageType != file.expected {
t.Fatal("Invalid image type")
}
}
}
func TestVipsSave(t *testing.T) {
image, _, _ := vipsRead(readImage("test.jpg"))
options := vipsSaveOptions{Quality: 95, Type: JPEG, Interlace: true}
buf, err := vipsSave(image, options)
if err != nil {
t.Fatal("Cannot save the image")
}
if len(buf) == 0 {
t.Fatal("Empty image")
}
}
func TestVipsRotate(t *testing.T) {
files := []struct {
name string
rotate Angle
}{
{"test.jpg", D90},
{"test_square.jpg", D45},
}
for _, file := range files {
image, _, _ := vipsRead(readImage(file.name))
newImg, err := vipsRotate(image, file.rotate)
if err != nil {
t.Fatal("Cannot rotate the image")
}
buf, _ := vipsSave(newImg, vipsSaveOptions{Quality: 95})
if len(buf) == 0 {
t.Fatal("Empty image")
}
}
}
func TestVipsZoom(t *testing.T) {
image, _, _ := vipsRead(readImage("test.jpg"))
newImg, err := vipsZoom(image, 1)
if err != nil {
t.Fatal("Cannot save the image")
}
buf, _ := vipsSave(newImg, vipsSaveOptions{Quality: 95})
if len(buf) == 0 {
t.Fatal("Empty image")
}
}
func TestVipsWatermark(t *testing.T) {
image, _, _ := vipsRead(readImage("test.jpg"))
watermark := Watermark{
Text: "Copy me if you can",
Font: "sans bold 12",
Opacity: 0.5,
Width: 200,
DPI: 100,
Margin: 100,
Background: Color{255, 255, 255},
}
newImg, err := vipsWatermark(image, watermark)
if err != nil {
t.Errorf("Cannot add watermark: %s", err)
}
buf, _ := vipsSave(newImg, vipsSaveOptions{Quality: 95})
if len(buf) == 0 {
t.Fatal("Empty image")
}
}
func TestVipsImageType(t *testing.T) {
imgType := vipsImageType(readImage("test.jpg"))
if imgType != JPEG {
t.Fatal("Invalid image type")
}
}
func TestVipsMemory(t *testing.T) {
mem := VipsMemory()
if mem.Memory < 1024 {
t.Fatal("Invalid memory")
}
if mem.Allocations == 0 {
t.Fatal("Invalid memory allocations")
}
}
func readImage(file string) []byte {
img, _ := os.Open(path.Join("fixtures", file))
buf, _ := ioutil.ReadAll(img)
defer img.Close()
return buf
}