Skip to content

Commit 3dad989

Browse files
authored
Merge pull request #2145 from kleisauke/force-100ms
vips2webp: force low duration frames to 100ms
2 parents 6666b94 + 62b0939 commit 3dad989

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

libvips/foreign/vips2webp.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,18 @@ write_webp_anim( VipsWebPWrite *write, VipsImage *image, int page_height )
328328

329329
/* There might just be the old gif-delay field. This is centiseconds.
330330
*/
331-
gif_delay = 4;
331+
gif_delay = 10;
332332
if( vips_image_get_typeof( image, "gif-delay" ) &&
333333
vips_image_get_int( image, "gif-delay", &gif_delay ) )
334334
return( -1 );
335335

336+
/* Force frames with a small or no duration to 100ms
337+
* to be consistent with web browsers and other
338+
* transcoding tools.
339+
*/
340+
if( gif_delay <= 1 )
341+
gif_delay = 10;
342+
336343
/* New images have an array of ints instead.
337344
*/
338345
delay = NULL;
@@ -371,7 +378,8 @@ write_webp_anim( VipsWebPWrite *write, VipsImage *image, int page_height )
371378
page_index = top / page_height;
372379
if( delay &&
373380
page_index < delay_length )
374-
timestamp_ms += delay[page_index];
381+
timestamp_ms += delay[page_index] <= 10 ?
382+
100 : delay[page_index];
375383
else
376384
timestamp_ms += gif_delay * 10;
377385
}

test/test-suite/test_foreign.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,10 +682,14 @@ def webp_valid(im):
682682
x1 = pyvips.Image.new_from_file(GIF_ANIM_FILE, n=-1)
683683
w1 = x1.webpsave_buffer(Q=10)
684684

685+
# our test gif has delay 0 for the first frame set in error,
686+
# when converting to WebP this should result in a 100ms delay.
687+
expected_delay = [100 if d <= 10 else d for d in x1.get("delay")]
688+
685689
x2 = pyvips.Image.new_from_buffer(w1, "", n=-1)
686690
assert x1.width == x2.width
687691
assert x1.height == x2.height
688-
assert x1.get("delay") == x2.get("delay")
692+
assert expected_delay == x2.get("delay")
689693
assert x1.get("page-height") == x2.get("page-height")
690694
assert x1.get("gif-loop") == x2.get("gif-loop")
691695

0 commit comments

Comments
 (0)