]> Cypherpunks repositories - gostls13.git/commit
image/gif: try harder to use global color table
authorJed Denlea <jed@fastly.com>
Wed, 4 Oct 2017 22:36:07 +0000 (15:36 -0700)
committerNigel Tao <nigeltao@golang.org>
Fri, 13 Oct 2017 04:28:53 +0000 (04:28 +0000)
commit31cd20a70e22eeaba287067265fadb57df134b82
tree9d48a4594bde32458bdfe460519dd36810f16ebd
parente45e490296a514067144210e42d74d8f318014f1
image/gif: try harder to use global color table

The GIF format allows for an image to contain a global color table which
might be used for some or every frame in an animated GIF.  This palette
contains 24-bit opaque RGB values.  An individual frame may use the
global palette and enable transparency by picking one number to be
transparent, instead of the color value in the palette.

image/gif decodes a GIF, which contains an []*image.Paletted that holds
each frame.  When decoded, if a frame has a transparent color and uses
the global palette, a copy of the global []color.Color is made, and the
transparency color index is replaced with color.RGBA{}.

When encoding a GIF, each frame's palette is encoded to the form it
might exist in a GIF, up to 768 bytes "RGBRGBRGBRGB...". If a frame's
encoded palette is equal to the encoded global color table, the frame
will be encoded with the flag set to use the global color table,
otherwise the frame's palette will be included.

So, if the color in the global color table that matches the transparent
index of one frame wasn't black (and it frequently is not), reencoding a
GIF will likely result in a larger file because each frame's palette
will have to be encoded inline.

This commit takes a frame's transparent color index into account when
comparing an individual image.Paletted's encoded color table to the
global color table.

Fixes #22137

Change-Id: I5460021da6e4d7ce19198d5f94a8ce714815bc08
Reviewed-on: https://go-review.googlesource.com/68313
Reviewed-by: Nigel Tao <nigeltao@golang.org>
src/image/gif/writer.go
src/image/gif/writer_test.go