Imports #
"compress/zlib"
"encoding/binary"
"fmt"
"hash"
"hash/crc32"
"image"
"image/color"
"io"
"bufio"
"compress/zlib"
"encoding/binary"
"hash/crc32"
"image"
"image/color"
"io"
"strconv"
"compress/zlib"
"encoding/binary"
"fmt"
"hash"
"hash/crc32"
"image"
"image/color"
"io"
"bufio"
"compress/zlib"
"encoding/binary"
"hash/crc32"
"image"
"image/color"
"io"
"strconv"
const BestCompression CompressionLevel = *ast.UnaryExprconst BestSpeed CompressionLevel = *ast.UnaryExprconst DefaultCompression CompressionLevel = 0const NoCompression CompressionLevel = *ast.UnaryExprA cb is a combination of color type and bit depth.
const cbG1A cb is a combination of color type and bit depth.
const cbG16A cb is a combination of color type and bit depth.
const cbG2A cb is a combination of color type and bit depth.
const cbG4A cb is a combination of color type and bit depth.
const cbG8A cb is a combination of color type and bit depth.
const cbGA16A cb is a combination of color type and bit depth.
const cbGA8A cb is a combination of color type and bit depth.
const cbInvalid = iotaA cb is a combination of color type and bit depth.
const cbP1A cb is a combination of color type and bit depth.
const cbP2A cb is a combination of color type and bit depth.
const cbP4A cb is a combination of color type and bit depth.
const cbP8A cb is a combination of color type and bit depth.
const cbTC16A cb is a combination of color type and bit depth.
const cbTC8A cb is a combination of color type and bit depth.
const cbTCA16A cb is a combination of color type and bit depth.
const cbTCA8var chunkOrderError = *ast.CallExprColor type, as per the PNG spec.
const ctGrayscale = 0Color type, as per the PNG spec.
const ctGrayscaleAlpha = 4Color type, as per the PNG spec.
const ctPaletted = 3Color type, as per the PNG spec.
const ctTrueColor = 2Color type, as per the PNG spec.
const ctTrueColorAlpha = 6Decoding stage. The PNG specification says that the IHDR, PLTE (if present), tRNS (if present), IDAT and IEND chunks must appear in that order. There may be multiple IDAT chunks, and IDAT chunks must be sequential (i.e. they may not have any other chunks between them). https://www.w3.org/TR/PNG/#5ChunkOrdering
const dsSeenIDATDecoding stage. The PNG specification says that the IHDR, PLTE (if present), tRNS (if present), IDAT and IEND chunks must appear in that order. There may be multiple IDAT chunks, and IDAT chunks must be sequential (i.e. they may not have any other chunks between them). https://www.w3.org/TR/PNG/#5ChunkOrdering
const dsSeenIENDDecoding stage. The PNG specification says that the IHDR, PLTE (if present), tRNS (if present), IDAT and IEND chunks must appear in that order. There may be multiple IDAT chunks, and IDAT chunks must be sequential (i.e. they may not have any other chunks between them). https://www.w3.org/TR/PNG/#5ChunkOrdering
const dsSeenIHDRDecoding stage. The PNG specification says that the IHDR, PLTE (if present), tRNS (if present), IDAT and IEND chunks must appear in that order. There may be multiple IDAT chunks, and IDAT chunks must be sequential (i.e. they may not have any other chunks between them). https://www.w3.org/TR/PNG/#5ChunkOrdering
const dsSeenPLTEDecoding stage. The PNG specification says that the IHDR, PLTE (if present), tRNS (if present), IDAT and IEND chunks must appear in that order. There may be multiple IDAT chunks, and IDAT chunks must be sequential (i.e. they may not have any other chunks between them). https://www.w3.org/TR/PNG/#5ChunkOrdering
const dsSeentRNSDecoding stage. The PNG specification says that the IHDR, PLTE (if present), tRNS (if present), IDAT and IEND chunks must appear in that order. There may be multiple IDAT chunks, and IDAT chunks must be sequential (i.e. they may not have any other chunks between them). https://www.w3.org/TR/PNG/#5ChunkOrdering
const dsStart = iotaFilter type, as per the PNG spec.
const ftAverage = 3Filter type, as per the PNG spec.
const ftNone = 0Filter type, as per the PNG spec.
const ftPaeth = 4Filter type, as per the PNG spec.
const ftSub = 1Filter type, as per the PNG spec.
const ftUp = 2intSize is either 32 or 64.
const intSize = *ast.BinaryExprinterlacing defines Adam7 interlacing, with 7 passes of reduced images. See https://www.w3.org/TR/PNG/#8Interlace
var interlacing = []interlaceScan{...}Interlace type.
const itAdam7 = 1Interlace type.
const itNone = 0Filter type, as per the PNG spec.
const nFilter = 5const pngHeader = "\x89PNG\r\n\x1a\n"CompressionLevel indicates the compression level.
type CompressionLevel intEncoderBuffer holds the buffers used for encoding PNG images.
type EncoderBuffer encoderA FormatError reports that the input is not a valid PNG.
type FormatError stringAn UnsupportedError reports that the input uses a valid but unimplemented PNG feature.
type UnsupportedError stringEncoderBufferPool is an interface for getting and returning temporary instances of the [EncoderBuffer] struct. This can be used to reuse buffers when encoding multiple images.
type EncoderBufferPool interface {
Get() *EncoderBuffer
Put(*EncoderBuffer)
}type opaquer interface {
Opaque() bool
}Encoder configures encoding PNG images.
type Encoder struct {
CompressionLevel CompressionLevel
BufferPool EncoderBufferPool
}type decoder struct {
r io.Reader
img image.Image
crc hash.Hash32
width int
height int
depth int
palette color.Palette
cb int
stage int
idatLength uint32
tmp [*ast.BinaryExpr]byte
interlace int
useTransparent bool
transparent [6]byte
}type encoder struct {
enc *Encoder
w io.Writer
m image.Image
cb int
err error
header [8]byte
footer [4]byte
tmp [*ast.BinaryExpr]byte
cr [nFilter][]uint8
pr []uint8
zw *zlib.Writer
zwLevel int
bw *bufio.Writer
}interlaceScan defines the placement and size of a pass for Adam7 interlacing.
type interlaceScan struct {
xFactor int
yFactor int
xOffset int
yOffset int
}Decode reads a PNG image from r and returns it as an [image.Image]. The type of Image returned depends on the PNG contents.
func Decode(r io.Reader) (image.Image, error)DecodeConfig returns the color model and dimensions of a PNG image without decoding the entire image.
func DecodeConfig(r io.Reader) (image.Config, error)Encode writes the Image m to w in PNG format.
func (enc *Encoder) Encode(w io.Writer, m image.Image) errorEncode writes the Image m to w in PNG format. Any Image may be encoded, but images that are not [image.NRGBA] might be encoded lossily.
func Encode(w io.Writer, m image.Image) errorfunc (e FormatError) Error() stringfunc (e UnsupportedError) Error() stringRead presents one or more IDAT chunks as one continuous stream (minus the intermediate chunk headers and footers). If the PNG data looked like: ... len0 IDAT xxx crc0 len1 IDAT yy crc1 len2 IEND crc2 then this reader presents xxxyy. For well-formed PNG data, the decoder state immediately before the first Read call is that d.r is positioned between the first IDAT and xxx, and the decoder state immediately after the last Read call is that d.r is positioned between yy and crc1.
func (d *decoder) Read(p []byte) (int, error)An encoder is an io.Writer that satisfies writes by writing PNG IDAT chunks, including an 8-byte header and 4-byte CRC checksum per Write call. Such calls should be relatively infrequent, since writeIDATs uses a [bufio.Writer]. This method should only be called from writeIDATs (via writeImage). No other code should treat an encoder as an io.Writer.
func (e *encoder) Write(b []byte) (int, error)func abs(x int) intThe absolute value of a byte interpreted as a signed int8.
func abs8(d uint8) intfunc cbPaletted(cb int) boolfunc cbTrueColor(cb int) boolfunc (d *decoder) checkHeader() errordecode decodes the IDAT data into an image.
func (d *decoder) decode() (image.Image, error)Chooses the filter to use for encoding the current row, and applies it. The return value is the index of the filter and also of the row in cr that has had it applied.
func filter(cr *[nFilter][]byte, pr []byte, bpp int) intfilterPaeth applies the Paeth filter to the cdat slice. cdat is the current row's data, pdat is the previous row's data.
func filterPaeth(cdat []byte, pdat []byte, bytesPerPixel int)func init()This function is required because we want the zero value of Encoder.CompressionLevel to map to zlib.DefaultCompression.
func levelToZlib(l CompressionLevel) intmergePassInto merges a single pass into a full sized image.
func (d *decoder) mergePassInto(dst image.Image, src image.Image, pass int)Returns whether or not the image is fully opaque.
func opaque(m image.Image) boolpaeth implements the Paeth filter function, as per the PNG specification.
func paeth(a uint8, b uint8, c uint8) uint8func (d *decoder) parseChunk(configOnly bool) errorfunc (d *decoder) parseIDAT(length uint32) (err error)func (d *decoder) parseIEND(length uint32) errorfunc (d *decoder) parseIHDR(length uint32) errorfunc (d *decoder) parsePLTE(length uint32) errorfunc (d *decoder) parsetRNS(length uint32) errorreadImagePass reads a single image pass, sized according to the pass number.
func (d *decoder) readImagePass(r io.Reader, pass int, allocateOnly bool) (image.Image, error)func (d *decoder) verifyChecksum() errorfunc (e *encoder) writeChunk(b []byte, name string)Write the actual image data to one or more IDAT chunks.
func (e *encoder) writeIDATs()func (e *encoder) writeIEND()func (e *encoder) writeIHDR()func (e *encoder) writeImage(w io.Writer, m image.Image, cb int, level int) errorfunc (e *encoder) writePLTEAndTRNS(p color.Palette)Generated with Arrow