Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use LZW encoding when saving GIF images #5291

Merged
merged 5 commits into from Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/releasenotes/8.2.0.rst
Expand Up @@ -108,6 +108,16 @@ TODO
Other Changes
=============

GIF writer uses LZW encoding
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

GIF files are now written using LZW encoding, which will generate smaller files,
typically about 70% of the size generated by the older encoder.

The pixel data is encoded using the format specified in the [Compuserve GIF
standard](https://www.w3.org/Graphics/GIF/spec-gif89a.txt). The older encoder
used a variant of run-length encoding that was compatible but less efficient.

Libraqm and FriBiDi linking
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
41 changes: 17 additions & 24 deletions src/libImaging/Gif.h
Expand Up @@ -9,10 +9,10 @@

/* Max size for a LZW code word. */

#define GIFBITS 12
#define GIFBITS 12

#define GIFTABLE (1 << GIFBITS)
#define GIFBUFFER (1 << GIFBITS)
#define GIFTABLE (1<<GIFBITS)
#define GIFBUFFER (1<<GIFBITS)

typedef struct {
/* CONFIGURATION */
Expand Down Expand Up @@ -62,11 +62,8 @@ typedef struct {

} GIFDECODERSTATE;

typedef struct GIFENCODERBLOCK_T {
struct GIFENCODERBLOCK_T *next;
int size;
UINT8 data[255];
} GIFENCODERBLOCK;
/* For GIF LZW encoder. */
#define TABLE_SIZE 8192

typedef struct {
/* CONFIGURATION */
Expand All @@ -84,21 +81,17 @@ typedef struct {
/* PRIVATE CONTEXT (set by encoder) */

/* Interlace parameters */
int step, repeat;

/* Output bit buffer */
INT32 bitbuffer;
int bitcount;

/* Output buffer list (linked list) */
GIFENCODERBLOCK *block; /* current block */
GIFENCODERBLOCK *flush; /* output queue */
GIFENCODERBLOCK *free; /* if not null, use this */

/* Fields used for run-length encoding */
int first; /* true if we haven't read the first pixel */
int last; /* last byte value seen */
int count; /* how many bytes with that value we've seen */
int lastcode;
int step;

/* For GIF LZW encoder. */
UINT32 put_state;
UINT32 entry_state;
UINT32 clear_code, end_code, next_code, max_code;
UINT32 code_width, code_bits_left, buf_bits_left;
UINT32 code_buffer;
UINT32 head, tail;
int probe;
UINT32 code;
UINT32 codes[TABLE_SIZE];

} GIFENCODERSTATE;