/
thumbnail_test.go
417 lines (336 loc) · 11.5 KB
/
thumbnail_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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package thumbnail
import (
"fmt"
"github.com/die-net/fotomat/format"
"github.com/die-net/fotomat/vips"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"strconv"
"testing"
)
func TestMain(m *testing.M) {
vips.Initialize()
vips.LeakSet(true)
r := m.Run()
vips.ThreadShutdown()
vips.Shutdown()
os.Exit(r)
}
func TestValidation(t *testing.T) {
// Return ErrUnknownFormat on a text file.
assert.Equal(t, tryNew("notimage.txt"), format.ErrUnknownFormat)
// Return ErrUnknownFormat on a truncated image.
assert.Equal(t, tryNew("bad.jpg"), format.ErrUnknownFormat)
// Refuse to load a 1x1 pixel image.
assert.Equal(t, tryNew("1px.png"), ErrTooSmall)
// Load a 2x2 pixel image.
assert.Nil(t, tryNew("2px.png"))
// Return ErrTooBig on a 34000x16 PNG image.
assert.Equal(t, tryNew("34000px.png"), ErrTooBig)
// Refuse to load a 213328 pixel JPEG image into 1000 pixel buffer.
// TODO: Add back MaxBufferPixels.
_, err := Thumbnail(image("watermelon.jpg"), Options{Width: 200, Height: 300, MaxBufferPixels: 1000})
assert.Equal(t, err, ErrTooBig)
// Succeed in loading a 213328 pixel JPEG image into 10000 pixel buffer.
_, err = Thumbnail(image("watermelon.jpg"), Options{Width: 200, Height: 300, MaxBufferPixels: 10000})
assert.Nil(t, err)
}
func tryNew(filename string) error {
_, err := Thumbnail(image(filename), Options{Width: 200, Height: 200})
return err
}
func TestThumbnail(t *testing.T) {
img := image("watermelon.jpg")
m, err := format.MetadataBytes(img)
if !assert.Nil(t, err) {
return
}
assert.Equal(t, m.Width, 398)
assert.Equal(t, m.Height, 536)
// Verify scaling down to fit completely into box.
thumb, err := Thumbnail(img, Options{Width: 200, Height: 300})
if assert.Nil(t, err) {
assert.Nil(t, isSize(thumb, format.Jpeg, 200, 270, false))
}
// Verify scaling down to have width fit.
thumb, err = Thumbnail(img, Options{Width: 200})
if assert.Nil(t, err) {
assert.Nil(t, isSize(thumb, format.Jpeg, 200, 270, false))
}
// Verify scaling down to have height fit.
thumb, err = Thumbnail(img, Options{Height: 300})
if assert.Nil(t, err) {
assert.Nil(t, isSize(thumb, format.Jpeg, 223, 300, false))
}
// Verify that we don't scale up.
thumb, err = Thumbnail(img, Options{Width: 2048, Height: 2048})
if assert.Nil(t, err) {
assert.Nil(t, isSize(thumb, format.Jpeg, 398, 536, false))
}
}
func TestCrop(t *testing.T) {
img := image("watermelon.jpg")
m, err := format.MetadataBytes(img)
assert.Nil(t, err)
assert.Equal(t, m.Width, 398)
assert.Equal(t, m.Height, 536)
// Verify cropping to fit.
thumb, err := Thumbnail(img, Options{Width: 300, Height: 400, Crop: true})
if assert.Nil(t, err) {
assert.Nil(t, isSize(thumb, format.Jpeg, 300, 400, false))
}
// Verify cropping to fit, too big.
thumb, err = Thumbnail(img, Options{Width: 2000, Height: 1500, Crop: true})
if assert.Nil(t, err) {
assert.Nil(t, isSize(thumb, format.Jpeg, 398, 299, false))
}
}
func TestBlurSharpen(t *testing.T) {
img := image("watermelon.jpg")
thumb, err := Thumbnail(img, Options{Width: 300, Height: 400})
assert.Nil(t, err)
l := len(thumb)
thumb, err = Thumbnail(img, Options{Width: 300, Height: 400, BlurSigma: 0.8})
if assert.Nil(t, err) {
assert.True(t, len(thumb) < l) // Blurry photos will be smaller
}
thumb, err = Thumbnail(img, Options{Width: 300, Height: 400, Sharpen: true})
assert.Nil(t, err)
if assert.Nil(t, err) {
assert.True(t, len(thumb) > l) // Sharpened photos will be larger
}
}
func TestAlpha(t *testing.T) {
img := image("noalpha.png")
assert.Nil(t, isSize(img, format.Png, 100, 50, true))
// Test that we remove the alpha channel from an image that's not using it.
thumb, err := Thumbnail(img, Options{Width: 100, Height: 100, Save: format.SaveOptions{Format: format.Png}})
if assert.Nil(t, err) {
assert.Nil(t, isSize(thumb, format.Png, 100, 50, false))
}
img = image("somealpha.png")
assert.Nil(t, isSize(img, format.Png, 100, 50, true))
// Test that we leave the alpha channel for an image that is using it.
thumb, err = Thumbnail(img, Options{Width: 100, Height: 100, Save: format.SaveOptions{Format: format.Png}})
if assert.Nil(t, err) {
assert.Nil(t, isSize(thumb, format.Png, 100, 50, true))
}
}
func TestRotation(t *testing.T) {
for i := 1; i <= 8; i++ {
// Verify that New() correctly translates dimensions.
img := image("orient" + strconv.Itoa(i) + ".jpg")
m, err := format.MetadataBytes(img)
if !assert.Nil(t, err) {
continue
}
assert.Equal(t, m.Width, 48)
assert.Equal(t, m.Height, 80)
// Verify that img.Thumbnail() maintains orientation.
thumb, err := Thumbnail(img, Options{Width: 40, Height: 40})
if assert.Nil(t, err) {
assert.Nil(t, isSize(thumb, format.Jpeg, 24, 40, false))
}
// TODO: Figure out how to test crop.
}
}
func TestConversion(t *testing.T) {
var formatTest = []struct {
filename string
in format.Format
outLossless format.Format
}{
{"2px.gif", format.Gif, format.Png},
{"2px.png", format.Png, format.Png},
{"2px.jpg", format.Jpeg, format.Jpeg},
{"2px.webp", format.Webp, format.Png},
}
for _, f := range formatTest {
img := image(f.filename)
m, err := format.MetadataBytes(img)
if assert.Nil(t, err, "format: %s", f.in) {
assert.Equal(t, m.Format, f.in, "format: %s", f.in)
assert.Equal(t, m.Width, 2, "format: %s", f.in)
assert.Equal(t, m.Height, 3, "format: %s", f.in)
for _, of := range []format.Format{format.Png, format.Jpeg, format.Webp} {
// If we ask for a specific format, it should return that.
thumb, err := Thumbnail(img, Options{Width: 1024, Height: 1024, Save: format.SaveOptions{Format: of}})
if assert.Nil(t, err, "formats: %s -> %s", f.in, of) {
assert.Nil(t, isSize(thumb, of, 2, 3, false), "formats: %s -> %s", f.in, of)
}
}
}
}
}
func TestScalingJpeg(t *testing.T) {
testScalingFormat(t, format.Jpeg)
}
func TestScalingPng(t *testing.T) {
testScalingFormat(t, format.Png)
}
func TestScalingWebp(t *testing.T) {
testScalingFormat(t, format.Webp)
}
func testScalingFormat(t *testing.T, f format.Format) {
blob, err := flowersFormat(f)
if !assert.Nil(t, err) {
return
}
// Try scaling to some difficult sizes and make sure we get the expected size back.
// We have different code paths for different image formats, so we try for each.
for _, size := range []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, 129, 255, 256} {
thumb, err := Thumbnail(blob, Options{Width: size, Height: size, FastResize: true, Save: format.SaveOptions{Format: f}})
if assert.Nil(t, err) {
h := (169*size + 255) / 256
if h < 1 {
h = 1
}
assert.Nil(t, isSize(thumb, f, size, h, false))
}
}
}
func TestBadOptions(t *testing.T) {
for _, f := range []format.Format{format.Png, format.Jpeg, format.Webp} {
blob, err := flowersFormat(f)
if !assert.Nil(t, err, "format: %s", f) {
continue
}
// Try feeding some bad options and make sure we get nothing back.
for _, of := range []format.Format{format.Png, format.Jpeg, format.Webp} {
thumb, err := Thumbnail(blob, Options{Width: -1, Height: -1, Save: format.SaveOptions{Format: of}})
assert.Error(t, err, "format: %s -> %s, size: %d", f, of)
assert.False(t, thumb != nil, "format: %s -> %s, size: %d", f, of)
}
}
}
func TestOptionsJson(t *testing.T) {
o := Options{Width: 400, Height: 300, BlurSigma: 0.5, Save: format.SaveOptions{Format: format.Webp, Lossless: true}}
j, err := o.ToJSON()
assert.Nil(t, err)
v, err := OptionsFromJSON(j)
if assert.Nil(t, err) {
assert.Equal(t, o, v)
}
_, err = OptionsFromJSON([]byte(""))
assert.Error(t, err)
}
func BenchmarkThumbnailJpeg_16(b *testing.B) {
benchThumbnail(b, format.Jpeg, Options{Width: 16, Height: 16})
}
func BenchmarkThumbnailJpeg_32(b *testing.B) {
benchThumbnail(b, format.Jpeg, Options{Width: 32, Height: 32})
}
func BenchmarkThumbnailJpeg_64(b *testing.B) {
benchThumbnail(b, format.Jpeg, Options{Width: 64, Height: 64})
}
func BenchmarkThumbnailJpeg_128(b *testing.B) {
benchThumbnail(b, format.Jpeg, Options{Width: 128, Height: 128})
}
func BenchmarkThumbnailJpeg_256(b *testing.B) {
benchThumbnail(b, format.Jpeg, Options{Width: 256, Height: 256})
}
func BenchmarkThumbnailJpeg_24(b *testing.B) {
benchThumbnail(b, format.Jpeg, Options{Width: 24, Height: 24})
}
func BenchmarkThumbnailJpeg_48(b *testing.B) {
benchThumbnail(b, format.Jpeg, Options{Width: 48, Height: 48})
}
func BenchmarkThumbnailJpeg_96(b *testing.B) {
benchThumbnail(b, format.Jpeg, Options{Width: 96, Height: 96})
}
func BenchmarkThumbnailJpeg_192(b *testing.B) {
benchThumbnail(b, format.Jpeg, Options{Width: 192, Height: 192})
}
func BenchmarkThumbnailPng_16(b *testing.B) {
benchThumbnail(b, format.Png, Options{Width: 16, Height: 16})
}
func BenchmarkThumbnailPng_32(b *testing.B) {
benchThumbnail(b, format.Png, Options{Width: 32, Height: 32})
}
func BenchmarkThumbnailPng_64(b *testing.B) {
benchThumbnail(b, format.Png, Options{Width: 64, Height: 64})
}
func BenchmarkThumbnailPng_128(b *testing.B) {
benchThumbnail(b, format.Png, Options{Width: 128, Height: 128})
}
func BenchmarkThumbnailPng_256(b *testing.B) {
benchThumbnail(b, format.Png, Options{Width: 256, Height: 256})
}
func BenchmarkThumbnailPng_24(b *testing.B) {
benchThumbnail(b, format.Png, Options{Width: 24, Height: 24})
}
func BenchmarkThumbnailPng_48(b *testing.B) {
benchThumbnail(b, format.Png, Options{Width: 48, Height: 48})
}
func BenchmarkThumbnailPng_96(b *testing.B) {
benchThumbnail(b, format.Png, Options{Width: 96, Height: 96})
}
func BenchmarkThumbnailPng_192(b *testing.B) {
benchThumbnail(b, format.Png, Options{Width: 192, Height: 192})
}
func BenchmarkThumbnailWebp_16(b *testing.B) {
benchThumbnail(b, format.Webp, Options{Width: 16, Height: 16})
}
func BenchmarkThumbnailWebp_32(b *testing.B) {
benchThumbnail(b, format.Webp, Options{Width: 32, Height: 32})
}
func BenchmarkThumbnailWebp_64(b *testing.B) {
benchThumbnail(b, format.Webp, Options{Width: 64, Height: 64})
}
func BenchmarkThumbnailWebp_128(b *testing.B) {
benchThumbnail(b, format.Webp, Options{Width: 128, Height: 128})
}
func BenchmarkThumbnailWebp_256(b *testing.B) {
benchThumbnail(b, format.Webp, Options{Width: 256, Height: 256})
}
func BenchmarkThumbnailWebp_24(b *testing.B) {
benchThumbnail(b, format.Webp, Options{Width: 24, Height: 24})
}
func BenchmarkThumbnailWebp_48(b *testing.B) {
benchThumbnail(b, format.Webp, Options{Width: 48, Height: 48})
}
func BenchmarkThumbnailWebp_96(b *testing.B) {
benchThumbnail(b, format.Webp, Options{Width: 96, Height: 96})
}
func BenchmarkThumbnailWebp_192(b *testing.B) {
benchThumbnail(b, format.Webp, Options{Width: 192, Height: 192})
}
func benchThumbnail(b *testing.B, f format.Format, o Options) {
o.Save.Format = f
blob, err := flowersFormat(f)
assert.Nil(b, err)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := Thumbnail(blob, o)
assert.Nil(b, err)
}
})
}
func flowersFormat(f format.Format) ([]byte, error) {
return Thumbnail(image("flowers.png"), Options{Save: format.SaveOptions{Format: f}})
}
func isSize(image []byte, f format.Format, width, height int, alpha bool) error {
m, err := format.MetadataBytes(image)
if err != nil {
return err
}
if m.Width != width || m.Height != height {
return fmt.Errorf("Got %dx%d != want %dx%d", m.Width, m.Height, width, height)
}
if m.Format != f {
return fmt.Errorf("Format %s!=%s", m.Format, f)
}
if m.HasAlpha != alpha {
return fmt.Errorf("HasAlpha %t!=%t", m.HasAlpha, alpha)
}
return nil
}
func image(filename string) []byte {
bytes, err := ioutil.ReadFile("../testdata/" + filename)
if err != nil {
panic(err)
}
return bytes
}