From 2680ee40a0258ccdd1475fca0941922ae45bbf39 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Thu, 30 May 2019 08:25:30 +0200 Subject: [PATCH] Save_GIF(): fix update of nb_bits after writing last code fixes http://pulkomandy.tk/projects/GrafX2/ticket/125 --- src/fileformats.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/fileformats.c b/src/fileformats.c index 808ecd44..3cba0772 100644 --- a/src/fileformats.c +++ b/src/fileformats.c @@ -5313,10 +5313,20 @@ void Save_GIF(T_IO_Context * context) // Write the last code (before EOF) GIF_set_code(GIF_file, &GIF, GIF_buffer, current_string); - // We think we don't need to update GIF.nb_bits here because - // we are writing a string already in the table, - // but we may be wrong as the table grow for each code... - // So in very rare cases there might be a problem. + // we need to update alphabet_free / GIF.nb_bits here because + // the decoder will update them after each code, + // so in very rare cases there might be a problem if we + // don't do it. + // see http://pulkomandy.tk/projects/GrafX2/ticket/125 + if(alphabet_free < 4096) + { + alphabet_free++; + if ((alphabet_free > alphabet_max+1) && (GIF.nb_bits < 12)) + { + GIF.nb_bits++; + alphabet_max = (1 << GIF.nb_bits) - 1; + } + } GIF_set_code(GIF_file, &GIF, GIF_buffer, eof); // 257 for 8bpp // Code de End d'image if (GIF.remainder_bits!=0)