Bumpkit icon indicating copy to clipboard operation
Bumpkit copied to clipboard

Gif repeat bug

Open Nickesm opened this issue 11 years ago • 8 comments

The Repeat property of the gif encoder class works with the values: -1 = No repeat. 0 = Repeat forever. n = +Iterations. (if n set to 3, the frames will be displayed 4 times)

The problem is, if i set n to 1, iterates 3 times. But not 2 times as expected!

Nickesm avatar Feb 02 '14 05:02 Nickesm

If I set it to one I am getting two iterations. I can't work out how to make it go through the frames once only.

cdonges avatar Feb 19 '14 03:02 cdonges

Yep, same with me, maybe removing the "NETSCAPE 2.0" tag will fix that.

Nickesm avatar Feb 21 '14 22:02 Nickesm

Just posting to say I've seen this issue, but will not be able to attack it right now. I am hopeful time will be available soon for this library.

DataDink avatar Feb 21 '14 22:02 DataDink

Alright, I'm gonna try removing the ApplicationIdentification tag or changing the FileVersion to the first GIF specification.

Nickesm avatar Feb 21 '14 23:02 Nickesm

Looks like different browsers implement the standard differently: https://bugzilla.mozilla.org/show_bug.cgi?id=222176

cdonges avatar Feb 24 '14 03:02 cdonges

oh, no!

Nickesm avatar Feb 24 '14 03:02 Nickesm

If I read the property item 0x5101 of a gif that only plays once without repeating I get the value {1, 0} which translates to 1 which if we set in the encoder will cause it to play once then loop once.

This, to me means that the signature of the gif encoder is incorrect as Microsoft are interpreting the loopcount value as the number of times to play the animation not the number of times to loop the animation. (Does that make sense?)

GifEncoder(stream, null, null, n)

Should make the animation play n times rather than play n+1 times.

Anyhow.. to stop the animation playing again if the signature worked that way you would edit the InitHeader method as follows: It's trickier to work out otherwise since you will never get -1 by reading the property item.

private void InitHeader(Stream sourceGif, int w, int h)
{
    // File Header
    WriteString(FileType);
    WriteString(FileVersion);
    WriteShort(_width.GetValueOrDefault(w)); // Initial Logical Width
    WriteShort(_height.GetValueOrDefault(h)); // Initial Logical Height
    sourceGif.Position = SourceGlobalColorInfoPosition;
    WriteByte(sourceGif.ReadByte()); // Global Color Table Info
    WriteByte(0); // Background Color Index
    WriteByte(0); // Pixel aspect ratio
    WriteColorTable(sourceGif);

    // App Extension Header
    int count = _repeatCount.GetValueOrDefault(0);
    if (count != 1){
        count = Math.Max(0, count - 1); // 0 means loop infinitely.
        WriteShort(ApplicationExtensionBlockIdentifier);
        WriteByte(ApplicationBlockSize);
        WriteString(ApplicationIdentification);
        WriteByte(3); // Application block length
        WriteByte(1);
        WriteShort(_repeatCount.GetValueOrDefault(0)); // Repeat count for images.
        WriteByte(0); // terminator
   }
}

I'd have submitted a pull request but I don't want to change the signature of the method.

JimBobSquarePants avatar Oct 19 '14 12:10 JimBobSquarePants

Hey guys,

Changing the signature should probably be avoided, but adding overloads - etc should be fine. As I am noting on other requests here: I am not actively maintaining this project. Please feel free to make pull requests.

DataDink avatar Apr 05 '17 14:04 DataDink