redwarp / gifdecoder

An implementation of a gif decoder written 100% in Kotlin, plus an associated Drawable for Android
Apache License 2.0
47 stars 6 forks source link

Disposal not correct sometimes? #19

Closed PauGuillamon closed 2 years ago

PauGuillamon commented 2 years ago

HI there, found another little bug :)

I was testing with lots of different gifs and downloaded some from Giphy, I found some stickers are not rendered correctly. For example this one (and others): https://media0.giphy.com/media/kayGhRlOKbJHCqnj9m/giphy.gif ends up in including the first frame when rendering the second:

image

I debugged a bit, this gif has 2 frames with different disposal method, the first frame being RESTORE_TO_BACKGROUND and the second DO_NOT_DISPOSE. Not sure whether that's common or not, but other tools render it correctly.

I have created a branch on my fork so you can quickly test it https://github.com/PauGuillamon/gifdecoder/commits/PGJ/car_gunna_incorrect_disposal I'm not sure what would be the best fix here, since I don't know the specs of GIF.

Let me know if I can help in any way. Thank you!

redwarp commented 2 years ago

Oh nice! I mean, not nice that there is a bug, but I love issues that can be reproduced :-D Let me take a look

redwarp commented 2 years ago

Okay! I got it! Something that I apparently misunderstood in the GIF spec, though I still can't find confirmation for it. But other decoders seem to do that, so I should:

After a loop, when the frame goes back to frame number 0, I should clear the pixel content and start from fresh. I was not doing that, but instead would apply the disposal method of the last frame.

In your example, the car has 2 frames. Frames number 0, frame number 1. Frame number 1 specifies "do not dispose", so I would then draw frame 0 after the loop over the previous frame. Instead, I should clear the canvas.

redwarp commented 2 years ago

Fixed by https://github.com/redwarp/gifdecoder/commit/2556af6478e65938864b500891d4d3fb9579a023

redwarp commented 2 years ago

(I had to edit the hex code of a gif to add a test case :-D)

redwarp commented 2 years ago

Version 0.8.1 released!

PauGuillamon commented 2 years ago

That's awesome, really fast fix! Thank you!