rsc.io/go@v0.0.0-20150416155037-e040fd465409/src/image/gif/reader.go (about)

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