Home | History | Annotate | Download | only in gif
      1 // Copyright 2011 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 // Package gif implements a GIF image decoder and encoder.
      6 //
      7 // The GIF specification is at http://www.w3.org/Graphics/GIF/spec-gif89a.txt.
      8 package gif
      9 
     10 import (
     11 	"bufio"
     12 	"compress/lzw"
     13 	"errors"
     14 	"fmt"
     15 	"image"
     16 	"image/color"
     17 	"io"
     18 )
     19 
     20 var (
     21 	errNotEnough = errors.New("gif: not enough image data")
     22 	errTooMuch   = errors.New("gif: too much image data")
     23 	errBadPixel  = errors.New("gif: invalid pixel value")
     24 )
     25 
     26 // If the io.Reader does not also have ReadByte, then decode will introduce its own buffering.
     27 type reader interface {
     28 	io.Reader
     29 	io.ByteReader
     30 }
     31 
     32 // Masks etc.
     33 const (
     34 	// Fields.
     35 	fColorTable         = 1 << 7
     36 	fInterlace          = 1 << 6
     37 	fColorTableBitsMask = 7
     38 
     39 	// Graphic control flags.
     40 	gcTransparentColorSet = 1 << 0
     41 	gcDisposalMethodMask  = 7 << 2
     42 )
     43 
     44 // Disposal Methods.
     45 const (
     46 	DisposalNone       = 0x01
     47 	DisposalBackground = 0x02
     48 	DisposalPrevious   = 0x03
     49 )
     50 
     51 // Section indicators.
     52 const (
     53 	sExtension       = 0x21
     54 	sImageDescriptor = 0x2C
     55 	sTrailer         = 0x3B
     56 )
     57 
     58 // Extensions.
     59 const (
     60 	eText           = 0x01 // Plain Text
     61 	eGraphicControl = 0xF9 // Graphic Control
     62 	eComment        = 0xFE // Comment
     63 	eApplication    = 0xFF // Application
     64 )
     65 
     66 // decoder is the type used to decode a GIF file.
     67 type decoder struct {
     68 	r reader
     69 
     70 	// From header.
     71 	vers            string
     72 	width           int
     73 	height          int
     74 	loopCount       int
     75 	delayTime       int
     76 	backgroundIndex byte
     77 	disposalMethod  byte
     78 
     79 	// From image descriptor.
     80 	imageFields byte
     81 
     82 	// From graphics control.
     83 	transparentIndex    byte
     84 	hasTransparentIndex bool
     85 
     86 	// Computed.
     87 	globalColorTable color.Palette
     88 
     89 	// Used when decoding.
     90 	delay    []int
     91 	disposal []byte
     92 	image    []*image.Paletted
     93 	tmp      [1024]byte // must be at least 768 so we can read color table
     94 }
     95 
     96 // blockReader parses the block structure of GIF image data, which
     97 // comprises (n, (n bytes)) blocks, with 1 <= n <= 255.  It is the
     98 // reader given to the LZW decoder, which is thus immune to the
     99 // blocking.  After the LZW decoder completes, there will be a 0-byte
    100 // block remaining (0, ()), which is consumed when checking that the
    101 // blockReader is exhausted.
    102 type blockReader struct {
    103 	r     reader
    104 	slice []byte
    105 	err   error
    106 	tmp   [256]byte
    107 }
    108 
    109 func (b *blockReader) Read(p []byte) (int, error) {
    110 	if b.err != nil {
    111 		return 0, b.err
    112 	}
    113 	if len(p) == 0 {
    114 		return 0, nil
    115 	}
    116 	if len(b.slice) == 0 {
    117 		var blockLen uint8
    118 		blockLen, b.err = b.r.ReadByte()
    119 		if b.err != nil {
    120 			return 0, b.err
    121 		}
    122 		if blockLen == 0 {
    123 			b.err = io.EOF
    124 			return 0, b.err
    125 		}
    126 		b.slice = b.tmp[:blockLen]
    127 		if _, b.err = io.ReadFull(b.r, b.slice); b.err != nil {
    128 			return 0, b.err
    129 		}
    130 	}
    131 	n := copy(p, b.slice)
    132 	b.slice = b.slice[n:]
    133 	return n, nil
    134 }
    135 
    136 // decode reads a GIF image from r and stores the result in d.
    137 func (d *decoder) decode(r io.Reader, configOnly bool) error {
    138 	// Add buffering if r does not provide ReadByte.
    139 	if rr, ok := r.(reader); ok {
    140 		d.r = rr
    141 	} else {
    142 		d.r = bufio.NewReader(r)
    143 	}
    144 
    145 	err := d.readHeaderAndScreenDescriptor()
    146 	if err != nil {
    147 		return err
    148 	}
    149 	if configOnly {
    150 		return nil
    151 	}
    152 
    153 	for {
    154 		c, err := d.r.ReadByte()
    155 		if err != nil {
    156 			return err
    157 		}
    158 		switch c {
    159 		case sExtension:
    160 			if err = d.readExtension(); err != nil {
    161 				return err
    162 			}
    163 
    164 		case sImageDescriptor:
    165 			m, err := d.newImageFromDescriptor()
    166 			if err != nil {
    167 				return err
    168 			}
    169 			useLocalColorTable := d.imageFields&fColorTable != 0
    170 			if useLocalColorTable {
    171 				m.Palette, err = d.readColorTable(d.imageFields)
    172 				if err != nil {
    173 					return err
    174 				}
    175 			} else {
    176 				if d.globalColorTable == nil {
    177 					return errors.New("gif: no color table")
    178 				}
    179 				m.Palette = d.globalColorTable
    180 			}
    181 			if d.hasTransparentIndex && int(d.transparentIndex) < len(m.Palette) {
    182 				if !useLocalColorTable {
    183 					// Clone the global color table.
    184 					m.Palette = append(color.Palette(nil), d.globalColorTable...)
    185 				}
    186 				m.Palette[d.transparentIndex] = color.RGBA{}
    187 			}
    188 			litWidth, err := d.r.ReadByte()
    189 			if err != nil {
    190 				return err
    191 			}
    192 			if litWidth < 2 || litWidth > 8 {
    193 				return fmt.Errorf("gif: pixel size in decode out of range: %d", litWidth)
    194 			}
    195 			// A wonderfully Go-like piece of magic.
    196 			br := &blockReader{r: d.r}
    197 			lzwr := lzw.NewReader(br, lzw.LSB, int(litWidth))
    198 			defer lzwr.Close()
    199 			if _, err = io.ReadFull(lzwr, m.Pix); err != nil {
    200 				if err != io.ErrUnexpectedEOF {
    201 					return err
    202 				}
    203 				return errNotEnough
    204 			}
    205 			// Both lzwr and br should be exhausted. Reading from them should
    206 			// yield (0, io.EOF).
    207 			//
    208 			// The spec (Appendix F - Compression), says that "An End of
    209 			// Information code... must be the last code output by the encoder
    210 			// for an image". In practice, though, giflib (a widely used C
    211 			// library) does not enforce this, so we also accept lzwr returning
    212 			// io.ErrUnexpectedEOF (meaning that the encoded stream hit io.EOF
    213 			// before the LZW decoder saw an explict end code), provided that
    214 			// the io.ReadFull call above successfully read len(m.Pix) bytes.
    215 			// See https://golang.org/issue/9856 for an example GIF.
    216 			if n, err := lzwr.Read(d.tmp[:1]); n != 0 || (err != io.EOF && err != io.ErrUnexpectedEOF) {
    217 				if err != nil {
    218 					return err
    219 				}
    220 				return errTooMuch
    221 			}
    222 			if n, err := br.Read(d.tmp[:1]); n != 0 || err != io.EOF {
    223 				if err != nil {
    224 					return err
    225 				}
    226 				return errTooMuch
    227 			}
    228 
    229 			// Check that the color indexes are inside the palette.
    230 			if len(m.Palette) < 256 {
    231 				for _, pixel := range m.Pix {
    232 					if int(pixel) >= len(m.Palette) {
    233 						return errBadPixel
    234 					}
    235 				}
    236 			}
    237 
    238 			// Undo the interlacing if necessary.
    239 			if d.imageFields&fInterlace != 0 {
    240 				uninterlace(m)
    241 			}
    242 
    243 			d.image = append(d.image, m)
    244 			d.delay = append(d.delay, d.delayTime)
    245 			d.disposal = append(d.disposal, d.disposalMethod)
    246 			// The GIF89a spec, Section 23 (Graphic Control Extension) says:
    247 			// "The scope of this extension is the first graphic rendering block
    248 			// to follow." We therefore reset the GCE fields to zero.
    249 			d.delayTime = 0
    250 			d.hasTransparentIndex = false
    251 
    252 		case sTrailer:
    253 			if len(d.image) == 0 {
    254 				return io.ErrUnexpectedEOF
    255 			}
    256 			return nil
    257 
    258 		default:
    259 			return fmt.Errorf("gif: unknown block type: 0x%.2x", c)
    260 		}
    261 	}
    262 }
    263 
    264 func (d *decoder) readHeaderAndScreenDescriptor() error {
    265 	_, err := io.ReadFull(d.r, d.tmp[:13])
    266 	if err != nil {
    267 		return err
    268 	}
    269 	d.vers = string(d.tmp[:6])
    270 	if d.vers != "GIF87a" && d.vers != "GIF89a" {
    271 		return fmt.Errorf("gif: can't recognize format %s", d.vers)
    272 	}
    273 	d.width = int(d.tmp[6]) + int(d.tmp[7])<<8
    274 	d.height = int(d.tmp[8]) + int(d.tmp[9])<<8
    275 	if fields := d.tmp[10]; fields&fColorTable != 0 {
    276 		d.backgroundIndex = d.tmp[11]
    277 		// readColorTable overwrites the contents of d.tmp, but that's OK.
    278 		if d.globalColorTable, err = d.readColorTable(fields); err != nil {
    279 			return err
    280 		}
    281 	}
    282 	// d.tmp[12] is the Pixel Aspect Ratio, which is ignored.
    283 	return nil
    284 }
    285 
    286 func (d *decoder) readColorTable(fields byte) (color.Palette, error) {
    287 	n := 1 << (1 + uint(fields&fColorTableBitsMask))
    288 	_, err := io.ReadFull(d.r, d.tmp[:3*n])
    289 	if err != nil {
    290 		return nil, fmt.Errorf("gif: short read on color table: %s", err)
    291 	}
    292 	j, p := 0, make(color.Palette, n)
    293 	for i := range p {
    294 		p[i] = color.RGBA{d.tmp[j+0], d.tmp[j+1], d.tmp[j+2], 0xFF}
    295 		j += 3
    296 	}
    297 	return p, nil
    298 }
    299 
    300 func (d *decoder) readExtension() error {
    301 	extension, err := d.r.ReadByte()
    302 	if err != nil {
    303 		return err
    304 	}
    305 	size := 0
    306 	switch extension {
    307 	case eText:
    308 		size = 13
    309 	case eGraphicControl:
    310 		return d.readGraphicControl()
    311 	case eComment:
    312 		// nothing to do but read the data.
    313 	case eApplication:
    314 		b, err := d.r.ReadByte()
    315 		if err != nil {
    316 			return err
    317 		}
    318 		// The spec requires size be 11, but Adobe sometimes uses 10.
    319 		size = int(b)
    320 	default:
    321 		return fmt.Errorf("gif: unknown extension 0x%.2x", extension)
    322 	}
    323 	if size > 0 {
    324 		if _, err := io.ReadFull(d.r, d.tmp[:size]); err != nil {
    325 			return err
    326 		}
    327 	}
    328 
    329 	// Application Extension with "NETSCAPE2.0" as string and 1 in data means
    330 	// this extension defines a loop count.
    331 	if extension == eApplication && string(d.tmp[:size]) == "NETSCAPE2.0" {
    332 		n, err := d.readBlock()
    333 		if n == 0 || err != nil {
    334 			return err
    335 		}
    336 		if n == 3 && d.tmp[0] == 1 {
    337 			d.loopCount = int(d.tmp[1]) | int(d.tmp[2])<<8
    338 		}
    339 	}
    340 	for {
    341 		n, err := d.readBlock()
    342 		if n == 0 || err != nil {
    343 			return err
    344 		}
    345 	}
    346 }
    347 
    348 func (d *decoder) readGraphicControl() error {
    349 	if _, err := io.ReadFull(d.r, d.tmp[:6]); err != nil {
    350 		return fmt.Errorf("gif: can't read graphic control: %s", err)
    351 	}
    352 	flags := d.tmp[1]
    353 	d.disposalMethod = (flags & gcDisposalMethodMask) >> 2
    354 	d.delayTime = int(d.tmp[2]) | int(d.tmp[3])<<8
    355 	if flags&gcTransparentColorSet != 0 {
    356 		d.transparentIndex = d.tmp[4]
    357 		d.hasTransparentIndex = true
    358 	}
    359 	return nil
    360 }
    361 
    362 func (d *decoder) newImageFromDescriptor() (*image.Paletted, error) {
    363 	if _, err := io.ReadFull(d.r, d.tmp[:9]); err != nil {
    364 		return nil, fmt.Errorf("gif: can't read image descriptor: %s", err)
    365 	}
    366 	left := int(d.tmp[0]) + int(d.tmp[1])<<8
    367 	top := int(d.tmp[2]) + int(d.tmp[3])<<8
    368 	width := int(d.tmp[4]) + int(d.tmp[5])<<8
    369 	height := int(d.tmp[6]) + int(d.tmp[7])<<8
    370 	d.imageFields = d.tmp[8]
    371 
    372 	// The GIF89a spec, Section 20 (Image Descriptor) says:
    373 	// "Each image must fit within the boundaries of the Logical
    374 	// Screen, as defined in the Logical Screen Descriptor."
    375 	bounds := image.Rect(left, top, left+width, top+height)
    376 	if bounds != bounds.Intersect(image.Rect(0, 0, d.width, d.height)) {
    377 		return nil, errors.New("gif: frame bounds larger than image bounds")
    378 	}
    379 	return image.NewPaletted(bounds, nil), nil
    380 }
    381 
    382 func (d *decoder) readBlock() (int, error) {
    383 	n, err := d.r.ReadByte()
    384 	if n == 0 || err != nil {
    385 		return 0, err
    386 	}
    387 	return io.ReadFull(d.r, d.tmp[:n])
    388 }
    389 
    390 // interlaceScan defines the ordering for a pass of the interlace algorithm.
    391 type interlaceScan struct {
    392 	skip, start int
    393 }
    394 
    395 // interlacing represents the set of scans in an interlaced GIF image.
    396 var interlacing = []interlaceScan{
    397 	{8, 0}, // Group 1 : Every 8th. row, starting with row 0.
    398 	{8, 4}, // Group 2 : Every 8th. row, starting with row 4.
    399 	{4, 2}, // Group 3 : Every 4th. row, starting with row 2.
    400 	{2, 1}, // Group 4 : Every 2nd. row, starting with row 1.
    401 }
    402 
    403 // uninterlace rearranges the pixels in m to account for interlaced input.
    404 func uninterlace(m *image.Paletted) {
    405 	var nPix []uint8
    406 	dx := m.Bounds().Dx()
    407 	dy := m.Bounds().Dy()
    408 	nPix = make([]uint8, dx*dy)
    409 	offset := 0 // steps through the input by sequential scan lines.
    410 	for _, pass := range interlacing {
    411 		nOffset := pass.start * dx // steps through the output as defined by pass.
    412 		for y := pass.start; y < dy; y += pass.skip {
    413 			copy(nPix[nOffset:nOffset+dx], m.Pix[offset:offset+dx])
    414 			offset += dx
    415 			nOffset += dx * pass.skip
    416 		}
    417 	}
    418 	m.Pix = nPix
    419 }
    420 
    421 // Decode reads a GIF image from r and returns the first embedded
    422 // image as an image.Image.
    423 func Decode(r io.Reader) (image.Image, error) {
    424 	var d decoder
    425 	if err := d.decode(r, false); err != nil {
    426 		return nil, err
    427 	}
    428 	return d.image[0], nil
    429 }
    430 
    431 // GIF represents the possibly multiple images stored in a GIF file.
    432 type GIF struct {
    433 	Image     []*image.Paletted // The successive images.
    434 	Delay     []int             // The successive delay times, one per frame, in 100ths of a second.
    435 	LoopCount int               // The loop count.
    436 	// Disposal is the successive disposal methods, one per frame. For
    437 	// backwards compatibility, a nil Disposal is valid to pass to EncodeAll,
    438 	// and implies that each frame's disposal method is 0 (no disposal
    439 	// specified).
    440 	Disposal []byte
    441 	// Config is the global color table (palette), width and height. A nil or
    442 	// empty-color.Palette Config.ColorModel means that each frame has its own
    443 	// color table and there is no global color table. Each frame's bounds must
    444 	// be within the rectangle defined by the two points (0, 0) and
    445 	// (Config.Width, Config.Height).
    446 	//
    447 	// For backwards compatibility, a zero-valued Config is valid to pass to
    448 	// EncodeAll, and implies that the overall GIF's width and height equals
    449 	// the first frame's bounds' Rectangle.Max point.
    450 	Config image.Config
    451 	// BackgroundIndex is the background index in the global color table, for
    452 	// use with the DisposalBackground disposal method.
    453 	BackgroundIndex byte
    454 }
    455 
    456 // DecodeAll reads a GIF image from r and returns the sequential frames
    457 // and timing information.
    458 func DecodeAll(r io.Reader) (*GIF, error) {
    459 	var d decoder
    460 	if err := d.decode(r, false); err != nil {
    461 		return nil, err
    462 	}
    463 	gif := &GIF{
    464 		Image:     d.image,
    465 		LoopCount: d.loopCount,
    466 		Delay:     d.delay,
    467 		Disposal:  d.disposal,
    468 		Config: image.Config{
    469 			ColorModel: d.globalColorTable,
    470 			Width:      d.width,
    471 			Height:     d.height,
    472 		},
    473 		BackgroundIndex: d.backgroundIndex,
    474 	}
    475 	return gif, nil
    476 }
    477 
    478 // DecodeConfig returns the global color model and dimensions of a GIF image
    479 // without decoding the entire image.
    480 func DecodeConfig(r io.Reader) (image.Config, error) {
    481 	var d decoder
    482 	if err := d.decode(r, true); err != nil {
    483 		return image.Config{}, err
    484 	}
    485 	return image.Config{
    486 		ColorModel: d.globalColorTable,
    487 		Width:      d.width,
    488 		Height:     d.height,
    489 	}, nil
    490 }
    491 
    492 func init() {
    493 	image.RegisterFormat("gif", "GIF8?a", Decode, DecodeConfig)
    494 }
    495