github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/src/image/jpeg/scan.go (about)

     1  // Copyright 2012 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  )
    10  
    11  // makeImg allocates and initializes the destination image.
    12  func (d *decoder) makeImg(mxx, myy int) {
    13  	if d.nComp == 1 {
    14  		m := image.NewGray(image.Rect(0, 0, 8*mxx, 8*myy))
    15  		d.img1 = m.SubImage(image.Rect(0, 0, d.width, d.height)).(*image.Gray)
    16  		return
    17  	}
    18  
    19  	h0 := d.comp[0].h
    20  	v0 := d.comp[0].v
    21  	hRatio := h0 / d.comp[1].h
    22  	vRatio := v0 / d.comp[1].v
    23  	var subsampleRatio image.YCbCrSubsampleRatio
    24  	switch hRatio<<4 | vRatio {
    25  	case 0x11:
    26  		subsampleRatio = image.YCbCrSubsampleRatio444
    27  	case 0x12:
    28  		subsampleRatio = image.YCbCrSubsampleRatio440
    29  	case 0x21:
    30  		subsampleRatio = image.YCbCrSubsampleRatio422
    31  	case 0x22:
    32  		subsampleRatio = image.YCbCrSubsampleRatio420
    33  	case 0x41:
    34  		subsampleRatio = image.YCbCrSubsampleRatio411
    35  	case 0x42:
    36  		subsampleRatio = image.YCbCrSubsampleRatio410
    37  	default:
    38  		panic("unreachable")
    39  	}
    40  	m := image.NewYCbCr(image.Rect(0, 0, 8*h0*mxx, 8*v0*myy), subsampleRatio)
    41  	d.img3 = m.SubImage(image.Rect(0, 0, d.width, d.height)).(*image.YCbCr)
    42  
    43  	if d.nComp == 4 {
    44  		h3, v3 := d.comp[3].h, d.comp[3].v
    45  		d.blackPix = make([]byte, 8*h3*mxx*8*v3*myy)
    46  		d.blackStride = 8 * h3 * mxx
    47  	}
    48  }
    49  
    50  // Specified in section B.2.3.
    51  func (d *decoder) processSOS(n int) error {
    52  	if d.nComp == 0 {
    53  		return FormatError("missing SOF marker")
    54  	}
    55  	if n < 6 || 4+2*d.nComp < n || n%2 != 0 {
    56  		return FormatError("SOS has wrong length")
    57  	}
    58  	if err := d.readFull(d.tmp[:n]); err != nil {
    59  		return err
    60  	}
    61  	nComp := int(d.tmp[0])
    62  	if n != 4+2*nComp {
    63  		return FormatError("SOS length inconsistent with number of components")
    64  	}
    65  	var scan [maxComponents]struct {
    66  		compIndex uint8
    67  		td        uint8 // DC table selector.
    68  		ta        uint8 // AC table selector.
    69  	}
    70  	totalHV := 0
    71  	for i := 0; i < nComp; i++ {
    72  		cs := d.tmp[1+2*i] // Component selector.
    73  		compIndex := -1
    74  		for j, comp := range d.comp[:d.nComp] {
    75  			if cs == comp.c {
    76  				compIndex = j
    77  			}
    78  		}
    79  		if compIndex < 0 {
    80  			return FormatError("unknown component selector")
    81  		}
    82  		scan[i].compIndex = uint8(compIndex)
    83  		// Section B.2.3 states that "the value of Cs_j shall be different from
    84  		// the values of Cs_1 through Cs_(j-1)". Since we have previously
    85  		// verified that a frame's component identifiers (C_i values in section
    86  		// B.2.2) are unique, it suffices to check that the implicit indexes
    87  		// into d.comp are unique.
    88  		for j := 0; j < i; j++ {
    89  			if scan[i].compIndex == scan[j].compIndex {
    90  				return FormatError("repeated component selector")
    91  			}
    92  		}
    93  		totalHV += d.comp[compIndex].h * d.comp[compIndex].v
    94  
    95  		scan[i].td = d.tmp[2+2*i] >> 4
    96  		if scan[i].td > maxTh {
    97  			return FormatError("bad Td value")
    98  		}
    99  		scan[i].ta = d.tmp[2+2*i] & 0x0f
   100  		if scan[i].ta > maxTh {
   101  			return FormatError("bad Ta value")
   102  		}
   103  	}
   104  	// Section B.2.3 states that if there is more than one component then the
   105  	// total H*V values in a scan must be <= 10.
   106  	if d.nComp > 1 && totalHV > 10 {
   107  		return FormatError("total sampling factors too large")
   108  	}
   109  
   110  	// zigStart and zigEnd are the spectral selection bounds.
   111  	// ah and al are the successive approximation high and low values.
   112  	// The spec calls these values Ss, Se, Ah and Al.
   113  	//
   114  	// For progressive JPEGs, these are the two more-or-less independent
   115  	// aspects of progression. Spectral selection progression is when not
   116  	// all of a block's 64 DCT coefficients are transmitted in one pass.
   117  	// For example, three passes could transmit coefficient 0 (the DC
   118  	// component), coefficients 1-5, and coefficients 6-63, in zig-zag
   119  	// order. Successive approximation is when not all of the bits of a
   120  	// band of coefficients are transmitted in one pass. For example,
   121  	// three passes could transmit the 6 most significant bits, followed
   122  	// by the second-least significant bit, followed by the least
   123  	// significant bit.
   124  	//
   125  	// For baseline JPEGs, these parameters are hard-coded to 0/63/0/0.
   126  	zigStart, zigEnd, ah, al := int32(0), int32(blockSize-1), uint32(0), uint32(0)
   127  	if d.progressive {
   128  		zigStart = int32(d.tmp[1+2*nComp])
   129  		zigEnd = int32(d.tmp[2+2*nComp])
   130  		ah = uint32(d.tmp[3+2*nComp] >> 4)
   131  		al = uint32(d.tmp[3+2*nComp] & 0x0f)
   132  		if (zigStart == 0 && zigEnd != 0) || zigStart > zigEnd || blockSize <= zigEnd {
   133  			return FormatError("bad spectral selection bounds")
   134  		}
   135  		if zigStart != 0 && nComp != 1 {
   136  			return FormatError("progressive AC coefficients for more than one component")
   137  		}
   138  		if ah != 0 && ah != al+1 {
   139  			return FormatError("bad successive approximation values")
   140  		}
   141  	}
   142  
   143  	// mxx and myy are the number of MCUs (Minimum Coded Units) in the image.
   144  	h0, v0 := d.comp[0].h, d.comp[0].v // The h and v values from the Y components.
   145  	mxx := (d.width + 8*h0 - 1) / (8 * h0)
   146  	myy := (d.height + 8*v0 - 1) / (8 * v0)
   147  	if d.img1 == nil && d.img3 == nil {
   148  		d.makeImg(mxx, myy)
   149  	}
   150  	if d.progressive {
   151  		for i := 0; i < nComp; i++ {
   152  			compIndex := scan[i].compIndex
   153  			if d.progCoeffs[compIndex] == nil {
   154  				d.progCoeffs[compIndex] = make([]block, mxx*myy*d.comp[compIndex].h*d.comp[compIndex].v)
   155  			}
   156  		}
   157  	}
   158  
   159  	d.bits = bits{}
   160  	mcu, expectedRST := 0, uint8(rst0Marker)
   161  	var (
   162  		// b is the decoded coefficients, in natural (not zig-zag) order.
   163  		b  block
   164  		dc [maxComponents]int32
   165  		// bx and by are the location of the current block, in units of 8x8
   166  		// blocks: the third block in the first row has (bx, by) = (2, 0).
   167  		bx, by     int
   168  		blockCount int
   169  	)
   170  	for my := 0; my < myy; my++ {
   171  		for mx := 0; mx < mxx; mx++ {
   172  			for i := 0; i < nComp; i++ {
   173  				compIndex := scan[i].compIndex
   174  				hi := d.comp[compIndex].h
   175  				vi := d.comp[compIndex].v
   176  				for j := 0; j < hi*vi; j++ {
   177  					// The blocks are traversed one MCU at a time. For 4:2:0 chroma
   178  					// subsampling, there are four Y 8x8 blocks in every 16x16 MCU.
   179  					//
   180  					// For a baseline 32x16 pixel image, the Y blocks visiting order is:
   181  					//	0 1 4 5
   182  					//	2 3 6 7
   183  					//
   184  					// For progressive images, the interleaved scans (those with nComp > 1)
   185  					// are traversed as above, but non-interleaved scans are traversed left
   186  					// to right, top to bottom:
   187  					//	0 1 2 3
   188  					//	4 5 6 7
   189  					// Only DC scans (zigStart == 0) can be interleaved. AC scans must have
   190  					// only one component.
   191  					//
   192  					// To further complicate matters, for non-interleaved scans, there is no
   193  					// data for any blocks that are inside the image at the MCU level but
   194  					// outside the image at the pixel level. For example, a 24x16 pixel 4:2:0
   195  					// progressive image consists of two 16x16 MCUs. The interleaved scans
   196  					// will process 8 Y blocks:
   197  					//	0 1 4 5
   198  					//	2 3 6 7
   199  					// The non-interleaved scans will process only 6 Y blocks:
   200  					//	0 1 2
   201  					//	3 4 5
   202  					if nComp != 1 {
   203  						bx = hi*mx + j%hi
   204  						by = vi*my + j/hi
   205  					} else {
   206  						q := mxx * hi
   207  						bx = blockCount % q
   208  						by = blockCount / q
   209  						blockCount++
   210  						if bx*8 >= d.width || by*8 >= d.height {
   211  							continue
   212  						}
   213  					}
   214  
   215  					// Load the previous partially decoded coefficients, if applicable.
   216  					if d.progressive {
   217  						b = d.progCoeffs[compIndex][by*mxx*hi+bx]
   218  					} else {
   219  						b = block{}
   220  					}
   221  
   222  					if ah != 0 {
   223  						if err := d.refine(&b, &d.huff[acTable][scan[i].ta], zigStart, zigEnd, 1<<al); err != nil {
   224  							return err
   225  						}
   226  					} else {
   227  						zig := zigStart
   228  						if zig == 0 {
   229  							zig++
   230  							// Decode the DC coefficient, as specified in section F.2.2.1.
   231  							value, err := d.decodeHuffman(&d.huff[dcTable][scan[i].td])
   232  							if err != nil {
   233  								return err
   234  							}
   235  							if value > 16 {
   236  								return UnsupportedError("excessive DC component")
   237  							}
   238  							dcDelta, err := d.receiveExtend(value)
   239  							if err != nil {
   240  								return err
   241  							}
   242  							dc[compIndex] += dcDelta
   243  							b[0] = dc[compIndex] << al
   244  						}
   245  
   246  						if zig <= zigEnd && d.eobRun > 0 {
   247  							d.eobRun--
   248  						} else {
   249  							// Decode the AC coefficients, as specified in section F.2.2.2.
   250  							huff := &d.huff[acTable][scan[i].ta]
   251  							for ; zig <= zigEnd; zig++ {
   252  								value, err := d.decodeHuffman(huff)
   253  								if err != nil {
   254  									return err
   255  								}
   256  								val0 := value >> 4
   257  								val1 := value & 0x0f
   258  								if val1 != 0 {
   259  									zig += int32(val0)
   260  									if zig > zigEnd {
   261  										break
   262  									}
   263  									ac, err := d.receiveExtend(val1)
   264  									if err != nil {
   265  										return err
   266  									}
   267  									b[unzig[zig]] = ac << al
   268  								} else {
   269  									if val0 != 0x0f {
   270  										d.eobRun = uint16(1 << val0)
   271  										if val0 != 0 {
   272  											bits, err := d.decodeBits(int32(val0))
   273  											if err != nil {
   274  												return err
   275  											}
   276  											d.eobRun |= uint16(bits)
   277  										}
   278  										d.eobRun--
   279  										break
   280  									}
   281  									zig += 0x0f
   282  								}
   283  							}
   284  						}
   285  					}
   286  
   287  					if d.progressive {
   288  						// Save the coefficients.
   289  						d.progCoeffs[compIndex][by*mxx*hi+bx] = b
   290  						// At this point, we could call reconstructBlock to dequantize and perform the
   291  						// inverse DCT, to save early stages of a progressive image to the *image.YCbCr
   292  						// buffers (the whole point of progressive encoding), but in Go, the jpeg.Decode
   293  						// function does not return until the entire image is decoded, so we "continue"
   294  						// here to avoid wasted computation. Instead, reconstructBlock is called on each
   295  						// accumulated block by the reconstructProgressiveImage method after all of the
   296  						// SOS markers are processed.
   297  						continue
   298  					}
   299  					if err := d.reconstructBlock(&b, bx, by, int(compIndex)); err != nil {
   300  						return err
   301  					}
   302  				} // for j
   303  			} // for i
   304  			mcu++
   305  			if d.ri > 0 && mcu%d.ri == 0 && mcu < mxx*myy {
   306  				// A more sophisticated decoder could use RST[0-7] markers to resynchronize from corrupt input,
   307  				// but this one assumes well-formed input, and hence the restart marker follows immediately.
   308  				if err := d.readFull(d.tmp[:2]); err != nil {
   309  					return err
   310  				}
   311  				if d.tmp[0] != 0xff || d.tmp[1] != expectedRST {
   312  					return FormatError("bad RST marker")
   313  				}
   314  				expectedRST++
   315  				if expectedRST == rst7Marker+1 {
   316  					expectedRST = rst0Marker
   317  				}
   318  				// Reset the Huffman decoder.
   319  				d.bits = bits{}
   320  				// Reset the DC components, as per section F.2.1.3.1.
   321  				dc = [maxComponents]int32{}
   322  				// Reset the progressive decoder state, as per section G.1.2.2.
   323  				d.eobRun = 0
   324  			}
   325  		} // for mx
   326  	} // for my
   327  
   328  	return nil
   329  }
   330  
   331  // refine decodes a successive approximation refinement block, as specified in
   332  // section G.1.2.
   333  func (d *decoder) refine(b *block, h *huffman, zigStart, zigEnd, delta int32) error {
   334  	// Refining a DC component is trivial.
   335  	if zigStart == 0 {
   336  		if zigEnd != 0 {
   337  			panic("unreachable")
   338  		}
   339  		bit, err := d.decodeBit()
   340  		if err != nil {
   341  			return err
   342  		}
   343  		if bit {
   344  			b[0] |= delta
   345  		}
   346  		return nil
   347  	}
   348  
   349  	// Refining AC components is more complicated; see sections G.1.2.2 and G.1.2.3.
   350  	zig := zigStart
   351  	if d.eobRun == 0 {
   352  	loop:
   353  		for ; zig <= zigEnd; zig++ {
   354  			z := int32(0)
   355  			value, err := d.decodeHuffman(h)
   356  			if err != nil {
   357  				return err
   358  			}
   359  			val0 := value >> 4
   360  			val1 := value & 0x0f
   361  
   362  			switch val1 {
   363  			case 0:
   364  				if val0 != 0x0f {
   365  					d.eobRun = uint16(1 << val0)
   366  					if val0 != 0 {
   367  						bits, err := d.decodeBits(int32(val0))
   368  						if err != nil {
   369  							return err
   370  						}
   371  						d.eobRun |= uint16(bits)
   372  					}
   373  					break loop
   374  				}
   375  			case 1:
   376  				z = delta
   377  				bit, err := d.decodeBit()
   378  				if err != nil {
   379  					return err
   380  				}
   381  				if !bit {
   382  					z = -z
   383  				}
   384  			default:
   385  				return FormatError("unexpected Huffman code")
   386  			}
   387  
   388  			zig, err = d.refineNonZeroes(b, zig, zigEnd, int32(val0), delta)
   389  			if err != nil {
   390  				return err
   391  			}
   392  			if zig > zigEnd {
   393  				return FormatError("too many coefficients")
   394  			}
   395  			if z != 0 {
   396  				b[unzig[zig]] = z
   397  			}
   398  		}
   399  	}
   400  	if d.eobRun > 0 {
   401  		d.eobRun--
   402  		if _, err := d.refineNonZeroes(b, zig, zigEnd, -1, delta); err != nil {
   403  			return err
   404  		}
   405  	}
   406  	return nil
   407  }
   408  
   409  // refineNonZeroes refines non-zero entries of b in zig-zag order. If nz >= 0,
   410  // the first nz zero entries are skipped over.
   411  func (d *decoder) refineNonZeroes(b *block, zig, zigEnd, nz, delta int32) (int32, error) {
   412  	for ; zig <= zigEnd; zig++ {
   413  		u := unzig[zig]
   414  		if b[u] == 0 {
   415  			if nz == 0 {
   416  				break
   417  			}
   418  			nz--
   419  			continue
   420  		}
   421  		bit, err := d.decodeBit()
   422  		if err != nil {
   423  			return 0, err
   424  		}
   425  		if !bit {
   426  			continue
   427  		}
   428  		if b[u] >= 0 {
   429  			b[u] += delta
   430  		} else {
   431  			b[u] -= delta
   432  		}
   433  	}
   434  	return zig, nil
   435  }
   436  
   437  func (d *decoder) reconstructProgressiveImage() error {
   438  	// The h0, mxx, by and bx variables have the same meaning as in the
   439  	// processSOS method.
   440  	h0 := d.comp[0].h
   441  	mxx := (d.width + 8*h0 - 1) / (8 * h0)
   442  	for i := 0; i < d.nComp; i++ {
   443  		if d.progCoeffs[i] == nil {
   444  			continue
   445  		}
   446  		v := 8 * d.comp[0].v / d.comp[i].v
   447  		h := 8 * d.comp[0].h / d.comp[i].h
   448  		stride := mxx * d.comp[i].h
   449  		for by := 0; by*v < d.height; by++ {
   450  			for bx := 0; bx*h < d.width; bx++ {
   451  				if err := d.reconstructBlock(&d.progCoeffs[i][by*stride+bx], bx, by, i); err != nil {
   452  					return err
   453  				}
   454  			}
   455  		}
   456  	}
   457  	return nil
   458  }
   459  
   460  // reconstructBlock dequantizes, performs the inverse DCT and stores the block
   461  // to the image.
   462  func (d *decoder) reconstructBlock(b *block, bx, by, compIndex int) error {
   463  	qt := &d.quant[d.comp[compIndex].tq]
   464  	for zig := 0; zig < blockSize; zig++ {
   465  		b[unzig[zig]] *= qt[zig]
   466  	}
   467  	idct(b)
   468  	dst, stride := []byte(nil), 0
   469  	if d.nComp == 1 {
   470  		dst, stride = d.img1.Pix[8*(by*d.img1.Stride+bx):], d.img1.Stride
   471  	} else {
   472  		switch compIndex {
   473  		case 0:
   474  			dst, stride = d.img3.Y[8*(by*d.img3.YStride+bx):], d.img3.YStride
   475  		case 1:
   476  			dst, stride = d.img3.Cb[8*(by*d.img3.CStride+bx):], d.img3.CStride
   477  		case 2:
   478  			dst, stride = d.img3.Cr[8*(by*d.img3.CStride+bx):], d.img3.CStride
   479  		case 3:
   480  			dst, stride = d.blackPix[8*(by*d.blackStride+bx):], d.blackStride
   481  		default:
   482  			return UnsupportedError("too many components")
   483  		}
   484  	}
   485  	// Level shift by +128, clip to [0, 255], and write to dst.
   486  	for y := 0; y < 8; y++ {
   487  		y8 := y * 8
   488  		yStride := y * stride
   489  		for x := 0; x < 8; x++ {
   490  			c := b[y8+x]
   491  			if c < -128 {
   492  				c = 0
   493  			} else if c > 127 {
   494  				c = 255
   495  			} else {
   496  				c += 128
   497  			}
   498  			dst[yStride+x] = uint8(c)
   499  		}
   500  	}
   501  	return nil
   502  }