]> Cypherpunks repositories - gostls13.git/commitdiff
image/gif: implement transparency.
authorRob Pike <r@golang.org>
Mon, 9 May 2011 00:26:16 +0000 (17:26 -0700)
committerRob Pike <r@golang.org>
Mon, 9 May 2011 00:26:16 +0000 (17:26 -0700)
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

src/pkg/image/gif/reader.go

index b7f4afd1257469b900117ec7902400f71e8d2604..6548090dd98ff523ed79e5cce3fef5b811f5d33f 100644 (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 {