github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/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: https://www.w3.org/Graphics/JPEG/itu-t81.pdf.
     8  package jpeg
     9  
    10  import (
    11  	"image"
    12  	"image/color"
    13  	"image/internal/imageutil"
    14  	"io"
    15  )
    16  
    17  // TODO(nigeltao): fix up the doc comment style so that sentences start with
    18  // the name of the type or function that they annotate.
    19  
    20  // A FormatError reports that the input is not a valid JPEG.
    21  type FormatError string
    22  
    23  func (e FormatError) Error() string { return "invalid JPEG format: " + string(e) }
    24  
    25  // An UnsupportedError reports that the input uses a valid but unimplemented JPEG feature.
    26  type UnsupportedError string
    27  
    28  func (e UnsupportedError) Error() string { return "unsupported JPEG feature: " + string(e) }
    29  
    30  var errUnsupportedSubsamplingRatio = UnsupportedError("luma/chroma subsampling ratio")
    31  
    32  // Component specification, specified in section B.2.2.
    33  type component struct {
    34  	h  int   // Horizontal sampling factor.
    35  	v  int   // Vertical sampling factor.
    36  	c  uint8 // Component identifier.
    37  	tq uint8 // Quantization table destination selector.
    38  }
    39  
    40  const (
    41  	dcTable = 0
    42  	acTable = 1
    43  	maxTc   = 1
    44  	maxTh   = 3
    45  	maxTq   = 3
    46  
    47  	maxComponents = 4
    48  )
    49  
    50  const (
    51  	sof0Marker = 0xc0 // Start Of Frame (Baseline Sequential).
    52  	sof1Marker = 0xc1 // Start Of Frame (Extended Sequential).
    53  	sof2Marker = 0xc2 // Start Of Frame (Progressive).
    54  	dhtMarker  = 0xc4 // Define Huffman Table.
    55  	rst0Marker = 0xd0 // ReSTart (0).
    56  	rst7Marker = 0xd7 // ReSTart (7).
    57  	soiMarker  = 0xd8 // Start Of Image.
    58  	eoiMarker  = 0xd9 // End Of Image.
    59  	sosMarker  = 0xda // Start Of Scan.
    60  	dqtMarker  = 0xdb // Define Quantization Table.
    61  	driMarker  = 0xdd // Define Restart Interval.
    62  	comMarker  = 0xfe // COMment.
    63  	// "APPlication specific" markers aren't part of the JPEG spec per se,
    64  	// but in practice, their use is described at
    65  	// https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html
    66  	app0Marker  = 0xe0
    67  	app14Marker = 0xee
    68  	app15Marker = 0xef
    69  )
    70  
    71  // See https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html#Adobe
    72  const (
    73  	adobeTransformUnknown = 0
    74  	adobeTransformYCbCr   = 1
    75  	adobeTransformYCbCrK  = 2
    76  )
    77  
    78  // unzig maps from the zig-zag ordering to the natural ordering. For example,
    79  // unzig[3] is the column and row of the fourth element in zig-zag order. The
    80  // value is 16, which means first column (16%8 == 0) and third row (16/8 == 2).
    81  var unzig = [blockSize]int{
    82  	0, 1, 8, 16, 9, 2, 3, 10,
    83  	17, 24, 32, 25, 18, 11, 4, 5,
    84  	12, 19, 26, 33, 40, 48, 41, 34,
    85  	27, 20, 13, 6, 7, 14, 21, 28,
    86  	35, 42, 49, 56, 57, 50, 43, 36,
    87  	29, 22, 15, 23, 30, 37, 44, 51,
    88  	58, 59, 52, 45, 38, 31, 39, 46,
    89  	53, 60, 61, 54, 47, 55, 62, 63,
    90  }
    91  
    92  // Deprecated: Reader is not used by the image/jpeg package and should
    93  // not be used by others. It is kept for compatibility.
    94  type Reader interface {
    95  	io.ByteReader
    96  	io.Reader
    97  }
    98  
    99  // bits holds the unprocessed bits that have been taken from the byte-stream.
   100  // The n least significant bits of a form the unread bits, to be read in MSB to
   101  // LSB order.
   102  type bits struct {
   103  	a uint32 // accumulator.
   104  	m uint32 // mask. m==1<<(n-1) when n>0, with m==0 when n==0.
   105  	n int32  // the number of unread bits in a.
   106  }
   107  
   108  type decoder struct {
   109  	r    io.Reader
   110  	bits bits
   111  	// bytes is a byte buffer, similar to a bufio.Reader, except that it
   112  	// has to be able to unread more than 1 byte, due to byte stuffing.
   113  	// Byte stuffing is specified in section F.1.2.3.
   114  	bytes struct {
   115  		// buf[i:j] are the buffered bytes read from the underlying
   116  		// io.Reader that haven't yet been passed further on.
   117  		buf  [4096]byte
   118  		i, j int
   119  		// nUnreadable is the number of bytes to back up i after
   120  		// overshooting. It can be 0, 1 or 2.
   121  		nUnreadable int
   122  	}
   123  	width, height int
   124  
   125  	img1        *image.Gray
   126  	img3        *image.YCbCr
   127  	blackPix    []byte
   128  	blackStride int
   129  
   130  	ri    int // Restart Interval.
   131  	nComp int
   132  
   133  	// As per section 4.5, there are four modes of operation (selected by the
   134  	// SOF? markers): sequential DCT, progressive DCT, lossless and
   135  	// hierarchical, although this implementation does not support the latter
   136  	// two non-DCT modes. Sequential DCT is further split into baseline and
   137  	// extended, as per section 4.11.
   138  	baseline    bool
   139  	progressive bool
   140  
   141  	jfif                bool
   142  	adobeTransformValid bool
   143  	adobeTransform      uint8
   144  	eobRun              uint16 // End-of-Band run, specified in section G.1.2.2.
   145  
   146  	comp       [maxComponents]component
   147  	progCoeffs [maxComponents][]block // Saved state between progressive-mode scans.
   148  	huff       [maxTc + 1][maxTh + 1]huffman
   149  	quant      [maxTq + 1]block // Quantization tables, in zig-zag order.
   150  	tmp        [2 * blockSize]byte
   151  }
   152  
   153  // fill fills up the d.bytes.buf buffer from the underlying io.Reader. It
   154  // should only be called when there are no unread bytes in d.bytes.
   155  func (d *decoder) fill() error {
   156  	if d.bytes.i != d.bytes.j {
   157  		panic("jpeg: fill called when unread bytes exist")
   158  	}
   159  	// Move the last 2 bytes to the start of the buffer, in case we need
   160  	// to call unreadByteStuffedByte.
   161  	if d.bytes.j > 2 {
   162  		d.bytes.buf[0] = d.bytes.buf[d.bytes.j-2]
   163  		d.bytes.buf[1] = d.bytes.buf[d.bytes.j-1]
   164  		d.bytes.i, d.bytes.j = 2, 2
   165  	}
   166  	// Fill in the rest of the buffer.
   167  	n, err := d.r.Read(d.bytes.buf[d.bytes.j:])
   168  	d.bytes.j += n
   169  	if n > 0 {
   170  		err = nil
   171  	}
   172  	return err
   173  }
   174  
   175  // unreadByteStuffedByte undoes the most recent readByteStuffedByte call,
   176  // giving a byte of data back from d.bits to d.bytes. The Huffman look-up table
   177  // requires at least 8 bits for look-up, which means that Huffman decoding can
   178  // sometimes overshoot and read one or two too many bytes. Two-byte overshoot
   179  // can happen when expecting to read a 0xff 0x00 byte-stuffed byte.
   180  func (d *decoder) unreadByteStuffedByte() {
   181  	d.bytes.i -= d.bytes.nUnreadable
   182  	d.bytes.nUnreadable = 0
   183  	if d.bits.n >= 8 {
   184  		d.bits.a >>= 8
   185  		d.bits.n -= 8
   186  		d.bits.m >>= 8
   187  	}
   188  }
   189  
   190  // readByte returns the next byte, whether buffered or not buffered. It does
   191  // not care about byte stuffing.
   192  func (d *decoder) readByte() (x byte, err error) {
   193  	for d.bytes.i == d.bytes.j {
   194  		if err = d.fill(); err != nil {
   195  			return 0, err
   196  		}
   197  	}
   198  	x = d.bytes.buf[d.bytes.i]
   199  	d.bytes.i++
   200  	d.bytes.nUnreadable = 0
   201  	return x, nil
   202  }
   203  
   204  // errMissingFF00 means that readByteStuffedByte encountered an 0xff byte (a
   205  // marker byte) that wasn't the expected byte-stuffed sequence 0xff, 0x00.
   206  var errMissingFF00 = FormatError("missing 0xff00 sequence")
   207  
   208  // readByteStuffedByte is like readByte but is for byte-stuffed Huffman data.
   209  func (d *decoder) readByteStuffedByte() (x byte, err error) {
   210  	// Take the fast path if d.bytes.buf contains at least two bytes.
   211  	if d.bytes.i+2 <= d.bytes.j {
   212  		x = d.bytes.buf[d.bytes.i]
   213  		d.bytes.i++
   214  		d.bytes.nUnreadable = 1
   215  		if x != 0xff {
   216  			return x, err
   217  		}
   218  		if d.bytes.buf[d.bytes.i] != 0x00 {
   219  			return 0, errMissingFF00
   220  		}
   221  		d.bytes.i++
   222  		d.bytes.nUnreadable = 2
   223  		return 0xff, nil
   224  	}
   225  
   226  	d.bytes.nUnreadable = 0
   227  
   228  	x, err = d.readByte()
   229  	if err != nil {
   230  		return 0, err
   231  	}
   232  	d.bytes.nUnreadable = 1
   233  	if x != 0xff {
   234  		return x, nil
   235  	}
   236  
   237  	x, err = d.readByte()
   238  	if err != nil {
   239  		return 0, err
   240  	}
   241  	d.bytes.nUnreadable = 2
   242  	if x != 0x00 {
   243  		return 0, errMissingFF00
   244  	}
   245  	return 0xff, nil
   246  }
   247  
   248  // readFull reads exactly len(p) bytes into p. It does not care about byte
   249  // stuffing.
   250  func (d *decoder) readFull(p []byte) error {
   251  	// Unread the overshot bytes, if any.
   252  	if d.bytes.nUnreadable != 0 {
   253  		if d.bits.n >= 8 {
   254  			d.unreadByteStuffedByte()
   255  		}
   256  		d.bytes.nUnreadable = 0
   257  	}
   258  
   259  	for {
   260  		n := copy(p, d.bytes.buf[d.bytes.i:d.bytes.j])
   261  		p = p[n:]
   262  		d.bytes.i += n
   263  		if len(p) == 0 {
   264  			break
   265  		}
   266  		if err := d.fill(); err != nil {
   267  			if err == io.EOF {
   268  				err = io.ErrUnexpectedEOF
   269  			}
   270  			return err
   271  		}
   272  	}
   273  	return nil
   274  }
   275  
   276  // ignore ignores the next n bytes.
   277  func (d *decoder) ignore(n int) error {
   278  	// Unread the overshot bytes, if any.
   279  	if d.bytes.nUnreadable != 0 {
   280  		if d.bits.n >= 8 {
   281  			d.unreadByteStuffedByte()
   282  		}
   283  		d.bytes.nUnreadable = 0
   284  	}
   285  
   286  	for {
   287  		m := d.bytes.j - d.bytes.i
   288  		if m > n {
   289  			m = n
   290  		}
   291  		d.bytes.i += m
   292  		n -= m
   293  		if n == 0 {
   294  			break
   295  		}
   296  		if err := d.fill(); err != nil {
   297  			if err == io.EOF {
   298  				err = io.ErrUnexpectedEOF
   299  			}
   300  			return err
   301  		}
   302  	}
   303  	return nil
   304  }
   305  
   306  // Specified in section B.2.2.
   307  func (d *decoder) processSOF(n int) error {
   308  	if d.nComp != 0 {
   309  		return FormatError("multiple SOF markers")
   310  	}
   311  	switch n {
   312  	case 6 + 3*1: // Grayscale image.
   313  		d.nComp = 1
   314  	case 6 + 3*3: // YCbCr or RGB image.
   315  		d.nComp = 3
   316  	case 6 + 3*4: // YCbCrK or CMYK image.
   317  		d.nComp = 4
   318  	default:
   319  		return UnsupportedError("number of components")
   320  	}
   321  	if err := d.readFull(d.tmp[:n]); err != nil {
   322  		return err
   323  	}
   324  	// We only support 8-bit precision.
   325  	if d.tmp[0] != 8 {
   326  		return UnsupportedError("precision")
   327  	}
   328  	d.height = int(d.tmp[1])<<8 + int(d.tmp[2])
   329  	d.width = int(d.tmp[3])<<8 + int(d.tmp[4])
   330  	if int(d.tmp[5]) != d.nComp {
   331  		return FormatError("SOF has wrong length")
   332  	}
   333  
   334  	for i := 0; i < d.nComp; i++ {
   335  		d.comp[i].c = d.tmp[6+3*i]
   336  		// Section B.2.2 states that "the value of C_i shall be different from
   337  		// the values of C_1 through C_(i-1)".
   338  		for j := 0; j < i; j++ {
   339  			if d.comp[i].c == d.comp[j].c {
   340  				return FormatError("repeated component identifier")
   341  			}
   342  		}
   343  
   344  		d.comp[i].tq = d.tmp[8+3*i]
   345  		if d.comp[i].tq > maxTq {
   346  			return FormatError("bad Tq value")
   347  		}
   348  
   349  		hv := d.tmp[7+3*i]
   350  		h, v := int(hv>>4), int(hv&0x0f)
   351  		if h < 1 || 4 < h || v < 1 || 4 < v {
   352  			return FormatError("luma/chroma subsampling ratio")
   353  		}
   354  		if h == 3 || v == 3 {
   355  			return errUnsupportedSubsamplingRatio
   356  		}
   357  		switch d.nComp {
   358  		case 1:
   359  			// If a JPEG image has only one component, section A.2 says "this data
   360  			// is non-interleaved by definition" and section A.2.2 says "[in this
   361  			// case...] the order of data units within a scan shall be left-to-right
   362  			// and top-to-bottom... regardless of the values of H_1 and V_1". Section
   363  			// 4.8.2 also says "[for non-interleaved data], the MCU is defined to be
   364  			// one data unit". Similarly, section A.1.1 explains that it is the ratio
   365  			// of H_i to max_j(H_j) that matters, and similarly for V. For grayscale
   366  			// images, H_1 is the maximum H_j for all components j, so that ratio is
   367  			// always 1. The component's (h, v) is effectively always (1, 1): even if
   368  			// the nominal (h, v) is (2, 1), a 20x5 image is encoded in three 8x8
   369  			// MCUs, not two 16x8 MCUs.
   370  			h, v = 1, 1
   371  
   372  		case 3:
   373  			// For YCbCr images, we only support 4:4:4, 4:4:0, 4:2:2, 4:2:0,
   374  			// 4:1:1 or 4:1:0 chroma subsampling ratios. This implies that the
   375  			// (h, v) values for the Y component are either (1, 1), (1, 2),
   376  			// (2, 1), (2, 2), (4, 1) or (4, 2), and the Y component's values
   377  			// must be a multiple of the Cb and Cr component's values. We also
   378  			// assume that the two chroma components have the same subsampling
   379  			// ratio.
   380  			switch i {
   381  			case 0: // Y.
   382  				// We have already verified, above, that h and v are both
   383  				// either 1, 2 or 4, so invalid (h, v) combinations are those
   384  				// with v == 4.
   385  				if v == 4 {
   386  					return errUnsupportedSubsamplingRatio
   387  				}
   388  			case 1: // Cb.
   389  				if d.comp[0].h%h != 0 || d.comp[0].v%v != 0 {
   390  					return errUnsupportedSubsamplingRatio
   391  				}
   392  			case 2: // Cr.
   393  				if d.comp[1].h != h || d.comp[1].v != v {
   394  					return errUnsupportedSubsamplingRatio
   395  				}
   396  			}
   397  
   398  		case 4:
   399  			// For 4-component images (either CMYK or YCbCrK), we only support two
   400  			// hv vectors: [0x11 0x11 0x11 0x11] and [0x22 0x11 0x11 0x22].
   401  			// Theoretically, 4-component JPEG images could mix and match hv values
   402  			// but in practice, those two combinations are the only ones in use,
   403  			// and it simplifies the applyBlack code below if we can assume that:
   404  			//	- for CMYK, the C and K channels have full samples, and if the M
   405  			//	  and Y channels subsample, they subsample both horizontally and
   406  			//	  vertically.
   407  			//	- for YCbCrK, the Y and K channels have full samples.
   408  			switch i {
   409  			case 0:
   410  				if hv != 0x11 && hv != 0x22 {
   411  					return errUnsupportedSubsamplingRatio
   412  				}
   413  			case 1, 2:
   414  				if hv != 0x11 {
   415  					return errUnsupportedSubsamplingRatio
   416  				}
   417  			case 3:
   418  				if d.comp[0].h != h || d.comp[0].v != v {
   419  					return errUnsupportedSubsamplingRatio
   420  				}
   421  			}
   422  		}
   423  
   424  		d.comp[i].h = h
   425  		d.comp[i].v = v
   426  	}
   427  	return nil
   428  }
   429  
   430  // Specified in section B.2.4.1.
   431  func (d *decoder) processDQT(n int) error {
   432  loop:
   433  	for n > 0 {
   434  		n--
   435  		x, err := d.readByte()
   436  		if err != nil {
   437  			return err
   438  		}
   439  		tq := x & 0x0f
   440  		if tq > maxTq {
   441  			return FormatError("bad Tq value")
   442  		}
   443  		switch x >> 4 {
   444  		default:
   445  			return FormatError("bad Pq value")
   446  		case 0:
   447  			if n < blockSize {
   448  				break loop
   449  			}
   450  			n -= blockSize
   451  			if err := d.readFull(d.tmp[:blockSize]); err != nil {
   452  				return err
   453  			}
   454  			for i := range d.quant[tq] {
   455  				d.quant[tq][i] = int32(d.tmp[i])
   456  			}
   457  		case 1:
   458  			if n < 2*blockSize {
   459  				break loop
   460  			}
   461  			n -= 2 * blockSize
   462  			if err := d.readFull(d.tmp[:2*blockSize]); err != nil {
   463  				return err
   464  			}
   465  			for i := range d.quant[tq] {
   466  				d.quant[tq][i] = int32(d.tmp[2*i])<<8 | int32(d.tmp[2*i+1])
   467  			}
   468  		}
   469  	}
   470  	if n != 0 {
   471  		return FormatError("DQT has wrong length")
   472  	}
   473  	return nil
   474  }
   475  
   476  // Specified in section B.2.4.4.
   477  func (d *decoder) processDRI(n int) error {
   478  	if n != 2 {
   479  		return FormatError("DRI has wrong length")
   480  	}
   481  	if err := d.readFull(d.tmp[:2]); err != nil {
   482  		return err
   483  	}
   484  	d.ri = int(d.tmp[0])<<8 + int(d.tmp[1])
   485  	return nil
   486  }
   487  
   488  func (d *decoder) processApp0Marker(n int) error {
   489  	if n < 5 {
   490  		return d.ignore(n)
   491  	}
   492  	if err := d.readFull(d.tmp[:5]); err != nil {
   493  		return err
   494  	}
   495  	n -= 5
   496  
   497  	d.jfif = d.tmp[0] == 'J' && d.tmp[1] == 'F' && d.tmp[2] == 'I' && d.tmp[3] == 'F' && d.tmp[4] == '\x00'
   498  
   499  	if n > 0 {
   500  		return d.ignore(n)
   501  	}
   502  	return nil
   503  }
   504  
   505  func (d *decoder) processApp14Marker(n int) error {
   506  	if n < 12 {
   507  		return d.ignore(n)
   508  	}
   509  	if err := d.readFull(d.tmp[:12]); err != nil {
   510  		return err
   511  	}
   512  	n -= 12
   513  
   514  	if d.tmp[0] == 'A' && d.tmp[1] == 'd' && d.tmp[2] == 'o' && d.tmp[3] == 'b' && d.tmp[4] == 'e' {
   515  		d.adobeTransformValid = true
   516  		d.adobeTransform = d.tmp[11]
   517  	}
   518  
   519  	if n > 0 {
   520  		return d.ignore(n)
   521  	}
   522  	return nil
   523  }
   524  
   525  // decode reads a JPEG image from r and returns it as an image.Image.
   526  func (d *decoder) decode(r io.Reader, configOnly bool) (image.Image, error) {
   527  	d.r = r
   528  
   529  	// Check for the Start Of Image marker.
   530  	if err := d.readFull(d.tmp[:2]); err != nil {
   531  		return nil, err
   532  	}
   533  	if d.tmp[0] != 0xff || d.tmp[1] != soiMarker {
   534  		return nil, FormatError("missing SOI marker")
   535  	}
   536  
   537  	// Process the remaining segments until the End Of Image marker.
   538  	for {
   539  		err := d.readFull(d.tmp[:2])
   540  		if err != nil {
   541  			return nil, err
   542  		}
   543  		for d.tmp[0] != 0xff {
   544  			// Strictly speaking, this is a format error. However, libjpeg is
   545  			// liberal in what it accepts. As of version 9, next_marker in
   546  			// jdmarker.c treats this as a warning (JWRN_EXTRANEOUS_DATA) and
   547  			// continues to decode the stream. Even before next_marker sees
   548  			// extraneous data, jpeg_fill_bit_buffer in jdhuff.c reads as many
   549  			// bytes as it can, possibly past the end of a scan's data. It
   550  			// effectively puts back any markers that it overscanned (e.g. an
   551  			// "\xff\xd9" EOI marker), but it does not put back non-marker data,
   552  			// and thus it can silently ignore a small number of extraneous
   553  			// non-marker bytes before next_marker has a chance to see them (and
   554  			// print a warning).
   555  			//
   556  			// We are therefore also liberal in what we accept. Extraneous data
   557  			// is silently ignored.
   558  			//
   559  			// This is similar to, but not exactly the same as, the restart
   560  			// mechanism within a scan (the RST[0-7] markers).
   561  			//
   562  			// Note that extraneous 0xff bytes in e.g. SOS data are escaped as
   563  			// "\xff\x00", and so are detected a little further down below.
   564  			d.tmp[0] = d.tmp[1]
   565  			d.tmp[1], err = d.readByte()
   566  			if err != nil {
   567  				return nil, err
   568  			}
   569  		}
   570  		marker := d.tmp[1]
   571  		if marker == 0 {
   572  			// Treat "\xff\x00" as extraneous data.
   573  			continue
   574  		}
   575  		for marker == 0xff {
   576  			// Section B.1.1.2 says, "Any marker may optionally be preceded by any
   577  			// number of fill bytes, which are bytes assigned code X'FF'".
   578  			marker, err = d.readByte()
   579  			if err != nil {
   580  				return nil, err
   581  			}
   582  		}
   583  		if marker == eoiMarker { // End Of Image.
   584  			break
   585  		}
   586  		if rst0Marker <= marker && marker <= rst7Marker {
   587  			// Figures B.2 and B.16 of the specification suggest that restart markers should
   588  			// only occur between Entropy Coded Segments and not after the final ECS.
   589  			// However, some encoders may generate incorrect JPEGs with a final restart
   590  			// marker. That restart marker will be seen here instead of inside the processSOS
   591  			// method, and is ignored as a harmless error. Restart markers have no extra data,
   592  			// so we check for this before we read the 16-bit length of the segment.
   593  			continue
   594  		}
   595  
   596  		// Read the 16-bit length of the segment. The value includes the 2 bytes for the
   597  		// length itself, so we subtract 2 to get the number of remaining bytes.
   598  		if err = d.readFull(d.tmp[:2]); err != nil {
   599  			return nil, err
   600  		}
   601  		n := int(d.tmp[0])<<8 + int(d.tmp[1]) - 2
   602  		if n < 0 {
   603  			return nil, FormatError("short segment length")
   604  		}
   605  
   606  		switch marker {
   607  		case sof0Marker, sof1Marker, sof2Marker:
   608  			d.baseline = marker == sof0Marker
   609  			d.progressive = marker == sof2Marker
   610  			err = d.processSOF(n)
   611  			if configOnly && d.jfif {
   612  				return nil, err
   613  			}
   614  		case dhtMarker:
   615  			if configOnly {
   616  				err = d.ignore(n)
   617  			} else {
   618  				err = d.processDHT(n)
   619  			}
   620  		case dqtMarker:
   621  			if configOnly {
   622  				err = d.ignore(n)
   623  			} else {
   624  				err = d.processDQT(n)
   625  			}
   626  		case sosMarker:
   627  			if configOnly {
   628  				return nil, nil
   629  			}
   630  			err = d.processSOS(n)
   631  		case driMarker:
   632  			if configOnly {
   633  				err = d.ignore(n)
   634  			} else {
   635  				err = d.processDRI(n)
   636  			}
   637  		case app0Marker:
   638  			err = d.processApp0Marker(n)
   639  		case app14Marker:
   640  			err = d.processApp14Marker(n)
   641  		default:
   642  			if app0Marker <= marker && marker <= app15Marker || marker == comMarker {
   643  				err = d.ignore(n)
   644  			} else if marker < 0xc0 { // See Table B.1 "Marker code assignments".
   645  				err = FormatError("unknown marker")
   646  			} else {
   647  				err = UnsupportedError("unknown marker")
   648  			}
   649  		}
   650  		if err != nil {
   651  			return nil, err
   652  		}
   653  	}
   654  
   655  	if d.progressive {
   656  		if err := d.reconstructProgressiveImage(); err != nil {
   657  			return nil, err
   658  		}
   659  	}
   660  	if d.img1 != nil {
   661  		return d.img1, nil
   662  	}
   663  	if d.img3 != nil {
   664  		if d.blackPix != nil {
   665  			return d.applyBlack()
   666  		} else if d.isRGB() {
   667  			return d.convertToRGB()
   668  		}
   669  		return d.img3, nil
   670  	}
   671  	return nil, FormatError("missing SOS marker")
   672  }
   673  
   674  // applyBlack combines d.img3 and d.blackPix into a CMYK image. The formula
   675  // used depends on whether the JPEG image is stored as CMYK or YCbCrK,
   676  // indicated by the APP14 (Adobe) metadata.
   677  //
   678  // Adobe CMYK JPEG images are inverted, where 255 means no ink instead of full
   679  // ink, so we apply "v = 255 - v" at various points. Note that a double
   680  // inversion is a no-op, so inversions might be implicit in the code below.
   681  func (d *decoder) applyBlack() (image.Image, error) {
   682  	if !d.adobeTransformValid {
   683  		return nil, UnsupportedError("unknown color model: 4-component JPEG doesn't have Adobe APP14 metadata")
   684  	}
   685  
   686  	// If the 4-component JPEG image isn't explicitly marked as "Unknown (RGB
   687  	// or CMYK)" as per
   688  	// https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html#Adobe
   689  	// we assume that it is YCbCrK. This matches libjpeg's jdapimin.c.
   690  	if d.adobeTransform != adobeTransformUnknown {
   691  		// Convert the YCbCr part of the YCbCrK to RGB, invert the RGB to get
   692  		// CMY, and patch in the original K. The RGB to CMY inversion cancels
   693  		// out the 'Adobe inversion' described in the applyBlack doc comment
   694  		// above, so in practice, only the fourth channel (black) is inverted.
   695  		bounds := d.img3.Bounds()
   696  		img := image.NewRGBA(bounds)
   697  		imageutil.DrawYCbCr(img, bounds, d.img3, bounds.Min)
   698  		for iBase, y := 0, bounds.Min.Y; y < bounds.Max.Y; iBase, y = iBase+img.Stride, y+1 {
   699  			for i, x := iBase+3, bounds.Min.X; x < bounds.Max.X; i, x = i+4, x+1 {
   700  				img.Pix[i] = 255 - d.blackPix[(y-bounds.Min.Y)*d.blackStride+(x-bounds.Min.X)]
   701  			}
   702  		}
   703  		return &image.CMYK{
   704  			Pix:    img.Pix,
   705  			Stride: img.Stride,
   706  			Rect:   img.Rect,
   707  		}, nil
   708  	}
   709  
   710  	// The first three channels (cyan, magenta, yellow) of the CMYK
   711  	// were decoded into d.img3, but each channel was decoded into a separate
   712  	// []byte slice, and some channels may be subsampled. We interleave the
   713  	// separate channels into an image.CMYK's single []byte slice containing 4
   714  	// contiguous bytes per pixel.
   715  	bounds := d.img3.Bounds()
   716  	img := image.NewCMYK(bounds)
   717  
   718  	translations := [4]struct {
   719  		src    []byte
   720  		stride int
   721  	}{
   722  		{d.img3.Y, d.img3.YStride},
   723  		{d.img3.Cb, d.img3.CStride},
   724  		{d.img3.Cr, d.img3.CStride},
   725  		{d.blackPix, d.blackStride},
   726  	}
   727  	for t, translation := range translations {
   728  		subsample := d.comp[t].h != d.comp[0].h || d.comp[t].v != d.comp[0].v
   729  		for iBase, y := 0, bounds.Min.Y; y < bounds.Max.Y; iBase, y = iBase+img.Stride, y+1 {
   730  			sy := y - bounds.Min.Y
   731  			if subsample {
   732  				sy /= 2
   733  			}
   734  			for i, x := iBase+t, bounds.Min.X; x < bounds.Max.X; i, x = i+4, x+1 {
   735  				sx := x - bounds.Min.X
   736  				if subsample {
   737  					sx /= 2
   738  				}
   739  				img.Pix[i] = 255 - translation.src[sy*translation.stride+sx]
   740  			}
   741  		}
   742  	}
   743  	return img, nil
   744  }
   745  
   746  func (d *decoder) isRGB() bool {
   747  	if d.jfif {
   748  		return false
   749  	}
   750  	if d.adobeTransformValid && d.adobeTransform == adobeTransformUnknown {
   751  		// https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html#Adobe
   752  		// says that 0 means Unknown (and in practice RGB) and 1 means YCbCr.
   753  		return true
   754  	}
   755  	return d.comp[0].c == 'R' && d.comp[1].c == 'G' && d.comp[2].c == 'B'
   756  }
   757  
   758  func (d *decoder) convertToRGB() (image.Image, error) {
   759  	cScale := d.comp[0].h / d.comp[1].h
   760  	bounds := d.img3.Bounds()
   761  	img := image.NewRGBA(bounds)
   762  	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
   763  		po := img.PixOffset(bounds.Min.X, y)
   764  		yo := d.img3.YOffset(bounds.Min.X, y)
   765  		co := d.img3.COffset(bounds.Min.X, y)
   766  		for i, iMax := 0, bounds.Max.X-bounds.Min.X; i < iMax; i++ {
   767  			img.Pix[po+4*i+0] = d.img3.Y[yo+i]
   768  			img.Pix[po+4*i+1] = d.img3.Cb[co+i/cScale]
   769  			img.Pix[po+4*i+2] = d.img3.Cr[co+i/cScale]
   770  			img.Pix[po+4*i+3] = 255
   771  		}
   772  	}
   773  	return img, nil
   774  }
   775  
   776  // Decode reads a JPEG image from r and returns it as an image.Image.
   777  func Decode(r io.Reader) (image.Image, error) {
   778  	var d decoder
   779  	return d.decode(r, false)
   780  }
   781  
   782  // DecodeConfig returns the color model and dimensions of a JPEG image without
   783  // decoding the entire image.
   784  func DecodeConfig(r io.Reader) (image.Config, error) {
   785  	var d decoder
   786  	if _, err := d.decode(r, true); err != nil {
   787  		return image.Config{}, err
   788  	}
   789  	switch d.nComp {
   790  	case 1:
   791  		return image.Config{
   792  			ColorModel: color.GrayModel,
   793  			Width:      d.width,
   794  			Height:     d.height,
   795  		}, nil
   796  	case 3:
   797  		cm := color.YCbCrModel
   798  		if d.isRGB() {
   799  			cm = color.RGBAModel
   800  		}
   801  		return image.Config{
   802  			ColorModel: cm,
   803  			Width:      d.width,
   804  			Height:     d.height,
   805  		}, nil
   806  	case 4:
   807  		return image.Config{
   808  			ColorModel: color.CMYKModel,
   809  			Width:      d.width,
   810  			Height:     d.height,
   811  		}, nil
   812  	}
   813  	return image.Config{}, FormatError("missing SOF marker")
   814  }
   815  
   816  func init() {
   817  	image.RegisterFormat("jpeg", "\xff\xd8", Decode, DecodeConfig)
   818  }