Skip to content

Commit

Permalink
Merge pull request #5291 from raygard/giflzw
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Mar 31, 2021
2 parents 7275331 + 06dfbb8 commit 54e9f3b
Show file tree
Hide file tree
Showing 3 changed files with 320 additions and 280 deletions.
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;

0 comments on commit 54e9f3b

Please sign in to comment.