func (d *decoder) readImagePass(r io.Reader, pass int, allocateOnly bool) (image.Image, error) {
	bitsPerPixel := 0
	pixOffset := 0
	var (
		gray     *image.Gray
		rgba     *image.RGBA
		paletted *image.Paletted
		nrgba    *image.NRGBA
		gray16   *image.Gray16
		rgba64   *image.RGBA64
		nrgba64  *image.NRGBA64
		img      image.Image
	)
	width, height := d.width, d.height
	if d.interlace == itAdam7 && !allocateOnly {
		p := interlacing[pass]
		// Add the multiplication factor and subtract one, effectively rounding up.
		width = (width - p.xOffset + p.xFactor - 1) / p.xFactor
		height = (height - p.yOffset + p.yFactor - 1) / p.yFactor
		// A PNG image can't have zero width or height, but for an interlaced
		// image, an individual pass might have zero width or height. If so, we
		// shouldn't even read a per-row filter type byte, so return early.
		if width == 0 || height == 0 {
			return nil, nil
		}
	}
	switch d.cb {
	case cbG1, cbG2, cbG4, cbG8:
		bitsPerPixel = d.depth
		if d.useTransparent {
			nrgba = image.NewNRGBA(image.Rect(0, 0, width, height))
			img = nrgba
		} else {
			gray = image.NewGray(image.Rect(0, 0, width, height))
			img = gray
		}
	case cbGA8:
		bitsPerPixel = 16
		nrgba = image.NewNRGBA(image.Rect(0, 0, width, height))
		img = nrgba
	case cbTC8:
		bitsPerPixel = 24
		if d.useTransparent {
			nrgba = image.NewNRGBA(image.Rect(0, 0, width, height))
			img = nrgba
		} else {
			rgba = image.NewRGBA(image.Rect(0, 0, width, height))
			img = rgba
		}
	case cbP1, cbP2, cbP4, cbP8:
		bitsPerPixel = d.depth
		paletted = image.NewPaletted(image.Rect(0, 0, width, height), d.palette)
		img = paletted
	case cbTCA8:
		bitsPerPixel = 32
		nrgba = image.NewNRGBA(image.Rect(0, 0, width, height))
		img = nrgba
	case cbG16:
		bitsPerPixel = 16
		if d.useTransparent {
			nrgba64 = image.NewNRGBA64(image.Rect(0, 0, width, height))
			img = nrgba64
		} else {
			gray16 = image.NewGray16(image.Rect(0, 0, width, height))
			img = gray16
		}
	case cbGA16:
		bitsPerPixel = 32
		nrgba64 = image.NewNRGBA64(image.Rect(0, 0, width, height))
		img = nrgba64
	case cbTC16:
		bitsPerPixel = 48
		if d.useTransparent {
			nrgba64 = image.NewNRGBA64(image.Rect(0, 0, width, height))
			img = nrgba64
		} else {
			rgba64 = image.NewRGBA64(image.Rect(0, 0, width, height))
			img = rgba64
		}
	case cbTCA16:
		bitsPerPixel = 64
		nrgba64 = image.NewNRGBA64(image.Rect(0, 0, width, height))
		img = nrgba64
	}
	if allocateOnly {
		return img, nil
	}
	bytesPerPixel := (bitsPerPixel + 7) / 8

	// The +1 is for the per-row filter type, which is at cr[0].
	rowSize := 1 + (bitsPerPixel*width+7)/8
	// cr and pr are the bytes for the current and previous row.
	cr := make([]uint8, rowSize)
	pr := make([]uint8, rowSize)

	for y := 0; y < height; y++ {
		// Read the decompressed bytes.
		_, err := io.ReadFull(r, cr)
		if err != nil {
			if err == io.EOF || err == io.ErrUnexpectedEOF {
				return nil, FormatError("not enough pixel data")
			}
			return nil, err
		}

		// Apply the filter.
		cdat := cr[1:]
		pdat := pr[1:]
		switch cr[0] {
		case ftNone:
			// No-op.
		case ftSub:
			for i := bytesPerPixel; i < len(cdat); i++ {
				cdat[i] += cdat[i-bytesPerPixel]
			}
		case ftUp:
			for i, p := range pdat {
				cdat[i] += p
			}
		case ftAverage:
			// The first column has no column to the left of it, so it is a
			// special case. We know that the first column exists because we
			// check above that width != 0, and so len(cdat) != 0.
			for i := 0; i < bytesPerPixel; i++ {
				cdat[i] += pdat[i] / 2
			}
			for i := bytesPerPixel; i < len(cdat); i++ {
				cdat[i] += uint8((int(cdat[i-bytesPerPixel]) + int(pdat[i])) / 2)
			}
		case ftPaeth:
			filterPaeth(cdat, pdat, bytesPerPixel)
		default:
			return nil, FormatError("bad filter type")
		}

		// Convert from bytes to colors.
		switch d.cb {
		case cbG1:
			if d.useTransparent {
				ty := d.transparent[1]
				for x := 0; x < width; x += 8 {
					b := cdat[x/8]
					for x2 := 0; x2 < 8 && x+x2 < width; x2++ {
						ycol := (b >> 7) * 0xff
						acol := uint8(0xff)
						if ycol == ty {
							acol = 0x00
						}
						nrgba.SetNRGBA(x+x2, y, color.NRGBA{ycol, ycol, ycol, acol})
						b <<= 1
					}
				}
			} else {
				for x := 0; x < width; x += 8 {
					b := cdat[x/8]
					for x2 := 0; x2 < 8 && x+x2 < width; x2++ {
						gray.SetGray(x+x2, y, color.Gray{(b >> 7) * 0xff})
						b <<= 1
					}
				}
			}
		case cbG2:
			if d.useTransparent {
				ty := d.transparent[1]
				for x := 0; x < width; x += 4 {
					b := cdat[x/4]
					for x2 := 0; x2 < 4 && x+x2 < width; x2++ {
						ycol := (b >> 6) * 0x55
						acol := uint8(0xff)
						if ycol == ty {
							acol = 0x00
						}
						nrgba.SetNRGBA(x+x2, y, color.NRGBA{ycol, ycol, ycol, acol})
						b <<= 2
					}
				}
			} else {
				for x := 0; x < width; x += 4 {
					b := cdat[x/4]
					for x2 := 0; x2 < 4 && x+x2 < width; x2++ {
						gray.SetGray(x+x2, y, color.Gray{(b >> 6) * 0x55})
						b <<= 2
					}
				}
			}
		case cbG4:
			if d.useTransparent {
				ty := d.transparent[1]
				for x := 0; x < width; x += 2 {
					b := cdat[x/2]
					for x2 := 0; x2 < 2 && x+x2 < width; x2++ {
						ycol := (b >> 4) * 0x11
						acol := uint8(0xff)
						if ycol == ty {
							acol = 0x00
						}
						nrgba.SetNRGBA(x+x2, y, color.NRGBA{ycol, ycol, ycol, acol})
						b <<= 4
					}
				}
			} else {
				for x := 0; x < width; x += 2 {
					b := cdat[x/2]
					for x2 := 0; x2 < 2 && x+x2 < width; x2++ {
						gray.SetGray(x+x2, y, color.Gray{(b >> 4) * 0x11})
						b <<= 4
					}
				}
			}
		case cbG8:
			if d.useTransparent {
				ty := d.transparent[1]
				for x := 0; x < width; x++ {
					ycol := cdat[x]
					acol := uint8(0xff)
					if ycol == ty {
						acol = 0x00
					}
					nrgba.SetNRGBA(x, y, color.NRGBA{ycol, ycol, ycol, acol})
				}
			} else {
				copy(gray.Pix[pixOffset:], cdat)
				pixOffset += gray.Stride
			}
		case cbGA8:
			for x := 0; x < width; x++ {
				ycol := cdat[2*x+0]
				nrgba.SetNRGBA(x, y, color.NRGBA{ycol, ycol, ycol, cdat[2*x+1]})
			}
		case cbTC8:
			if d.useTransparent {
				pix, i, j := nrgba.Pix, pixOffset, 0
				tr, tg, tb := d.transparent[1], d.transparent[3], d.transparent[5]
				for x := 0; x < width; x++ {
					r := cdat[j+0]
					g := cdat[j+1]
					b := cdat[j+2]
					a := uint8(0xff)
					if r == tr && g == tg && b == tb {
						a = 0x00
					}
					pix[i+0] = r
					pix[i+1] = g
					pix[i+2] = b
					pix[i+3] = a
					i += 4
					j += 3
				}
				pixOffset += nrgba.Stride
			} else {
				pix, i, j := rgba.Pix, pixOffset, 0
				for x := 0; x < width; x++ {
					pix[i+0] = cdat[j+0]
					pix[i+1] = cdat[j+1]
					pix[i+2] = cdat[j+2]
					pix[i+3] = 0xff
					i += 4
					j += 3
				}
				pixOffset += rgba.Stride
			}
		case cbP1:
			for x := 0; x < width; x += 8 {
				b := cdat[x/8]
				for x2 := 0; x2 < 8 && x+x2 < width; x2++ {
					idx := b >> 7
					if len(paletted.Palette) <= int(idx) {
						paletted.Palette = paletted.Palette[:int(idx)+1]
					}
					paletted.SetColorIndex(x+x2, y, idx)
					b <<= 1
				}
			}
		case cbP2:
			for x := 0; x < width; x += 4 {
				b := cdat[x/4]
				for x2 := 0; x2 < 4 && x+x2 < width; x2++ {
					idx := b >> 6
					if len(paletted.Palette) <= int(idx) {
						paletted.Palette = paletted.Palette[:int(idx)+1]
					}
					paletted.SetColorIndex(x+x2, y, idx)
					b <<= 2
				}
			}
		case cbP4:
			for x := 0; x < width; x += 2 {
				b := cdat[x/2]
				for x2 := 0; x2 < 2 && x+x2 < width; x2++ {
					idx := b >> 4
					if len(paletted.Palette) <= int(idx) {
						paletted.Palette = paletted.Palette[:int(idx)+1]
					}
					paletted.SetColorIndex(x+x2, y, idx)
					b <<= 4
				}
			}
		case cbP8:
			if len(paletted.Palette) != 255 {
				for x := 0; x < width; x++ {
					if len(paletted.Palette) <= int(cdat[x]) {
						paletted.Palette = paletted.Palette[:int(cdat[x])+1]
					}
				}
			}
			copy(paletted.Pix[pixOffset:], cdat)
			pixOffset += paletted.Stride
		case cbTCA8:
			copy(nrgba.Pix[pixOffset:], cdat)
			pixOffset += nrgba.Stride
		case cbG16:
			if d.useTransparent {
				ty := uint16(d.transparent[0])<<8 | uint16(d.transparent[1])
				for x := 0; x < width; x++ {
					ycol := uint16(cdat[2*x+0])<<8 | uint16(cdat[2*x+1])
					acol := uint16(0xffff)
					if ycol == ty {
						acol = 0x0000
					}
					nrgba64.SetNRGBA64(x, y, color.NRGBA64{ycol, ycol, ycol, acol})
				}
			} else {
				for x := 0; x < width; x++ {
					ycol := uint16(cdat[2*x+0])<<8 | uint16(cdat[2*x+1])
					gray16.SetGray16(x, y, color.Gray16{ycol})
				}
			}
		case cbGA16:
			for x := 0; x < width; x++ {
				ycol := uint16(cdat[4*x+0])<<8 | uint16(cdat[4*x+1])
				acol := uint16(cdat[4*x+2])<<8 | uint16(cdat[4*x+3])
				nrgba64.SetNRGBA64(x, y, color.NRGBA64{ycol, ycol, ycol, acol})
			}
		case cbTC16:
			if d.useTransparent {
				tr := uint16(d.transparent[0])<<8 | uint16(d.transparent[1])
				tg := uint16(d.transparent[2])<<8 | uint16(d.transparent[3])
				tb := uint16(d.transparent[4])<<8 | uint16(d.transparent[5])
				for x := 0; x < width; x++ {
					rcol := uint16(cdat[6*x+0])<<8 | uint16(cdat[6*x+1])
					gcol := uint16(cdat[6*x+2])<<8 | uint16(cdat[6*x+3])
					bcol := uint16(cdat[6*x+4])<<8 | uint16(cdat[6*x+5])
					acol := uint16(0xffff)
					if rcol == tr && gcol == tg && bcol == tb {
						acol = 0x0000
					}
					nrgba64.SetNRGBA64(x, y, color.NRGBA64{rcol, gcol, bcol, acol})
				}
			} else {
				for x := 0; x < width; x++ {
					rcol := uint16(cdat[6*x+0])<<8 | uint16(cdat[6*x+1])
					gcol := uint16(cdat[6*x+2])<<8 | uint16(cdat[6*x+3])
					bcol := uint16(cdat[6*x+4])<<8 | uint16(cdat[6*x+5])
					rgba64.SetRGBA64(x, y, color.RGBA64{rcol, gcol, bcol, 0xffff})
				}
			}
		case cbTCA16:
			for x := 0; x < width; x++ {
				rcol := uint16(cdat[8*x+0])<<8 | uint16(cdat[8*x+1])
				gcol := uint16(cdat[8*x+2])<<8 | uint16(cdat[8*x+3])
				bcol := uint16(cdat[8*x+4])<<8 | uint16(cdat[8*x+5])
				acol := uint16(cdat[8*x+6])<<8 | uint16(cdat[8*x+7])
				nrgba64.SetNRGBA64(x, y, color.NRGBA64{rcol, gcol, bcol, acol})
			}
		}

		// The current row for y is the previous row for y+1.
		pr, cr = cr, pr
	}

	return img, nil
}
func (d *decoder) readImagePass(r io.Reader, pass int, allocateOnly bool) (image.Image, error) {
	bitsPerPixel := 0
	pixOffset := 0
	var (
		gray     *image.Gray
		rgba     *image.RGBA
		paletted *image.Paletted
		nrgba    *image.NRGBA
		gray16   *image.Gray16
		rgba64   *image.RGBA64
		nrgba64  *image.NRGBA64
		img      image.Image
	)
	width, height := d.width, d.height
	if d.interlace == itAdam7 && !allocateOnly {
		p := interlacing[pass]
		
		width = (width - p.xOffset + p.xFactor - 1) / p.xFactor
		height = (height - p.yOffset + p.yFactor - 1) / p.yFactor
		
		
		
		if width == 0 || height == 0 {
			return nil, nil
		}
	}
	switch d.cb {
	case cbG1, cbG2, cbG4, cbG8:
		bitsPerPixel = d.depth
		if d.useTransparent {
			nrgba = image.NewNRGBA(image.Rect(0, 0, width, height))
			img = nrgba
		} else {
			gray = image.NewGray(image.Rect(0, 0, width, height))
			img = gray
		}
	case cbGA8:
		bitsPerPixel = 16
		nrgba = image.NewNRGBA(image.Rect(0, 0, width, height))
		img = nrgba
	case cbTC8:
		bitsPerPixel = 24
		if d.useTransparent {
			nrgba = image.NewNRGBA(image.Rect(0, 0, width, height))
			img = nrgba
		} else {
			rgba = image.NewRGBA(image.Rect(0, 0, width, height))
			img = rgba
		}
	case cbP1, cbP2, cbP4, cbP8:
		bitsPerPixel = d.depth
		paletted = image.NewPaletted(image.Rect(0, 0, width, height), d.palette)
		img = paletted
	case cbTCA8:
		bitsPerPixel = 32
		nrgba = image.NewNRGBA(image.Rect(0, 0, width, height))
		img = nrgba
	case cbG16:
		bitsPerPixel = 16
		if d.useTransparent {
			nrgba64 = image.NewNRGBA64(image.Rect(0, 0, width, height))
			img = nrgba64
		} else {
			gray16 = image.NewGray16(image.Rect(0, 0, width, height))
			img = gray16
		}
	case cbGA16:
		bitsPerPixel = 32
		nrgba64 = image.NewNRGBA64(image.Rect(0, 0, width, height))
		img = nrgba64
	case cbTC16:
		bitsPerPixel = 48
		if d.useTransparent {
			nrgba64 = image.NewNRGBA64(image.Rect(0, 0, width, height))
			img = nrgba64
		} else {
			rgba64 = image.NewRGBA64(image.Rect(0, 0, width, height))
			img = rgba64
		}
	case cbTCA16:
		bitsPerPixel = 64
		nrgba64 = image.NewNRGBA64(image.Rect(0, 0, width, height))
		img = nrgba64
	}
	if allocateOnly {
		return img, nil
	}
	bytesPerPixel := (bitsPerPixel + 7) / 8

	
	rowSize := 1 + (bitsPerPixel*width+7)/8
	
	cr := make([]uint8, rowSize)
	pr := make([]uint8, rowSize)

	for y := 0; y < height; y++ {
		
		_, err := io.ReadFull(r, cr)
		if err != nil {
			if err == io.EOF || err == io.ErrUnexpectedEOF {
				return nil, FormatError("not enough pixel data")
			}
			return nil, err
		}

		
		cdat := cr[1:]
		pdat := pr[1:]
		switch cr[0] {
		case ftNone:
			
		case ftSub:
			for i := bytesPerPixel; i < len(cdat); i++ {
				cdat[i] += cdat[i-bytesPerPixel]
			}
		case ftUp:
			for i, p := range pdat {
				cdat[i] += p
			}
		case ftAverage:
			
			
			
			for i := 0; i < bytesPerPixel; i++ {
				cdat[i] += pdat[i] / 2
			}
			for i := bytesPerPixel; i < len(cdat); i++ {
				cdat[i] += uint8((int(cdat[i-bytesPerPixel]) + int(pdat[i])) / 2)
			}
		case ftPaeth:
			filterPaeth(cdat, pdat, bytesPerPixel)
		default:
			return nil, FormatError("bad filter type")
		}

		
		switch d.cb {
		case cbG1:
			if d.useTransparent {
				ty := d.transparent[1]
				for x := 0; x < width; x += 8 {
					b := cdat[x/8]
					for x2 := 0; x2 < 8 && x+x2 < width; x2++ {
						ycol := (b >> 7) * 0xff
						acol := uint8(0xff)
						if ycol == ty {
							acol = 0x00
						}
						nrgba.SetNRGBA(x+x2, y, color.NRGBA{ycol, ycol, ycol, acol})
						b <<= 1
					}
				}
			} else {
				for x := 0; x < width; x += 8 {
					b := cdat[x/8]
					for x2 := 0; x2 < 8 && x+x2 < width; x2++ {
						gray.SetGray(x+x2, y, color.Gray{(b >> 7) * 0xff})
						b <<= 1
					}
				}
			}
		case cbG2:
			if d.useTransparent {
				ty := d.transparent[1]
				for x := 0; x < width; x += 4 {
					b := cdat[x/4]
					for x2 := 0; x2 < 4 && x+x2 < width; x2++ {
						ycol := (b >> 6) * 0x55
						acol := uint8(0xff)
						if ycol == ty {
							acol = 0x00
						}
						nrgba.SetNRGBA(x+x2, y, color.NRGBA{ycol, ycol, ycol, acol})
						b <<= 2
					}
				}
			} else {
				for x := 0; x < width; x += 4 {
					b := cdat[x/4]
					for x2 := 0; x2 < 4 && x+x2 < width; x2++ {
						gray.SetGray(x+x2, y, color.Gray{(b >> 6) * 0x55})
						b <<= 2
					}
				}
			}
		case cbG4:
			if d.useTransparent {
				ty := d.transparent[1]
				for x := 0; x < width; x += 2 {
					b := cdat[x/2]
					for x2 := 0; x2 < 2 && x+x2 < width; x2++ {
						ycol := (b >> 4) * 0x11
						acol := uint8(0xff)
						if ycol == ty {
							acol = 0x00
						}
						nrgba.SetNRGBA(x+x2, y, color.NRGBA{ycol, ycol, ycol, acol})
						b <<= 4
					}
				}
			} else {
				for x := 0; x < width; x += 2 {
					b := cdat[x/2]
					for x2 := 0; x2 < 2 && x+x2 < width; x2++ {
						gray.SetGray(x+x2, y, color.Gray{(b >> 4) * 0x11})
						b <<= 4
					}
				}
			}
		case cbG8:
			if d.useTransparent {
				ty := d.transparent[1]
				for x := 0; x < width; x++ {
					ycol := cdat[x]
					acol := uint8(0xff)
					if ycol == ty {
						acol = 0x00
					}
					nrgba.SetNRGBA(x, y, color.NRGBA{ycol, ycol, ycol, acol})
				}
			} else {
				copy(gray.Pix[pixOffset:], cdat)
				pixOffset += gray.Stride
			}
		case cbGA8:
			for x := 0; x < width; x++ {
				ycol := cdat[2*x+0]
				nrgba.SetNRGBA(x, y, color.NRGBA{ycol, ycol, ycol, cdat[2*x+1]})
			}
		case cbTC8:
			if d.useTransparent {
				pix, i, j := nrgba.Pix, pixOffset, 0
				tr, tg, tb := d.transparent[1], d.transparent[3], d.transparent[5]
				for x := 0; x < width; x++ {
					r := cdat[j+0]
					g := cdat[j+1]
					b := cdat[j+2]
					a := uint8(0xff)
					if r == tr && g == tg && b == tb {
						a = 0x00
					}
					pix[i+0] = r
					pix[i+1] = g
					pix[i+2] = b
					pix[i+3] = a
					i += 4
					j += 3
				}
				pixOffset += nrgba.Stride
			} else {
				pix, i, j := rgba.Pix, pixOffset, 0
				for x := 0; x < width; x++ {
					pix[i+0] = cdat[j+0]
					pix[i+1] = cdat[j+1]
					pix[i+2] = cdat[j+2]
					pix[i+3] = 0xff
					i += 4
					j += 3
				}
				pixOffset += rgba.Stride
			}
		case cbP1:
			for x := 0; x < width; x += 8 {
				b := cdat[x/8]
				for x2 := 0; x2 < 8 && x+x2 < width; x2++ {
					idx := b >> 7
					if len(paletted.Palette) <= int(idx) {
						paletted.Palette = paletted.Palette[:int(idx)+1]
					}
					paletted.SetColorIndex(x+x2, y, idx)
					b <<= 1
				}
			}
		case cbP2:
			for x := 0; x < width; x += 4 {
				b := cdat[x/4]
				for x2 := 0; x2 < 4 && x+x2 < width; x2++ {
					idx := b >> 6
					if len(paletted.Palette) <= int(idx) {
						paletted.Palette = paletted.Palette[:int(idx)+1]
					}
					paletted.SetColorIndex(x+x2, y, idx)
					b <<= 2
				}
			}
		case cbP4:
			for x := 0; x < width; x += 2 {
				b := cdat[x/2]
				for x2 := 0; x2 < 2 && x+x2 < width; x2++ {
					idx := b >> 4
					if len(paletted.Palette) <= int(idx) {
						paletted.Palette = paletted.Palette[:int(idx)+1]
					}
					paletted.SetColorIndex(x+x2, y, idx)
					b <<= 4
				}
			}
		case cbP8:
			if len(paletted.Palette) != 255 {
				for x := 0; x < width; x++ {
					if len(paletted.Palette) <= int(cdat[x]) {
						paletted.Palette = paletted.Palette[:int(cdat[x])+1]
					}
				}
			}
			copy(paletted.Pix[pixOffset:], cdat)
			pixOffset += paletted.Stride
		case cbTCA8:
			copy(nrgba.Pix[pixOffset:], cdat)
			pixOffset += nrgba.Stride
		case cbG16:
			if d.useTransparent {
				ty := uint16(d.transparent[0])<<8 | uint16(d.transparent[1])
				for x := 0; x < width; x++ {
					ycol := uint16(cdat[2*x+0])<<8 | uint16(cdat[2*x+1])
					acol := uint16(0xffff)
					if ycol == ty {
						acol = 0x0000
					}
					nrgba64.SetNRGBA64(x, y, color.NRGBA64{ycol, ycol, ycol, acol})
				}
			} else {
				for x := 0; x < width; x++ {
					ycol := uint16(cdat[2*x+0])<<8 | uint16(cdat[2*x+1])
					gray16.SetGray16(x, y, color.Gray16{ycol})
				}
			}
		case cbGA16:
			for x := 0; x < width; x++ {
				ycol := uint16(cdat[4*x+0])<<8 | uint16(cdat[4*x+1])
				acol := uint16(cdat[4*x+2])<<8 | uint16(cdat[4*x+3])
				nrgba64.SetNRGBA64(x, y, color.NRGBA64{ycol, ycol, ycol, acol})
			}
		case cbTC16:
			if d.useTransparent {
				tr := uint16(d.transparent[0])<<8 | uint16(d.transparent[1])
				tg := uint16(d.transparent[2])<<8 | uint16(d.transparent[3])
				tb := uint16(d.transparent[4])<<8 | uint16(d.transparent[5])
				for x := 0; x < width; x++ {
					rcol := uint16(cdat[6*x+0])<<8 | uint16(cdat[6*x+1])
					gcol := uint16(cdat[6*x+2])<<8 | uint16(cdat[6*x+3])
					bcol := uint16(cdat[6*x+4])<<8 | uint16(cdat[6*x+5])
					acol := uint16(0xffff)
					if rcol == tr && gcol == tg && bcol == tb {
						acol = 0x0000
					}
					nrgba64.SetNRGBA64(x, y, color.NRGBA64{rcol, gcol, bcol, acol})
				}
			} else {
				for x := 0; x < width; x++ {
					rcol := uint16(cdat[6*x+0])<<8 | uint16(cdat[6*x+1])
					gcol := uint16(cdat[6*x+2])<<8 | uint16(cdat[6*x+3])
					bcol := uint16(cdat[6*x+4])<<8 | uint16(cdat[6*x+5])
					rgba64.SetRGBA64(x, y, color.RGBA64{rcol, gcol, bcol, 0xffff})
				}
			}
		case cbTCA16:
			for x := 0; x < width; x++ {
				rcol := uint16(cdat[8*x+0])<<8 | uint16(cdat[8*x+1])
				gcol := uint16(cdat[8*x+2])<<8 | uint16(cdat[8*x+3])
				bcol := uint16(cdat[8*x+4])<<8 | uint16(cdat[8*x+5])
				acol := uint16(cdat[8*x+6])<<8 | uint16(cdat[8*x+7])
				nrgba64.SetNRGBA64(x, y, color.NRGBA64{rcol, gcol, bcol, acol})
			}
		}

		
		pr, cr = cr, pr
	}

	return img, nil
}
func ( d * decoder ) readImagePass ( r io . Reader , pass int , allocateOnly bool ) ( image . Image , error ) { bitsPerPixel := 0 pixOffset := 0 var ( gray * image . Gray rgba * image . RGBA paletted * image . Paletted nrgba * image . NRGBA gray16 * image . Gray16 rgba64 * image . RGBA64 nrgba64 * image . NRGBA64 img image . Image ) width , height := d . width , d . height if d . interlace == itAdam7 && ! allocateOnly { p := interlacing [ pass ] // Add the multiplication factor and subtract one, effectively rounding up. width = ( width - p . xOffset + p . xFactor - 1 ) / p . xFactor height = ( height - p . yOffset + p . yFactor - 1 ) / p . yFactor // A PNG image can't have zero width or height, but for an interlaced // image, an individual pass might have zero width or height. If so, we // shouldn't even read a per-row filter type byte, so return early. if width == 0 || height == 0 { return nil , nil } } switch d . cb { case cbG1 , cbG2 , cbG4 , cbG8 : bitsPerPixel = d . depth if d . useTransparent { nrgba = image . NewNRGBA ( image . Rect ( 0 , 0 , width , height ) ) img = nrgba } else { gray = image . NewGray ( image . Rect ( 0 , 0 , width , height ) ) img = gray } case cbGA8 : bitsPerPixel = 16 nrgba = image . NewNRGBA ( image . Rect ( 0 , 0 , width , height ) ) img = nrgba case cbTC8 : bitsPerPixel = 24 if d . useTransparent { nrgba = image . NewNRGBA ( image . Rect ( 0 , 0 , width , height ) ) img = nrgba } else { rgba = image . NewRGBA ( image . Rect ( 0 , 0 , width , height ) ) img = rgba } case cbP1 , cbP2 , cbP4 , cbP8 : bitsPerPixel = d . depth paletted = image . NewPaletted ( image . Rect ( 0 , 0 , width , height ) , d . palette ) img = paletted case cbTCA8 : bitsPerPixel = 32 nrgba = image . NewNRGBA ( image . Rect ( 0 , 0 , width , height ) ) img = nrgba case cbG16 : bitsPerPixel = 16 if d . useTransparent { nrgba64 = image . NewNRGBA64 ( image . Rect ( 0 , 0 , width , height ) ) img = nrgba64 } else { gray16 = image . NewGray16 ( image . Rect ( 0 , 0 , width , height ) ) img = gray16 } case cbGA16 : bitsPerPixel = 32 nrgba64 = image . NewNRGBA64 ( image . Rect ( 0 , 0 , width , height ) ) img = nrgba64 case cbTC16 : bitsPerPixel = 48 if d . useTransparent { nrgba64 = image . NewNRGBA64 ( image . Rect ( 0 , 0 , width , height ) ) img = nrgba64 } else { rgba64 = image . NewRGBA64 ( image . Rect ( 0 , 0 , width , height ) ) img = rgba64 } case cbTCA16 : bitsPerPixel = 64 nrgba64 = image . NewNRGBA64 ( image . Rect ( 0 , 0 , width , height ) ) img = nrgba64 } if allocateOnly { return img , nil } bytesPerPixel := ( bitsPerPixel + 7 ) / 8 // The +1 is for the per-row filter type, which is at cr[0]. rowSize := 1 + ( bitsPerPixel * width + 7 ) / 8 // cr and pr are the bytes for the current and previous row. cr := make ( [ ] uint8 , rowSize ) pr := make ( [ ] uint8 , rowSize ) for y := 0 ; y < height ; y ++ { // Read the decompressed bytes. _ , err := io . ReadFull ( r , cr ) if err != nil { if err == io . EOF || err == io . ErrUnexpectedEOF { return nil , FormatError ( " " ) } return nil , err } // Apply the filter. cdat := cr [ 1 : ] pdat := pr [ 1 : ] switch cr [ 0 ] { case ftNone : // No-op. case ftSub : for i := bytesPerPixel ; i < len ( cdat ) ; i ++ { cdat [ i ] += cdat [ i - bytesPerPixel ] } case ftUp : for i , p := range pdat { cdat [ i ] += p } case ftAverage : // The first column has no column to the left of it, so it is a // special case. We know that the first column exists because we // check above that width != 0, and so len(cdat) != 0. for i := 0 ; i < bytesPerPixel ; i ++ { cdat [ i ] += pdat [ i ] / 2 } for i := bytesPerPixel ; i < len ( cdat ) ; i ++ { cdat [ i ] += uint8 ( ( int ( cdat [ i - bytesPerPixel ] ) + int ( pdat [ i ] ) ) / 2 ) } case ftPaeth : filterPaeth ( cdat , pdat , bytesPerPixel ) default : return nil , FormatError ( " " ) } // Convert from bytes to colors. switch d . cb { case cbG1 : if d . useTransparent { ty := d . transparent [ 1 ] for x := 0 ; x < width ; x += 8 { b := cdat [ x / 8 ] for x2 := 0 ; x2 < 8 && x + x2 < width ; x2 ++ { ycol := ( b >> 7 ) * 0xff acol := uint8 ( 0xff ) if ycol == ty { acol = 0x00 } nrgba . SetNRGBA ( x + x2 , y , color . NRGBA { ycol , ycol , ycol , acol } ) b <<= 1 } } } else { for x := 0 ; x < width ; x += 8 { b := cdat [ x / 8 ] for x2 := 0 ; x2 < 8 && x + x2 < width ; x2 ++ { gray . SetGray ( x + x2 , y , color . Gray { ( b >> 7 ) * 0xff } ) b <<= 1 } } } case cbG2 : if d . useTransparent { ty := d . transparent [ 1 ] for x := 0 ; x < width ; x += 4 { b := cdat [ x / 4 ] for x2 := 0 ; x2 < 4 && x + x2 < width ; x2 ++ { ycol := ( b >> 6 ) * 0x55 acol := uint8 ( 0xff ) if ycol == ty { acol = 0x00 } nrgba . SetNRGBA ( x + x2 , y , color . NRGBA { ycol , ycol , ycol , acol } ) b <<= 2 } } } else { for x := 0 ; x < width ; x += 4 { b := cdat [ x / 4 ] for x2 := 0 ; x2 < 4 && x + x2 < width ; x2 ++ { gray . SetGray ( x + x2 , y , color . Gray { ( b >> 6 ) * 0x55 } ) b <<= 2 } } } case cbG4 : if d . useTransparent { ty := d . transparent [ 1 ] for x := 0 ; x < width ; x += 2 { b := cdat [ x / 2 ] for x2 := 0 ; x2 < 2 && x + x2 < width ; x2 ++ { ycol := ( b >> 4 ) * 0x11 acol := uint8 ( 0xff ) if ycol == ty { acol = 0x00 } nrgba . SetNRGBA ( x + x2 , y , color . NRGBA { ycol , ycol , ycol , acol } ) b <<= 4 } } } else { for x := 0 ; x < width ; x += 2 { b := cdat [ x / 2 ] for x2 := 0 ; x2 < 2 && x + x2 < width ; x2 ++ { gray . SetGray ( x + x2 , y , color . Gray { ( b >> 4 ) * 0x11 } ) b <<= 4 } } } case cbG8 : if d . useTransparent { ty := d . transparent [ 1 ] for x := 0 ; x < width ; x ++ { ycol := cdat [ x ] acol := uint8 ( 0xff ) if ycol == ty { acol = 0x00 } nrgba . SetNRGBA ( x , y , color . NRGBA { ycol , ycol , ycol , acol } ) } } else { copy ( gray . Pix [ pixOffset : ] , cdat ) pixOffset += gray . Stride } case cbGA8 : for x := 0 ; x < width ; x ++ { ycol := cdat [ 2 * x + 0 ] nrgba . SetNRGBA ( x , y , color . NRGBA { ycol , ycol , ycol , cdat [ 2 * x + 1 ] } ) } case cbTC8 : if d . useTransparent { pix , i , j := nrgba . Pix , pixOffset , 0 tr , tg , tb := d . transparent [ 1 ] , d . transparent [ 3 ] , d . transparent [ 5 ] for x := 0 ; x < width ; x ++ { r := cdat [ j + 0 ] g := cdat [ j + 1 ] b := cdat [ j + 2 ] a := uint8 ( 0xff ) if r == tr && g == tg && b == tb { a = 0x00 } pix [ i + 0 ] = r pix [ i + 1 ] = g pix [ i + 2 ] = b pix [ i + 3 ] = a i += 4 j += 3 } pixOffset += nrgba . Stride } else { pix , i , j := rgba . Pix , pixOffset , 0 for x := 0 ; x < width ; x ++ { pix [ i + 0 ] = cdat [ j + 0 ] pix [ i + 1 ] = cdat [ j + 1 ] pix [ i + 2 ] = cdat [ j + 2 ] pix [ i + 3 ] = 0xff i += 4 j += 3 } pixOffset += rgba . Stride } case cbP1 : for x := 0 ; x < width ; x += 8 { b := cdat [ x / 8 ] for x2 := 0 ; x2 < 8 && x + x2 < width ; x2 ++ { idx := b >> 7 if len ( paletted . Palette ) <= int ( idx ) { paletted . Palette = paletted . Palette [ : int ( idx ) + 1 ] } paletted . SetColorIndex ( x + x2 , y , idx ) b <<= 1 } } case cbP2 : for x := 0 ; x < width ; x += 4 { b := cdat [ x / 4 ] for x2 := 0 ; x2 < 4 && x + x2 < width ; x2 ++ { idx := b >> 6 if len ( paletted . Palette ) <= int ( idx ) { paletted . Palette = paletted . Palette [ : int ( idx ) + 1 ] } paletted . SetColorIndex ( x + x2 , y , idx ) b <<= 2 } } case cbP4 : for x := 0 ; x < width ; x += 2 { b := cdat [ x / 2 ] for x2 := 0 ; x2 < 2 && x + x2 < width ; x2 ++ { idx := b >> 4 if len ( paletted . Palette ) <= int ( idx ) { paletted . Palette = paletted . Palette [ : int ( idx ) + 1 ] } paletted . SetColorIndex ( x + x2 , y , idx ) b <<= 4 } } case cbP8 : if len ( paletted . Palette ) != 255 { for x := 0 ; x < width ; x ++ { if len ( paletted . Palette ) <= int ( cdat [ x ] ) { paletted . Palette = paletted . Palette [ : int ( cdat [ x ] ) + 1 ] } } } copy ( paletted . Pix [ pixOffset : ] , cdat ) pixOffset += paletted . Stride case cbTCA8 : copy ( nrgba . Pix [ pixOffset : ] , cdat ) pixOffset += nrgba . Stride case cbG16 : if d . useTransparent { ty := uint16 ( d . transparent [ 0 ] ) << 8 | uint16 ( d . transparent [ 1 ] ) for x := 0 ; x < width ; x ++ { ycol := uint16 ( cdat [ 2 * x + 0 ] ) << 8 | uint16 ( cdat [ 2 * x + 1 ] ) acol := uint16 ( 0xffff ) if ycol == ty { acol = 0x0000 } nrgba64 . SetNRGBA64 ( x , y , color . NRGBA64 { ycol , ycol , ycol , acol } ) } } else { for x := 0 ; x < width ; x ++ { ycol := uint16 ( cdat [ 2 * x + 0 ] ) << 8 | uint16 ( cdat [ 2 * x + 1 ] ) gray16 . SetGray16 ( x , y , color . Gray16 { ycol } ) } } case cbGA16 : for x := 0 ; x < width ; x ++ { ycol := uint16 ( cdat [ 4 * x + 0 ] ) << 8 | uint16 ( cdat [ 4 * x + 1 ] ) acol := uint16 ( cdat [ 4 * x + 2 ] ) << 8 | uint16 ( cdat [ 4 * x + 3 ] ) nrgba64 . SetNRGBA64 ( x , y , color . NRGBA64 { ycol , ycol , ycol , acol } ) } case cbTC16 : if d . useTransparent { tr := uint16 ( d . transparent [ 0 ] ) << 8 | uint16 ( d . transparent [ 1 ] ) tg := uint16 ( d . transparent [ 2 ] ) << 8 | uint16 ( d . transparent [ 3 ] ) tb := uint16 ( d . transparent [ 4 ] ) << 8 | uint16 ( d . transparent [ 5 ] ) for x := 0 ; x < width ; x ++ { rcol := uint16 ( cdat [ 6 * x + 0 ] ) << 8 | uint16 ( cdat [ 6 * x + 1 ] ) gcol := uint16 ( cdat [ 6 * x + 2 ] ) << 8 | uint16 ( cdat [ 6 * x + 3 ] ) bcol := uint16 ( cdat [ 6 * x + 4 ] ) << 8 | uint16 ( cdat [ 6 * x + 5 ] ) acol := uint16 ( 0xffff ) if rcol == tr && gcol == tg && bcol == tb { acol = 0x0000 } nrgba64 . SetNRGBA64 ( x , y , color . NRGBA64 { rcol , gcol , bcol , acol } ) } } else { for x := 0 ; x < width ; x ++ { rcol := uint16 ( cdat [ 6 * x + 0 ] ) << 8 | uint16 ( cdat [ 6 * x + 1 ] ) gcol := uint16 ( cdat [ 6 * x + 2 ] ) << 8 | uint16 ( cdat [ 6 * x + 3 ] ) bcol := uint16 ( cdat [ 6 * x + 4 ] ) << 8 | uint16 ( cdat [ 6 * x + 5 ] ) rgba64 . SetRGBA64 ( x , y , color . RGBA64 { rcol , gcol , bcol , 0xffff } ) } } case cbTCA16 : for x := 0 ; x < width ; x ++ { rcol := uint16 ( cdat [ 8 * x + 0 ] ) << 8 | uint16 ( cdat [ 8 * x + 1 ] ) gcol := uint16 ( cdat [ 8 * x + 2 ] ) << 8 | uint16 ( cdat [ 8 * x + 3 ] ) bcol := uint16 ( cdat [ 8 * x + 4 ] ) << 8 | uint16 ( cdat [ 8 * x + 5 ] ) acol := uint16 ( cdat [ 8 * x + 6 ] ) << 8 | uint16 ( cdat [ 8 * x + 7 ] ) nrgba64 . SetNRGBA64 ( x , y , color . NRGBA64 { rcol , gcol , bcol , acol } ) } } // The current row for y is the previous row for y+1. pr , cr = cr , pr } return img , nil }
// readImagePass reads a single image pass, sized according to the pass number.
readImagePass reads a single image pass sized according to the pass number .