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

View as plain text