Skip to content

gif: fix incorrect loop_count calculation#726

Draft
jsm174 wants to merge 1 commit intolibsdl-org:mainfrom
jsm174:main
Draft

gif: fix incorrect loop_count calculation#726
jsm174 wants to merge 1 commit intolibsdl-org:mainfrom
jsm174:main

Conversation

@jsm174
Copy link
Copy Markdown

@jsm174 jsm174 commented Apr 10, 2026

This PR supersedes #725 with new logic.

The GIF89a spec has no concept of looping. When the NETSCAPE extension is present, the stored value is the number of repeats after the first play.

As of now, if the NETSCAPE extension is not present, loop_count will be 0 which means users trying to use this value would think they need to loop indefinitely.

The value for the IMG_PROP_METADATA_LOOP_COUNT_NUMBER property could be:

  • No NETSCAPE extension: loop_count = 1 (play once)
  • NETSCAPE repeat=0: loop_count = 0 (infinite)
  • NETSCAPE repeat=N: loop_count = N+1 (total plays)

This also updates the encoder to skip writing the NETSCAPE extension if loop_count=1, ie:

  • loop_count == 1: skip NETSCAPE extension entirely (play once)
  • loop_count == 0: write NETSCAPE with repeat=0 (infinite)
  • loop_count > 1: write NETSCAPE with repeat=loop_count-1

see here, here, and here

(If this is not acceptable, and we need a new IMG_PROP_METADATA property, I can modify this commit. At the end of the day, I just need an accurate way to know if I need to loop, and if so, how many times.)

@jsm174
Copy link
Copy Markdown
Author

jsm174 commented Apr 10, 2026

➜  ~ gifsicle --no-loopcount base.gif > 1.gif
➜  ~ grep -aob "NETSCAPE2.0" 1.gif
➜  ~ gifsicle --loopcount=1 base.gif > 2.gif
➜  ~ grep -aob "NETSCAPE2.0" 2.gif
22:NETSCAPE2.0
➜  ~ gifsicle --loopcount=2 base.gif > 3.gif
➜  ~ grep -aob "NETSCAPE2.0" 3.gif
22:NETSCAPE2.0
➜  ~ gifsicle --loopcount=0 base.gif > 0.gif
➜  ~ grep -aob "NETSCAPE2.0" 0.gif
22:NETSCAPE2.0

I guess some of the confusion is that it's not really loop count but "repeats after the first play".

With what I provided, SDL_image would now abstract this concept away.

So I'm definitely fine with changing to IMG_PROP_METADATA_NETSCAPE_EXTENSION, just let me know what the name should be and what type of value it should be.

@jsm174 jsm174 marked this pull request as draft April 10, 2026 12:16
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.

1 participant