github.com/go-oss/image@v0.1.1-0.20230517025328-001b78555e78/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
     6  
     7  import (
     8  	"image"
     9  	"io"
    10  	_ "unsafe" // for go:linkname
    11  )
    12  
    13  // Component specification, specified in section B.2.2.
    14  type component struct {
    15  	h  int   // Horizontal sampling factor.
    16  	v  int   // Vertical sampling factor.
    17  	c  uint8 // Component identifier.
    18  	tq uint8 // Quantization table destination selector.
    19  }
    20  
    21  const (
    22  	maxTc = 1
    23  	maxTh = 3
    24  	maxTq = 3
    25  
    26  	maxComponents = 4
    27  )
    28  
    29  // bits holds the unprocessed bits that have been taken from the byte-stream.
    30  // The n least significant bits of a form the unread bits, to be read in MSB to
    31  // LSB order.
    32  type bits struct {
    33  	a uint32 // accumulator.
    34  	m uint32 // mask. m==1<<(n-1) when n>0, with m==0 when n==0.
    35  	n int32  // the number of unread bits in a.
    36  }
    37  
    38  type decoder struct {
    39  	r    io.Reader
    40  	bits bits
    41  	// bytes is a byte buffer, similar to a bufio.Reader, except that it
    42  	// has to be able to unread more than 1 byte, due to byte stuffing.
    43  	// Byte stuffing is specified in section F.1.2.3.
    44  	bytes struct {
    45  		// buf[i:j] are the buffered bytes read from the underlying
    46  		// io.Reader that haven't yet been passed further on.
    47  		buf  [4096]byte
    48  		i, j int
    49  		// nUnreadable is the number of bytes to back up i after
    50  		// overshooting. It can be 0, 1 or 2.
    51  		nUnreadable int
    52  	}
    53  	width, height int
    54  
    55  	img1        *image.Gray
    56  	img3        *image.YCbCr
    57  	blackPix    []byte
    58  	blackStride int
    59  
    60  	ri    int // Restart Interval.
    61  	nComp int
    62  
    63  	// As per section 4.5, there are four modes of operation (selected by the
    64  	// SOF? markers): sequential DCT, progressive DCT, lossless and
    65  	// hierarchical, although this implementation does not support the latter
    66  	// two non-DCT modes. Sequential DCT is further split into baseline and
    67  	// extended, as per section 4.11.
    68  	baseline    bool
    69  	progressive bool
    70  
    71  	jfif                bool
    72  	adobeTransformValid bool
    73  	adobeTransform      uint8
    74  	eobRun              uint16 // End-of-Band run, specified in section G.1.2.2.
    75  
    76  	comp       [maxComponents]component
    77  	progCoeffs [maxComponents][]block // Saved state between progressive-mode scans.
    78  	huff       [maxTc + 1][maxTh + 1]huffman
    79  	quant      [maxTq + 1]block // Quantization tables, in zig-zag order.
    80  	tmp        [2 * blockSize]byte
    81  }