From daecfc08b1110f333eff6795496013ef5a2af0df Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 12 May 2018 14:41:32 +1000 Subject: [PATCH] Add comment about decoding malformed GIFs --- std/gif/decode_gif.wuffs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 44afe5e2..e042564b 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -300,6 +300,21 @@ pri func decoder.decode_id?(dst base.io_writer, src base.io_reader)() { } } + // Other GIF implementations accept GIF files that aren't completely spec + // compliant. For example, the test/data/gifplayer-muybridge.gif file + // (created by the gifsicle program) is accepted by other GIF decoders. + // However, in that file, frame #61's embedded LZW data is truncated, + // finishing with only 8 of the 9 bits required of the LZW end code. The + // end code itself, 0x81, is representable in only 8 bits, but the number + // of bits for the decoder to read has ticked over from 8 to 9 just before + // that end code is encountered. + // + // To accomodate such malformed GIFs, we detect when the previous frame's + // LZW decoding ended abruptly. The previous LZW decode 'works', in that it + // decodes as much pixel data as is available, but without seeing that end + // code (i.e. returning the "ok" status code), the LZW decoder is stuck in + // a coroutine-in-progress lzw_decoder.decode call, still waiting for that + // end code. To cancel that coroutine, we reset the LZW decoder. if this.previous_lzw_decode_ended_abruptly { this.lzw.reset() }