forked from discord/lilliput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webp_test.go
476 lines (431 loc) · 13.3 KB
/
webp_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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package lilliput
import (
"os"
"reflect"
"testing"
"time"
)
const (
destinationBufferSize = 10 * 1024 * 1024
)
func TestWebPOperations(t *testing.T) {
t.Run("NewWebpDecoder", testNewWebpDecoder)
t.Run("WebpDecoder_Header", testWebpDecoderHeader)
t.Run("NewWebpEncoder", testNewWebpEncoder)
t.Run("WebpDecoder_DecodeTo", testWebpDecoderDecodeTo)
t.Run("WebpEncoder_Encode", testWebpEncoderEncode)
t.Run("NewWebpEncoderWithAnimatedWebPSource", testNewWebpEncoderWithAnimatedWebPSource)
t.Run("NewWebpEncoderWithAnimatedGIFSource", testNewWebpEncoderWithAnimatedGIFSource)
}
func testNewWebpDecoder(t *testing.T) {
testWebPImage, err := os.ReadFile("testdata/tears_of_steel_no_icc.webp")
if err != nil {
t.Fatalf("Unexpected error while reading webp image: %v", err)
}
decoder, err := newWebpDecoder(testWebPImage)
if err != nil {
t.Fatalf("Unexpected error while decoding webp image data: %v", err)
}
defer decoder.Close()
}
func testWebpDecoderHeader(t *testing.T) {
testWebPImage, err := os.ReadFile("testdata/tears_of_steel_no_icc.webp")
if err != nil {
t.Fatalf("Unexpected error while reading webp image: %v", err)
}
decoder, err := newWebpDecoder(testWebPImage)
if err != nil {
t.Fatalf("Unexpected error while decoding webp image data: %v", err)
}
defer decoder.Close()
header, err := decoder.Header()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if reflect.TypeOf(header).String() != "*lilliput.ImageHeader" {
t.Fatalf("Expected type *lilliput.ImageHeader, got %v", reflect.TypeOf(header))
}
}
func testNewWebpEncoder(t *testing.T) {
testCases := []struct {
name string
filename string
}{
{"No ICC Profile", "testdata/tears_of_steel_no_icc.webp"},
{"With ICC Profile", "testdata/tears_of_steel_icc.webp"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
testWebPImage, err := os.ReadFile(tc.filename)
if err != nil {
t.Fatalf("Unexpected error while reading webp image: %v", err)
}
decoder, err := newWebpDecoder(testWebPImage)
if err != nil {
t.Fatalf("Unexpected error while decoding webp image data: %v", err)
}
defer decoder.Close()
dstBuf := make([]byte, destinationBufferSize)
encoder, err := newWebpEncoder(decoder, dstBuf)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer encoder.Close()
})
}
}
func testWebpDecoderDecodeTo(t *testing.T) {
testCases := []struct {
name string
filename string
}{
{"No ICC Profile", "testdata/tears_of_steel_no_icc.webp"},
{"With ICC Profile", "testdata/tears_of_steel_icc.webp"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
testWebPImage, err := os.ReadFile(tc.filename)
if err != nil {
t.Fatalf("Failed to read webp image: %v", err)
}
decoder, err := newWebpDecoder(testWebPImage)
if err != nil {
t.Fatalf("Failed to create a new webp decoder: %v", err)
}
defer decoder.Close()
header, err := decoder.Header()
if err != nil {
t.Fatalf("Failed to get the header: %v", err)
}
framebuffer := NewFramebuffer(header.width, header.height)
if err = decoder.DecodeTo(framebuffer); err != nil {
t.Errorf("DecodeTo failed unexpectedly: %v", err)
}
})
}
// Test invalid framebuffer
t.Run("Invalid Framebuffer", func(t *testing.T) {
testWebPImage, _ := os.ReadFile("testdata/tears_of_steel_no_icc.webp")
decoder, _ := newWebpDecoder(testWebPImage)
defer decoder.Close()
if err := decoder.DecodeTo(nil); err == nil {
t.Error("DecodeTo with nil framebuffer should fail, but it did not")
}
})
}
func testWebpEncoderEncode(t *testing.T) {
testCases := []struct {
name string
filename string
quality int
}{
{"No ICC Profile", "testdata/tears_of_steel_no_icc.webp", 60},
{"With ICC Profile", "testdata/tears_of_steel_icc.webp", 80},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
testWebPImage, err := os.ReadFile(tc.filename)
if err != nil {
t.Fatalf("Failed to read webp image: %v", err)
}
decoder, err := newWebpDecoder(testWebPImage)
if err != nil {
t.Fatalf("Failed to create a new webp decoder: %v", err)
}
defer decoder.Close()
header, err := decoder.Header()
if err != nil {
t.Fatalf("Failed to get the header: %v", err)
}
framebuffer := NewFramebuffer(header.width, header.height)
if err = framebuffer.resizeMat(header.width, header.height, header.pixelType); err != nil {
t.Fatalf("Failed to resize the framebuffer: %v", err)
}
dstBuf := make([]byte, destinationBufferSize)
encoder, err := newWebpEncoder(decoder, dstBuf)
if err != nil {
t.Fatalf("Failed to create a new webp encoder: %v", err)
}
defer encoder.Close()
options := map[int]int{WebpQuality: tc.quality}
encodedData, err := encoder.Encode(framebuffer, options)
if err != nil {
t.Fatalf("Encode failed unexpectedly: %v", err)
}
if encodedData, err = encoder.Encode(nil, options); err != nil {
t.Fatalf("Encode of empty frame failed unexpectedly: %v", err)
}
if len(encodedData) == 0 {
t.Fatalf("Encoded data is empty, but it should not be")
}
})
}
// Test nil framebuffer
t.Run("Nil Framebuffer", func(t *testing.T) {
testWebPImage, err := os.ReadFile("testdata/tears_of_steel_icc.webp")
if err != nil {
t.Fatalf("Failed to read webp image: %v", err)
}
decoder, err := newWebpDecoder(testWebPImage)
if err != nil {
t.Fatalf("Failed to create a new webp decoder: %v", err)
}
defer decoder.Close()
dstBuf := make([]byte, destinationBufferSize)
encoder, err := newWebpEncoder(decoder, dstBuf)
if err != nil {
t.Fatalf("Failed to create a new webp encoder: %v", err)
}
defer encoder.Close()
header, _ := decoder.Header()
framebuffer := NewFramebuffer(header.width, header.height)
options := map[int]int{}
if err = framebuffer.resizeMat(header.width, header.height, header.pixelType); err != nil {
t.Fatalf("Failed to resize the framebuffer: %v", err)
}
if _, err = encoder.Encode(nil, options); err == nil {
t.Error("Encoding a nil framebuffer should fail, but it did not")
}
})
}
func testNewWebpEncoderWithAnimatedWebPSource(t *testing.T) {
testCases := []struct {
name string
inputPath string
outputPath string
width int
height int
quality int
resizeMethod ImageOpsSizeMethod
disableAnimatedOutput bool
}{
{
name: "Animated WebP - Party Discord",
inputPath: "testdata/party-discord.webp",
outputPath: "testdata/out/party-discord_out_webpsource_resize.webp",
width: 26,
height: 17,
quality: 60,
resizeMethod: ImageOpsResize,
},
{
name: "Animated WebP - Resize #1",
inputPath: "testdata/ferry_sunset.webp",
outputPath: "testdata/out/ferry_sunset_out_resize.webp",
width: 266,
height: 99,
quality: 60,
resizeMethod: ImageOpsResize,
},
{
name: "Animated WebP - Resize #2",
inputPath: "testdata/animated-webp-supported.webp",
outputPath: "testdata/out/animated-webp-supported_out_resize.webp",
width: 400,
height: 400,
quality: 60,
resizeMethod: ImageOpsResize,
},
{
name: "Animated WebP - Fit #1",
inputPath: "testdata/animated-webp-supported.webp",
outputPath: "testdata/out/animated-webp-supported_out_fit.webp",
width: 400,
height: 400,
quality: 80,
resizeMethod: ImageOpsFit,
},
{
name: "Animated WebP - No resize",
inputPath: "testdata/animated-webp-supported.webp",
outputPath: "testdata/out/animated-webp-supported_out_no_resize.webp",
width: 0,
height: 0,
quality: 100,
resizeMethod: ImageOpsNoResize,
},
{
name: "Animated WebP - Fit #2",
inputPath: "testdata/animated-webp-supported.webp",
outputPath: "testdata/out/animated-webp-supported_out.webp",
width: 200,
height: 200,
quality: 60,
resizeMethod: ImageOpsFit,
},
{
name: "Animated WebP - create single frame",
inputPath: "testdata/big_buck_bunny_720_5s.webp",
outputPath: "testdata/out/big_buck_bunny_720_5s_single_frame_out.webp",
width: 200,
height: 200,
quality: 60,
resizeMethod: ImageOpsFit,
disableAnimatedOutput: true,
},
{
name: "Animated WebP - Crashing input",
inputPath: "testdata/8202024-BGS-Headless-Horseman-OO-1200x1200-optimize.webp",
outputPath: "testdata/out/8202024-BGS-Headless-Horseman-OO-1200x1200-optimize_out.webp",
width: 200,
height: 200,
quality: 60,
resizeMethod: ImageOpsFit,
},
{
name: "Animated WebP - complex dispose and blend",
inputPath: "testdata/complex_dispose_and_blend.webp",
outputPath: "testdata/out/complex_dispose_and_blend_out.webp",
width: 960,
height: 540,
quality: 60,
resizeMethod: ImageOpsFit,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var err error
var testWebPImage []byte
var decoder *webpDecoder
// Read the input WebP file
testWebPImage, err = os.ReadFile(tc.inputPath)
if err != nil {
t.Errorf("Unexpected error while reading %s: %v", tc.inputPath, err)
return
}
// Decode the WebP image
if decoder, err = newWebpDecoder(testWebPImage); err != nil {
t.Errorf("Unexpected error while decoding %s: %v", tc.inputPath, err)
return
}
dstBuf := make([]byte, destinationBufferSize)
options := &ImageOptions{
FileType: ".webp",
NormalizeOrientation: true,
EncodeOptions: map[int]int{WebpQuality: tc.quality},
ResizeMethod: tc.resizeMethod,
Width: tc.width,
Height: tc.height,
EncodeTimeout: time.Second * 300,
DisableAnimatedOutput: tc.disableAnimatedOutput,
}
ops := NewImageOps(2000)
var newDst []byte
newDst, err = ops.Transform(decoder, options, dstBuf)
if err != nil {
decoder.Close()
ops.Close()
t.Errorf("Transform() error for %s: %v", tc.inputPath, err)
return
}
// verify length of newDst
if len(newDst) == 0 {
decoder.Close()
ops.Close()
t.Errorf("Transform() returned empty data for %s", tc.inputPath)
}
// write the new WebP image to disk
if tc.outputPath != "" {
// create output directory if it does not exist
if _, err := os.Stat("testdata/out"); os.IsNotExist(err) {
if err = os.Mkdir("testdata/out", 0755); err != nil {
decoder.Close()
ops.Close()
t.Errorf("Failed to create output directory: %v", err)
return
}
}
if err = os.WriteFile(tc.outputPath, newDst, 0644); err != nil {
decoder.Close()
ops.Close()
t.Errorf("Failed to write %s: %v", tc.outputPath, err)
}
}
decoder.Close()
ops.Close()
})
}
}
func testNewWebpEncoderWithAnimatedGIFSource(t *testing.T) {
testCases := []struct {
name string
inputPath string
outputPath string
width int
height int
quality int
resizeMethod ImageOpsSizeMethod
}{
{
name: "Animated GIF with alpha channel",
inputPath: "testdata/party-discord.gif",
outputPath: "testdata/out/party-discord_out_resize.webp",
width: 27,
height: 17,
quality: 80,
resizeMethod: ImageOpsResize,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var err error
var testWebPImage []byte
var decoder *gifDecoder
// Read the input GIF file
testWebPImage, err = os.ReadFile(tc.inputPath)
if err != nil {
t.Errorf("Unexpected error while reading %s: %v", tc.inputPath, err)
return
}
// Decode the GIF image
if decoder, err = newGifDecoder(testWebPImage); err != nil {
t.Errorf("Unexpected error while decoding %s: %v", tc.inputPath, err)
return
}
dstBuf := make([]byte, destinationBufferSize)
options := &ImageOptions{
FileType: ".webp",
NormalizeOrientation: true,
EncodeOptions: map[int]int{WebpQuality: tc.quality},
ResizeMethod: tc.resizeMethod,
Width: tc.width,
Height: tc.height,
EncodeTimeout: time.Second * 300,
}
ops := NewImageOps(2000)
var newDst []byte
newDst, err = ops.Transform(decoder, options, dstBuf)
if err != nil {
decoder.Close()
ops.Close()
t.Errorf("Transform() error for %s: %v", tc.inputPath, err)
return
}
// verify length of newDst
if len(newDst) == 0 {
decoder.Close()
ops.Close()
t.Errorf("Transform() returned empty data for %s", tc.inputPath)
}
// write the new WebP image to disk
if tc.outputPath != "" {
// create output directory if it does not exist
if _, err := os.Stat("testdata/out"); os.IsNotExist(err) {
if err = os.Mkdir("testdata/out", 0755); err != nil {
decoder.Close()
ops.Close()
t.Errorf("Failed to create output directory: %v", err)
return
}
}
if err = os.WriteFile(tc.outputPath, newDst, 0644); err != nil {
decoder.Close()
ops.Close()
t.Errorf("Failed to write %s: %v", tc.outputPath, err)
}
}
decoder.Close()
ops.Close()
})
}
}