forked from shifr/vips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvips_test.go
122 lines (111 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
package vips
import (
"bytes"
"image"
"image/jpeg"
"io/ioutil"
"os"
"testing"
)
func BenchmarkParallel(b *testing.B) {
options := Options{Width: 800, Height: 600, Crop: true}
f, err := os.Open("testdata/1.jpg")
if err != nil {
b.Fatal(err)
}
buf, err := ioutil.ReadAll(f)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := Resize(buf, options)
if err != nil {
b.Fatal(err)
}
}
})
b.StopTimer()
}
func BenchmarkSerialized(b *testing.B) {
options := Options{Width: 800, Height: 600, Crop: true}
f, err := os.Open("testdata/1.jpg")
if err != nil {
b.Fatal(err)
}
buf, err := ioutil.ReadAll(f)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Resize(buf, options)
if err != nil {
b.Fatal(err)
}
}
b.StopTimer()
}
func TestResize(t *testing.T) {
var testCases = []struct {
origWidth int
origHeight int
maxWidth int
maxHeight int
expectedWidth uint
expectedHeight uint
}{
{5, 5, 10, 10, 5, 5},
{10, 10, 5, 5, 5, 5},
{10, 50, 10, 10, 2, 10},
{50, 10, 10, 10, 10, 2},
{50, 100, 60, 90, 45, 90},
{120, 100, 60, 90, 60, 50},
{200, 250, 200, 150, 120, 150},
}
for index, mt := range testCases {
img := image.NewGray16(image.Rect(0, 0, mt.origWidth, mt.origHeight))
buf := new(bytes.Buffer)
err := jpeg.Encode(buf, img, nil)
if err != nil {
t.Errorf(
"%d. jpeg.Encode(buf, img, nil) error: %#v",
index, err)
}
options := Options{
Width: mt.maxWidth,
Height: mt.maxHeight,
Crop: false,
Enlarge: false,
Extend: EXTEND_WHITE,
Interpolator: NOHALO,
Gravity: CENTRE,
Quality: 90,
}
newImg, err := Resize(buf.Bytes(), options)
if err != nil {
t.Errorf(
"%d. Resize(imgData, %#v) error: %#v",
index, options, err)
}
outImg, err := jpeg.Decode(bytes.NewReader(newImg))
if err != nil {
t.Errorf(
"%d. jpeg.Decode(newImg) error: %#v",
index, err)
}
newWidth := uint(outImg.Bounds().Dx())
newHeight := uint(outImg.Bounds().Dy())
if newWidth != mt.expectedWidth ||
newHeight != mt.expectedHeight {
t.Fatalf("%d. Resize(imgData, %#v) => "+
"width: %v, height: %v, want width: %v, height: %v, "+
"originl size: %vx%v",
index, options,
newWidth, newHeight, mt.expectedWidth, mt.expectedHeight,
mt.origWidth, mt.origHeight,
)
}
}
}