Source file src/image/jpeg/reader.go

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

View as plain text