github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/src/image/jpeg/reader.go (about)

     1  // Copyright 2009 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 jpeg implements a JPEG image decoder and encoder.
     6  //
     7  // JPEG is defined in ITU-T T.81: http://www.w3.org/Graphics/JPEG/itu-t81.pdf.
     8  package jpeg
     9  
    10  import (
    11  	"image"
    12  	"image/color"
    13  	"io"
    14  )
    15  
    16  // TODO(nigeltao): fix up the doc comment style so that sentences start with
    17  // the name of the type or function that they annotate.
    18  
    19  // A FormatError reports that the input is not a valid JPEG.
    20  type FormatError string
    21  
    22  func (e FormatError) Error() string { return "invalid JPEG format: " + string(e) }
    23  
    24  // An UnsupportedError reports that the input uses a valid but unimplemented JPEG feature.
    25  type UnsupportedError string
    26  
    27  func (e UnsupportedError) Error() string { return "unsupported JPEG feature: " + string(e) }
    28  
    29  // Component specification, specified in section B.2.2.
    30  type component struct {
    31  	h  int   // Horizontal sampling factor.
    32  	v  int   // Vertical sampling factor.
    33  	c  uint8 // Component identifier.
    34  	tq uint8 // Quantization table destination selector.
    35  }
    36  
    37  const (
    38  	dcTable = 0
    39  	acTable = 1
    40  	maxTc   = 1
    41  	maxTh   = 3
    42  	maxTq   = 3
    43  
    44  	// A grayscale JPEG image has only a Y component.
    45  	nGrayComponent = 1
    46  	// A color JPEG image has Y, Cb and Cr components.
    47  	nColorComponent = 3
    48  
    49  	// We only support 4:4:4, 4:4:0, 4:2:2 and 4:2:0 downsampling, and therefore the
    50  	// number of luma samples per chroma sample is at most 2 in the horizontal
    51  	// and 2 in the vertical direction.
    52  	maxH = 2
    53  	maxV = 2
    54  )
    55  
    56  const (
    57  	soiMarker   = 0xd8 // Start Of Image.
    58  	eoiMarker   = 0xd9 // End Of Image.
    59  	sof0Marker  = 0xc0 // Start Of Frame (Baseline).
    60  	sof2Marker  = 0xc2 // Start Of Frame (Progressive).
    61  	dhtMarker   = 0xc4 // Define Huffman Table.
    62  	dqtMarker   = 0xdb // Define Quantization Table.
    63  	sosMarker   = 0xda // Start Of Scan.
    64  	driMarker   = 0xdd // Define Restart Interval.
    65  	rst0Marker  = 0xd0 // ReSTart (0).
    66  	rst7Marker  = 0xd7 // ReSTart (7).
    67  	app0Marker  = 0xe0 // APPlication specific (0).
    68  	app15Marker = 0xef // APPlication specific (15).
    69  	comMarker   = 0xfe // COMment.
    70  )
    71  
    72  // unzig maps from the zig-zag ordering to the natural ordering. For example,
    73  // unzig[3] is the column and row of the fourth element in zig-zag order. The
    74  // value is 16, which means first column (16%8 == 0) and third row (16/8 == 2).
    75  var unzig = [blockSize]int{
    76  	0, 1, 8, 16, 9, 2, 3, 10,
    77  	17, 24, 32, 25, 18, 11, 4, 5,
    78  	12, 19, 26, 33, 40, 48, 41, 34,
    79  	27, 20, 13, 6, 7, 14, 21, 28,
    80  	35, 42, 49, 56, 57, 50, 43, 36,
    81  	29, 22, 15, 23, 30, 37, 44, 51,
    82  	58, 59, 52, 45, 38, 31, 39, 46,
    83  	53, 60, 61, 54, 47, 55, 62, 63,
    84  }
    85  
    86  // Reader is deprecated.
    87  type Reader interface {
    88  	io.ByteReader
    89  	io.Reader
    90  }
    91  
    92  // bits holds the unprocessed bits that have been taken from the byte-stream.
    93  // The n least significant bits of a form the unread bits, to be read in MSB to
    94  // LSB order.
    95  type bits struct {
    96  	a uint32 // accumulator.
    97  	m uint32 // mask. m==1<<(n-1) when n>0, with m==0 when n==0.
    98  	n int32  // the number of unread bits in a.
    99  }
   100  
   101  type decoder struct {
   102  	r    io.Reader
   103  	bits bits
   104  	// bytes is a byte buffer, similar to a bufio.Reader, except that it
   105  	// has to be able to unread more than 1 byte, due to byte stuffing.
   106  	// Byte stuffing is specified in section F.1.2.3.
   107  	bytes struct {
   108  		// buf[i:j] are the buffered bytes read from the underlying
   109  		// io.Reader that haven't yet been passed further on.
   110  		buf  [4096]byte
   111  		i, j int
   112  		// nUnreadable is the number of bytes to back up i after
   113  		// overshooting. It can be 0, 1 or 2.
   114  		nUnreadable int
   115  	}
   116  	width, height int
   117  	img1          *image.Gray
   118  	img3          *image.YCbCr
   119  	ri            int // Restart Interval.
   120  	nComp         int
   121  	progressive   bool
   122  	eobRun        uint16 // End-of-Band run, specified in section G.1.2.2.
   123  	comp          [nColorComponent]component
   124  	progCoeffs    [nColorComponent][]block // Saved state between progressive-mode scans.
   125  	huff          [maxTc + 1][maxTh + 1]huffman
   126  	quant         [maxTq + 1]block // Quantization tables, in zig-zag order.
   127  	tmp           [blockSize + 1]byte
   128  }
   129  
   130  // fill fills up the d.bytes.buf buffer from the underlying io.Reader. It
   131  // should only be called when there are no unread bytes in d.bytes.
   132  func (d *decoder) fill() error {
   133  	if d.bytes.i != d.bytes.j {
   134  		panic("jpeg: fill called when unread bytes exist")
   135  	}
   136  	// Move the last 2 bytes to the start of the buffer, in case we need
   137  	// to call unreadByteStuffedByte.
   138  	if d.bytes.j > 2 {
   139  		d.bytes.buf[0] = d.bytes.buf[d.bytes.j-2]
   140  		d.bytes.buf[1] = d.bytes.buf[d.bytes.j-1]
   141  		d.bytes.i, d.bytes.j = 2, 2
   142  	}
   143  	// Fill in the rest of the buffer.
   144  	n, err := d.r.Read(d.bytes.buf[d.bytes.j:])
   145  	d.bytes.j += n
   146  	return err
   147  }
   148  
   149  // unreadByteStuffedByte undoes the most recent readByteStuffedByte call,
   150  // giving a byte of data back from d.bits to d.bytes. The Huffman look-up table
   151  // requires at least 8 bits for look-up, which means that Huffman decoding can
   152  // sometimes overshoot and read one or two too many bytes. Two-byte overshoot
   153  // can happen when expecting to read a 0xff 0x00 byte-stuffed byte.
   154  func (d *decoder) unreadByteStuffedByte() {
   155  	if d.bytes.nUnreadable == 0 {
   156  		panic("jpeg: unreadByteStuffedByte call cannot be fulfilled")
   157  	}
   158  	d.bytes.i -= d.bytes.nUnreadable
   159  	d.bytes.nUnreadable = 0
   160  	if d.bits.n >= 8 {
   161  		d.bits.a >>= 8
   162  		d.bits.n -= 8
   163  		d.bits.m >>= 8
   164  	}
   165  }
   166  
   167  // readByte returns the next byte, whether buffered or not buffered. It does
   168  // not care about byte stuffing.
   169  func (d *decoder) readByte() (x byte, err error) {
   170  	for d.bytes.i == d.bytes.j {
   171  		if err = d.fill(); err != nil {
   172  			return 0, err
   173  		}
   174  	}
   175  	x = d.bytes.buf[d.bytes.i]
   176  	d.bytes.i++
   177  	d.bytes.nUnreadable = 0
   178  	return x, nil
   179  }
   180  
   181  // errMissingFF00 means that readByteStuffedByte encountered an 0xff byte (a
   182  // marker byte) that wasn't the expected byte-stuffed sequence 0xff, 0x00.
   183  var errMissingFF00 = FormatError("missing 0xff00 sequence")
   184  
   185  // readByteStuffedByte is like readByte but is for byte-stuffed Huffman data.
   186  func (d *decoder) readByteStuffedByte() (x byte, err error) {
   187  	// Take the fast path if d.bytes.buf contains at least two bytes.
   188  	if d.bytes.i+2 <= d.bytes.j {
   189  		x = d.bytes.buf[d.bytes.i]
   190  		d.bytes.i++
   191  		d.bytes.nUnreadable = 1
   192  		if x != 0xff {
   193  			return x, err
   194  		}
   195  		if d.bytes.buf[d.bytes.i] != 0x00 {
   196  			return 0, errMissingFF00
   197  		}
   198  		d.bytes.i++
   199  		d.bytes.nUnreadable = 2
   200  		return 0xff, nil
   201  	}
   202  
   203  	x, err = d.readByte()
   204  	if err != nil {
   205  		return 0, err
   206  	}
   207  	if x != 0xff {
   208  		d.bytes.nUnreadable = 1
   209  		return x, nil
   210  	}
   211  
   212  	x, err = d.readByte()
   213  	if err != nil {
   214  		d.bytes.nUnreadable = 1
   215  		return 0, err
   216  	}
   217  	d.bytes.nUnreadable = 2
   218  	if x != 0x00 {
   219  		return 0, errMissingFF00
   220  	}
   221  	return 0xff, nil
   222  }
   223  
   224  // readFull reads exactly len(p) bytes into p. It does not care about byte
   225  // stuffing.
   226  func (d *decoder) readFull(p []byte) error {
   227  	// Unread the overshot bytes, if any.
   228  	if d.bytes.nUnreadable != 0 {
   229  		if d.bits.n >= 8 {
   230  			d.unreadByteStuffedByte()
   231  		}
   232  		d.bytes.nUnreadable = 0
   233  	}
   234  
   235  	for {
   236  		n := copy(p, d.bytes.buf[d.bytes.i:d.bytes.j])
   237  		p = p[n:]
   238  		d.bytes.i += n
   239  		if len(p) == 0 {
   240  			break
   241  		}
   242  		if err := d.fill(); err != nil {
   243  			if err == io.EOF {
   244  				err = io.ErrUnexpectedEOF
   245  			}
   246  			return err
   247  		}
   248  	}
   249  	return nil
   250  }
   251  
   252  // ignore ignores the next n bytes.
   253  func (d *decoder) ignore(n int) error {
   254  	// Unread the overshot bytes, if any.
   255  	if d.bytes.nUnreadable != 0 {
   256  		if d.bits.n >= 8 {
   257  			d.unreadByteStuffedByte()
   258  		}
   259  		d.bytes.nUnreadable = 0
   260  	}
   261  
   262  	for {
   263  		m := d.bytes.j - d.bytes.i
   264  		if m > n {
   265  			m = n
   266  		}
   267  		d.bytes.i += m
   268  		n -= m
   269  		if n == 0 {
   270  			break
   271  		}
   272  		if err := d.fill(); err != nil {
   273  			if err == io.EOF {
   274  				err = io.ErrUnexpectedEOF
   275  			}
   276  			return err
   277  		}
   278  	}
   279  	return nil
   280  }
   281  
   282  // Specified in section B.2.2.
   283  func (d *decoder) processSOF(n int) error {
   284  	switch n {
   285  	case 6 + 3*nGrayComponent:
   286  		d.nComp = nGrayComponent
   287  	case 6 + 3*nColorComponent:
   288  		d.nComp = nColorComponent
   289  	default:
   290  		return UnsupportedError("SOF has wrong length")
   291  	}
   292  	if err := d.readFull(d.tmp[:n]); err != nil {
   293  		return err
   294  	}
   295  	// We only support 8-bit precision.
   296  	if d.tmp[0] != 8 {
   297  		return UnsupportedError("precision")
   298  	}
   299  	d.height = int(d.tmp[1])<<8 + int(d.tmp[2])
   300  	d.width = int(d.tmp[3])<<8 + int(d.tmp[4])
   301  	if int(d.tmp[5]) != d.nComp {
   302  		return UnsupportedError("SOF has wrong number of image components")
   303  	}
   304  	for i := 0; i < d.nComp; i++ {
   305  		d.comp[i].c = d.tmp[6+3*i]
   306  		d.comp[i].tq = d.tmp[8+3*i]
   307  		if d.nComp == nGrayComponent {
   308  			// If a JPEG image has only one component, section A.2 says "this data
   309  			// is non-interleaved by definition" and section A.2.2 says "[in this
   310  			// case...] the order of data units within a scan shall be left-to-right
   311  			// and top-to-bottom... regardless of the values of H_1 and V_1". Section
   312  			// 4.8.2 also says "[for non-interleaved data], the MCU is defined to be
   313  			// one data unit". Similarly, section A.1.1 explains that it is the ratio
   314  			// of H_i to max_j(H_j) that matters, and similarly for V. For grayscale
   315  			// images, H_1 is the maximum H_j for all components j, so that ratio is
   316  			// always 1. The component's (h, v) is effectively always (1, 1): even if
   317  			// the nominal (h, v) is (2, 1), a 20x5 image is encoded in three 8x8
   318  			// MCUs, not two 16x8 MCUs.
   319  			d.comp[i].h = 1
   320  			d.comp[i].v = 1
   321  			continue
   322  		}
   323  		hv := d.tmp[7+3*i]
   324  		d.comp[i].h = int(hv >> 4)
   325  		d.comp[i].v = int(hv & 0x0f)
   326  		// For color images, we only support 4:4:4, 4:4:0, 4:2:2 or 4:2:0 chroma
   327  		// downsampling ratios. This implies that the (h, v) values for the Y
   328  		// component are either (1, 1), (1, 2), (2, 1) or (2, 2), and the (h, v)
   329  		// values for the Cr and Cb components must be (1, 1).
   330  		if i == 0 {
   331  			if hv != 0x11 && hv != 0x21 && hv != 0x22 && hv != 0x12 {
   332  				return UnsupportedError("luma/chroma downsample ratio")
   333  			}
   334  		} else if hv != 0x11 {
   335  			return UnsupportedError("luma/chroma downsample ratio")
   336  		}
   337  	}
   338  	return nil
   339  }
   340  
   341  // Specified in section B.2.4.1.
   342  func (d *decoder) processDQT(n int) error {
   343  	const qtLength = 1 + blockSize
   344  	for ; n >= qtLength; n -= qtLength {
   345  		if err := d.readFull(d.tmp[:qtLength]); err != nil {
   346  			return err
   347  		}
   348  		pq := d.tmp[0] >> 4
   349  		if pq != 0 {
   350  			return UnsupportedError("bad Pq value")
   351  		}
   352  		tq := d.tmp[0] & 0x0f
   353  		if tq > maxTq {
   354  			return FormatError("bad Tq value")
   355  		}
   356  		for i := range d.quant[tq] {
   357  			d.quant[tq][i] = int32(d.tmp[i+1])
   358  		}
   359  	}
   360  	if n != 0 {
   361  		return FormatError("DQT has wrong length")
   362  	}
   363  	return nil
   364  }
   365  
   366  // Specified in section B.2.4.4.
   367  func (d *decoder) processDRI(n int) error {
   368  	if n != 2 {
   369  		return FormatError("DRI has wrong length")
   370  	}
   371  	if err := d.readFull(d.tmp[:2]); err != nil {
   372  		return err
   373  	}
   374  	d.ri = int(d.tmp[0])<<8 + int(d.tmp[1])
   375  	return nil
   376  }
   377  
   378  // decode reads a JPEG image from r and returns it as an image.Image.
   379  func (d *decoder) decode(r io.Reader, configOnly bool) (image.Image, error) {
   380  	d.r = r
   381  
   382  	// Check for the Start Of Image marker.
   383  	if err := d.readFull(d.tmp[:2]); err != nil {
   384  		return nil, err
   385  	}
   386  	if d.tmp[0] != 0xff || d.tmp[1] != soiMarker {
   387  		return nil, FormatError("missing SOI marker")
   388  	}
   389  
   390  	// Process the remaining segments until the End Of Image marker.
   391  	for {
   392  		err := d.readFull(d.tmp[:2])
   393  		if err != nil {
   394  			return nil, err
   395  		}
   396  		for d.tmp[0] != 0xff {
   397  			// Strictly speaking, this is a format error. However, libjpeg is
   398  			// liberal in what it accepts. As of version 9, next_marker in
   399  			// jdmarker.c treats this as a warning (JWRN_EXTRANEOUS_DATA) and
   400  			// continues to decode the stream. Even before next_marker sees
   401  			// extraneous data, jpeg_fill_bit_buffer in jdhuff.c reads as many
   402  			// bytes as it can, possibly past the end of a scan's data. It
   403  			// effectively puts back any markers that it overscanned (e.g. an
   404  			// "\xff\xd9" EOI marker), but it does not put back non-marker data,
   405  			// and thus it can silently ignore a small number of extraneous
   406  			// non-marker bytes before next_marker has a chance to see them (and
   407  			// print a warning).
   408  			//
   409  			// We are therefore also liberal in what we accept. Extraneous data
   410  			// is silently ignored.
   411  			//
   412  			// This is similar to, but not exactly the same as, the restart
   413  			// mechanism within a scan (the RST[0-7] markers).
   414  			//
   415  			// Note that extraneous 0xff bytes in e.g. SOS data are escaped as
   416  			// "\xff\x00", and so are detected a little further down below.
   417  			d.tmp[0] = d.tmp[1]
   418  			d.tmp[1], err = d.readByte()
   419  			if err != nil {
   420  				return nil, err
   421  			}
   422  		}
   423  		marker := d.tmp[1]
   424  		if marker == 0 {
   425  			// Treat "\xff\x00" as extraneous data.
   426  			continue
   427  		}
   428  		for marker == 0xff {
   429  			// Section B.1.1.2 says, "Any marker may optionally be preceded by any
   430  			// number of fill bytes, which are bytes assigned code X'FF'".
   431  			marker, err = d.readByte()
   432  			if err != nil {
   433  				return nil, err
   434  			}
   435  		}
   436  		if marker == eoiMarker { // End Of Image.
   437  			break
   438  		}
   439  		if rst0Marker <= marker && marker <= rst7Marker {
   440  			// Figures B.2 and B.16 of the specification suggest that restart markers should
   441  			// only occur between Entropy Coded Segments and not after the final ECS.
   442  			// However, some encoders may generate incorrect JPEGs with a final restart
   443  			// marker. That restart marker will be seen here instead of inside the processSOS
   444  			// method, and is ignored as a harmless error. Restart markers have no extra data,
   445  			// so we check for this before we read the 16-bit length of the segment.
   446  			continue
   447  		}
   448  
   449  		// Read the 16-bit length of the segment. The value includes the 2 bytes for the
   450  		// length itself, so we subtract 2 to get the number of remaining bytes.
   451  		if err = d.readFull(d.tmp[:2]); err != nil {
   452  			return nil, err
   453  		}
   454  		n := int(d.tmp[0])<<8 + int(d.tmp[1]) - 2
   455  		if n < 0 {
   456  			return nil, FormatError("short segment length")
   457  		}
   458  
   459  		switch {
   460  		case marker == sof0Marker || marker == sof2Marker: // Start Of Frame.
   461  			d.progressive = marker == sof2Marker
   462  			err = d.processSOF(n)
   463  			if configOnly {
   464  				return nil, err
   465  			}
   466  		case marker == dhtMarker: // Define Huffman Table.
   467  			err = d.processDHT(n)
   468  		case marker == dqtMarker: // Define Quantization Table.
   469  			err = d.processDQT(n)
   470  		case marker == sosMarker: // Start Of Scan.
   471  			err = d.processSOS(n)
   472  		case marker == driMarker: // Define Restart Interval.
   473  			err = d.processDRI(n)
   474  		case app0Marker <= marker && marker <= app15Marker || marker == comMarker: // APPlication specific, or COMment.
   475  			err = d.ignore(n)
   476  		default:
   477  			err = UnsupportedError("unknown marker")
   478  		}
   479  		if err != nil {
   480  			return nil, err
   481  		}
   482  	}
   483  	if d.img1 != nil {
   484  		return d.img1, nil
   485  	}
   486  	if d.img3 != nil {
   487  		return d.img3, nil
   488  	}
   489  	return nil, FormatError("missing SOS marker")
   490  }
   491  
   492  // Decode reads a JPEG image from r and returns it as an image.Image.
   493  func Decode(r io.Reader) (image.Image, error) {
   494  	var d decoder
   495  	return d.decode(r, false)
   496  }
   497  
   498  // DecodeConfig returns the color model and dimensions of a JPEG image without
   499  // decoding the entire image.
   500  func DecodeConfig(r io.Reader) (image.Config, error) {
   501  	var d decoder
   502  	if _, err := d.decode(r, true); err != nil {
   503  		return image.Config{}, err
   504  	}
   505  	switch d.nComp {
   506  	case nGrayComponent:
   507  		return image.Config{
   508  			ColorModel: color.GrayModel,
   509  			Width:      d.width,
   510  			Height:     d.height,
   511  		}, nil
   512  	case nColorComponent:
   513  		return image.Config{
   514  			ColorModel: color.YCbCrModel,
   515  			Width:      d.width,
   516  			Height:     d.height,
   517  		}, nil
   518  	}
   519  	return image.Config{}, FormatError("missing SOF marker")
   520  }
   521  
   522  func init() {
   523  	image.RegisterFormat("jpeg", "\xff\xd8", Decode, DecodeConfig)
   524  }