github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/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  				qt := &d.quant[d.comp[compIndex].tq]
   177  				for j := 0; j < hi*vi; j++ {
   178  					// The blocks are traversed one MCU at a time. For 4:2:0 chroma
   179  					// subsampling, there are four Y 8x8 blocks in every 16x16 MCU.
   180  					//
   181  					// For a baseline 32x16 pixel image, the Y blocks visiting order is:
   182  					//	0 1 4 5
   183  					//	2 3 6 7
   184  					//
   185  					// For progressive images, the interleaved scans (those with nComp > 1)
   186  					// are traversed as above, but non-interleaved scans are traversed left
   187  					// to right, top to bottom:
   188  					//	0 1 2 3
   189  					//	4 5 6 7
   190  					// Only DC scans (zigStart == 0) can be interleaved. AC scans must have
   191  					// only one component.
   192  					//
   193  					// To further complicate matters, for non-interleaved scans, there is no
   194  					// data for any blocks that are inside the image at the MCU level but
   195  					// outside the image at the pixel level. For example, a 24x16 pixel 4:2:0
   196  					// progressive image consists of two 16x16 MCUs. The interleaved scans
   197  					// will process 8 Y blocks:
   198  					//	0 1 4 5
   199  					//	2 3 6 7
   200  					// The non-interleaved scans will process only 6 Y blocks:
   201  					//	0 1 2
   202  					//	3 4 5
   203  					if nComp != 1 {
   204  						bx = hi*mx + j%hi
   205  						by = vi*my + j/hi
   206  					} else {
   207  						q := mxx * hi
   208  						bx = blockCount % q
   209  						by = blockCount / q
   210  						blockCount++
   211  						if bx*8 >= d.width || by*8 >= d.height {
   212  							continue
   213  						}
   214  					}
   215  
   216  					// Load the previous partially decoded coefficients, if applicable.
   217  					if d.progressive {
   218  						b = d.progCoeffs[compIndex][by*mxx*hi+bx]
   219  					} else {
   220  						b = block{}
   221  					}
   222  
   223  					if ah != 0 {
   224  						if err := d.refine(&b, &d.huff[acTable][scan[i].ta], zigStart, zigEnd, 1<<al); err != nil {
   225  							return err
   226  						}
   227  					} else {
   228  						zig := zigStart
   229  						if zig == 0 {
   230  							zig++
   231  							// Decode the DC coefficient, as specified in section F.2.2.1.
   232  							value, err := d.decodeHuffman(&d.huff[dcTable][scan[i].td])
   233  							if err != nil {
   234  								return err
   235  							}
   236  							if value > 16 {
   237  								return UnsupportedError("excessive DC component")
   238  							}
   239  							dcDelta, err := d.receiveExtend(value)
   240  							if err != nil {
   241  								return err
   242  							}
   243  							dc[compIndex] += dcDelta
   244  							b[0] = dc[compIndex] << al
   245  						}
   246  
   247  						if zig <= zigEnd && d.eobRun > 0 {
   248  							d.eobRun--
   249  						} else {
   250  							// Decode the AC coefficients, as specified in section F.2.2.2.
   251  							huff := &d.huff[acTable][scan[i].ta]
   252  							for ; zig <= zigEnd; zig++ {
   253  								value, err := d.decodeHuffman(huff)
   254  								if err != nil {
   255  									return err
   256  								}
   257  								val0 := value >> 4
   258  								val1 := value & 0x0f
   259  								if val1 != 0 {
   260  									zig += int32(val0)
   261  									if zig > zigEnd {
   262  										break
   263  									}
   264  									ac, err := d.receiveExtend(val1)
   265  									if err != nil {
   266  										return err
   267  									}
   268  									b[unzig[zig]] = ac << al
   269  								} else {
   270  									if val0 != 0x0f {
   271  										d.eobRun = uint16(1 << val0)
   272  										if val0 != 0 {
   273  											bits, err := d.decodeBits(int32(val0))
   274  											if err != nil {
   275  												return err
   276  											}
   277  											d.eobRun |= uint16(bits)
   278  										}
   279  										d.eobRun--
   280  										break
   281  									}
   282  									zig += 0x0f
   283  								}
   284  							}
   285  						}
   286  					}
   287  
   288  					if d.progressive {
   289  						if zigEnd != blockSize-1 || al != 0 {
   290  							// We haven't completely decoded this 8x8 block. Save the coefficients.
   291  							d.progCoeffs[compIndex][by*mxx*hi+bx] = b
   292  							// At this point, we could execute the rest of the loop body to dequantize and
   293  							// perform the inverse DCT, to save early stages of a progressive image to the
   294  							// *image.YCbCr buffers (the whole point of progressive encoding), but in Go,
   295  							// the jpeg.Decode function does not return until the entire image is decoded,
   296  							// so we "continue" here to avoid wasted computation.
   297  							continue
   298  						}
   299  					}
   300  
   301  					// Dequantize, perform the inverse DCT and store the block to the image.
   302  					for zig := 0; zig < blockSize; zig++ {
   303  						b[unzig[zig]] *= qt[zig]
   304  					}
   305  					idct(&b)
   306  					dst, stride := []byte(nil), 0
   307  					if d.nComp == 1 {
   308  						dst, stride = d.img1.Pix[8*(by*d.img1.Stride+bx):], d.img1.Stride
   309  					} else {
   310  						switch compIndex {
   311  						case 0:
   312  							dst, stride = d.img3.Y[8*(by*d.img3.YStride+bx):], d.img3.YStride
   313  						case 1:
   314  							dst, stride = d.img3.Cb[8*(by*d.img3.CStride+bx):], d.img3.CStride
   315  						case 2:
   316  							dst, stride = d.img3.Cr[8*(by*d.img3.CStride+bx):], d.img3.CStride
   317  						case 3:
   318  							dst, stride = d.blackPix[8*(by*d.blackStride+bx):], d.blackStride
   319  						default:
   320  							return UnsupportedError("too many components")
   321  						}
   322  					}
   323  					// Level shift by +128, clip to [0, 255], and write to dst.
   324  					for y := 0; y < 8; y++ {
   325  						y8 := y * 8
   326  						yStride := y * stride
   327  						for x := 0; x < 8; x++ {
   328  							c := b[y8+x]
   329  							if c < -128 {
   330  								c = 0
   331  							} else if c > 127 {
   332  								c = 255
   333  							} else {
   334  								c += 128
   335  							}
   336  							dst[yStride+x] = uint8(c)
   337  						}
   338  					}
   339  				} // for j
   340  			} // for i
   341  			mcu++
   342  			if d.ri > 0 && mcu%d.ri == 0 && mcu < mxx*myy {
   343  				// A more sophisticated decoder could use RST[0-7] markers to resynchronize from corrupt input,
   344  				// but this one assumes well-formed input, and hence the restart marker follows immediately.
   345  				if err := d.readFull(d.tmp[:2]); err != nil {
   346  					return err
   347  				}
   348  				if d.tmp[0] != 0xff || d.tmp[1] != expectedRST {
   349  					return FormatError("bad RST marker")
   350  				}
   351  				expectedRST++
   352  				if expectedRST == rst7Marker+1 {
   353  					expectedRST = rst0Marker
   354  				}
   355  				// Reset the Huffman decoder.
   356  				d.bits = bits{}
   357  				// Reset the DC components, as per section F.2.1.3.1.
   358  				dc = [maxComponents]int32{}
   359  				// Reset the progressive decoder state, as per section G.1.2.2.
   360  				d.eobRun = 0
   361  			}
   362  		} // for mx
   363  	} // for my
   364  
   365  	return nil
   366  }
   367  
   368  // refine decodes a successive approximation refinement block, as specified in
   369  // section G.1.2.
   370  func (d *decoder) refine(b *block, h *huffman, zigStart, zigEnd, delta int32) error {
   371  	// Refining a DC component is trivial.
   372  	if zigStart == 0 {
   373  		if zigEnd != 0 {
   374  			panic("unreachable")
   375  		}
   376  		bit, err := d.decodeBit()
   377  		if err != nil {
   378  			return err
   379  		}
   380  		if bit {
   381  			b[0] |= delta
   382  		}
   383  		return nil
   384  	}
   385  
   386  	// Refining AC components is more complicated; see sections G.1.2.2 and G.1.2.3.
   387  	zig := zigStart
   388  	if d.eobRun == 0 {
   389  	loop:
   390  		for ; zig <= zigEnd; zig++ {
   391  			z := int32(0)
   392  			value, err := d.decodeHuffman(h)
   393  			if err != nil {
   394  				return err
   395  			}
   396  			val0 := value >> 4
   397  			val1 := value & 0x0f
   398  
   399  			switch val1 {
   400  			case 0:
   401  				if val0 != 0x0f {
   402  					d.eobRun = uint16(1 << val0)
   403  					if val0 != 0 {
   404  						bits, err := d.decodeBits(int32(val0))
   405  						if err != nil {
   406  							return err
   407  						}
   408  						d.eobRun |= uint16(bits)
   409  					}
   410  					break loop
   411  				}
   412  			case 1:
   413  				z = delta
   414  				bit, err := d.decodeBit()
   415  				if err != nil {
   416  					return err
   417  				}
   418  				if !bit {
   419  					z = -z
   420  				}
   421  			default:
   422  				return FormatError("unexpected Huffman code")
   423  			}
   424  
   425  			zig, err = d.refineNonZeroes(b, zig, zigEnd, int32(val0), delta)
   426  			if err != nil {
   427  				return err
   428  			}
   429  			if zig > zigEnd {
   430  				return FormatError("too many coefficients")
   431  			}
   432  			if z != 0 {
   433  				b[unzig[zig]] = z
   434  			}
   435  		}
   436  	}
   437  	if d.eobRun > 0 {
   438  		d.eobRun--
   439  		if _, err := d.refineNonZeroes(b, zig, zigEnd, -1, delta); err != nil {
   440  			return err
   441  		}
   442  	}
   443  	return nil
   444  }
   445  
   446  // refineNonZeroes refines non-zero entries of b in zig-zag order. If nz >= 0,
   447  // the first nz zero entries are skipped over.
   448  func (d *decoder) refineNonZeroes(b *block, zig, zigEnd, nz, delta int32) (int32, error) {
   449  	for ; zig <= zigEnd; zig++ {
   450  		u := unzig[zig]
   451  		if b[u] == 0 {
   452  			if nz == 0 {
   453  				break
   454  			}
   455  			nz--
   456  			continue
   457  		}
   458  		bit, err := d.decodeBit()
   459  		if err != nil {
   460  			return 0, err
   461  		}
   462  		if !bit {
   463  			continue
   464  		}
   465  		if b[u] >= 0 {
   466  			b[u] += delta
   467  		} else {
   468  			b[u] -= delta
   469  		}
   470  	}
   471  	return zig, nil
   472  }