forked from nfnt/resize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize_test.go
177 lines (143 loc) · 3.33 KB
/
resize_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package resize
import (
"image"
"image/color"
"runtime"
"testing"
)
var img = image.NewGray16(image.Rect(0, 0, 3, 3))
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
img.Set(1, 1, color.White)
}
func Test_Param1(t *testing.T) {
m, _ := Resize(0, 0, img, NearestNeighbor)
if m.Bounds() != img.Bounds() {
t.Fail()
}
}
func Test_Param2(t *testing.T) {
m, err := Resize(100, 0, img, NearestNeighbor)
if err != nil {
t.Fail()
}
if m.Bounds() != image.Rect(0, 0, 100, 100) {
t.Fail()
}
}
func Test_ZeroImg(t *testing.T) {
zeroImg := image.NewGray16(image.Rect(0, 0, 0, 0))
m, err := Resize(0, 0, zeroImg, NearestNeighbor)
if err != nil {
t.Fail()
}
if m.Bounds() != zeroImg.Bounds() {
t.Fail()
}
}
func Test_CorrectResize(t *testing.T) {
zeroImg := image.NewGray16(image.Rect(0, 0, 256, 256))
m, err := Resize(60, 0, zeroImg, NearestNeighbor)
if err != nil {
t.Fail()
}
if m.Bounds() != image.Rect(0, 0, 60, 60) {
t.Fail()
}
}
func Test_SameColor(t *testing.T) {
img := image.NewRGBA(image.Rect(0, 0, 20, 20))
for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
img.SetRGBA(x, y, color.RGBA{0x80, 0x80, 0x80, 0xFF})
}
}
out, err := Resize(10, 10, img, Lanczos3)
if err != nil {
t.Fail()
}
for y := out.Bounds().Min.Y; y < out.Bounds().Max.Y; y++ {
for x := out.Bounds().Min.X; x < out.Bounds().Max.X; x++ {
color := img.At(x, y).(color.RGBA)
if color.R != 0x80 || color.G != 0x80 || color.B != 0x80 || color.A != 0xFF {
t.Fail()
}
}
}
}
func Test_Bounds(t *testing.T) {
img := image.NewRGBA(image.Rect(20, 10, 200, 99))
out, err := Resize(80, 80, img, Lanczos2)
if err != nil {
t.Fail()
}
out.At(0, 0)
}
func Test_SameSizeReturnsOriginal(t *testing.T) {
img := image.NewRGBA(image.Rect(0, 0, 10, 10))
out, err := Resize(0, 0, img, Lanczos2)
if err != nil {
t.Fail()
}
if img != out {
t.Fail()
}
out, _ = Resize(10, 10, img, Lanczos2)
if img != out {
t.Fail()
}
}
func Benchmark_BigResizeLanczos3(b *testing.B) {
var m image.Image
var err error
for i := 0; i < b.N; i++ {
m, err = Resize(1000, 1000, img, Lanczos3)
if err != nil {
b.FailNow()
}
}
m.At(0, 0)
}
func Benchmark_Reduction(b *testing.B) {
largeImg := image.NewRGBA(image.Rect(0, 0, 1000, 1000))
var m image.Image
var err error
for i := 0; i < b.N; i++ {
m, err = Resize(300, 300, largeImg, Bicubic)
if err != nil {
b.FailNow()
}
}
m.At(0, 0)
}
// Benchmark resize of 16 MPix jpeg image to 800px width.
func jpegThumb(b *testing.B, interp InterpolationFunction) {
input := image.NewYCbCr(image.Rect(0, 0, 4896, 3264), image.YCbCrSubsampleRatio422)
var output image.Image
var err error
for i := 0; i < b.N; i++ {
output, err = Resize(800, 0, input, interp)
if err != nil {
b.FailNow()
}
}
output.At(0, 0)
}
func Benchmark_LargeJpegThumbNearestNeighbor(b *testing.B) {
jpegThumb(b, NearestNeighbor)
}
func Benchmark_LargeJpegThumbBilinear(b *testing.B) {
jpegThumb(b, Bilinear)
}
func Benchmark_LargeJpegThumbBicubic(b *testing.B) {
jpegThumb(b, Bicubic)
}
func Benchmark_LargeJpegThumbMitchellNetravali(b *testing.B) {
jpegThumb(b, MitchellNetravali)
}
func Benchmark_LargeJpegThumbLanczos2(b *testing.B) {
jpegThumb(b, Lanczos2)
}
func Benchmark_LargeJpegThumbLanczos3(b *testing.B) {
jpegThumb(b, Lanczos3)
}