Fix swapped pixels in GifDecoder's output #3002
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
StandardGifDecoder
uses modified public domain code to decode the GIF. Some changes were done to it that introduced a subtle bug: the initial two pixels are swapped, as well as the pixels after eachCLEAR
command in the LZW stream.Comparing with the original code, the bug was introduced because the loop that pops the bytes from the stack into the output pixel array was moved. This code motion did not take into account the
continue
in the branch guarded byoldCode == NULL_CODE
. The pixel that this branch pushes to the stack was supposed to be immediately transferred to the output, but since the stack-popping loop was moved, it happens after addingfirst
to the output pixels, thereby swapping the two initial pixels.The fix is simple: instead of pushing to the stack in the
oldCode == NULL_CODE
branch, send the pixel directly to the output pixel array.This PR includes a fix, a small test GIF with 8 white-black-white-black pixels, and a JUnit test that fails without the fix.