1
0
mirror of https://github.com/golang/go synced 2024-11-21 19:54:41 -07:00

image/gif: implement transparency.

At least, as I understand it. The spec is unclear about what happens
with a local color map.

R=nigeltao, r2
CC=golang-dev
https://golang.org/cl/4515045
This commit is contained in:
Rob Pike 2011-05-08 17:26:16 -07:00
parent 38d7bcf5e2
commit a54dca8357

View File

@ -69,13 +69,13 @@ type decoder struct {
// From image descriptor.
imageFields byte
// From graphics control.
transparentIndex byte
// Computed.
pixelSize uint
globalColorMap image.PalettedColorModel
// Computed but unused (TODO).
transparentIndex byte
// Used when decoding.
delay []int
image []*image.Paletted
@ -165,6 +165,8 @@ Loop:
if err != nil {
break
}
// TODO: do we set transparency in this map too? That would be
// d.setTransparency(m.Palette)
} else {
m.Palette = d.globalColorMap
}
@ -304,13 +306,19 @@ func (d *decoder) readGraphicControl() os.Error {
}
d.flags = d.tmp[1]
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]
return os.ErrorString("gif: can't handle transparency")
d.setTransparency(d.globalColorMap)
}
return nil
}
func (d *decoder) setTransparency(colorMap image.PalettedColorModel) {
if int(d.transparentIndex) < len(colorMap) {
colorMap[d.transparentIndex] = image.RGBAColor{}
}
}
func (d *decoder) newImageFromDescriptor() (*image.Paletted, os.Error) {
if _, err := io.ReadFull(d.r, d.tmp[0:9]); err != nil {
return nil, fmt.Errorf("gif: can't read image descriptor: %s", err)
@ -336,8 +344,7 @@ func (d *decoder) readBlock() (int, os.Error) {
// Decode reads a GIF image from r and returns the first embedded
// image as an image.Image.
// Limitation: The file must be 8 bits per pixel and have no interlacing
// or transparency.
// Limitation: The file must be 8 bits per pixel and have no interlacing.
func Decode(r io.Reader) (image.Image, os.Error) {
var d decoder
if err := d.decode(r, false); err != nil {
@ -355,8 +362,7 @@ type GIF struct {
// DecodeAll reads a GIF image from r and returns the sequential frames
// and timing information.
// Limitation: The file must be 8 bits per pixel and have no interlacing
// or transparency.
// Limitation: The file must be 8 bits per pixel and have no interlacing.
func DecodeAll(r io.Reader) (*GIF, os.Error) {
var d decoder
if err := d.decode(r, false); err != nil {