Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: make gif-loop consistent between GIF and WEBP #1362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 18, 2019

Conversation

deftomat
Copy link
Contributor

@deftomat deftomat commented Jul 8, 2019

We discovered that converting GIF into WEBP can unexpectedly change number of iterations.

The problem is that gif-loop=2 in GIF means 3 iterations and just 2 iterations in WEBP.

So, this PR is trying to unify gif-loop for both formats.

Now, the default GIF value is 1 iteration and when gifload found netscape extension block, then it will change it to 0 (infinite loop) or to the real number of iterations. So, for example, when we found 2, we store it as 3 as viewer should iterate 3 times until it stops.

Now, it is possible to load GIF and immediately save it into WEBP without worrying that the number of iterations will reduce by 1.

I think that this change can be a breaking change for GIF but unfortunately, it is also a bug for WEBP.

Maybe, we should use different name, for example: loop

closes #1502

@jcupitt
Copy link
Member

jcupitt commented Jul 13, 2019

Hello @deftomat,

I hadn't realized that GIF loop == 1 actually meant play twice (ie. rewind once).

I think the gif-loop metadata item should keep the GIF meaning (otherwise the name is confusing and it could break old code), but I agree that we need a new loop tag with a single defined meaning that the imagemagick, libwebp and giflib loaders and savers all respect.

So how about:

  • gif-loop is simply the value in a GIF file. We tag this as deprecated, but still set it in the loaders for compatibility.
  • loop has webp meaning, ie. 0 == forever, 1 == play once, 2 == play twice, etc.

For reference, giflib: 0 == forever, 1 == play twice, 2 == play 3 times, etc. IM uses the same convention as webp.

What do you think?

@jcupitt
Copy link
Member

jcupitt commented Jul 13, 2019

Handy for testing:

#!/usr/bin/python3

import sys
import pyvips

im = pyvips.Image.new_from_file(sys.argv[1], n=-1, access="sequential")
im = im.copy()
im.set("gif-loop", int(sys.argv[3]))
im.write_to_file(sys.argv[2])

Then:

$ ./set-loop.py ~/pics/dancing-banana.webp x.gif 1
$

@deftomat
Copy link
Contributor Author

deftomat commented Jul 15, 2019

Hi. Yes, it sounds good. I will update it.

@deftomat
Copy link
Contributor Author

deftomat commented Aug 5, 2019

@jcupitt I'm not exactly sure how we want to deprecate gif-loop. I understand that gifload.c will fill loop and gif-loop properties but what about magicksave.c? Which option should have the priority? User can forgot to update both options. Unfortunately, there is no gif-loop setter so I cannot update both gif-loop and loop when one of them change.

deftomat referenced this pull request in deftomat/sharp Aug 14, 2019
@deftomat
Copy link
Contributor Author

deftomat commented Sep 9, 2019

ping @jcupitt πŸ™‚

@jcupitt
Copy link
Member

jcupitt commented Sep 9, 2019

Hiya, sorry for not responding. I think the savers should all do this:

  1. If "loop" is set, use that, and don't look for "gif-loop". It has the webp meaning, so 0 == forever, 1 == play once, 2 == play twice etc.
  2. Only if "loop" is not set, check "gif-loop". This has the gif meaning, so 0 == forever, 1 == play twice, 2 == play three times etc.

libvips has a deprecation system for header fields, so we can add "gif-loop" to that and it'll vanish from all interfaces, though it'll still work if you use it explicitly.

@deftomat
Copy link
Contributor Author

deftomat commented Dec 17, 2019

@jcupitt
Sorry for keeping this PR open for so long.

I add loop field with the correct (webp) meaning to both webp and gif.

Also, I add .editorconfig as it could help contributors. Config will force supported editors to use tabs instead of spaces and stops them from removing the trailing spaces. I was fighting with my editor as I had a massive amount of changes after each save 😞

@jcupitt jcupitt merged commit 501fc38 into libvips:master Dec 18, 2019
@jcupitt
Copy link
Member

jcupitt commented Dec 18, 2019

OK, let's merge. I can see a couple of tiny things, but I'll zap them in another patch.

Thank you very much for working on this, @deftomat !

jcupitt added a commit that referenced this pull request Dec 18, 2019
@jcupitt
Copy link
Member

jcupitt commented Dec 18, 2019

I added some other file types to .editorconfig, adjusted some formatting, and added "gif-loop" to the list of deprecated (and hidden) metadata fields.

@deftomat
Copy link
Contributor Author

AWESOME, thanks πŸ‘

Is there an automated way how to format files in this project? Because I always forgot to put spaces into IFs 😞

@jcupitt
Copy link
Member

jcupitt commented Dec 19, 2019

I keep meaning to write an indent file, but I'm too lazy :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make gif-loop consistent between GIF and WEBP
2 participants