lecram / gifenc

small C GIF encoder
267 stars 34 forks source link

Support LZW Compress? #13

Open wishWinds opened 2 years ago

wishWinds commented 2 years ago

Hello.

I'm a little new for C. It seems there is no LZW compress code in function put_image. is that True?

Sorry to bother you. Thanks for you code.

lecram commented 2 years ago

Hi @wishWinds.

LZW compression is there. The LZW table is stored as a Trie data structure. put_image() searches for the longest sequence of input pixels present on the table (lines 226-229) then outputs the key (i.e. code) for that sequence (line 231). If there's still space in the table, we add a new entry for the match sequence concatenated with the next input pixel (line 235). If the table is full we clear it so that LZW can adapt to new input (lines 237-239). The function actually writing to the file is put_key(), which just writes the bits representing a key/code in the LZW table.

Hope that helps.