Commit ff6b9223 authored by Nigel Tao's avatar Nigel Tao Committed by Russ Cox

image/gif: respect local color table transparency.

Fixes #6441.

R=r
CC=andybons, golang-dev
https://golang.org/cl/13829043
parent 96072557
...@@ -79,7 +79,8 @@ type decoder struct { ...@@ -79,7 +79,8 @@ type decoder struct {
imageFields byte imageFields byte
// From graphics control. // From graphics control.
transparentIndex byte transparentIndex byte
hasTransparentIndex bool
// Computed. // Computed.
pixelSize uint pixelSize uint
...@@ -175,11 +176,12 @@ func (d *decoder) decode(r io.Reader, configOnly bool) error { ...@@ -175,11 +176,12 @@ func (d *decoder) decode(r io.Reader, configOnly bool) error {
if err != nil { if err != nil {
return err return err
} }
// TODO: do we set transparency in this map too? That would be
// d.setTransparency(m.Palette)
} else { } else {
m.Palette = d.globalColorMap m.Palette = d.globalColorMap
} }
if d.hasTransparentIndex && int(d.transparentIndex) < len(m.Palette) {
m.Palette[d.transparentIndex] = color.RGBA{}
}
litWidth, err := d.r.ReadByte() litWidth, err := d.r.ReadByte()
if err != nil { if err != nil {
return err return err
...@@ -228,7 +230,11 @@ func (d *decoder) decode(r io.Reader, configOnly bool) error { ...@@ -228,7 +230,11 @@ func (d *decoder) decode(r io.Reader, configOnly bool) error {
d.image = append(d.image, m) d.image = append(d.image, m)
d.delay = append(d.delay, d.delayTime) d.delay = append(d.delay, d.delayTime)
d.delayTime = 0 // TODO: is this correct, or should we hold on to the value? // The GIF89a spec, Section 23 (Graphic Control Extension) says:
// "The scope of this extension is the first graphic rendering block
// to follow." We therefore reset the GCE fields to zero.
d.delayTime = 0
d.hasTransparentIndex = false
case sTrailer: case sTrailer:
if len(d.image) == 0 { if len(d.image) == 0 {
...@@ -339,17 +345,11 @@ func (d *decoder) readGraphicControl() error { ...@@ -339,17 +345,11 @@ func (d *decoder) readGraphicControl() error {
d.delayTime = int(d.tmp[2]) | int(d.tmp[3])<<8 d.delayTime = int(d.tmp[2]) | int(d.tmp[3])<<8
if d.flags&gcTransparentColorSet != 0 { if d.flags&gcTransparentColorSet != 0 {
d.transparentIndex = d.tmp[4] d.transparentIndex = d.tmp[4]
d.setTransparency(d.globalColorMap) d.hasTransparentIndex = true
} }
return nil return nil
} }
func (d *decoder) setTransparency(colorMap color.Palette) {
if int(d.transparentIndex) < len(colorMap) {
colorMap[d.transparentIndex] = color.RGBA{}
}
}
func (d *decoder) newImageFromDescriptor() (*image.Paletted, error) { func (d *decoder) newImageFromDescriptor() (*image.Paletted, error) {
if _, err := io.ReadFull(d.r, d.tmp[0:9]); err != nil { if _, err := io.ReadFull(d.r, d.tmp[0:9]); err != nil {
return nil, fmt.Errorf("gif: can't read image descriptor: %s", err) return nil, fmt.Errorf("gif: can't read image descriptor: %s", err)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment