...
Run Format

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

View as plain text